From f06f481747af416e2416beeb2dca527391d4114e Mon Sep 17 00:00:00 2001 From: Brian Kim Date: Fri, 13 Aug 2021 13:46:07 -0400 Subject: [PATCH] add a test to make sure pollInterval triggers onCompleted Confirms #5531 is fixed. --- src/react/hooks/__tests__/useQuery.test.tsx | 51 +++++++++++++++++++++ 1 file changed, 51 insertions(+) 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', () => {