diff --git a/src/react/hooks/__tests__/useQuery.test.tsx b/src/react/hooks/__tests__/useQuery.test.tsx index 239321f87b3..8e17b4dea7f 100644 --- a/src/react/hooks/__tests__/useQuery.test.tsx +++ b/src/react/hooks/__tests__/useQuery.test.tsx @@ -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 }) => ( + + {children} + + ), + }, + ); + + 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', () => {