diff --git a/packages/apollo-client/src/core/__tests__/fetchPolicies.ts b/packages/apollo-client/src/core/__tests__/fetchPolicies.ts index 93a6f8a501c..4a47c701c26 100644 --- a/packages/apollo-client/src/core/__tests__/fetchPolicies.ts +++ b/packages/apollo-client/src/core/__tests__/fetchPolicies.ts @@ -21,6 +21,7 @@ import subscribeAndCount from '../../util/subscribeAndCount'; import { withWarning } from '../../util/wrap'; import { mockSingleLink } from '../../__mocks__/mockLinks'; +import { NetworkStatus } from '../networkStatus'; const query = gql` query { @@ -345,3 +346,111 @@ describe('no-cache', () => { }); }); }); + +describe('cache-and-network', function() { + it('gives appropriate networkStatus for refetched queries', done => { + const client = new ApolloClient({ + link: ApolloLink.empty(), + cache: new InMemoryCache(), + resolvers: { + Query: { + hero(_data, args) { + return { + __typename: 'Hero', + ...args, + name: 'Luke Skywalker', + }; + }, + }, + }, + }); + + const observable = client.watchQuery({ + query: gql` + query FetchLuke($id: String) { + hero(id: $id) @client { + id + name + } + } + `, + fetchPolicy: 'cache-and-network', + variables: { id: '1' }, + notifyOnNetworkStatusChange: true, + }); + + function dataWithId(id: number | string) { + return { + hero: { + __typename: 'Hero', + id: String(id), + name: 'Luke Skywalker', + }, + }; + } + + subscribeAndCount(done, observable, (count, result) => { + if (count === 1) { + expect(result).toEqual({ + data: void 0, + loading: true, + networkStatus: NetworkStatus.loading, + stale: true, + }); + } else if (count === 2) { + expect(result).toEqual({ + data: dataWithId(1), + loading: false, + networkStatus: NetworkStatus.ready, + stale: false, + }); + return observable.setVariables({ id: '2' }); + } else if (count === 3) { + expect(result).toEqual({ + data: dataWithId(1), + loading: true, + networkStatus: NetworkStatus.setVariables, + stale: false, + }); + } else if (count === 4) { + expect(result).toEqual({ + data: dataWithId(2), + loading: false, + networkStatus: NetworkStatus.ready, + stale: false, + }); + return observable.refetch(); + } else if (count === 5) { + expect(result).toEqual({ + data: dataWithId(2), + loading: true, + networkStatus: NetworkStatus.refetch, + stale: false, + }); + } else if (count === 6) { + expect(result).toEqual({ + data: dataWithId(2), + loading: false, + networkStatus: NetworkStatus.ready, + stale: false, + }); + return observable.refetch({ id: '3' }); + } else if (count === 7) { + expect(result).toEqual({ + data: dataWithId(2), + loading: true, + networkStatus: NetworkStatus.setVariables, + stale: false, + }); + } else if (count === 8) { + expect(result).toEqual({ + data: dataWithId(3), + loading: false, + networkStatus: NetworkStatus.ready, + stale: false, + }); + done(); + } + }); + }); +}); diff --git a/packages/apollo-client/src/data/queries.ts b/packages/apollo-client/src/data/queries.ts index dc4f2fbac4d..efbe266034d 100644 --- a/packages/apollo-client/src/data/queries.ts +++ b/packages/apollo-client/src/data/queries.ts @@ -151,13 +151,14 @@ export class QueryStore { } public markQueryResultClient(queryId: string, complete: boolean) { - if (!this.store || !this.store[queryId]) return; - - this.store[queryId].networkError = null; - this.store[queryId].previousVariables = null; - this.store[queryId].networkStatus = complete - ? NetworkStatus.ready - : NetworkStatus.loading; + const storeValue = this.store && this.store[queryId]; + if (storeValue) { + this.store[queryId].networkError = null; + this.store[queryId].previousVariables = null; + if (complete) { + this.store[queryId].networkStatus = NetworkStatus.ready; + } + } } public stopQuery(queryId: string) {