Skip to content

Commit

Permalink
Use test render util
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfranklin committed Nov 11, 2024
1 parent e9c9656 commit e72d73f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 45 deletions.
1 change: 0 additions & 1 deletion react/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ module.exports = {
'!src/**/*.spec.{js,jsx,ts,tsx}',
// Exclude test utilities
'!src/test/**/*',
'!src/testUtil.ts',
'!src/__fixtures__/**/*',
// Exclude secrets and other files
'!src/secret_local*ts',
Expand Down
14 changes: 3 additions & 11 deletions react/src/pages/Callback/Callback.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import React from 'react';
import { Provider } from 'react-redux';
import { MemoryRouter } from 'react-router-dom';
import { render } from '@testing-library/react';
import store from '../../redux/store';
import { renderInTest } from '@hazmapper/test/testUtil';
import Callback from './Callback';

test('renders callback', async () => {
const { getByText } = render(
<Provider store={store}>
<MemoryRouter initialEntries={['/callback']}>
<Callback />
</MemoryRouter>
</Provider>
);
const { getByText } = renderInTest(<Callback />, '/callback');

expect(getByText(/Logging in/)).toBeDefined();
});
18 changes: 3 additions & 15 deletions react/src/pages/Login/Login.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import React from 'react';
import { render, waitFor } from '@testing-library/react';
import { waitFor } from '@testing-library/react';
import Login from './Login';
import { Provider } from 'react-redux';
import store from '../../redux/store';
import { QueryClientProvider } from 'react-query';
import { testQueryClient } from '../../test/testUtil';
import { MemoryRouter } from 'react-router';
import { renderInTest } from '../../test/testUtil';

beforeAll(() => {
const mockLocation = {
Expand All @@ -25,15 +21,7 @@ afterAll(() => {
});

test('renders login', async () => {
const { getByText } = render(
<Provider store={store}>
<MemoryRouter>
<QueryClientProvider client={testQueryClient}>
<Login />
</QueryClientProvider>
</MemoryRouter>
</Provider>
);
const { getByText } = renderInTest(<Login />);
expect(getByText(/Logging in/)).toBeDefined();

await waitFor(() => {
Expand Down
21 changes: 3 additions & 18 deletions react/src/pages/MainMenu/MainMenu.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import React from 'react';
import { render, waitFor } from '@testing-library/react';
import { http, HttpResponse } from 'msw';
import { waitFor } from '@testing-library/react';
import MainMenu from './MainMenu';
import { QueryClientProvider } from 'react-query';
import { testQueryClient, server } from '@hazmapper/test/testUtil';
import { Provider } from 'react-redux';
import store from '../../redux/store';
import { MemoryRouter } from 'react-router-dom';
import { testDevConfiguration } from '@hazmapper/__fixtures__/appConfigurationFixture';
import { systems } from '@hazmapper/__fixtures__/systemsFixture';
import { renderInTest } from '@hazmapper/test/testUtil';

jest.mock('@hazmapper/hooks/user/useAuthenticatedUser', () => ({
__esModule: true,
Expand All @@ -20,15 +13,7 @@ jest.mock('@hazmapper/hooks/user/useAuthenticatedUser', () => ({
}));

test('renders menu', async () => {
const { getByText } = render(
<Provider store={store}>
<MemoryRouter>
<QueryClientProvider client={testQueryClient}>
<MainMenu />
</QueryClientProvider>
</MemoryRouter>
</Provider>
);
const { getByText } = renderInTest(<MainMenu />);

// Wait for the "Main Menu" text to be rendered and then check it
await waitFor(() => {
Expand Down
33 changes: 33 additions & 0 deletions react/src/test/testUtil.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, { ReactElement } from 'react';
import { render } from '@testing-library/react';
import { Provider } from 'react-redux';
import { QueryClient, QueryClientProvider } from 'react-query';
import { MemoryRouter } from 'react-router-dom';
import { setupServer } from 'msw/node';
import store from '@hazmapper/redux/store';
import { defaultHandlers } from '@hazmapper/test/handlers';

export const server = setupServer(...defaultHandlers);

export const testQueryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
cacheTime: 0,
staleTime: 0,
useErrorBoundary: true,
},
},
});

export function renderInTest(children: ReactElement, path = '/') {
return render(
<Provider store={store}>
<MemoryRouter initialEntries={[path]}>
<QueryClientProvider client={testQueryClient}>
{children}
</QueryClientProvider>
</MemoryRouter>
</Provider>
);
}

0 comments on commit e72d73f

Please sign in to comment.