From b04d045dff5170ffe911f6027f028a9f630e200b Mon Sep 17 00:00:00 2001 From: adamwhitingnhs Date: Wed, 18 Mar 2026 16:12:49 +0000 Subject: [PATCH] [PRMP-1614] Fix patient guard --- app/src/router/guards/patientGuard/PatientGuard.test.tsx | 6 +++++- app/src/router/guards/patientGuard/PatientGuard.tsx | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/src/router/guards/patientGuard/PatientGuard.test.tsx b/app/src/router/guards/patientGuard/PatientGuard.test.tsx index ed9873351b..f46f17a42e 100644 --- a/app/src/router/guards/patientGuard/PatientGuard.test.tsx +++ b/app/src/router/guards/patientGuard/PatientGuard.test.tsx @@ -1,4 +1,4 @@ -import { render, RenderResult, waitFor } from '@testing-library/react'; +import { render, RenderResult, waitFor, screen } from '@testing-library/react'; import { routes } from '../../../types/generic/routes'; import PatientGuard from './PatientGuard'; import { buildPatientDetails } from '../../../helpers/test/testBuilders'; @@ -25,6 +25,10 @@ describe('AuthGuard', () => { renderGuard(); + expect(screen.queryAllByText(`patient number: ${patientDetails?.nhsNumber}`)).toHaveLength( + 0, + ); + await waitFor(async () => { expect(mockedUseNavigate).toHaveBeenCalledWith(routes.SEARCH_PATIENT); }); diff --git a/app/src/router/guards/patientGuard/PatientGuard.tsx b/app/src/router/guards/patientGuard/PatientGuard.tsx index 2f7f9b6e10..f94cedd41f 100644 --- a/app/src/router/guards/patientGuard/PatientGuard.tsx +++ b/app/src/router/guards/patientGuard/PatientGuard.tsx @@ -11,11 +11,17 @@ type Props = { const PatientGuard = ({ children, navigationPath }: Props): React.JSX.Element => { const patient = usePatient(); const navigate = useNavigate(); + useEffect(() => { if (!patient) { navigate(navigationPath || routes.SEARCH_PATIENT); } }, [patient, navigate, navigationPath]); + + if (!patient) { + return <>; + } + return <>{children}; };