From cd09343caf1d87692ea0d17697bbd5b24135bf83 Mon Sep 17 00:00:00 2001 From: Jon Wire Date: Thu, 9 Jun 2022 14:35:41 -0500 Subject: [PATCH] added test cases for delete --- packages/datastore/__tests__/DataStore.ts | 80 ++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/packages/datastore/__tests__/DataStore.ts b/packages/datastore/__tests__/DataStore.ts index 36be1bf0c89..88df077d90d 100644 --- a/packages/datastore/__tests__/DataStore.ts +++ b/packages/datastore/__tests__/DataStore.ts @@ -383,7 +383,7 @@ describe('DataStore observeQuery, with fake-indexeddb and fake sync', () => { }) ); } - }, 100); + }, 1); } catch (error) { done(error); } @@ -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', () => {