Skip to content

Commit

Permalink
Add test for no-cache fetchPolicy + notifyOnNetworkStatusChange + wat…
Browse files Browse the repository at this point in the history
…chQuery
  • Loading branch information
Jenn Creighton committed Mar 12, 2021
1 parent 0ba32b3 commit b4cd8fd
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/core/__tests__/fetchPolicies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,106 @@ describe('no-cache', () => {
}))
.then(resolve, reject);
});

itAsync('gives appropriate networkStatus for watched queries', (resolve, reject) => {
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: 'no-cache',
variables: { id: '1' },
notifyOnNetworkStatusChange: true,
});

function dataWithId(id: number | string) {
return {
hero: {
__typename: 'Hero',
id: String(id),
name: 'Luke Skywalker',
},
};
}

subscribeAndCount(reject, observable, (count, result) => {
if (count === 1) {
expect(result).toEqual({
data: dataWithId(1),
loading: false,
networkStatus: NetworkStatus.ready,
});
expect(client.cache.extract(true)).toEqual({});
return observable.setVariables({ id: '2' });
} else if (count === 2) {
expect(result).toEqual({
data: {},
loading: true,
networkStatus: NetworkStatus.setVariables,
partial: true,
});
} else if (count === 3) {
expect(result).toEqual({
data: dataWithId(2),
loading: false,
networkStatus: NetworkStatus.ready,
});
expect(client.cache.extract(true)).toEqual({});
return observable.refetch();
} else if (count === 4) {
expect(result).toEqual({
data: dataWithId(2),
loading: true,
networkStatus: NetworkStatus.refetch,
});
expect(client.cache.extract(true)).toEqual({});
} else if (count === 5) {
expect(result).toEqual({
data: dataWithId(2),
loading: false,
networkStatus: NetworkStatus.ready,
});
expect(client.cache.extract(true)).toEqual({});
return observable.refetch({ id: '3' });
} else if (count === 6) {
expect(result).toEqual({
data: {},
loading: true,
networkStatus: NetworkStatus.setVariables,
partial: true,
});
expect(client.cache.extract(true)).toEqual({});
} else if (count === 7) {
expect(result).toEqual({
data: dataWithId(3),
loading: false,
networkStatus: NetworkStatus.ready,
});
expect(client.cache.extract(true)).toEqual({});
resolve();
}
});
});
});
});

Expand Down

0 comments on commit b4cd8fd

Please sign in to comment.