Skip to content

Commit

Permalink
[PRMDR-873] improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
joefong-nhs committed May 17, 2024
1 parent 455b6f1 commit 63aec83
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
51 changes: 48 additions & 3 deletions app/src/components/blocks/_arf/selectStage/SelectStage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ import { PatientDetails } from '../../../../types/generic/patientDetails';
import usePatient from '../../../../helpers/hooks/usePatient';
import uploadDocuments, { uploadDocumentToS3 } from '../../../../helpers/requests/uploadDocuments';
import { useState } from 'react';
// import axios from 'axios';
import { createMemoryHistory } from 'history';
import { SUBMISSION_STATE } from '../../../../types/pages/documentSearchResultsPage/types';
import { routes } from '../../../../types/generic/routes';

const mockedUseNavigate = jest.fn();

// jest.mock('axios');
jest.mock('../../../../helpers/requests/uploadDocuments');
const mockSetStage = jest.fn();
jest.mock('../../../../helpers/hooks/useBaseAPIHeaders');
Expand All @@ -32,8 +35,9 @@ jest.mock('../../../../helpers/utils/toFileList', () => ({
default: () => [],
}));
jest.mock('../../../../helpers/hooks/usePatient');

jest.mock('react-router', () => ({
useNavigate: () => jest.fn(),
useNavigate: () => mockedUseNavigate,
useLocation: () => jest.fn(),
}));
const mockedUsePatient = usePatient as jest.Mock;
Expand Down Expand Up @@ -427,6 +431,47 @@ describe('<SelectStage />', () => {
expect(mockSetStage).toHaveBeenCalledWith(UPLOAD_STAGE.Complete);
});
});

it('navigates to session expire page when API returns 403', async () => {
const errorResponse = {
response: {
status: 403,
message: 'Forbidden',
},
};
mockUploadDocument.mockRejectedValue(errorResponse);

renderApp();
act(() => {
userEvent.upload(screen.getByTestId('ARF-input'), [documentOne]);
userEvent.click(screen.getByRole('button', { name: 'Upload' }));
});

await waitFor(() => {
expect(mockedUseNavigate).toHaveBeenCalledWith(routes.SESSION_EXPIRED);
});
});

it('navigates to error page when API returns other error', async () => {
const errorResponse = {
response: {
status: 502,
},
};
mockUploadDocument.mockRejectedValue(errorResponse);

renderApp();
act(() => {
userEvent.upload(screen.getByTestId('ARF-input'), [documentOne]);
userEvent.click(screen.getByRole('button', { name: 'Upload' }));
});

await waitFor(() => {
expect(mockedUseNavigate).toHaveBeenCalledWith(
expect.stringContaining('/server-error?encodedError='),
);
});
});
});

const renderApp = () => {
Expand Down
4 changes: 2 additions & 2 deletions app/src/components/blocks/_arf/selectStage/SelectStage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ function SelectStage({ setDocuments, setStage, documents }: Props) {
const baseHeaders = useBaseAPIHeaders();
const navigate = useNavigate();
const arfInputRef = useRef<HTMLInputElement | null>(null);
// const lgInputRef = useRef<HTMLInputElement | null>(null);
// const lgInputRef = useRef<HTMLInputElement | null>(null); // NO SONAR
const patientDetails = usePatient();
const nhsNumber: string = patientDetails?.nhsNumber ?? '';
const mergedDocuments = [...arfDocuments, ...lgDocuments];
const hasFileInput = mergedDocuments.length > 0;

const { handleSubmit, control, formState, setError } = useForm();

// const lgController = useController(lloydGeorgeFormConfig(control));
// const lgController = useController(lloydGeorgeFormConfig(control)); // NO SONAR
const arfController = useController(ARFFormConfig(control));

const submitDocuments = async () => {
Expand Down

0 comments on commit 63aec83

Please sign in to comment.