Skip to content

Commit

Permalink
add a test for #8497
Browse files Browse the repository at this point in the history
  • Loading branch information
brainkim committed Aug 18, 2021
1 parent 1834f5f commit 410e1ca
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/react/hooks/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,75 @@ describe('useQuery Hook', () => {
await expect(waitForNextUpdate({ timeout: 20 })).rejects.toThrow('Timed out');
});

it('should not persist errors when variables change', async () => {
const query = gql`
query hello($id: ID) { hello }
`;

const mocks = [
{
request: {
query,
variables: { id: 1 },
},
result: {
errors: [new GraphQLError('error')]
},
},
{
request: {
query,
variables: { id: 2 },
},
result: {
data: { hello: 'world' },
},
}
];

const cache = new InMemoryCache();
const wrapper = ({ children }: any) => (
<MockedProvider mocks={mocks} cache={cache}>
{children}
</MockedProvider>
);

const { result, rerender, waitForNextUpdate } = renderHook(
({ id }) => useQuery(
query, {
variables: {
id,
},
},
),
{
wrapper,
initialProps: { id: 1 },
},
);

expect(result.current.loading).toBe(true);
expect(result.current.data).toBe(undefined);
expect(result.current.error).toBe(undefined);

await waitForNextUpdate();

expect(result.current.loading).toBe(false);
expect(result.current.data).toBe(undefined);
expect(result.current.error).toBeInstanceOf(ApolloError);
expect(result.current.error!.message).toBe('error');

rerender({ id: 2 });
expect(result.current.loading).toBe(true);
expect(result.current.data).toBe(undefined);
expect(result.current.error).toBe(undefined);

await waitForNextUpdate();
expect(result.current.loading).toBe(false);
expect(result.current.data).toEqual({ hello: 'world' });
expect(result.current.error).toBe(undefined);
});

it('should render multiple errors when refetching', async () => {
const query = gql`{ hello }`;
const mocks = [
Expand Down

0 comments on commit 410e1ca

Please sign in to comment.