From 70c3a916023114f67be6a8ea20c5d3de581aad64 Mon Sep 17 00:00:00 2001 From: Johnny Bouder Date: Thu, 27 Jun 2024 13:46:29 -0400 Subject: [PATCH] Update unit tests to properly mock react query calls. --- src/pages/dashboard/dashboard.test.tsx | 17 +++++++++++------ src/pages/details/details.test.tsx | 17 +++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/pages/dashboard/dashboard.test.tsx b/src/pages/dashboard/dashboard.test.tsx index 682f2f4c..016ca9ce 100644 --- a/src/pages/dashboard/dashboard.test.tsx +++ b/src/pages/dashboard/dashboard.test.tsx @@ -11,7 +11,13 @@ import { User } from '../../types/user'; import { Dashboard } from './dashboard'; describe('Dashboard', () => { - const queryClient = new QueryClient(); + const queryClient = new QueryClient({ + defaultOptions: { + queries: { + retry: false, + }, + }, + }); const componentWrapper = ( @@ -27,19 +33,16 @@ describe('Dashboard', () => { const mock = new MockAdapter(axios); beforeAll(() => { mock.reset(); - queryClient.setDefaultOptions({ - queries: { - retry: false, // Disable retries for tests - }, - }); }); beforeEach(() => { queryClient.clear(); + mock.reset(); }); test('should render successfully', async () => { mock.onGet(new RegExp('/spacecraft')).reply(200, mockData); + queryClient.setQueryData(['dashboard'], mockData.items); jest.spyOn(useAuthMock, 'default').mockReturnValue({ isSignedIn: true, isLoading: false, @@ -66,6 +69,8 @@ describe('Dashboard', () => { mock .onGet(new RegExp('/spacecraft')) .reply(500, { message: 'Internal Server Error' }); + queryClient.setQueryData(['dashboard'], null); + const { baseElement } = render(componentWrapper); await act(async () => { expect(baseElement).toBeTruthy(); diff --git a/src/pages/details/details.test.tsx b/src/pages/details/details.test.tsx index 2ae75073..399c8e2b 100644 --- a/src/pages/details/details.test.tsx +++ b/src/pages/details/details.test.tsx @@ -16,7 +16,13 @@ jest.mock('react-router-dom', () => ({ })); describe('Details', () => { - const queryClient = new QueryClient(); + const queryClient = new QueryClient({ + defaultOptions: { + queries: { + retry: false, + }, + }, + }); const componentWrapper = ( @@ -32,19 +38,16 @@ describe('Details', () => { const mock = new MockAdapter(axios); beforeAll(() => { mock.reset(); - queryClient.setDefaultOptions({ - queries: { - retry: false, // Disable retries for tests - }, - }); }); beforeEach(() => { queryClient.clear(); + mock.reset(); }); test('should render successfully', async () => { mock.onGet(new RegExp('/spacecraft/*')).reply(200, mockData.items[0]); + queryClient.setQueryData(['details'], mockData.items[0]); jest.spyOn(useAuthMock, 'default').mockReturnValue({ isSignedIn: true, isLoading: false, @@ -65,6 +68,8 @@ describe('Details', () => { mock .onGet(new RegExp('/spacecraft/*')) .reply(500, { message: 'Internal Server Error' }); + queryClient.setQueryData(['details'], null); + const { baseElement } = render(componentWrapper); expect(baseElement).toBeTruthy(); expect(baseElement.querySelector('h1')?.textContent).toEqual('Details');