diff --git a/packages/apollo-client/src/core/__tests__/QueryManager/index.ts b/packages/apollo-client/src/core/__tests__/QueryManager/index.ts index 91ad49bdd87..1d8638e67be 100644 --- a/packages/apollo-client/src/core/__tests__/QueryManager/index.ts +++ b/packages/apollo-client/src/core/__tests__/QueryManager/index.ts @@ -3933,6 +3933,7 @@ describe('QueryManager', () => { }); }); }); + describe('refetchQueries', () => { const oldWarn = console.warn; let warned: any; @@ -4447,6 +4448,119 @@ describe('QueryManager', () => { done(); }); }); + + describe('awaitRefetchQueries', () => { + function awaitRefetchTest({ awaitRefetchQueries }) { + const query = gql` + query getAuthors($id: ID!) { + author(id: $id) { + firstName + lastName + } + } + `; + + const queryData = { + author: { + firstName: 'John', + lastName: 'Smith', + }, + }; + + const mutation = gql` + mutation changeAuthorName { + changeAuthorName(newName: "Jack Smith") { + firstName + lastName + } + } + `; + + const mutationData = { + changeAuthorName: { + firstName: 'Jack', + lastName: 'Smith', + }, + }; + + const secondReqData = { + author: { + firstName: 'Jane', + lastName: 'Johnson', + }, + }; + + const variables = { id: '1234' }; + + const queryManager = mockQueryManager( + { + request: { query, variables }, + result: { data: queryData }, + }, + { + request: { query: mutation }, + result: { data: mutationData }, + }, + { + request: { query, variables }, + result: { data: secondReqData }, + }, + ); + + const observable = queryManager.watchQuery({ + query, + variables, + notifyOnNetworkStatusChange: false, + }); + + let mutationComplete = false; + return observableToPromise( + { observable }, + result => { + expect(stripSymbols(result.data)).toEqual(queryData); + const mutateOptions = { + mutation, + refetchQueries: ['getAuthors'], + }; + if (awaitRefetchQueries) { + mutateOptions.awaitRefetchQueries = awaitRefetchQueries; + } + queryManager.mutate(mutateOptions).then(() => { + mutationComplete = true; + }); + }, + result => { + if (awaitRefetchQueries) { + expect(mutationComplete).not.toBeTruthy(); + } else { + expect(mutationComplete).toBeTruthy(); + } + expect(stripSymbols(observable.currentResult().data)).toEqual( + secondReqData, + ); + expect(stripSymbols(result.data)).toEqual(secondReqData); + }, + ); + } + + it( + 'should not wait for `refetchQueries` to complete before resolving ' + + 'the mutation, when `awaitRefetchQueries` is falsy', + () => { + awaitRefetchTest({ awaitRefetchQueries: undefined }); + awaitRefetchTest({ awaitRefetchQueries: false }); + }, + ); + + it( + 'should wait for `refetchQueries` to complete before resolving ' + + 'the mutation, when `awaitRefetchQueries` is `true`', + () => { + awaitRefetchTest({ awaitRefetchQueries: true }); + }, + ); + }); + describe('store watchers', () => { it('does not fill up the store on resolved queries', () => { const query1 = gql`