Skip to content

Commit 96deba1

Browse files
committed
Add tests for the selectyourrole page
1 parent 80d6d8e commit 96deba1

File tree

1 file changed

+175
-3
lines changed

1 file changed

+175
-3
lines changed

packages/cpt-ui/__tests__/SelectYourRolePage.test.jsx

Lines changed: 175 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import "@testing-library/jest-dom";
3-
import { render, screen, waitFor } from "@testing-library/react";
4-
import SelectYourRolePage from "@/app/selectyourrole/page";
3+
import { render, screen, waitFor, within } from "@testing-library/react";
4+
import SelectYourRolePage from "../app/selectyourrole/page";
55
import { AuthContext } from "@/context/AuthProvider";
66

77
// Mock `next/navigation` globally
@@ -42,7 +42,7 @@ describe("SelectYourRolePage", () => {
4242

4343
// Wait for error summary to appear
4444
await waitFor(() => {
45-
const errorHeading = screen.getByRole("heading", { name: /Error during role selection/i })
45+
const errorHeading = screen.getByRole("heading", { name: /Error during role selection/i });
4646
expect(errorHeading).toBeInTheDocument();
4747
const errorText = screen.getByText("No login session found");
4848
expect(errorText).toBeInTheDocument();
@@ -184,5 +184,177 @@ describe("SelectYourRolePage", () => {
184184
});
185185

186186
expect(tableRoleCell).toBeInTheDocument();
187+
});
188+
189+
it("renders correctly when auth.isSignedIn is undefined and fetchTrackerUserInfo is not triggered", async () => {
190+
renderWithAuth({
191+
isSignedIn: undefined,
192+
idToken: "",
193+
});
194+
195+
// Verify no error or loading state appears
196+
await waitFor(() => {
197+
const errorHeading = screen.queryByRole("heading", { name: /Error during role selection/i });
198+
expect(errorHeading).not.toBeInTheDocument();
199+
});
200+
});
201+
202+
it("handles missing roles gracefully in trackerUserInfoData", async () => {
203+
const mockUserInfo = {
204+
roles_with_access: [],
205+
roles_without_access: [],
206+
};
207+
208+
mockFetch.mockResolvedValue({
209+
status: 200,
210+
json: async () => ({
211+
userInfo: mockUserInfo,
212+
}),
213+
});
214+
215+
renderWithAuth({
216+
isSignedIn: true,
217+
idToken: "fake-token",
218+
});
219+
220+
// Expect no roles to render
221+
await waitFor(() => {
222+
const rolesMessage = screen.queryByText("No roles found");
223+
expect(rolesMessage).not.toBeInTheDocument();
224+
});
225+
});
226+
227+
it("handles the case when auth is undefined", async () => {
228+
renderWithAuth(undefined); // Simulate `auth` being undefined
229+
230+
// Wait for the error summary to render
231+
await waitFor(() => {
232+
const errorHeading = screen.getByRole("heading", { name: /Error during role selection/i });
233+
expect(errorHeading).toBeInTheDocument();
234+
235+
const errorText = screen.getByText("No login session found");
236+
expect(errorText).toBeInTheDocument();
237+
});
238+
});
239+
240+
it("sets error state when auth.isSignedIn is false", async () => {
241+
renderWithAuth({
242+
isSignedIn: false,
243+
idToken: "",
244+
});
245+
246+
// Wait for the error summary to render
247+
await waitFor(() => {
248+
const errorSummary = screen.getByRole("heading", { name: /Error during role selection/i });
249+
expect(errorSummary).toBeInTheDocument();
250+
const errorMessage = screen.getByText("No login session found");
251+
expect(errorMessage).toBeInTheDocument();
252+
});
253+
});
254+
255+
it("renders a row for roles without access in the table", async () => {
256+
const mockUserInfo = {
257+
roles_with_access: [],
258+
roles_without_access: [
259+
{
260+
role_name: "Technician",
261+
role_id: "tech1",
262+
org_code: "ORG456",
263+
org_name: "Tech Org",
264+
site_name: "Technician Site",
265+
site_address: "2 Fake Street",
266+
},
267+
],
268+
};
269+
270+
mockFetch.mockResolvedValue({
271+
status: 200,
272+
json: async () => ({
273+
userInfo: mockUserInfo,
274+
}),
187275
});
276+
277+
renderWithAuth({
278+
isSignedIn: true,
279+
idToken: "fake-token",
280+
});
281+
282+
// Wait for the table to render
283+
await waitFor(() => {
284+
const table = screen.getByRole("table");
285+
const tableCellOrg = within(table).getByText("Tech Org (ODS: ORG456)");
286+
expect(tableCellOrg).toBeInTheDocument();
287+
288+
const tableCellRole = within(table).getByText("Technician");
289+
expect(tableCellRole).toBeInTheDocument();
290+
});
291+
});
292+
293+
it("renders correctly with role_name having special formatting", async () => {
294+
const mockUserInfo = {
295+
roles_with_access: [],
296+
roles_without_access: [
297+
{
298+
role_name: "Role:Pharmacist",
299+
role_id: "pharm1",
300+
org_code: "ORG123",
301+
org_name: "Test Pharmacy Org",
302+
site_name: "Pharmacy Site",
303+
site_address: "1 Fake Street",
304+
},
305+
],
306+
};
307+
308+
mockFetch.mockResolvedValue({
309+
status: 200,
310+
json: async () => ({
311+
userInfo: mockUserInfo,
312+
}),
313+
});
314+
315+
renderWithAuth({
316+
isSignedIn: true,
317+
idToken: "fake-token",
318+
});
319+
320+
// Wait for the card to render
321+
await waitFor(() => {
322+
const roleName = screen.getByText("Pharmacist");
323+
expect(roleName).toBeInTheDocument();
324+
});
325+
});
326+
327+
it("renders correctly with role_name absent", async () => {
328+
const mockUserInfo = {
329+
roles_with_access: [],
330+
roles_without_access: [
331+
{
332+
role_name: undefined,
333+
role_id: "pharm1",
334+
org_code: "ORG123",
335+
org_name: "Test Pharmacy Org",
336+
site_name: "Pharmacy Site",
337+
site_address: "1 Fake Street",
338+
},
339+
],
340+
};
341+
342+
mockFetch.mockResolvedValue({
343+
status: 200,
344+
json: async () => ({
345+
userInfo: mockUserInfo,
346+
}),
347+
});
348+
349+
renderWithAuth({
350+
isSignedIn: true,
351+
idToken: "fake-token",
352+
});
353+
354+
// Wait for the card to render
355+
await waitFor(() => {
356+
const roleName = screen.getByText("No Role Name");
357+
expect(roleName).toBeInTheDocument();
358+
});
359+
});
188360
});

0 commit comments

Comments
 (0)