Skip to content

Commit 141a6ee

Browse files
CHORE SB Reduce test duplication a little more.
1 parent bf9ce1a commit 141a6ee

File tree

2 files changed

+56
-128
lines changed

2 files changed

+56
-128
lines changed

src/app/_components/nbs/NBSBookingAction.test.tsx

Lines changed: 55 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -13,168 +13,96 @@ describe("NBSBookingAction", () => {
1313
window.open = jest.fn() as never;
1414
});
1515

16+
const renderVariants = [
17+
{ type: "button", index: 0 },
18+
{ type: "anchor", index: 0 },
19+
{ type: "actionLink", index: 1 },
20+
] as const;
21+
22+
const testScenarios = [
23+
{
24+
name: "should open NBS SSO link in same window when action is clicked within NHS app",
25+
context: { hasContextLoaded: true, isOpenInMobileApp: true },
26+
expectedTarget: "_self",
27+
shouldCall: true,
28+
},
29+
{
30+
name: "should open NBS SSO link in new window when action is clicked outside NHS app",
31+
context: { hasContextLoaded: true, isOpenInMobileApp: false },
32+
expectedTarget: "_blank",
33+
shouldCall: true,
34+
},
35+
{
36+
name: "given browser context has not loaded, should do nothing when action is clicked",
37+
context: { hasContextLoaded: false, isOpenInMobileApp: undefined },
38+
expectedTarget: "",
39+
shouldCall: false,
40+
},
41+
];
42+
1643
describe("Given vaccine type", () => {
17-
const renderAndClickNBSBookingAction = (
18-
displayText: string,
19-
renderAs: "anchor" | "button" | "actionLink",
20-
whichElement: number = 0,
21-
): HTMLElement => {
44+
const renderAndClick = (renderAs: "anchor" | "button" | "actionLink", whichElement: number) => {
2245
render(
2346
<NBSBookingActionForVaccine
2447
vaccineType={VaccineType.RSV}
25-
displayText={displayText}
48+
displayText="test"
2649
renderAs={renderAs}
2750
reduceBottomPadding={false}
2851
/>,
2952
);
30-
31-
const role: "button" | "link" = renderAs === "button" ? "button" : "link"; // "anchor" and "actionLink" are both links
32-
33-
const element: HTMLElement = screen.getAllByRole(role, { name: displayText })[whichElement];
34-
expect(element).toBeVisible();
53+
const role = renderAs === "button" ? "button" : "link";
54+
const element = screen.getAllByRole(role, { name: "test" })[whichElement];
3555
element.click();
36-
37-
return element;
3856
};
3957

40-
it("should open NBS SSO link in same window when action is clicked within NHS app", () => {
41-
(useBrowserContext as jest.Mock).mockReturnValue({
42-
hasContextLoaded: true,
43-
isOpenInMobileApp: true,
44-
});
45-
46-
// render as button
47-
renderAndClickNBSBookingAction("test", "button");
48-
expect(window.open).toHaveBeenCalledWith("/api/sso-to-nbs?vaccine=rsv", "_self");
49-
50-
// render as anchor
51-
renderAndClickNBSBookingAction("test", "anchor");
52-
expect(window.open).toHaveBeenCalledWith("/api/sso-to-nbs?vaccine=rsv", "_self");
53-
54-
// render as action link
55-
renderAndClickNBSBookingAction("test", "actionLink", 1);
56-
expect(window.open).toHaveBeenCalledWith("/api/sso-to-nbs?vaccine=rsv", "_self");
57-
});
58-
59-
it("should open NBS SSO link in new window when action is clicked outside NHS app", () => {
60-
(useBrowserContext as jest.Mock).mockReturnValue({
61-
hasContextLoaded: true,
62-
isOpenInMobileApp: false,
63-
});
64-
65-
// render as button
66-
renderAndClickNBSBookingAction("test", "button");
67-
expect(window.open).toHaveBeenCalledWith("/api/sso-to-nbs?vaccine=rsv", "_blank");
58+
it.each(testScenarios)("$name", ({ context, expectedTarget, shouldCall }) => {
59+
(useBrowserContext as jest.Mock).mockReturnValue(context);
6860

69-
// render as anchor
70-
renderAndClickNBSBookingAction("test", "anchor");
71-
expect(window.open).toHaveBeenCalledWith("/api/sso-to-nbs?vaccine=rsv", "_blank");
61+
renderVariants.forEach(({ type, index }) => {
62+
jest.clearAllMocks();
7263

73-
// render as action link
74-
renderAndClickNBSBookingAction("test", "actionLink", 1);
75-
expect(window.open).toHaveBeenCalledWith("/api/sso-to-nbs?vaccine=rsv", "_blank");
76-
});
64+
renderAndClick(type, index);
7765

78-
it("given browser context has not loaded, should do nothing when action is clicked", () => {
79-
(useBrowserContext as jest.Mock).mockReturnValue({
80-
hasContextLoaded: false,
81-
isOpenInMobileApp: undefined,
66+
if (shouldCall) {
67+
expect(window.open).toHaveBeenCalledWith("/api/sso-to-nbs?vaccine=rsv", expectedTarget);
68+
} else {
69+
expect(window.open).not.toHaveBeenCalled();
70+
}
8271
});
83-
84-
// render as button
85-
renderAndClickNBSBookingAction("test", "button");
86-
expect(window.open).not.toHaveBeenCalled();
87-
88-
// render as anchor
89-
renderAndClickNBSBookingAction("test", "anchor");
90-
expect(window.open).not.toHaveBeenCalled();
91-
92-
// render as action link
93-
renderAndClickNBSBookingAction("test", "actionLink", 1);
94-
expect(window.open).not.toHaveBeenCalled();
9572
});
9673
});
9774

9875
describe("Given base URL", () => {
9976
const url = randomURL();
10077

101-
const renderAndClickNBSBookingAction = (
102-
displayText: string,
103-
renderAs: "anchor" | "button" | "actionLink",
104-
whichElement: number = 0,
105-
): HTMLElement => {
78+
const renderAndClick = (renderAs: "anchor" | "button" | "actionLink", whichElement: number) => {
10679
render(
10780
<NBSBookingActionForBaseUrl
10881
url={url.href}
109-
displayText={displayText}
82+
displayText="test"
11083
renderAs={renderAs}
11184
reduceBottomPadding={false}
11285
/>,
11386
);
114-
115-
const role: "button" | "link" = renderAs === "button" ? "button" : "link"; // "anchor" and "actionLink" are both links
116-
117-
const element: HTMLElement = screen.getAllByRole(role, { name: displayText })[whichElement];
87+
const role = renderAs === "button" ? "button" : "link";
88+
const element = screen.getAllByRole(role, { name: "test" })[whichElement];
11889
element.click();
119-
120-
return element;
12190
};
12291

123-
it("should open NBS SSO link in same window when action is clicked within NHS app", () => {
124-
(useBrowserContext as jest.Mock).mockReturnValue({
125-
hasContextLoaded: true,
126-
isOpenInMobileApp: true,
127-
});
128-
129-
// render as button
130-
renderAndClickNBSBookingAction("test", "button");
131-
expect(window.open).toHaveBeenCalledWith(`/api/sso-to-nbs?redirectTarget=${url.href}`, "_self");
92+
it.each(testScenarios)("$name", ({ context, expectedTarget, shouldCall }) => {
93+
(useBrowserContext as jest.Mock).mockReturnValue(context);
13294

133-
// render as anchor
134-
renderAndClickNBSBookingAction("test", "anchor");
135-
expect(window.open).toHaveBeenCalledWith(`/api/sso-to-nbs?redirectTarget=${url.href}`, "_self");
95+
renderVariants.forEach(({ type, index }) => {
96+
jest.clearAllMocks();
13697

137-
// render as action link
138-
renderAndClickNBSBookingAction("test", "actionLink", 1);
139-
expect(window.open).toHaveBeenCalledWith(`/api/sso-to-nbs?redirectTarget=${url.href}`, "_self");
140-
});
98+
renderAndClick(type, index);
14199

142-
it("should open NBS SSO link in new window when action is clicked outside NHS app", () => {
143-
(useBrowserContext as jest.Mock).mockReturnValue({
144-
hasContextLoaded: true,
145-
isOpenInMobileApp: false,
100+
if (shouldCall) {
101+
expect(window.open).toHaveBeenCalledWith(`/api/sso-to-nbs?redirectTarget=${url.href}`, expectedTarget);
102+
} else {
103+
expect(window.open).not.toHaveBeenCalled();
104+
}
146105
});
147-
148-
// render as button
149-
renderAndClickNBSBookingAction("test", "button");
150-
expect(window.open).toHaveBeenCalledWith(`/api/sso-to-nbs?redirectTarget=${url.href}`, "_blank");
151-
152-
// render as anchor
153-
renderAndClickNBSBookingAction("test", "anchor");
154-
expect(window.open).toHaveBeenCalledWith(`/api/sso-to-nbs?redirectTarget=${url.href}`, "_blank");
155-
156-
// render as action link
157-
renderAndClickNBSBookingAction("test", "actionLink", 1);
158-
expect(window.open).toHaveBeenCalledWith(`/api/sso-to-nbs?redirectTarget=${url.href}`, "_blank");
159-
});
160-
161-
it("given browser context has not loaded, should do nothing when action is clicked", () => {
162-
(useBrowserContext as jest.Mock).mockReturnValue({
163-
hasContextLoaded: false,
164-
isOpenInMobileApp: undefined,
165-
});
166-
167-
// render as button
168-
renderAndClickNBSBookingAction("test", "button");
169-
expect(window.open).not.toHaveBeenCalled();
170-
171-
// render as anchor
172-
renderAndClickNBSBookingAction("test", "anchor");
173-
expect(window.open).not.toHaveBeenCalled();
174-
175-
// render as action link
176-
renderAndClickNBSBookingAction("test", "actionLink", 1);
177-
expect(window.open).not.toHaveBeenCalled();
178106
});
179107
});
180108
});

src/app/_components/nbs/NBSBookingAction.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const NBSBookingAction = ({
101101
case "actionLink": {
102102
// Ref: https://main--65aa76b29d00a047fe683b95.chromatic.com/?path=/docs/navigation-actionlink--docs
103103
return (
104-
<ActionLink asElement={"a"} rel="noopener" target={"_blank"} href={url}>
104+
<ActionLink asElement={"a"} rel="noopener" target={"_blank"} href={url} onClick={handleClick}>
105105
{displayText}
106106
</ActionLink>
107107
);

0 commit comments

Comments
 (0)