Skip to content

Commit cba93ca

Browse files
add more unit test coverage
1 parent b81083b commit cba93ca

File tree

2 files changed

+127
-11
lines changed

2 files changed

+127
-11
lines changed

app/src/pages/documentSearchResultsPage/DocumentSearchResultsPage.test.tsx

Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
buildSearchResult,
88
buildUserAuth,
99
} from '../../helpers/test/testBuilders';
10-
import { routes } from '../../types/generic/routes';
10+
import { routeChildren, routes } from '../../types/generic/routes';
1111
import axios from 'axios';
1212
import usePatient from '../../helpers/hooks/usePatient';
1313
import * as ReactRouter from 'react-router-dom';
@@ -17,6 +17,8 @@ import { afterEach, beforeEach, describe, expect, it, vi, Mock, Mocked } from 'v
1717
import SessionProvider, { Session } from '../../providers/sessionProvider/SessionProvider';
1818
import { REPOSITORY_ROLE } from '../../types/generic/authRole';
1919
import getDocumentSearchResults from '../../helpers/requests/getDocumentSearchResults';
20+
import getDocument from '../../helpers/requests/getDocument';
21+
import useConfig from '../../helpers/hooks/useConfig';
2022

2123
const mockedUseNavigate = vi.fn();
2224
vi.mock('react-router-dom', async () => ({
@@ -31,10 +33,13 @@ vi.mock('../../helpers/hooks/useBaseAPIHeaders');
3133
vi.mock('../../helpers/hooks/usePatient');
3234
vi.mock('../../helpers/hooks/useConfig');
3335
vi.mock('../../helpers/requests/getDocumentSearchResults');
36+
vi.mock('../../helpers/requests/getDocument');
3437

3538
const mockedAxios = axios as Mocked<typeof axios>;
3639
const mockedUsePatient = usePatient as Mock;
3740
const mockedGetSearchResults = getDocumentSearchResults as Mock;
41+
const mockedGetDocument = getDocument as Mock;
42+
const mockedUseConfig = useConfig as Mock;
3843
const mockPatient = buildPatientDetails();
3944

4045
let history = createMemoryHistory({
@@ -52,6 +57,11 @@ describe('<DocumentSearchResultsPage />', () => {
5257

5358
import.meta.env.VITE_ENVIRONMENT = 'vitest';
5459
mockedUsePatient.mockReturnValue(mockPatient);
60+
mockedUseConfig.mockReturnValue({
61+
featureFlags: {
62+
uploadDocumentIteration3Enabled: true,
63+
},
64+
});
5565
});
5666
afterEach(() => {
5767
vi.clearAllMocks();
@@ -66,9 +76,7 @@ describe('<DocumentSearchResultsPage />', () => {
6676
])(
6777
'renders the page after a successful response from api when role is $role',
6878
async ({ role, title }) => {
69-
mockedAxios.get.mockResolvedValue(async () => {
70-
return Promise.resolve([buildSearchResult()]);
71-
});
79+
mockedGetSearchResults.mockResolvedValue([buildSearchResult()]);
7280

7381
renderPage(history, role);
7482

@@ -118,6 +126,22 @@ describe('<DocumentSearchResultsPage />', () => {
118126
).not.toBeInTheDocument();
119127
expect(screen.queryByTestId('delete-all-documents-btn')).not.toBeInTheDocument();
120128
});
129+
130+
it('displays a service error when document search fails with bad request', async () => {
131+
const errorResponse = {
132+
response: {
133+
status: 400,
134+
message: 'bad request',
135+
},
136+
};
137+
mockedGetSearchResults.mockRejectedValueOnce(errorResponse);
138+
139+
renderPage(history);
140+
141+
await waitFor(() => {
142+
expect(screen.getByTestId('service-error')).toBeInTheDocument();
143+
});
144+
});
121145
});
122146

123147
describe('Accessibility', () => {
@@ -191,6 +215,102 @@ describe('<DocumentSearchResultsPage />', () => {
191215
expect(mockedUseNavigate).toHaveBeenCalledWith(routes.SESSION_EXPIRED);
192216
});
193217
});
218+
219+
it('navigates to server error page when document search return 500 server error', async () => {
220+
const errorResponse = {
221+
response: {
222+
status: 500,
223+
message: 'An error occurred',
224+
},
225+
};
226+
mockedGetSearchResults.mockRejectedValueOnce(errorResponse);
227+
228+
renderPage(history);
229+
230+
await waitFor(() => {
231+
expect(mockedUseNavigate).toHaveBeenCalledWith(expect.stringContaining(routes.SERVER_ERROR));
232+
});
233+
});
234+
235+
it('loads the document and navigates to view screen on view link clicked', async () => {
236+
mockedGetSearchResults.mockResolvedValue([buildSearchResult()]);
237+
238+
renderPage(history);
239+
240+
await waitFor(() => {
241+
expect(
242+
screen.queryByRole('progressbar', { name: 'Loading...' }),
243+
).not.toBeInTheDocument();
244+
});
245+
246+
const viewLink = screen.getByTestId('view-0-link');
247+
await act(async () => {
248+
await userEvent.click(viewLink);
249+
});
250+
251+
expect(mockedGetDocument).toHaveBeenCalledTimes(1);
252+
expect(mockedUseNavigate).toHaveBeenCalledWith(routeChildren.DOCUMENT_VIEW);
253+
});
254+
255+
it('navigates to server error when load document fails with 500', async () => {
256+
mockedGetSearchResults.mockResolvedValue([buildSearchResult()]);
257+
258+
const errorResponse = {
259+
response: {
260+
status: 500,
261+
message: 'server error',
262+
},
263+
};
264+
mockedGetDocument.mockRejectedValue(errorResponse);
265+
266+
renderPage(history);
267+
268+
await waitFor(() => {
269+
expect(
270+
screen.queryByRole('progressbar', { name: 'Loading...' }),
271+
).not.toBeInTheDocument();
272+
});
273+
274+
const viewLink = screen.getByTestId('view-0-link');
275+
await act(async () => {
276+
await userEvent.click(viewLink);
277+
});
278+
279+
expect(mockedGetDocument).toHaveBeenCalledTimes(1);
280+
expect(mockedUseNavigate).toHaveBeenCalledWith(routeChildren.DOCUMENT_VIEW);
281+
expect(mockedUseNavigate).toHaveBeenCalledWith(
282+
expect.stringContaining(routes.SERVER_ERROR),
283+
);
284+
});
285+
286+
it('navigates to session expired when load document fails with 403', async () => {
287+
mockedGetSearchResults.mockResolvedValue([buildSearchResult()]);
288+
289+
const errorResponse = {
290+
response: {
291+
status: 403,
292+
message: 'forbidden',
293+
},
294+
};
295+
mockedGetDocument.mockRejectedValue(errorResponse);
296+
297+
renderPage(history);
298+
299+
await waitFor(() => {
300+
expect(
301+
screen.queryByRole('progressbar', { name: 'Loading...' }),
302+
).not.toBeInTheDocument();
303+
});
304+
305+
const viewLink = screen.getByTestId('view-0-link');
306+
await act(async () => {
307+
await userEvent.click(viewLink);
308+
});
309+
310+
expect(mockedGetDocument).toHaveBeenCalledTimes(1);
311+
expect(mockedUseNavigate).toHaveBeenCalledWith(routeChildren.DOCUMENT_VIEW);
312+
expect(mockedUseNavigate).toHaveBeenCalledWith(routes.SESSION_EXPIRED);
313+
});
194314
});
195315

196316
const renderPage = (history: History, role?: REPOSITORY_ROLE): void => {

app/src/pages/documentSearchResultsPage/DocumentSearchResultsPage.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ const DocumentSearchResultsPage = (): React.JSX.Element => {
6565
} catch (e) {
6666
const error = e as AxiosError;
6767
if (isMock(error)) {
68-
if (config.mockLocal.uploading) {
69-
setSubmissionState(SUBMISSION_STATE.BLOCKED);
70-
} else if (config.mockLocal.recordUploaded) {
68+
if (config.mockLocal.recordUploaded) {
7169
setSearchResults([
7270
buildSearchResult({
7371
documentSnomedCodeType: DOCUMENT_TYPE.LLOYD_GEORGE,
@@ -92,8 +90,6 @@ const DocumentSearchResultsPage = (): React.JSX.Element => {
9290
navigate(routes.SESSION_EXPIRED);
9391
} else if (error.response?.status && error.response?.status >= 500) {
9492
navigate(routes.SERVER_ERROR + errorToParams(error));
95-
} else if (error.response?.status === 423) {
96-
setSubmissionState(SUBMISSION_STATE.BLOCKED);
9793
} else {
9894
setSubmissionState(SUBMISSION_STATE.FAILED);
9995
}
@@ -112,8 +108,8 @@ const DocumentSearchResultsPage = (): React.JSX.Element => {
112108
...documentItem,
113109
});
114110
navigate(routeChildren.DOCUMENT_VIEW);
115-
116-
loadDocument(documentItem.id);
111+
112+
void loadDocument(documentItem.id);
117113
};
118114

119115
const loadDocument = async (documentId: string): Promise<void> => {

0 commit comments

Comments
 (0)