Skip to content

Commit df74446

Browse files
committed
Update tests for the selectyourrole page
1 parent 3935103 commit df74446

File tree

1 file changed

+167
-18
lines changed

1 file changed

+167
-18
lines changed
Lines changed: 167 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,188 @@
1-
import "@testing-library/jest-dom";
2-
import { render, screen } from "@testing-library/react";
3-
import SelectYourRolePage from "../app/selectyourrole/page";
41
import React from "react";
2+
import "@testing-library/jest-dom";
3+
import { render, screen, waitFor } from "@testing-library/react";
4+
import SelectYourRolePage from "@/app/selectyourrole/page";
5+
import { AuthContext } from "@/context/AuthContext";
56

67
// Mock `next/navigation` globally
78
jest.mock("next/navigation", () => ({
89
usePathname: jest.fn(),
910
useRouter: jest.fn(),
1011
}));
1112

12-
import { usePathname } from "next/navigation";
13+
// Define a global fetch mock
14+
const mockFetch = jest.fn();
15+
global.fetch = mockFetch;
1316

14-
describe("SelectYourRole Page", () => {
15-
it("renders a heading", () => {
16-
render(<SelectYourRolePage />);
17+
describe("SelectYourRolePage", () => {
18+
const mockAuthContextValue = {
19+
isSignedIn: false,
20+
idToken: "",
21+
};
1722

18-
const heading = screen.getByRole("heading", { level: 1 });
23+
// Helper: Renders component with optional custom AuthContext value
24+
const renderWithAuth = (authValue = mockAuthContextValue) => {
25+
return render(
26+
<AuthContext.Provider value={authValue}>
27+
<SelectYourRolePage />
28+
</AuthContext.Provider>
29+
);
30+
};
1931

20-
expect(heading).toBeInTheDocument();
21-
expect(heading).toHaveTextContent("Select your role");
32+
beforeEach(() => {
33+
jest.clearAllMocks();
2234
});
2335

24-
it("renders the caption", () => {
25-
render(<SelectYourRolePage />);
36+
it("renders a heading by default (not signed in) but eventually shows an error", async () => {
37+
// Not signed in
38+
renderWithAuth({
39+
isSignedIn: false,
40+
idToken: "",
41+
});
2642

27-
const caption = screen.getByText(/Select the role you wish to use to access the service/i);
43+
// Wait for error summary to appear
44+
await waitFor(() => {
45+
const errorHeading = screen.getByRole("heading", { name: /Error during role selection/i })
46+
expect(errorHeading).toBeInTheDocument();
47+
const errorText = screen.getByText("No login session found");
48+
expect(errorText).toBeInTheDocument();
49+
});
50+
});
2851

29-
expect(caption).toBeInTheDocument();
52+
it("renders loading state when signed in but fetch hasn't resolved yet", async () => {
53+
// Mock fetch to never resolve
54+
mockFetch.mockImplementation(() => new Promise(() => {}));
55+
56+
renderWithAuth({
57+
isSignedIn: true,
58+
idToken: "fake-token",
59+
});
60+
61+
// Should show "Loading..." text
62+
const loadingText = screen.getByText(/loading.../i);
63+
expect(loadingText).toBeInTheDocument();
3064
});
3165

32-
it("renders the main container", () => {
33-
render(<SelectYourRolePage />);
66+
it("renders error summary if fetch returns non-200 status", async () => {
67+
// Mock fetch to return 500 status
68+
mockFetch.mockResolvedValue({
69+
status: 500,
70+
});
3471

35-
const container = screen.getByRole("contentinfo");
72+
renderWithAuth({
73+
isSignedIn: true,
74+
idToken: "fake-token",
75+
});
3676

37-
expect(container).toBeInTheDocument();
77+
// Expect error summary to appear
78+
await waitFor(() => {
79+
const errorHeading = screen.getByRole("heading", { name: /Error during role selection/i });
80+
expect(errorHeading).toBeInTheDocument();
81+
const errorItem = screen.getByText("Failed to fetch CPT user info");
82+
expect(errorItem).toBeInTheDocument();
83+
});
3884
});
85+
86+
it("renders error summary if fetch returns 200 but the JSON body has no userInfo key", async () => {
87+
// Mock a 200 response without `userInfo`
88+
mockFetch.mockResolvedValue({
89+
status: 200,
90+
json: async () => ({}), // Missing userInfo
91+
});
92+
93+
renderWithAuth({
94+
isSignedIn: true,
95+
idToken: "fake-token",
96+
});
97+
98+
// Expect error summary to appear
99+
await waitFor(() => {
100+
const errorHeading = screen.getByRole("heading", { name: /Error during role selection/i });
101+
expect(errorHeading).toBeInTheDocument();
102+
const errorItem = screen.getByText("Failed to fetch CPT user info");
103+
expect(errorItem).toBeInTheDocument();
104+
});
105+
});
106+
107+
it("renders the page content when valid userInfo is returned", async () => {
108+
const mockUserInfo = {
109+
roles_with_access: [
110+
{
111+
role_name: "Pharmacist",
112+
role_id: "pharm1",
113+
org_code: "ORG123",
114+
org_name: "Test Pharmacy Org",
115+
site_name: "Pharmacy Site",
116+
site_address: "1 Fake Street",
117+
},
118+
],
119+
roles_without_access: [
120+
{
121+
role_name: "Technician",
122+
role_id: "tech1",
123+
org_code: "ORG456",
124+
org_name: "Tech Org",
125+
site_name: "Technician Site",
126+
site_address: "2 Fake Street",
127+
},
128+
],
129+
};
130+
131+
// Mock a successful 200 response
132+
mockFetch.mockResolvedValue({
133+
status: 200,
134+
json: async () => ({
135+
userInfo: mockUserInfo,
136+
}),
137+
});
138+
139+
renderWithAuth({
140+
isSignedIn: true,
141+
idToken: "fake-token",
142+
});
143+
144+
// Wait for normal state to appear (no errors)
145+
await waitFor(() => {
146+
// Title
147+
const heading = screen.getByRole("heading", { level: 1 });
148+
expect(heading).toHaveTextContent("Select your role"); // from SELECT_ROLE_PAGE_TEXT
149+
});
150+
151+
// Check the caption
152+
const caption = screen.getByText(/Select the role you wish to use to access the service/i);
153+
expect(caption).toBeInTheDocument();
154+
155+
// Check that the "contentinfo" container is rendered
156+
const container = screen.getByRole("contentinfo");
157+
expect(container).toBeInTheDocument();
158+
159+
// Check for confirm button text
160+
const confirmButton = screen.getByRole("button", { name: /Confirm and continue/i });
161+
expect(confirmButton).toBeInTheDocument();
162+
163+
// Roles Without Access details expander
164+
const expander = screen.getByText("Roles without access");
165+
expect(expander).toBeInTheDocument();
166+
167+
// Confirm the card is rendered
168+
const cardHeading = await screen.findByRole("heading", { name: /Tech Org \(ODS: ORG456\)/i });
169+
expect(cardHeading).toBeInTheDocument();
170+
171+
// Confirm the card for "Technician" is rendered
172+
const cardRole = screen.getByText("Technician", {
173+
selector: ".eps-card__roleName", // Target the class for role names inside the card
174+
});
175+
expect(cardRole).toBeInTheDocument();
176+
177+
// Confirm the table cell is rendered
178+
const tableCell = await screen.findByText(/Tech Org \(ODS: ORG456\)/i, { selector: "td" });
179+
expect(tableCell).toBeInTheDocument();
180+
181+
// Confirm the table cell for "Technician" is rendered
182+
const tableRoleCell = screen.getByText("Technician", {
183+
selector: "td", // Target the table cell
184+
});
185+
186+
expect(tableRoleCell).toBeInTheDocument();
187+
});
39188
});

0 commit comments

Comments
 (0)