Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRMDR-506 Refactor UI Tests #170

Merged
merged 21 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f34b8ba
Migrate patient details context to hook
RioKnightleyNHS Nov 21, 2023
5b4f0da
useRole throws expectation
RioKnightleyNHS Nov 21, 2023
f57dd2d
Move useBaseUrl to standalone hook
RioKnightleyNHS Nov 21, 2023
eada065
Remove configprovider import
RioKnightleyNHS Nov 21, 2023
ebe5120
Remove configprovider
RioKnightleyNHS Nov 22, 2023
7e67c73
Remove unused providers from uploading stage tests
RioKnightleyNHS Nov 22, 2023
ec6e03c
Mock use navigate on patient search
RioKnightleyNHS Nov 23, 2023
fd1f82e
Replace tests for patient result page
RioKnightleyNHS Nov 23, 2023
0ad421c
Add mock values for lg record unit tests and patient guard
RioKnightleyNHS Nov 24, 2023
f5a0b6a
Add router mocks for delete stage
RioKnightleyNHS Nov 24, 2023
cb7e888
Remove all patient props from components
RioKnightleyNHS Nov 27, 2023
3e57a20
Merge branch 'main' into prmdr-506
RioKnightleyNHS Nov 27, 2023
335127e
Remove configprovider from back button
RioKnightleyNHS Nov 27, 2023
b62262f
Mock usenavigate and uselocation for back button
RioKnightleyNHS Nov 27, 2023
c81ee9b
Remove patientdetails props from components
RioKnightleyNHS Nov 27, 2023
7905f72
Mock router tests for document components
RioKnightleyNHS Nov 28, 2023
3cc4665
Update authcallback router tests
RioKnightleyNHS Nov 28, 2023
4649b60
Merge branch 'main' into prmdr-506
RioKnightleyNHS Nov 28, 2023
65342f4
Fix build error on unrequired prop
RioKnightleyNHS Nov 28, 2023
a39c297
Remove baseApiHeaders throw
RioKnightleyNHS Nov 28, 2023
0a680e5
Add missing usePatient test
RioKnightleyNHS Nov 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions app/.storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import type { Preview } from '@storybook/react';
import '../src/styles/App.scss';
import { MemoryRouter } from 'react-router';
import PatientDetailsProvider from '../src/providers/patientProvider/PatientProvider';
import ConfigProvider from '../src/providers/configProvider/ConfigProvider';
import config from '../src/config';

import { buildPatientDetails } from '../src/helpers/test/testBuilders';
const preview: Preview = {
Expand All @@ -19,33 +17,31 @@ const preview: Preview = {
},
decorators: [
(Story) => (
<ConfigProvider config={config}>
<PatientDetailsProvider patientDetails={{ ...buildPatientDetails() }}>
<MemoryRouter initialEntries={['/']}>
<div
className="nhsuk-width-container"
style={{
margin: `0 auto`,
maxWidth: 960,
padding: `0 1.0875rem 1.45rem`,
minHeight: '75vh',
}}
<PatientDetailsProvider patientDetails={{ ...buildPatientDetails() }}>
<MemoryRouter initialEntries={['/']}>
<div
className="nhsuk-width-container"
style={{
margin: `0 auto`,
maxWidth: 960,
padding: `0 1.0875rem 1.45rem`,
minHeight: '75vh',
}}
>
<main
className="nhsuk-main-wrapper app-homepage"
id="maincontent"
role="main"
>
<main
className="nhsuk-main-wrapper app-homepage"
id="maincontent"
role="main"
>
<section className="app-homepage-content">
<div>
<Story />
</div>
</section>
</main>
</div>
</MemoryRouter>
</PatientDetailsProvider>
</ConfigProvider>
<section className="app-homepage-content">
<div>
<Story />
</div>
</section>
</main>
</div>
</MemoryRouter>
</PatientDetailsProvider>
),
],
};
Expand Down
14 changes: 5 additions & 9 deletions app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import React from 'react';
import './styles/App.scss';
import ConfigProvider from './providers/configProvider/ConfigProvider';
import config from './config';
import PatientDetailsProvider from './providers/patientProvider/PatientProvider';
import SessionProvider from './providers/sessionProvider/SessionProvider';
import AppRouter from './router/AppRouter';

function App() {
return (
<ConfigProvider config={config}>
<SessionProvider>
<PatientDetailsProvider>
<AppRouter />
</PatientDetailsProvider>
</SessionProvider>
</ConfigProvider>
<SessionProvider>
<PatientDetailsProvider>
<AppRouter />
</PatientDetailsProvider>
</SessionProvider>
);
}

Expand Down
15 changes: 14 additions & 1 deletion app/src/components/blocks/completeStage/CompleteStage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,23 @@ import {
import { buildPatientDetails, buildTextFile } from '../../../helpers/test/testBuilders';
import CompleteStage from './CompleteStage';
import { useNavigate } from 'react-router';
import usePatient from '../../../helpers/hooks/usePatient';

jest.mock('react-router');
jest.mock('../../../helpers/hooks/usePatient');

const mockedUsePatient = usePatient as jest.Mock;
const mockPatientDetails = buildPatientDetails();

describe('<CompleteStage />', () => {
beforeEach(() => {
process.env.REACT_APP_ENVIRONMENT = 'jest';
mockedUsePatient.mockReturnValue(mockPatientDetails);
});
afterEach(() => {
jest.clearAllMocks();
});

describe('Show complete stage', () => {
it('with successfully uploaded docs', async () => {
const navigateMock = jest.fn();
Expand Down Expand Up @@ -40,7 +53,7 @@ describe('<CompleteStage />', () => {
// @ts-ignore
useNavigate.mockImplementation(() => navigateMock);
const documents: Array<UploadDocument> = [documentOne, documentTwo, documentThree];
render(<CompleteStage patientDetails={mockPatientDetails} documents={documents} />);
render(<CompleteStage documents={documents} />);
expect(
await screen.findByRole('heading', { name: 'Upload Summary' }),
).toBeInTheDocument();
Expand Down
6 changes: 2 additions & 4 deletions app/src/components/blocks/completeStage/CompleteStage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import { Button } from 'nhsuk-react-components';
import { useNavigate } from 'react-router';
import { UploadDocument } from '../../../types/pages/UploadDocumentsPage/types';
import UploadSummary from '../uploadSummary/UploadSummary';
import { PatientDetails } from '../../../types/generic/patientDetails';
interface Props {
documents: Array<UploadDocument>;
patientDetails: PatientDetails;
}

function CompleteStage({ documents, patientDetails }: Props) {
function CompleteStage({ documents }: Props) {
const navigate = useNavigate();

return (
<>
<UploadSummary patientDetails={patientDetails} documents={documents} />
<UploadSummary documents={documents} />
<p style={{ fontWeight: '600' }}>
If you want to upload another patient&apos;s health record
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ import { act } from 'react-dom/test-utils';
import userEvent from '@testing-library/user-event';
import { DOCUMENT_TYPE } from '../../../types/pages/UploadDocumentsPage/types';
import axios from 'axios/index';
import * as ReactRouter from 'react-router';
import { createMemoryHistory } from 'history';
import useRole from '../../../helpers/hooks/useRole';
import { REPOSITORY_ROLE, authorisedRoles } from '../../../types/generic/authRole';
import { routes } from '../../../types/generic/routes';
import { LG_RECORD_STAGE } from '../../../types/blocks/lloydGeorgeStages';
import usePatient from '../../../helpers/hooks/usePatient';

jest.mock('../deletionConfirmationStage/DeletionConfirmationStage', () => () => (
<div>Deletion complete</div>
));
jest.mock('../../../helpers/hooks/useBaseAPIHeaders');
jest.mock('../../../helpers/hooks/useRole');
jest.mock('../../../helpers/hooks/usePatient');
jest.mock('axios');
const mockedUseNavigate = jest.fn();
jest.mock('react-router', () => ({
useNavigate: () => mockedUseNavigate,
}));

const mockedUseRole = useRole as jest.Mock;
const mockedAxios = axios as jest.Mocked<typeof axios>;
const mockedUsePatient = usePatient as jest.Mock;

const mockPatientDetails = buildPatientDetails();
const mockLgSearchResult = buildLgSearchResult();
Expand All @@ -29,6 +38,7 @@ const mockSetDownloadStage = jest.fn();
describe('DeleteDocumentsStage', () => {
beforeEach(() => {
process.env.REACT_APP_ENVIRONMENT = 'jest';
mockedUsePatient.mockReturnValue(mockPatientDetails);
});

afterEach(() => {
Expand Down Expand Up @@ -178,11 +188,6 @@ describe('DeleteDocumentsStage', () => {

describe('Navigation', () => {
it('navigates to home page when API call returns 403', async () => {
const history = createMemoryHistory({
initialEntries: ['/example'],
initialIndex: 1,
});

const errorResponse = {
response: {
status: 403,
Expand All @@ -192,9 +197,7 @@ describe('Navigation', () => {
mockedAxios.delete.mockImplementation(() => Promise.reject(errorResponse));
mockedUseRole.mockReturnValue(REPOSITORY_ROLE.PCSE);

renderComponent(DOCUMENT_TYPE.ALL, history);

expect(history.location.pathname).toBe('/example');
renderComponent(DOCUMENT_TYPE.ALL);

expect(screen.getByRole('radio', { name: 'Yes' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Continue' })).toBeInTheDocument();
Expand All @@ -205,32 +208,23 @@ describe('Navigation', () => {
});

await waitFor(() => {
expect(history.location.pathname).toBe(routes.HOME);
expect(mockedUseNavigate).toHaveBeenCalledWith(routes.HOME);
});
});
});

const homeRoute = '/example';
const renderComponent = (
docType: DOCUMENT_TYPE,
history = createMemoryHistory({
initialEntries: [homeRoute],
}),
) => {
const renderComponent = (docType: DOCUMENT_TYPE) => {
const props: Omit<Props, 'setStage' | 'setIsDeletingDocuments' | 'setDownloadStage'> = {
patientDetails: mockPatientDetails,
numberOfFiles: mockLgSearchResult.number_of_files,
docType,
};

render(
<ReactRouter.Router navigator={history} location={homeRoute}>
<DeleteDocumentsStage
{...props}
setStage={mockSetStage}
setIsDeletingDocuments={mockSetIsDeletingDocuments}
setDownloadStage={mockSetDownloadStage}
/>
</ReactRouter.Router>,
<DeleteDocumentsStage
{...props}
setStage={mockSetStage}
setIsDeletingDocuments={mockSetIsDeletingDocuments}
setDownloadStage={mockSetDownloadStage}
/>,
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import React, { Dispatch, SetStateAction, useState } from 'react';
import { FieldValues, useForm } from 'react-hook-form';
import { Button, Fieldset, Radios } from 'nhsuk-react-components';
import { getFormattedDate } from '../../../helpers/utils/formatDate';
import { PatientDetails } from '../../../types/generic/patientDetails';
import DeletionConfirmationStage from '../deletionConfirmationStage/DeletionConfirmationStage';
import deleteAllDocuments, { DeleteResponse } from '../../../helpers/requests/deleteAllDocuments';
import { useBaseAPIUrl } from '../../../providers/configProvider/ConfigProvider';
import useBaseAPIHeaders from '../../../helpers/hooks/useBaseAPIHeaders';
import { DOCUMENT_TYPE } from '../../../types/pages/UploadDocumentsPage/types';
import { DOWNLOAD_STAGE } from '../../../types/generic/downloadStage';
Expand All @@ -19,11 +17,12 @@ import { useNavigate } from 'react-router-dom';
import useRole from '../../../helpers/hooks/useRole';
import { REPOSITORY_ROLE } from '../../../types/generic/authRole';
import { LG_RECORD_STAGE } from '../../../types/blocks/lloydGeorgeStages';
import useBaseAPIUrl from '../../../helpers/hooks/useBaseAPIUrl';
import usePatient from '../../../helpers/hooks/usePatient';

export type Props = {
docType: DOCUMENT_TYPE;
numberOfFiles: number;
patientDetails: PatientDetails;
setStage?: Dispatch<SetStateAction<LG_RECORD_STAGE>>;
setIsDeletingDocuments?: Dispatch<SetStateAction<boolean>>;
setDownloadStage?: Dispatch<SetStateAction<DOWNLOAD_STAGE>>;
Expand All @@ -37,11 +36,11 @@ enum DELETE_DOCUMENTS_OPTION {
function DeleteDocumentsStage({
docType,
numberOfFiles,
patientDetails,
setStage,
setIsDeletingDocuments,
setDownloadStage,
}: Props) {
const patientDetails = usePatient();
const role = useRole();
const { register, handleSubmit } = useForm();
const { ref: deleteDocsRef, ...radioProps } = register('deleteDocs');
Expand Down Expand Up @@ -155,11 +154,7 @@ function DeleteDocumentsStage({
</form>
</>
) : (
<DeletionConfirmationStage
numberOfFiles={numberOfFiles}
patientDetails={patientDetails}
setStage={setStage}
/>
<DeletionConfirmationStage numberOfFiles={numberOfFiles} setStage={setStage} />
);
}
export default DeleteDocumentsStage;
Loading
Loading