Skip to content

Commit

Permalink
[PRMDR-803] add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RachelHowellNHS committed May 17, 2024
1 parent 4df58cb commit fffefe2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jest.mock('react-router');
jest.mock('../../../../helpers/hooks/useBaseAPIHeaders');
window.scrollTo = jest.fn() as jest.Mock;

const setStageMock = jest.fn();
const submitDocumentsMock = jest.fn();

const mockedUsePatient = usePatient as jest.Mock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from '@testing-library/react';
import { render, screen, waitFor } from '@testing-library/react';
import React from 'react';
import { buildPatientDetails, buildSearchResult } from '../../../../helpers/test/testBuilders';
import usePatient from '../../../../helpers/hooks/usePatient';
Expand All @@ -7,6 +7,7 @@ import { act } from 'react-dom/test-utils';
import LloydGeorgeSelectSearchResults from './LloydGeorgeSelectSearchResults';
import userEvent from '@testing-library/user-event';
import { routes } from '../../../../types/generic/routes';
import { SEARCH_AND_DOWNLOAD_STATE } from '../../../../types/pages/documentSearchResultsPage/types';

jest.mock('../../../../helpers/hooks/usePatient');
jest.mock('react-router', () => ({
Expand All @@ -18,11 +19,13 @@ jest.mock('react-router-dom', () => ({
useNavigate: () => mockNavigate,
}));

window.scrollTo = jest.fn() as jest.Mock;
const mockPatient = buildPatientDetails();
const mockedUsePatient = usePatient as jest.Mock;
const mockNavigate = jest.fn();
const mockSetSelectedDocuments = jest.fn();
const selectedDocuments = ['test-id-1', 'test-id-2'];
const mockSetSubmissionSearchState = jest.fn();
const mockSelectedDocuments = ['test-id-1', 'test-id-2'];
const searchResults = [
buildSearchResult({ fileName: '1of2_test.pdf', ID: 'test-id-1' }),
buildSearchResult({ fileName: '2of2_test.pdf', ID: 'test-id-2' }),
Expand All @@ -39,16 +42,14 @@ describe('LloydGeorgeSelectSearchResults', () => {
});

it('renders the page', () => {
act(() => {
render(
<LloydGeorgeSelectSearchResults
searchResults={searchResults}
setSubmissionSearchState={jest.fn}
selectedDocuments={selectedDocuments}
setSelectedDocuments={mockSetSelectedDocuments}
/>,
);
});
render(
<LloydGeorgeSelectSearchResults
searchResults={searchResults}
setSubmissionSearchState={mockSetSubmissionSearchState}
selectedDocuments={mockSelectedDocuments}
setSelectedDocuments={mockSetSelectedDocuments}
/>,
);

expect(
screen.getByRole('heading', {
Expand All @@ -65,19 +66,78 @@ describe('LloydGeorgeSelectSearchResults', () => {
});

it('navigates to start page when user clicks on start again link', () => {
render(
<LloydGeorgeSelectSearchResults
searchResults={searchResults}
setSubmissionSearchState={mockSetSubmissionSearchState}
selectedDocuments={mockSelectedDocuments}
setSelectedDocuments={mockSetSelectedDocuments}
/>,
);

act(() => {
render(
<LloydGeorgeSelectSearchResults
searchResults={searchResults}
setSubmissionSearchState={jest.fn}
selectedDocuments={selectedDocuments}
setSelectedDocuments={mockSetSelectedDocuments}
/>,
);
userEvent.click(screen.getByRole('link', { name: 'Start again' }));
});

userEvent.click(screen.getByRole('link', { name: 'Start again' }));

expect(mockNavigate).toHaveBeenCalledWith(routes.START);
});

it('sets submission state when download selected files button is clicked and not all files selected', () => {
render(
<LloydGeorgeSelectSearchResults
searchResults={searchResults}
setSubmissionSearchState={mockSetSubmissionSearchState}
selectedDocuments={mockSelectedDocuments}
setSelectedDocuments={mockSetSelectedDocuments}
/>,
);

act(() => {
userEvent.click(screen.getByTestId('download-selected-files-btn'));
});

expect(mockSetSubmissionSearchState).toHaveBeenCalledWith(
SEARCH_AND_DOWNLOAD_STATE.DOWNLOAD_SELECTED,
);
});

it('sets submission state and empties selected docs array when download selected files button is clicked but all files selected', () => {
render(
<LloydGeorgeSelectSearchResults
searchResults={searchResults}
setSubmissionSearchState={mockSetSubmissionSearchState}
selectedDocuments={['test-id-1', 'test-id-2', 'test-id-3']}
setSelectedDocuments={mockSetSelectedDocuments}
/>,
);

act(() => {
userEvent.click(screen.getByTestId('download-selected-files-btn'));
});

expect(mockSetSelectedDocuments).toHaveBeenCalledWith([]);
expect(mockSetSubmissionSearchState).toHaveBeenCalledWith(
SEARCH_AND_DOWNLOAD_STATE.DOWNLOAD_SELECTED,
);
});

it('shows error box when download selected files button is clicked but no files selected', async () => {
render(
<LloydGeorgeSelectSearchResults
searchResults={searchResults}
setSubmissionSearchState={mockSetSubmissionSearchState}
selectedDocuments={[]}
setSelectedDocuments={mockSetSelectedDocuments}
/>,
);

act(() => {
userEvent.click(screen.getByTestId('download-selected-files-btn'));
});

await waitFor(() => {
expect(screen.getByTestId('download-selection-error-box')).toBeInTheDocument();
});
expect(window.scrollTo).toHaveBeenCalledWith(0, 0);
});
});

0 comments on commit fffefe2

Please sign in to comment.