Skip to content

Commit

Permalink
add a test to make sure pollInterval triggers onCompleted
Browse files Browse the repository at this point in the history
Confirms #5531 is fixed.
  • Loading branch information
brainkim committed Aug 16, 2021
1 parent e7eab4c commit f06f481
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/react/hooks/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2088,6 +2088,57 @@ describe('useQuery Hook', () => {
await expect(waitForNextUpdate({ timeout: 20 })).rejects.toThrow('Timed out');
expect(onCompleted).toHaveBeenCalledTimes(1);
});

it('onCompleted should work with polling', async () => {
const query = gql`{ hello }`;
const mocks = [
{
request: { query },
result: { data: { hello: 'world 1' } },
},
{
request: { query },
result: { data: { hello: 'world 2' } },
},
{
request: { query },
result: { data: { hello: 'world 3' } },
},
];

const cache = new InMemoryCache();
const onCompleted = jest.fn();
const { result, waitForNextUpdate } = renderHook(
() => useQuery(query, {
onCompleted,
pollInterval: 10,
}),
{
wrapper: ({ children }) => (
<MockedProvider mocks={mocks} cache={cache}>
{children}
</MockedProvider>
),
},
);

expect(result.current.loading).toBe(true);

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

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

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

describe('Optimistic data', () => {
Expand Down

0 comments on commit f06f481

Please sign in to comment.