Skip to content

Commit

Permalink
added test cases for delete
Browse files Browse the repository at this point in the history
  • Loading branch information
svidgen committed Jun 9, 2022
1 parent 99be132 commit cd09343
Showing 1 changed file with 79 additions and 1 deletion.
80 changes: 79 additions & 1 deletion packages/datastore/__tests__/DataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ describe('DataStore observeQuery, with fake-indexeddb and fake sync', () => {
})
);
}
}, 100);
}, 1);
} catch (error) {
done(error);
}
Expand Down Expand Up @@ -520,6 +520,84 @@ describe('DataStore observeQuery, with fake-indexeddb and fake sync', () => {
}
})();
});

test('removes deleted items from the snapshot', done => {
(async () => {
try {
const expecteds = [5, 4];

for (let i = 0; i < 5; i++) {
await DataStore.save(
new Post({
title: `the post ${i}`,
})
);
}

const sub = DataStore.observeQuery(Post).subscribe(
({ items, isSynced }) => {
const expected = expecteds.shift() || 0;
expect(items.length).toBe(expected);

for (let i = 0; i < expected; i++) {
expect(items[i].title).toContain(`the post`);
}

if (expecteds.length === 0) {
sub.unsubscribe();
done();
}
}
);

setTimeout(async () => {
const itemToDelete = (await DataStore.query(Post)).pop();
await DataStore.delete(itemToDelete);
}, 1);
} catch (error) {
done(error);
}
})();
});

test('removes deleted items from the snapshot with a predicate', done => {
(async () => {
try {
const expecteds = [5, 4];

for (let i = 0; i < 5; i++) {
await DataStore.save(
new Post({
title: `the post ${i}`,
})
);
}

const sub = DataStore.observeQuery(Post, p =>
p.title('beginsWith', 'the post')
).subscribe(({ items, isSynced }) => {
const expected = expecteds.shift() || 0;
expect(items.length).toBe(expected);

for (let i = 0; i < expected; i++) {
expect(items[i].title).toContain(`the post`);
}

if (expecteds.length === 0) {
sub.unsubscribe();
done();
}
});

setTimeout(async () => {
const itemToDelete = (await DataStore.query(Post)).pop();
await DataStore.delete(itemToDelete);
}, 1);
} catch (error) {
done(error);
}
})();
});
});

describe('DataStore tests', () => {
Expand Down

0 comments on commit cd09343

Please sign in to comment.