Skip to content

Commit

Permalink
write a test for a change in ApolloClient.refetchQueries error handli…
Browse files Browse the repository at this point in the history
…ng (#9330)

* write a test for a change in ApolloClient.refetchQueries error handling
* Apply minor formatting
* Update error message to match latest changes.
* Fix typo in test description.

---------

Co-authored-by: Hugh Willson <hugh@octonary.com>
Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
  • Loading branch information
3 people committed Feb 20, 2023
1 parent 975b923 commit 43be05f
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/__tests__/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { HttpLink } from '../link/http';
import { InMemoryCache } from '../cache';
import { itAsync, withErrorSpy } from '../testing';
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
import { invariant } from 'ts-invariant';

describe('ApolloClient', () => {
describe('constructor', () => {
Expand Down Expand Up @@ -2315,4 +2316,69 @@ describe('ApolloClient', () => {
expect(data.widgets.length).toBe(2);
});
});

describe('refetchQueries', () => {
let invariantDebugSpy: jest.SpyInstance;

beforeEach(() => {
invariantDebugSpy = jest.spyOn(invariant, 'debug');
})

afterEach(() => {
invariantDebugSpy.mockRestore();
})

itAsync('should catch refetchQueries error when not caught explicitly', (resolve, reject) => {
const linkFn = jest.fn(() =>
new Observable<any>(observer => {
setTimeout(() => {
observer.error(new Error('refetch failed'));
});
})
).mockImplementationOnce(() => {
setTimeout(refetchQueries);
return Observable.of();
})

const client = new ApolloClient({
link: new ApolloLink(linkFn),
cache: new InMemoryCache()
});

const query = gql`
query someData {
foo {
bar
}
}
`;

const observable = client.watchQuery({
query,
fetchPolicy: 'network-only'
});

observable.subscribe({});

function refetchQueries() {
const result = client.refetchQueries({
include: 'all'
});

result.queries[0].subscribe({
error() {
setTimeout(() => {
try {
expect(invariantDebugSpy).toHaveBeenCalledTimes(1);
expect(invariantDebugSpy).toHaveBeenCalledWith('In client.refetchQueries, Promise.all promise rejected with error ApolloError: refetch failed');
resolve();
} catch (err) {
reject(err);
}
});
}
});
}
});
});
});

0 comments on commit 43be05f

Please sign in to comment.