From a702626ad50580866a25117fe6f5be2e3d77838c Mon Sep 17 00:00:00 2001 From: Josh Ribakoff Date: Sat, 18 Aug 2018 02:17:24 -0700 Subject: [PATCH] flattens: will handle dependent updates --- .../apollo-client/src/__tests__/optimistic.ts | 169 +++++++++--------- 1 file changed, 83 insertions(+), 86 deletions(-) diff --git a/packages/apollo-client/src/__tests__/optimistic.ts b/packages/apollo-client/src/__tests__/optimistic.ts index 2a51a1f06fe..3f644841503 100644 --- a/packages/apollo-client/src/__tests__/optimistic.ts +++ b/packages/apollo-client/src/__tests__/optimistic.ts @@ -1538,10 +1538,10 @@ describe('optimistic mutation results', () => { ); }); - it('two mutations, one fails', () => { + it('two mutations, one fails', async () => { expect.assertions(10); let subscriptionHandle: Subscription; - return setup( + await setup( { request: { query: mutation }, error: new Error('forbidden (test error)'), @@ -1557,95 +1557,92 @@ describe('optimistic mutation results', () => { // same order as before, otherwise the store can end up in an inconsistent state. // delay: 50, }, - ) - .then(() => { - // we have to actually subscribe to the query to be able to update it - return new Promise(resolve => { - const handle = client.watchQuery({ query }); - subscriptionHandle = handle.subscribe({ - next(res) { - resolve(res); - }, - }); - }); - }) - .then(() => { - const update = (proxy: any, mResult: any) => { - const data: any = proxy.readFragment({ - id: 'TodoList5', - fragment: gql` - fragment todoList on TodoList { - todos { - id - text - completed - __typename - } - } - `, - }); - - proxy.writeFragment({ - data: { - ...data, - todos: [mResult.data.createTodo, ...data.todos], - }, - id: 'TodoList5', - fragment: gql` - fragment todoList on TodoList { - todos { - id - text - completed - __typename - } - } - `, - }); - }; - const promise = client - .mutate({ - mutation, - optimisticResponse, - update, - }) - .catch(err => { - // it is ok to fail here - expect(err).toBeInstanceOf(Error); - expect(err.message).toBe('Network error: forbidden (test error)'); - return null; - }); + ); - const promise2 = client.mutate({ - mutation, - optimisticResponse: optimisticResponse2, - update, - }); + // we have to actually subscribe to the query to be able to update it + await new Promise(resolve => { + const handle = client.watchQuery({ query }); + subscriptionHandle = handle.subscribe({ + next(res) { + resolve(res); + }, + }); + }); - const dataInStore = (client.cache as InMemoryCache).extract(true); - expect((dataInStore['TodoList5'] as any).todos.length).toBe(5); - expect((dataInStore['Todo99'] as any).text).toBe( - 'Optimistically generated', - ); - expect((dataInStore['Todo66'] as any).text).toBe( - 'Optimistically generated 2', - ); + const update = (proxy: any, mResult: any) => { + const data: any = proxy.readFragment({ + id: 'TodoList5', + fragment: gql` + fragment todoList on TodoList { + todos { + id + text + completed + __typename + } + } + `, + }); - return Promise.all([promise, promise2]); + proxy.writeFragment({ + data: { + ...data, + todos: [mResult.data.createTodo, ...data.todos], + }, + id: 'TodoList5', + fragment: gql` + fragment todoList on TodoList { + todos { + id + text + completed + __typename + } + } + `, + }); + }; + const promise = client + .mutate({ + mutation, + optimisticResponse, + update, }) - .then(() => { - subscriptionHandle.unsubscribe(); - const dataInStore = (client.cache as InMemoryCache).extract(true); - expect((dataInStore['TodoList5'] as any).todos.length).toBe(4); - expect(stripSymbols(dataInStore)).not.toHaveProperty('Todo99'); - expect(dataInStore).toHaveProperty('Todo66'); - expect((dataInStore['TodoList5'] as any).todos).toContainEqual( - realIdValue('Todo66', 'Todo'), - ); - expect((dataInStore['TodoList5'] as any).todos).not.toContainEqual( - realIdValue('Todo99', 'Todo'), - ); + .catch(err => { + // it is ok to fail here + expect(err).toBeInstanceOf(Error); + expect(err.message).toBe('Network error: forbidden (test error)'); + return null; }); + + const promise2 = client.mutate({ + mutation, + optimisticResponse: optimisticResponse2, + update, + }); + + const dataInStore = (client.cache as InMemoryCache).extract(true); + expect((dataInStore['TodoList5'] as any).todos.length).toBe(5); + expect((dataInStore['Todo99'] as any).text).toBe( + 'Optimistically generated', + ); + expect((dataInStore['Todo66'] as any).text).toBe( + 'Optimistically generated 2', + ); + + await Promise.all([promise, promise2]); + + subscriptionHandle.unsubscribe(); + const dataInStore = (client.cache as InMemoryCache).extract(true); + expect((dataInStore['TodoList5'] as any).todos.length).toBe(4); + expect(stripSymbols(dataInStore)).not.toHaveProperty('Todo99'); + expect(dataInStore).toHaveProperty('Todo66'); + expect((dataInStore['TodoList5'] as any).todos).toContainEqual( + realIdValue('Todo66', 'Todo'), + ); + expect((dataInStore['TodoList5'] as any).todos).not.toContainEqual( + realIdValue('Todo99', 'Todo'), + ); }); it('will handle dependent updates', done => {