Skip to content

Commit

Permalink
Test that fetchPolicy is reset when variables change.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Jul 8, 2021
1 parent e254dd4 commit 8d89f49
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/core/__tests__/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,118 @@ describe('ObservableQuery', () => {
});
});

itAsync('resets fetchPolicy when variables change when using nextFetchPolicy', (resolve, reject) => {
// This query and variables are copied from react-apollo
const queryWithVars = gql`
query people($first: Int) {
allPeople(first: $first) {
people {
name
}
}
}
`;

const data = { allPeople: { people: [{ name: 'Luke Skywalker' }] } };
const variables1 = { first: 0 };

const data2 = { allPeople: { people: [{ name: 'Leia Skywalker' }] } };
const variables2 = { first: 1 };

const queryManager = mockQueryManager(
reject,
{
request: {
query: queryWithVars,
variables: variables1,
},
result: { data },
},
{
request: {
query: queryWithVars,
variables: variables2,
},
result: { data: data2 },
},
{
request: {
query: queryWithVars,
variables: variables1,
},
result: { data },
},
{
request: {
query: queryWithVars,
variables: variables2,
},
result: { data: data2 },
},
);

const observable = queryManager.watchQuery({
query: queryWithVars,
variables: variables1,
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first',
notifyOnNetworkStatusChange: true,
});

expect(observable.options.fetchPolicy).toBe('cache-and-network');
expect(observable.initialFetchPolicy).toBe('cache-and-network');

subscribeAndCount(reject, observable, (handleCount, result) => {
expect(result.error).toBeUndefined();

if (handleCount === 1) {
expect(result.data).toEqual(data);
expect(result.loading).toBe(false);
expect(observable.options.fetchPolicy).toBe('cache-first');
observable.refetch(variables2);
} else if (handleCount === 2) {
expect(result.loading).toBe(true);
expect(result.networkStatus).toBe(NetworkStatus.setVariables);
expect(observable.options.fetchPolicy).toBe('cache-first');
} else if (handleCount === 3) {
expect(result.data).toEqual(data2);
expect(result.loading).toBe(false);
expect(observable.options.fetchPolicy).toBe('cache-first');
observable.setOptions({
variables: variables1,
}).then(result => {
expect(result.data).toEqual(data);
}).catch(reject);
expect(observable.options.fetchPolicy).toBe('cache-and-network');
} else if (handleCount === 4) {
expect(result.loading).toBe(true);
expect(result.networkStatus).toBe(NetworkStatus.setVariables);
expect(observable.options.fetchPolicy).toBe('cache-first');
} else if (handleCount === 5) {
expect(result.data).toEqual(data);
expect(result.loading).toBe(false);
expect(observable.options.fetchPolicy).toBe('cache-first');
observable.reobserve({
variables: variables2,
}).then(result => {
expect(result.data).toEqual(data2);
}).catch(reject);
expect(observable.options.fetchPolicy).toBe('cache-and-network');
} else if (handleCount === 6) {
expect(result.data).toEqual(data2);
expect(result.loading).toBe(true);
expect(observable.options.fetchPolicy).toBe('cache-first');
} else if (handleCount === 7) {
expect(result.data).toEqual(data2);
expect(result.loading).toBe(false);
expect(observable.options.fetchPolicy).toBe('cache-first');
setTimeout(resolve, 10);
} else {
reject(`too many renders (${handleCount})`);
}
});
});

itAsync('cache-and-network refetch should run @client(always: true) resolvers when network request fails', (resolve, reject) => {
const query = gql`
query MixedQuery {
Expand Down

0 comments on commit 8d89f49

Please sign in to comment.