Skip to content

Commit

Permalink
Fix error display after record deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
RioKnightleyNHS authored Oct 4, 2024
1 parent ac3b5ea commit c3cd961
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,9 @@ describe('GP Workflow: View Lloyd George record', () => {
() => {
beforeEachConfiguration(Roles.GP_ADMIN);

let request = 0;
const replies = [
{ statusCode: 200, body: viewLloydGeorgePayload },
{ statusCode: 404 },
];

cy.intercept('GET', '/LloydGeorgeStitch*', (req) => {
req.reply(replies[request]);
request = request + 1;
cy.intercept('GET', '/LloydGeorgeStitch*', {
statusCode: 200,
body: viewLloydGeorgePayload,
}).as('lloydGeorgeStitch');

cy.intercept('GET', '/SearchDocumentReferences*', {
Expand Down Expand Up @@ -276,7 +270,17 @@ describe('GP Workflow: View Lloyd George record', () => {
`NHS number: ${formatNhsNumber(searchPatientPayload.nhsNumber)}`,
).should('be.visible');

cy.intercept('GET', '/LloydGeorgeStitch*', {
statusCode: 404,
}).as('lg-reload');
cy.getByTestId('lg-return-btn').click();
cy.wait('@lg-reload');

// Assert
cy.getByTestId('pdf-card').should('include.text', 'Lloyd George record');
cy.getByTestId('no-records-title').should('be.visible');
cy.getByTestId('upload-patient-record-text').should('be.visible');
cy.getByTestId('upload-patient-record-button').should('be.visible');
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ let history: MemoryHistory = createMemoryHistory({
const mockedUseRole = useRole as jest.Mock;
const mockedAxios = axios as jest.Mocked<typeof axios>;
const mockedUsePatient = usePatient as jest.Mock;

const mockResetDocState = jest.fn();
const mockPatientDetails = buildPatientDetails();
const mockLgSearchResult = buildLgSearchResult();

Expand Down Expand Up @@ -168,6 +168,26 @@ describe('DeleteSubmitStage', () => {
});
});

it('calls resetDocState when the Yes is selected and Continue clicked', async () => {
mockedUseRole.mockReturnValue(REPOSITORY_ROLE.GP_ADMIN);

mockedAxios.delete.mockReturnValue(Promise.resolve({ status: 200, data: 'Success' }));

renderComponent(DOCUMENT_TYPE.LLOYD_GEORGE, history);

expect(screen.getByRole('radio', { name: 'Yes' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Continue' })).toBeInTheDocument();

act(() => {
userEvent.click(screen.getByRole('radio', { name: 'Yes' }));
userEvent.click(screen.getByRole('button', { name: 'Continue' }));
});

await waitFor(() => {
expect(mockResetDocState).toHaveBeenCalled();
});
});

it('renders DeletionConfirmationStage when the Yes is selected and Continue clicked, when user role is PCSE', async () => {
mockedUseRole.mockReturnValue(REPOSITORY_ROLE.PCSE);

Expand Down Expand Up @@ -343,6 +363,7 @@ const renderComponent = (docType: DOCUMENT_TYPE, history: MemoryHistory) => {
numberOfFiles: mockLgSearchResult.number_of_files,
docType,
recordType: docType.toString(),
resetDocState: mockResetDocState,
};

return render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type Props = {
numberOfFiles: number;
setDownloadStage?: Dispatch<SetStateAction<DOWNLOAD_STAGE>>;
recordType: string;
resetDocState: () => void;
};

enum DELETE_DOCUMENTS_OPTION {
Expand All @@ -42,9 +43,10 @@ enum DELETE_DOCUMENTS_OPTION {
type IndexViewProps = {
docType: DOCUMENT_TYPE;
recordType: string;
resetDocState: () => void;
};

const DeleteSubmitStageIndexView = ({ docType, recordType }: IndexViewProps) => {
const DeleteSubmitStageIndexView = ({ docType, recordType, resetDocState }: IndexViewProps) => {
const patientDetails = usePatient();
const role = useRole();
const { register, handleSubmit } = useForm();
Expand All @@ -62,6 +64,7 @@ const DeleteSubmitStageIndexView = ({ docType, recordType }: IndexViewProps) =>

const handleYesOption = async () => {
const onSuccess = () => {
resetDocState();
setDeletionStage(SUBMISSION_STATE.SUCCEEDED);
if (userIsGP) {
navigate(routeChildren.LLOYD_GEORGE_DELETE_COMPLETE);
Expand Down Expand Up @@ -189,7 +192,13 @@ const DeleteSubmitStageIndexView = ({ docType, recordType }: IndexViewProps) =>
);
};

function DeleteSubmitStage({ docType, numberOfFiles, setDownloadStage, recordType }: Props) {
function DeleteSubmitStage({
docType,
numberOfFiles,
setDownloadStage,
recordType,
resetDocState,
}: Props) {
useTitle({ pageTitle: 'Delete files' });

return (
Expand All @@ -198,7 +207,11 @@ function DeleteSubmitStage({ docType, numberOfFiles, setDownloadStage, recordTyp
<Route
index
element={
<DeleteSubmitStageIndexView docType={docType} recordType={recordType} />
<DeleteSubmitStageIndexView
docType={docType}
recordType={recordType}
resetDocState={resetDocState}
/>
}
/>
<Route
Expand All @@ -208,6 +221,7 @@ function DeleteSubmitStage({ docType, numberOfFiles, setDownloadStage, recordTyp
docType={docType}
numberOfFiles={numberOfFiles}
recordType={recordType}
resetDocState={resetDocState}
/>
}
></Route>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const mockUseNavigate = jest.fn();
const mockUsePatient = usePatient as jest.Mock;
const mockPatientDetails = buildPatientDetails();
const mockDownloadStage = jest.fn();
const mockResetDocState = jest.fn();

const testFileName1 = 'John_1';
const testFileName2 = 'John_2';
Expand Down Expand Up @@ -178,6 +179,7 @@ const renderComponent = (history: MemoryHistory, numberOfFiles: number, recordTy
numberOfFiles={numberOfFiles}
recordType={recordType}
setDownloadStage={mockDownloadStage}
resetDocState={mockResetDocState}
/>
</ReactRouter.Router>,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export type Props = {
numberOfFiles: number;
recordType: string;
setDownloadStage: Dispatch<SetStateAction<DOWNLOAD_STAGE>>;
resetDocState: () => void;
};

function RemoveRecordStage({ numberOfFiles, recordType, setDownloadStage }: Props) {
function RemoveRecordStage({ numberOfFiles, recordType, setDownloadStage, resetDocState }: Props) {
useTitle({ pageTitle: 'Remove record' });
const patientDetails = usePatient();
const [submissionState, setSubmissionState] = useState(SUBMISSION_STATE.PENDING);
Expand Down Expand Up @@ -191,6 +192,7 @@ function RemoveRecordStage({ numberOfFiles, recordType, setDownloadStage }: Prop
docType={DOCUMENT_TYPE.LLOYD_GEORGE}
numberOfFiles={numberOfFiles}
recordType="Lloyd George"
resetDocState={resetDocState}
/>
}
></Route>
Expand All @@ -201,6 +203,7 @@ function RemoveRecordStage({ numberOfFiles, recordType, setDownloadStage }: Prop
docType={DOCUMENT_TYPE.LLOYD_GEORGE}
numberOfFiles={numberOfFiles}
recordType="ARF"
resetDocState={resetDocState}
/>
}
></Route>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ function DocumentSearchResultsPage() {
const baseUrl = useBaseAPIUrl();
const baseHeaders = useBaseAPIHeaders();
const config = useConfig();

const mounted = useRef(false);

useEffect(() => {
const onPageLoad = async () => {
setSubmissionState(SUBMISSION_STATE.PENDING);
Expand Down Expand Up @@ -101,6 +101,7 @@ function DocumentSearchResultsPage() {
recordType="ARF"
numberOfFiles={searchResults.length}
docType={DOCUMENT_TYPE.ALL}
resetDocState={() => null}
/>
}
/>
Expand Down
10 changes: 9 additions & 1 deletion app/src/pages/lloydGeorgeRecordPage/LloydGeorgeRecordPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ function LloydGeorgeRecordPage() {
const [lastUpdated, setLastUpdated] = useState('');
const [cloudFrontUrl, setCloudFrontUrl] = useState('');

const resetDocState = () => {
setNumberOfFiles(0);
setLastUpdated('');
setTotalFileSizeInByte(0);
setCloudFrontUrl('');
setDownloadStage(DOWNLOAD_STAGE.INITIAL);
};

const refreshRecord = async () => {
const onSuccess = (
files_count: number,
Expand All @@ -51,7 +59,6 @@ function LloydGeorgeRecordPage() {
setLastUpdated(getFormattedDatetime(new Date(updated_date)));
setDownloadStage(DOWNLOAD_STAGE.SUCCEEDED);
setTotalFileSizeInByte(file_size);
setDownloadStage(DOWNLOAD_STAGE.SUCCEEDED);
setCloudFrontUrl(presign_url);
};

Expand Down Expand Up @@ -135,6 +142,7 @@ function LloydGeorgeRecordPage() {
setDownloadStage={setDownloadStage}
numberOfFiles={numberOfFiles}
recordType="Lloyd George"
resetDocState={resetDocState}
/>
}
/>
Expand Down

0 comments on commit c3cd961

Please sign in to comment.