diff --git a/src/react/components/__tests__/client/Query.test.tsx b/src/react/components/__tests__/client/Query.test.tsx index 3a277bb75a9..5b6c2d9d6b6 100644 --- a/src/react/components/__tests__/client/Query.test.tsx +++ b/src/react/components/__tests__/client/Query.test.tsx @@ -1785,16 +1785,20 @@ describe('Query component', () => { }); describe('Partial refetching', () => { - const origConsoleWarn = console.warn; + let errorSpy!: ReturnType; - beforeAll(() => { - console.warn = () => null; + beforeEach(() => { + errorSpy = jest.spyOn(console, 'error') + .mockImplementation(() => {}); }); afterAll(() => { - console.warn = origConsoleWarn; + errorSpy.mockRestore(); }); + // TODO(brian): This is a terrible legacy test which is causing console + // error calls no matter what I try and I do not want to care about it + // anymore :) itAsync( 'should attempt a refetch when the query result was marked as being ' + 'partial, the returned data was reset to an empty Object by the ' + @@ -1811,11 +1815,10 @@ describe('Query component', () => { } `; + let done = false; const allPeopleData = { allPeople: { people: [{ name: 'Luke Skywalker' }] }, }; - const errorSpy = jest.spyOn(console, 'error') - .mockImplementation(() => {}); const query = allPeopleQuery; const link = mockSingleLink( { request: { query }, result: { data: {} } }, @@ -1834,6 +1837,9 @@ describe('Query component', () => { try { if (!loading) { expect(data).toEqual(allPeopleData); + expect(errorSpy).toHaveBeenCalledTimes(1); + expect(errorSpy.mock.calls[0][0]).toMatch('Missing field'); + done = true; } } catch (err) { reject(err); @@ -1849,11 +1855,7 @@ describe('Query component', () => { ); - return wait().then(() => { - expect(errorSpy).toHaveBeenCalledTimes(1); - expect(errorSpy.mock.calls[0][0]).toMatch('Missing field'); - errorSpy.mockRestore(); - }).then(resolve, reject); + wait(() => done).then(resolve, reject); } ); diff --git a/src/react/hooks/__tests__/useQuery.test.tsx b/src/react/hooks/__tests__/useQuery.test.tsx index ab614da0092..f02250715ff 100644 --- a/src/react/hooks/__tests__/useQuery.test.tsx +++ b/src/react/hooks/__tests__/useQuery.test.tsx @@ -2241,6 +2241,7 @@ describe('useQuery Hook', () => { { request: { query }, result: { data: {} }, + delay: 20, }, { request: { query }, @@ -2270,34 +2271,25 @@ describe('useQuery Hook', () => { expect(result.current.loading).toBe(true); expect(result.current.data).toBe(undefined); + expect(result.current.error).toBe(undefined); expect(result.current.networkStatus).toBe(NetworkStatus.loading); - const updates = result.all.length; await waitForNextUpdate(); - expect(result.all.length - updates).toBe(2); - // waitForUpdate seems to miss the erroring render - const previous = result.all[result.all.length - 2]; - if (previous instanceof Error) { - throw previous; - } - - expect(previous.loading).toBe(true); - expect(previous.error).toBe(undefined); - expect(previous.data).toBe(undefined); - expect(result.current.loading).toBe(true); - expect(result.current.error).toBe(undefined); expect(result.current.data).toBe(undefined); + expect(result.current.error).toBe(undefined); expect(result.current.networkStatus).toBe(NetworkStatus.refetch); expect(errorSpy).toHaveBeenCalledTimes(1); expect(errorSpy.mock.calls[0][0]).toMatch('Missing field'); + errorSpy.mockRestore(); await waitForNextUpdate(); + expect(result.current.loading).toBe(false); expect(result.current.data).toEqual({ hello: 'world' }); + expect(result.current.error).toBe(undefined); expect(result.current.networkStatus).toBe(NetworkStatus.ready); - errorSpy.mockRestore(); }); it('should attempt a refetch when data is missing and partialRefetch is true 2', async () => { @@ -2318,7 +2310,7 @@ describe('useQuery Hook', () => { const errorSpy = jest.spyOn(console, 'error') .mockImplementation(() => {}); const link = mockSingleLink( - { request: { query }, result: { data: {} } }, + { request: { query }, result: { data: {} }, delay: 20 }, { request: { query }, result: { data }, delay: 20 } ); @@ -2343,24 +2335,13 @@ describe('useQuery Hook', () => { expect(result.current.loading).toBe(true); expect(result.current.data).toBe(undefined); + expect(result.current.error).toBe(undefined); expect(result.current.networkStatus).toBe(NetworkStatus.loading); - const updates = result.all.length; await waitForNextUpdate(); - expect(result.all.length - updates).toBe(2); - // waitForUpdate seems to miss the erroring render - const previous = result.all[result.all.length - 2]; - if (previous instanceof Error) { - throw previous; - } - - expect(previous.loading).toBe(true); - expect(previous.error).toBe(undefined); - expect(previous.data).toBe(undefined); - expect(result.current.loading).toBe(true); - expect(result.current.error).toBe(undefined); expect(result.current.data).toBe(undefined); + expect(result.current.error).toBe(undefined); expect(result.current.networkStatus).toBe(NetworkStatus.refetch); expect(errorSpy).toHaveBeenCalledTimes(1); @@ -2370,6 +2351,7 @@ describe('useQuery Hook', () => { await waitForNextUpdate(); expect(result.current.loading).toBe(false); expect(result.current.data).toEqual(data); + expect(result.current.error).toBe(undefined); expect(result.current.networkStatus).toBe(NetworkStatus.ready); }); @@ -2382,6 +2364,7 @@ describe('useQuery Hook', () => { { request: { query }, result: { data: {} }, + delay: 20, }, { request: { query }, @@ -2412,22 +2395,12 @@ describe('useQuery Hook', () => { expect(result.current.loading).toBe(true); expect(result.current.data).toBe(undefined); + expect(result.current.error).toBe(undefined); expect(result.current.networkStatus).toBe(NetworkStatus.loading); - const updates = result.all.length; await waitForNextUpdate(); - expect(result.all.length - updates).toBe(2); - // waitForUpdate seems to miss the erroring render - const previous = result.all[result.all.length - 2]; - if (previous instanceof Error) { - throw previous; - } - - expect(previous.loading).toBe(true); - expect(previous.error).toBe(undefined); - expect(previous.data).toBe(undefined); - expect(result.current.loading).toBe(true); + expect(result.current.error).toBe(undefined); expect(result.current.data).toBe(undefined); expect(result.current.networkStatus).toBe(NetworkStatus.refetch); @@ -2436,8 +2409,10 @@ describe('useQuery Hook', () => { errorSpy.mockRestore(); await waitForNextUpdate(); + expect(result.current.loading).toBe(false); expect(result.current.data).toEqual({ hello: 'world' }); + expect(result.current.error).toBe(undefined); expect(result.current.networkStatus).toBe(NetworkStatus.ready); }); });