Skip to content

Commit ea8442c

Browse files
TASK Sonar fix: reduce duplication in eligibility tests
1 parent c205186 commit ea8442c

File tree

2 files changed

+51
-88
lines changed

2 files changed

+51
-88
lines changed

src/app/_components/eligibility/EligibilityActions.test.tsx

Lines changed: 40 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ describe("EligibilityActions", () => {
2424
}),
2525
);
2626

27-
const content: HTMLElement = screen.getByText("Test Content");
28-
29-
expect(content).toBeVisible();
27+
expectContentStringToBeVisible("Test Content");
3028
});
3129

3230
it("should display multiple infotexts successfully", () => {
@@ -39,11 +37,7 @@ describe("EligibilityActions", () => {
3937
}),
4038
);
4139

42-
const content1: HTMLElement = screen.getByText("Test Content 1");
43-
const content2: HTMLElement = screen.getByText("Test Content 2");
44-
45-
expect(content1).toBeVisible();
46-
expect(content2).toBeVisible();
40+
expectEachContentStringToBeVisible(["Test Content 1", "Test Content 2"]);
4741
});
4842

4943
it("should display delineator depending on flag", () => {
@@ -80,9 +74,7 @@ describe("EligibilityActions", () => {
8074
}),
8175
);
8276

83-
const content: HTMLElement = screen.getByText("Test Content");
84-
85-
expect(content).toBeVisible();
77+
expectContentStringToBeVisible("Test Content");
8678
});
8779

8880
it("should display multiple cards successfully", () => {
@@ -95,11 +87,7 @@ describe("EligibilityActions", () => {
9587
}),
9688
);
9789

98-
const content1: HTMLElement = screen.getByText("Test Content 1");
99-
const content2: HTMLElement = screen.getByText("Test Content 2");
100-
101-
expect(content1).toBeVisible();
102-
expect(content2).toBeVisible();
90+
expectEachContentStringToBeVisible(["Test Content 1", "Test Content 2"]);
10391
});
10492

10593
it("should display delineator depending on flag", () => {
@@ -134,9 +122,7 @@ describe("EligibilityActions", () => {
134122
}),
135123
);
136124

137-
const content: HTMLElement = screen.getByText("Test Auth Action Content");
138-
139-
expect(content).toBeVisible();
125+
expectContentStringToBeVisible("Test Auth Action Content");
140126
});
141127

142128
it("should display button content successfully", () => {
@@ -146,10 +132,8 @@ describe("EligibilityActions", () => {
146132
}),
147133
);
148134

149-
const content: HTMLElement = screen.getByText("Test Content");
135+
expectContentStringToBeVisible("Test Content");
150136
const bookingButton: HTMLElement = screen.getByText("NBS Booking Link Test");
151-
152-
expect(content).toBeVisible();
153137
expect(bookingButton).toBeInTheDocument();
154138
});
155139

@@ -163,11 +147,7 @@ describe("EligibilityActions", () => {
163147
}),
164148
);
165149

166-
const content1: HTMLElement = screen.getByText("Test Content 1");
167-
const content2: HTMLElement = screen.getByText("Test Content 2");
168-
169-
expect(content1).toBeVisible();
170-
expect(content2).toBeVisible();
150+
expectEachContentStringToBeVisible(["Test Content 1", "Test Content 2"]);
171151
});
172152

173153
it("should display button without card if no description present", () => {
@@ -202,11 +182,8 @@ describe("EligibilityActions", () => {
202182
}),
203183
);
204184

205-
const content1: HTMLElement = screen.getByText("Test Content 1");
206-
const content2: HTMLElement = screen.getByText("Test Content 2");
207-
208-
expect(content1.closest(".nhsuk-card")?.nextElementSibling?.nextElementSibling?.tagName).toBe("HR");
209-
expect(content2.closest(".nhsuk-card")?.nextElementSibling?.nextElementSibling?.tagName).not.toBe("HR");
185+
expectDelineatorToBePresentAfterElementWithSelector("Test Content 1", ".nhsuk-card");
186+
expectDelineatorToNotBePresentAfterElementWithSelector("Test Content 2", ".nhsuk-card");
210187
});
211188
});
212189

@@ -220,9 +197,7 @@ describe("EligibilityActions", () => {
220197
}),
221198
);
222199

223-
const content: HTMLElement = screen.getByText("Test Auth Action Content");
224-
225-
expect(content).toBeVisible();
200+
expectContentStringToBeVisible("Test Auth Action Content");
226201
});
227202

228203
it("should display button content successfully", () => {
@@ -232,10 +207,8 @@ describe("EligibilityActions", () => {
232207
}),
233208
);
234209

235-
const content: HTMLElement = screen.getByText("Test Content");
210+
expectContentStringToBeVisible("Test Content");
236211
const bookingButton: HTMLElement = screen.getByText("NBS Booking Link Test");
237-
238-
expect(content).toBeVisible();
239212
expect(bookingButton).toBeInTheDocument();
240213
});
241214

@@ -249,11 +222,7 @@ describe("EligibilityActions", () => {
249222
}),
250223
);
251224

252-
const content1: HTMLElement = screen.getByText("Test Content 1");
253-
const content2: HTMLElement = screen.getByText("Test Content 2");
254-
255-
expect(content1).toBeVisible();
256-
expect(content2).toBeVisible();
225+
expectEachContentStringToBeVisible(["Test Content 1", "Test Content 2"]);
257226
});
258227

259228
it("should display button without info if no description present", () => {
@@ -288,15 +257,8 @@ describe("EligibilityActions", () => {
288257
}),
289258
);
290259

291-
const content1: HTMLElement = screen.getByText("Test Content 1");
292-
const content2: HTMLElement = screen.getByText("Test Content 2");
293-
294-
expect(
295-
content1.closest('[data-testid="action-paragraph"]')?.nextElementSibling?.nextElementSibling?.tagName,
296-
).toBe("HR");
297-
expect(
298-
content2.closest('[data-testid="action-paragraph"]')?.nextElementSibling?.nextElementSibling?.tagName,
299-
).not.toBe("HR");
260+
expectDelineatorToBePresentAfterElementWithSelector("Test Content 1", '[data-testid="action-paragraph"]');
261+
expectDelineatorToNotBePresentAfterElementWithSelector("Test Content 2", '[data-testid="action-paragraph"]');
300262
});
301263
});
302264

@@ -313,9 +275,7 @@ describe("EligibilityActions", () => {
313275
}),
314276
);
315277

316-
const content: HTMLElement = screen.getByText("Test Auth Action Content");
317-
318-
expect(content).toBeVisible();
278+
expectEachContentStringToBeVisible(["Test Auth Action Content"]);
319279
});
320280

321281
it("should display link content successfully", () => {
@@ -336,10 +296,8 @@ describe("EligibilityActions", () => {
336296
}),
337297
);
338298

339-
const content: HTMLElement = screen.getByText("Test Content");
299+
expectContentStringToBeVisible("Test Content");
340300
const link = screen.getByRole("link", { name: "Action Link Test" });
341-
342-
expect(content).toBeVisible();
343301
expect(link).toHaveAttribute("href", "https://example.com/bacon/");
344302
});
345303

@@ -353,11 +311,7 @@ describe("EligibilityActions", () => {
353311
}),
354312
);
355313

356-
const content1: HTMLElement = screen.getByText("Test Content 1");
357-
const content2: HTMLElement = screen.getByText("Test Content 2");
358-
359-
expect(content1).toBeVisible();
360-
expect(content2).toBeVisible();
314+
expectEachContentStringToBeVisible(["Test Content 1", "Test Content 2"]);
361315
});
362316

363317
it("should display link without info if no description present", () => {
@@ -392,16 +346,30 @@ describe("EligibilityActions", () => {
392346
}),
393347
);
394348

395-
const content1: HTMLElement = screen.getByText("Test Content 1");
396-
const content2: HTMLElement = screen.getByText("Test Content 2");
397-
398-
expect(
399-
content1.closest('[data-testid="action-paragraph"]')?.nextElementSibling?.nextElementSibling?.tagName,
400-
).toBe("HR");
401-
expect(
402-
content2.closest('[data-testid="action-paragraph"]')?.nextElementSibling?.nextElementSibling?.tagName,
403-
).not.toBe("HR");
349+
expectDelineatorToBePresentAfterElementWithSelector("Test Content 1", '[data-testid="action-paragraph"]');
350+
expectDelineatorToNotBePresentAfterElementWithSelector("Test Content 2", '[data-testid="action-paragraph"]');
404351
});
405352
});
406353
});
354+
355+
const expectContentStringToBeVisible = (content: string) => {
356+
expectEachContentStringToBeVisible([content]);
357+
};
358+
359+
const expectEachContentStringToBeVisible = (contentList: string[]) => {
360+
contentList.forEach((content) => {
361+
const contentElement: HTMLElement = screen.getByText(content);
362+
expect(contentElement).toBeVisible();
363+
});
364+
};
365+
366+
const expectDelineatorToBePresentAfterElementWithSelector = (content: string, selector: string) => {
367+
const contentElement: HTMLElement = screen.getByText(content);
368+
expect(contentElement.closest(selector)?.nextElementSibling?.nextElementSibling?.tagName).toBe("HR");
369+
};
370+
371+
const expectDelineatorToNotBePresentAfterElementWithSelector = (content: string, selector: string) => {
372+
const contentElement: HTMLElement = screen.getByText(content);
373+
expect(contentElement.closest(selector)?.nextElementSibling?.nextElementSibling?.tagName).not.toBe("HR");
374+
};
407375
});

src/app/_components/eligibility/SuitabilityRules.test.tsx

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ describe("SuitabilityRules", () => {
2323
}),
2424
);
2525

26-
const content: HTMLElement = screen.getByText("Test Content");
27-
28-
expect(content).toBeVisible();
26+
expectContentToBeVisible("Test Content");
2927
});
3028

3129
it("should display multiple paragraphs successfully", () => {
@@ -38,11 +36,8 @@ describe("SuitabilityRules", () => {
3836
}),
3937
);
4038

41-
const content1: HTMLElement = screen.getByText("Test Content 1");
42-
const content2: HTMLElement = screen.getByText("Test Content 2");
43-
44-
expect(content1).toBeVisible();
45-
expect(content2).toBeVisible();
39+
expectContentToBeVisible("Test Content 1");
40+
expectContentToBeVisible("Test Content 2");
4641
});
4742

4843
it("should display delineator depending on flag", () => {
@@ -81,9 +76,7 @@ describe("SuitabilityRules", () => {
8176
}),
8277
);
8378

84-
const content: HTMLElement = screen.getByText("Test Content");
85-
86-
expect(content).toBeVisible();
79+
expectContentToBeVisible("Test Content");
8780
});
8881

8982
it("should display multiple paragraphs successfully", () => {
@@ -96,11 +89,8 @@ describe("SuitabilityRules", () => {
9689
}),
9790
);
9891

99-
const content1: HTMLElement = screen.getByText("Test Content 1");
100-
const content2: HTMLElement = screen.getByText("Test Content 2");
101-
102-
expect(content1).toBeVisible();
103-
expect(content2).toBeVisible();
92+
expectContentToBeVisible("Test Content 1");
93+
expectContentToBeVisible("Test Content 2");
10494
});
10595

10696
it("should display delineator depending on flag", () => {
@@ -129,4 +119,9 @@ describe("SuitabilityRules", () => {
129119
});
130120
});
131121
});
122+
123+
const expectContentToBeVisible = (content: string) => {
124+
const contentElement: HTMLElement = screen.getByText(content);
125+
expect(contentElement).toBeVisible();
126+
};
132127
});

0 commit comments

Comments
 (0)