@@ -3,15 +3,19 @@ import {render, screen, waitFor} from "@testing-library/react"
33import { useRouter } from 'next/navigation'
44import React from "react"
55import SelectYourRolePage from "@/app/selectyourrole/page"
6+ import { AccessProvider } from "@/context/AccessProvider"
67import { AuthContext } from "@/context/AuthProvider"
7-
8- // Mock the card strings, so we have known text for the tests
8+ import { SELECT_YOUR_ROLE_PAGE_TEXT } from "@/constants/ui-strings/CardStrings"
99
1010// Mock the module and directly reference the variable
1111jest . mock ( "@/constants/ui-strings/CardStrings" , ( ) => {
1212 const SELECT_YOUR_ROLE_PAGE_TEXT = {
1313 title : "Select your role" ,
1414 caption : "Select the role you wish to use to access the service." ,
15+ titleNoAccess : "No access to the clinical prescription tracking service" ,
16+ captionNoAccess :
17+ "None of the roles on your Smartcard or other authenticators allow you to access the clinical prescription tracking service. " +
18+ "Contact your Registration Authority representative to obtain the correct code." ,
1519 insetText : {
1620 visuallyHidden : "Information: " ,
1721 message :
@@ -58,27 +62,39 @@ global.fetch = mockFetch
5862
5963// Default mock values for the `AuthContext` to simulate authentication state
6064const defaultAuthContext = {
61- error : null , // No errors by default
62- user : null , // User is initially null (not logged in)
63- isSignedIn : false , // Default state is "not signed in"
64- idToken : null , // No ID token available
65- accessToken : null , // No access token available
66- cognitoSignIn : jest . fn ( ) , // Mock Cognito sign-in function
67- cognitoSignOut : jest . fn ( ) , // Mock Cognito sign-out function
65+ error : null ,
66+ user : null ,
67+ isSignedIn : false ,
68+ idToken : null ,
69+ accessToken : null ,
70+ cognitoSignIn : jest . fn ( ) ,
71+ cognitoSignOut : jest . fn ( ) ,
72+ }
73+
74+ const defaultAccessContext = {
75+ rolesWithAccess : [ ] ,
76+ rolesWithoutAccess : [ ] ,
77+ loading : false ,
78+ error : null ,
6879}
6980
70- // Utility function to render the component with custom AuthContext overrides
71- const renderWithAuth = ( authOverrides = { } ) => {
81+ // Utility function to render with both AuthProvider and AccessProvider
82+ const renderWithAuthAndAccess = (
83+ authOverrides = { } ,
84+ accessOverrides = { }
85+ ) => {
7286 const authValue = { ...defaultAuthContext , ...authOverrides }
87+ const accessValue = { ...defaultAccessContext , ...accessOverrides }
88+
7389 return render (
7490 < AuthContext . Provider value = { authValue } >
75- < SelectYourRolePage />
91+ < AccessProvider >
92+ < SelectYourRolePage />
93+ </ AccessProvider >
7694 </ AuthContext . Provider >
7795 )
7896}
7997
80- import { SELECT_YOUR_ROLE_PAGE_TEXT } from "@/constants/ui-strings/CardStrings"
81-
8298describe ( "SelectYourRolePage" , ( ) => {
8399 // Clear all mock calls before each test to avoid state leaks
84100 beforeEach ( ( ) => {
@@ -90,10 +106,12 @@ describe("SelectYourRolePage", () => {
90106 mockFetch . mockImplementation ( ( ) => new Promise ( ( ) => { } ) )
91107
92108 // Render the page with user signed in
93- renderWithAuth ( { isSignedIn : true , idToken : "mock-id-token" } )
109+ renderWithAuthAndAccess ( { isSignedIn : true , idToken : "mock-id-token" } , { loading : true } )
94110
95111 // Verify that the loading text appears
96- const loadingText = screen . getByText ( SELECT_YOUR_ROLE_PAGE_TEXT . loadingMessage )
112+ const loadingText = screen . getByText (
113+ SELECT_YOUR_ROLE_PAGE_TEXT . loadingMessage
114+ )
97115 expect ( loadingText ) . toBeInTheDocument ( )
98116 } )
99117
@@ -102,11 +120,10 @@ describe("SelectYourRolePage", () => {
102120 mockFetch . mockResolvedValue ( { status : 500 } )
103121
104122 // Render the page with user signed in
105- renderWithAuth ( { isSignedIn : true , idToken : "mock-id-token" } )
123+ renderWithAuthAndAccess ( { isSignedIn : true , idToken : "mock-id-token" } )
106124
107125 // Wait for the error message to appear
108126 await waitFor ( ( ) => {
109- // Check for error summary heading
110127 const errorHeading = screen . getByRole ( "heading" , {
111128 name : SELECT_YOUR_ROLE_PAGE_TEXT . errorDuringRoleSelection ,
112129 } )
@@ -126,7 +143,7 @@ describe("SelectYourRolePage", () => {
126143 } )
127144
128145 // Render the page with user signed in
129- renderWithAuth ( { isSignedIn : true , idToken : "mock-id-token" } )
146+ renderWithAuthAndAccess ( { isSignedIn : true , idToken : "mock-id-token" } )
130147
131148 // Wait for the error message to appear
132149 await waitFor ( ( ) => {
@@ -170,45 +187,46 @@ describe("SelectYourRolePage", () => {
170187 } )
171188
172189 // Render the page with user signed in
173- renderWithAuth ( { isSignedIn : true , idToken : "mock-id-token" } )
190+ renderWithAuthAndAccess ( { isSignedIn : true , idToken : "mock-id-token" } )
174191
175192 // Wait for the main content to load
176193 await waitFor ( ( ) => {
177194 // Check for the page heading
178195 const heading = screen . getByRole ( "heading" , { level : 1 } )
179196 expect ( heading ) . toHaveTextContent ( SELECT_YOUR_ROLE_PAGE_TEXT . title )
180197 } )
198+ } )
181199
182- // Verify the page caption
183- const caption = screen . getByText ( SELECT_YOUR_ROLE_PAGE_TEXT . caption )
184- expect ( caption ) . toBeInTheDocument ( )
185-
186- // Verify the "Roles without access" section (expander)
187- const expanderText = SELECT_YOUR_ROLE_PAGE_TEXT . roles_without_access_table_title
188- const expander = screen . getByText ( expanderText )
189- expect ( expander ) . toBeInTheDocument ( )
200+ it ( "renders no access title and caption when no roles with access are available" , async ( ) => {
201+ // Mock user data with no roles with access
202+ const mockUserInfo = {
203+ roles_with_access : [ ] , // No roles with access
204+ roles_without_access : [
205+ {
206+ role_name : "Technician" ,
207+ org_name : "Tech Org" ,
208+ org_code : "ORG456" ,
209+ site_address : "2 Fake Street" ,
210+ } ,
211+ ] ,
212+ }
190213
191- // Check for the table data in "Roles without access"
192- const tableOrg = screen . getByText ( / T e c h O r g \( O D S : O R G 4 5 6 \) / i)
193- expect ( tableOrg ) . toBeInTheDocument ( )
194- const tableRole = screen . getByText ( "Technician" )
195- expect ( tableRole ) . toBeInTheDocument ( )
196- } )
214+ // Mock fetch to return 200 OK with valid userInfo
215+ mockFetch . mockResolvedValue ( {
216+ status : 200 ,
217+ json : async ( ) => ( { userInfo : mockUserInfo } ) ,
218+ } )
197219
198- it ( "renders error summary when not signed in" , async ( ) => {
199- // Render the page with `isSignedIn` set to false
200- renderWithAuth ( { isSignedIn : false , error : "Missing access or ID token" } )
220+ // Render the page with user signed in
221+ renderWithAuthAndAccess ( { isSignedIn : true , idToken : "mock-id-token" } )
201222
202- // Wait for the error message to appear
223+ // Wait for the main content to load
203224 await waitFor ( ( ) => {
204- // Check for error summary heading
205- const errorHeading = screen . getByRole ( "heading" , {
206- name : SELECT_YOUR_ROLE_PAGE_TEXT . errorDuringRoleSelection ,
207- } )
208- expect ( errorHeading ) . toBeInTheDocument ( )
209-
210- const errorItem = screen . getByText ( "Missing access or ID token" )
211- expect ( errorItem ) . toBeInTheDocument ( )
225+ // Check for the no-access title
226+ const heading = screen . getByRole ( "heading" , { level : 1 } )
227+ expect ( heading ) . toHaveTextContent (
228+ SELECT_YOUR_ROLE_PAGE_TEXT . titleNoAccess
229+ )
212230 } )
213231 } )
214232
@@ -225,49 +243,18 @@ describe("SelectYourRolePage", () => {
225243 roles_without_access : [ ] ,
226244 }
227245
228- // Mock fetch response
229- global . fetch = jest . fn ( ( ) =>
230- Promise . resolve ( {
231- ok : true ,
232- status : 200 ,
233- statusText : "OK" ,
234- headers : new Headers ( ) ,
235- redirected : false ,
236- type : "basic" ,
237- url : "" ,
238- clone : jest . fn ( ) ,
239- body : null ,
240- bodyUsed : false ,
241- text : jest . fn ( ) ,
242- json : jest . fn ( ( ) => Promise . resolve ( { userInfo : mockUserInfo } ) ) , // Simulate JSON body
243- } )
244- ) as jest . Mock
246+ mockFetch . mockResolvedValue ( {
247+ status : 200 ,
248+ json : async ( ) => ( { userInfo : mockUserInfo } ) ,
249+ } )
245250
246- // Mock useRouter's push function
247251 const mockPush = jest . fn ( ) ;
248252 ( useRouter as jest . Mock ) . mockReturnValue ( {
249253 push : mockPush ,
250254 } )
251255
252- // Mock AuthContext
253- const mockAuthContext = {
254- isSignedIn : true ,
255- idToken : mockJWT ,
256- accessToken : mockJWT ,
257- error : null ,
258- user : null ,
259- cognitoSignIn : jest . fn ( ) ,
260- cognitoSignOut : jest . fn ( ) ,
261- }
262-
263- // Render the component
264- render (
265- < AuthContext . Provider value = { mockAuthContext } >
266- < SelectYourRolePage />
267- </ AuthContext . Provider >
268- )
256+ renderWithAuthAndAccess ( { isSignedIn : true , idToken : "mock-id-token" } )
269257
270- // Wait for redirection
271258 await waitFor ( ( ) => {
272259 expect ( mockPush ) . toHaveBeenCalledWith ( "/searchforaprescription" )
273260 } )
0 commit comments