Skip to content

Commit 2f8b4f9

Browse files
VIA-629 AJ/EO Add tests for Vaccine.tsx to show callout and actions for Vaccines that have Campaigns
1 parent 89626aa commit 2f8b4f9

File tree

3 files changed

+68
-68
lines changed

3 files changed

+68
-68
lines changed

src/app/_components/content/MoreInformation.test.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@ import { ConfigMock, configBuilder } from "@test-data/config/builders";
66
import { mockStyledContent, mockStyledContentWithoutWhatSection } from "@test-data/content-api/data";
77
import { render, screen } from "@testing-library/react";
88

9-
10-
11-
12-
13-
14-
15-
16-
17-
18-
19-
20-
219
jest.mock("sanitize-data", () => ({ sanitize: jest.fn() }));
2210
jest.mock("@src/utils/config");
2311

src/app/_components/vaccine/Vaccine.test.tsx

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,19 @@ import { VaccineType } from "@src/models/vaccine";
77
import { getContentForVaccine } from "@src/services/content-api/content-service";
88
import { ContentErrorTypes } from "@src/services/content-api/types";
99
import { getEligibilityForPerson } from "@src/services/eligibility-api/domain/eligibility-filter-service";
10-
import { EligibilityErrorTypes, EligibilityForPersonType, EligibilityStatus } from "@src/services/eligibility-api/types";
10+
import {
11+
EligibilityErrorTypes,
12+
EligibilityForPersonType,
13+
EligibilityStatus,
14+
} from "@src/services/eligibility-api/types";
1115
import { Campaigns } from "@src/utils/campaigns/types";
1216
import config from "@src/utils/config";
1317
import { ConfigMock, configBuilder } from "@test-data/config/builders";
1418
import { mockStyledContent } from "@test-data/content-api/data";
1519
import { eligibilityContentBuilder } from "@test-data/eligibility-api/builders";
1620
import { render, screen } from "@testing-library/react";
17-
import { ReadonlyHeaders } from "next/dist/server/web/spec-extension/adapters/headers";
18-
import { headers } from "next/headers";
1921
import React, { JSX } from "react";
2022

21-
22-
23-
24-
25-
26-
27-
28-
29-
30-
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
41-
42-
43-
44-
4523
jest.mock("@src/services/content-api/content-service", () => ({
4624
getContentForVaccine: jest.fn(),
4725
}));
@@ -88,11 +66,9 @@ jest.mock("cheerio", () => ({
8866
attr: jest.fn(),
8967
}));
9068

91-
const $ = Object.assign(selectorImpl, {
69+
return Object.assign(selectorImpl, {
9270
html: jest.fn(() => "<p>HTML fragment</p>"),
9371
});
94-
95-
return $;
9672
}),
9773
}));
9874

@@ -121,28 +97,21 @@ const contentErrorResponse = {
12197
};
12298

12399
describe("Any vaccine page", () => {
124-
125100
const mockedConfig = config as ConfigMock;
126101

127102
beforeEach(() => {
128103
const defaultConfig = configBuilder()
129104
.withCampaigns(
130105
Campaigns.fromJson(
131106
JSON.stringify({
132-
COVID_19: [
133-
{ start: "2025-11-01T09:00:00Z", end: "2026-01-31T09:00:00Z" }
134-
],
107+
COVID_19: [{ start: "2025-11-01T09:00:00Z", end: "2026-01-31T09:00:00Z" }],
135108
}),
136109
)!,
137110
)
138111
.build();
139112
Object.assign(mockedConfig, defaultConfig);
140113
});
141114

142-
const renderNamedVaccinePage = async (vaccineType: VaccineType) => {
143-
render(await Vaccine({ vaccineType: vaccineType }));
144-
};
145-
146115
const renderRsvVaccinePage = async () => {
147116
await renderNamedVaccinePage(VaccineType.RSV);
148117
};
@@ -161,12 +130,6 @@ describe("Any vaccine page", () => {
161130
nhs_number: nhsNumber,
162131
},
163132
});
164-
const fakeHeaders: ReadonlyHeaders = {
165-
get(name: string): string | null {
166-
return `fake-${name}-header`;
167-
},
168-
} as ReadonlyHeaders;
169-
(headers as jest.Mock).mockResolvedValue(fakeHeaders);
170133
});
171134

172135
describe("shows content section, when content available", () => {
@@ -257,6 +220,61 @@ describe("Any vaccine page", () => {
257220
});
258221
});
259222

223+
describe("shows callouts and actions for Vaccines that handle campaigns (COVID_19)", () => {
224+
const mockedConfig = config as ConfigMock;
225+
const campaigns = new Campaigns({});
226+
const covid19VaccineType = VaccineType.COVID_19;
227+
228+
beforeEach(() => {
229+
(getContentForVaccine as jest.Mock).mockResolvedValue(contentSuccessResponse);
230+
(getEligibilityForPerson as jest.Mock).mockResolvedValue(eligibilitySuccessResponse);
231+
const defaultConfig = configBuilder().withCampaigns(campaigns).build();
232+
Object.assign(mockedConfig, defaultConfig);
233+
});
234+
235+
it("should include callout text when campaign is inactive", async () => {
236+
const inactiveCampaignSpy = jest.spyOn(campaigns, "isActive").mockReturnValue(false);
237+
await renderNamedVaccinePage(covid19VaccineType);
238+
239+
const calloutText: HTMLElement = screen.getByTestId("callout");
240+
241+
expect(inactiveCampaignSpy).toHaveBeenCalledWith(covid19VaccineType, expect.any(Date));
242+
expect(calloutText).toBeInTheDocument();
243+
inactiveCampaignSpy.mockRestore();
244+
});
245+
246+
it("should not include callout text when campaign is active", async () => {
247+
const activeCampaignSpy = jest.spyOn(campaigns, "isActive").mockReturnValue(true);
248+
await renderNamedVaccinePage(covid19VaccineType);
249+
250+
const calloutText: HTMLElement | null = screen.queryByTestId("callout");
251+
252+
expect(activeCampaignSpy).toHaveBeenCalledWith(covid19VaccineType, expect.any(Date));
253+
expect(calloutText).toBeNull();
254+
activeCampaignSpy.mockRestore();
255+
});
256+
257+
it("should include actions when campaign is active", async () => {
258+
const activeCampaignSpy = jest.spyOn(campaigns, "isActive").mockReturnValue(true);
259+
await renderNamedVaccinePage(covid19VaccineType);
260+
261+
const actions: HTMLElement = screen.getByTestId("eligibility-actions-mock");
262+
expect(activeCampaignSpy).toHaveBeenCalledWith(covid19VaccineType, expect.any(Date));
263+
expect(actions).toBeInTheDocument();
264+
activeCampaignSpy.mockRestore();
265+
});
266+
267+
it("should not include actions when campaign is inactive", async () => {
268+
const inactiveCampaignSpy = jest.spyOn(campaigns, "isActive").mockReturnValue(false);
269+
await renderNamedVaccinePage(covid19VaccineType);
270+
271+
const actions: HTMLElement | null = screen.queryByTestId("eligibility-actions-mock");
272+
expect(inactiveCampaignSpy).toHaveBeenCalledWith(covid19VaccineType, expect.any(Date));
273+
expect(actions).toBeNull();
274+
inactiveCampaignSpy.mockRestore();
275+
});
276+
});
277+
260278
describe("shows content section, when content load fails", () => {
261279
beforeEach(() => {
262280
(getContentForVaccine as jest.Mock).mockResolvedValue(contentErrorResponse);
@@ -440,6 +458,10 @@ describe("Any vaccine page", () => {
440458
});
441459
});
442460

461+
const renderNamedVaccinePage = async (vaccineType: VaccineType) => {
462+
render(await Vaccine({ vaccineType: vaccineType }));
463+
};
464+
443465
const expectRenderEligibilitySectionWith = (
444466
vaccineType: VaccineType,
445467
eligibilityForPerson: EligibilityForPersonType,

src/app/_components/vaccine/Vaccine.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ import { ContentErrorTypes, StyledVaccineContent } from "@src/services/content-a
1616
import { getEligibilityForPerson } from "@src/services/eligibility-api/domain/eligibility-filter-service";
1717
import { EligibilityErrorTypes, EligibilityForPersonType } from "@src/services/eligibility-api/types";
1818
import config from "@src/utils/config";
19-
import { UtcDateTimeFromStringSchema } from "@src/utils/date";
19+
import { getNow } from "@src/utils/date";
2020
import { profilePerformanceEnd, profilePerformanceStart } from "@src/utils/performance";
2121
import { requestScopedStorageWrapper } from "@src/utils/requestScopedStorageWrapper";
2222
import { Session } from "next-auth";
23-
import { headers } from "next/headers";
2423
import React, { JSX } from "react";
2524

2625
import styles from "./styles.module.css";
@@ -43,16 +42,7 @@ const VaccineComponent = async ({ vaccineType }: VaccineProps): Promise<JSX.Elem
4342
const vaccineInfo: VaccineDetails = VaccineInfo[vaccineType];
4443

4544
const campaigns = await config.CAMPAIGNS;
46-
const now = await _getNow();
47-
const isCampaignActive: boolean = campaigns.isActive(vaccineType, now);
48-
async function _getNow() {
49-
try {
50-
const headersList = await headers();
51-
return UtcDateTimeFromStringSchema.safeParse(headersList.get("x-e2e-datetime")).data ?? new Date();
52-
} catch {
53-
return new Date();
54-
}
55-
}
45+
const isCampaignActive: boolean = campaigns.isActive(vaccineType, await getNow());
5646

5747
let styledVaccineContent: StyledVaccineContent | undefined;
5848
let contentError: ContentErrorTypes | undefined;

0 commit comments

Comments
 (0)