diff --git a/app/src/router/guards/patientGuard/PatientGuard.test.tsx b/app/src/router/guards/patientGuard/PatientGuard.test.tsx index ed9873351..f46f17a42 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 2f7f9b6e1..f94cedd41 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}; };