Skip to content

Commit

Permalink
Update unit tests to properly mock react query calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouder committed Jun 27, 2024
1 parent f1c1207 commit 70c3a91
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
17 changes: 11 additions & 6 deletions src/pages/dashboard/dashboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
<AuthProvider>
<RecoilRoot>
Expand All @@ -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,
Expand All @@ -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();
Expand Down
17 changes: 11 additions & 6 deletions src/pages/details/details.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
<AuthProvider>
<RecoilRoot>
Expand All @@ -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,
Expand All @@ -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');
Expand Down

0 comments on commit 70c3a91

Please sign in to comment.