Skip to content

Commit 718393c

Browse files
Fix: [AEA-6003] - patient banner shown on wrong pages (#1862)
## Summary https://nhsd-jira.digital.nhs.uk/browse/AEA-6003 - Routine Change ### Details if a user clicked change role while searching for a prescription, then it would take them to the /site/change-your-role page but still then render the RBAC banner when its info had loaded. Now blocked it from showing on pages where it shouldn't, but still renders correctly when a user visits the search/view prescription pages
1 parent 4ee083f commit 718393c

File tree

4 files changed

+137
-5
lines changed

4 files changed

+137
-5
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import React from "react"
2+
import {render, screen} from "@testing-library/react"
3+
import {MemoryRouter} from "react-router-dom"
4+
import Layout from "@/Layout"
5+
import {PatientDetailsProvider} from "@/context/PatientDetailsProvider"
6+
import {AuthProvider} from "@/context/AuthProvider"
7+
import {PrescriptionInformationProvider} from "@/context/PrescriptionInformationProvider"
8+
import {FRONTEND_PATHS} from "@/constants/environment"
9+
10+
// Mock the components that aren't relevant to this test
11+
jest.mock("@/components/EpsHeader", () => {
12+
return function MockEpsHeader() {
13+
return <div data-testid="eps-header">EpsHeader</div>
14+
}
15+
})
16+
17+
jest.mock("@/components/EpsFooter", () => {
18+
return function MockEpsFooter() {
19+
return <div data-testid="eps-footer">EpsFooter</div>
20+
}
21+
})
22+
23+
jest.mock("@/components/RBACBanner", () => {
24+
return function MockRBACBanner() {
25+
return <div data-testid="rbac-banner">RBACBanner</div>
26+
}
27+
})
28+
29+
const TestWrapper = ({route, children}: {route: string; children?: React.ReactNode}) => (
30+
<MemoryRouter initialEntries={[route]}>
31+
<AuthProvider>
32+
<PatientDetailsProvider>
33+
<PrescriptionInformationProvider>
34+
{children}
35+
</PrescriptionInformationProvider>
36+
</PatientDetailsProvider>
37+
</AuthProvider>
38+
</MemoryRouter>
39+
)
40+
41+
describe("Layout Banner Visibility Tests", () => {
42+
it("should hide PatientDetailsBanner on /select-your-role page even with patient details", () => {
43+
render(
44+
<TestWrapper route={FRONTEND_PATHS.SELECT_YOUR_ROLE}>
45+
<Layout />
46+
</TestWrapper>
47+
)
48+
49+
// Should not show patient banner on select-your-role page
50+
expect(screen.queryByTestId("patient-details-banner")).not.toBeInTheDocument()
51+
52+
// Should show other components
53+
expect(screen.getByTestId("eps-header")).toBeInTheDocument()
54+
expect(screen.getByTestId("eps-footer")).toBeInTheDocument()
55+
expect(screen.getByTestId("rbac-banner")).toBeInTheDocument()
56+
})
57+
58+
it("should show PatientDetailsBanner on prescription list page when patient details exist", () => {
59+
render(
60+
<TestWrapper route={FRONTEND_PATHS.PRESCRIPTION_LIST_CURRENT}>
61+
<Layout />
62+
</TestWrapper>
63+
)
64+
65+
// PatientDetailsBanner will be rendered but won't show content without patient details
66+
// This tests that the banner is rendered on allowed pages
67+
expect(screen.getByTestId("eps-header")).toBeInTheDocument()
68+
expect(screen.getByTestId("eps-footer")).toBeInTheDocument()
69+
expect(screen.getByTestId("rbac-banner")).toBeInTheDocument()
70+
})
71+
72+
it("should hide PatientDetailsBanner on non-allowed pages", () => {
73+
render(
74+
<TestWrapper route="/some-other-page">
75+
<Layout />
76+
</TestWrapper>
77+
)
78+
79+
// Should not show patient banner on non-allowed pages
80+
expect(screen.queryByTestId("patient-details-banner")).not.toBeInTheDocument()
81+
})
82+
})

packages/cpt-ui/jest.setup.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,26 @@ jest.mock("@/constants/environment", () => ({
8585
PRESCRIPTION_LIST_CURRENT: "/prescription-list-current",
8686
PRESCRIPTION_LIST_FUTURE: "/prescription-list-future",
8787
PRESCRIPTION_LIST_PAST: "/prescription-list-past",
88+
COOKIES: "/cookies",
8889
LOGIN: "/login",
8990
LOGOUT: "/logout",
91+
SESSION_LOGGED_OUT: "/session-logged-out",
9092
SELECT_YOUR_ROLE: "/select-your-role",
9193
YOUR_SELECTED_ROLE: "/your-selected-role",
9294
CHANGE_YOUR_ROLE: "/change-your-role",
9395
SEARCH_BY_PRESCRIPTION_ID: "/search-by-prescription-id",
9496
SEARCH_BY_NHS_NUMBER: "/search-by-nhs-number",
9597
SEARCH_BY_BASIC_DETAILS: "/search-by-basic-details",
96-
TOO_MANY_SEARCH_RESULTS: "/too-many-search-results"
98+
PRESCRIPTION_DETAILS_PAGE: "/prescription-details",
99+
PATIENT_SEARCH_RESULTS: "/patient-search-results",
100+
TOO_MANY_SEARCH_RESULTS: "/too-many-search-results",
101+
PATIENT_NOT_FOUND: "/patient-not-found",
102+
NO_PATIENT_FOUND: "/no-patient-found",
103+
NO_PRESCRIPTIONS_FOUND: "/no-prescriptions-found",
104+
PRIVACY_NOTICE: "/privacy-notice",
105+
COOKIES_SELECTED: "/cookies-selected",
106+
SESSION_SELECTION: "/select-active-session",
107+
NOT_FOUND: "/notfound"
97108
},
98109
ALLOWED_NO_ROLE_PATHS: [
99110
"/login",
@@ -103,7 +114,31 @@ jest.mock("@/constants/environment", () => ({
103114
"/session-logged-out",
104115
"/cookies-selected",
105116
"/",
106-
"/select-active-session"
117+
"/select-active-session",
118+
"/select-your-role",
119+
"/change-your-role"
120+
],
121+
ALLOWED_NO_REDIRECT_PATHS: [
122+
"/login",
123+
"/logout",
124+
"/cookies",
125+
"/privacy-notice",
126+
"/session-logged-out",
127+
"/cookies-selected",
128+
"/",
129+
"/session-logged-out"
130+
],
131+
BANNER_ALLOWED_PATHS: [
132+
"/prescription-list-current",
133+
"/prescription-list-past",
134+
"/prescription-list-future",
135+
"/prescription-details"
136+
],
137+
AUTO_LOGIN_ENVIRONMENTS: [
138+
{environment: "dev", loginMethod: "mock"},
139+
{environment: "dev-pr", loginMethod: "mock"},
140+
{environment: "int", loginMethod: "cis2"},
141+
{environment: "prod", loginMethod: "cis2"}
107142
],
108143
PUBLIC_PATHS: [
109144
"/login",

packages/cpt-ui/src/Layout.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
import {Outlet} from "react-router-dom"
1+
import {Outlet, useLocation} from "react-router-dom"
22
import EpsHeader from "@/components/EpsHeader"
33
import RBACBanner from "@/components/RBACBanner"
44
import EpsFooter from "@/components/EpsFooter"
55
import PatientDetailsBanner from "@/components/PatientDetailsBanner"
66
import PrescriptionInformationBanner from "@/components/PrescriptionInformationBanner"
77
import {Fragment, ReactNode} from "react"
8+
import {normalizePath} from "@/helpers/utils"
9+
import {BANNER_ALLOWED_PATHS} from "@/constants/environment"
810

911
export default function Layout({children}: {children?: ReactNode}) {
12+
const location = useLocation()
13+
14+
const currentPath = normalizePath(location.pathname)
15+
const shouldShowBanners = !children && BANNER_ALLOWED_PATHS.includes(currentPath)
16+
1017
return (
1118
<Fragment>
1219
<EpsHeader />
13-
{!children && <PatientDetailsBanner />}
14-
{!children && <PrescriptionInformationBanner />}
20+
{shouldShowBanners && <PatientDetailsBanner />}
21+
{shouldShowBanners && <PrescriptionInformationBanner />}
1522
{children ?? <Outlet />}
1623
<RBACBanner />
1724
<EpsFooter />

packages/cpt-ui/src/constants/environment.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ export const ALLOWED_NO_REDIRECT_PATHS = [
104104
FRONTEND_PATHS.SESSION_LOGGED_OUT
105105
] as const
106106

107+
// pages where patient and prescription banners should be shown
108+
export const BANNER_ALLOWED_PATHS = [
109+
FRONTEND_PATHS.PRESCRIPTION_LIST_CURRENT,
110+
FRONTEND_PATHS.PRESCRIPTION_LIST_PAST,
111+
FRONTEND_PATHS.PRESCRIPTION_LIST_FUTURE,
112+
FRONTEND_PATHS.PRESCRIPTION_DETAILS_PAGE
113+
] as const
114+
107115
// Type for environment
108116
export type MockAuthEnvironment = "dev" | "dev-pr" | "ref" | "qa"
109117

0 commit comments

Comments
 (0)