Skip to content

Commit 9843f96

Browse files
donna-belsey-nhsanoop-surej-nhs
authored andcommitted
VIA-2 MD/DB Restore mocking of content layer in vaccine page unit tests
Re-introducing the concept of mocking allows testing of when errors are thrown in the content stack and de-couples the page unit tests from depending on the content stack being implemented correctly. A separate file, integration.test exists for testing that the real content stack and the individual vaccine pages work together correctly.
1 parent f497ac8 commit 9843f96

File tree

2 files changed

+56
-46
lines changed

2 files changed

+56
-46
lines changed
Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
import { configProvider } from "@src/utils/config";
21
import { render, screen } from "@testing-library/react";
32
import Vaccine6in1 from "@src/app/vaccines/6-in-1/page";
43
import Vaccine from "@src/app/_components/vaccine/Vaccine";
4+
import { getContentForVaccine } from "@src/services/content-api/gateway/content-reader-service";
5+
import { mockStyledContent } from "@test-data/content-api/data";
56

6-
jest.mock("@src/utils/config");
7+
jest.mock("@src/services/content-api/gateway/content-reader-service");
78
jest.mock("@src/app/_components/vaccine/Vaccine", () => jest.fn(() => <div />));
89

910
describe("6-in-1 vaccine page", () => {
10-
(configProvider as jest.Mock).mockImplementation(() => ({
11-
CONTENT_CACHE_PATH: "wiremock/__files/",
12-
PINO_LOG_LEVEL: "info",
13-
}));
11+
describe("when content loaded successfully", () => {
12+
beforeEach(() => {
13+
(getContentForVaccine as jest.Mock).mockResolvedValue(
14+
() => mockStyledContent,
15+
);
16+
(Vaccine as jest.Mock).mockImplementation(() => <div />);
17+
});
1418

15-
it("should contain back link to vaccination schedule page", () => {
16-
const pathToSchedulePage = "/schedule";
19+
it("should contain back link to vaccination schedule page", () => {
20+
const pathToSchedulePage = "/schedule";
1721

18-
render(Vaccine6in1());
22+
render(Vaccine6in1());
1923

20-
const linkToSchedulePage = screen.getByRole("link", { name: "Go back" });
21-
expect(linkToSchedulePage.getAttribute("href")).toBe(pathToSchedulePage);
22-
});
24+
const linkToSchedulePage = screen.getByRole("link", { name: "Go back" });
25+
expect(linkToSchedulePage.getAttribute("href")).toBe(pathToSchedulePage);
26+
});
2327

24-
it("should contain vaccine component", () => {
25-
render(Vaccine6in1());
28+
it("should contain vaccine component", () => {
29+
render(Vaccine6in1());
2630

27-
expect(Vaccine).toHaveBeenCalledWith(
28-
{
29-
name: "6-in-1",
30-
},
31-
undefined,
32-
);
31+
expect(Vaccine).toHaveBeenCalledWith(
32+
{
33+
name: "6-in-1",
34+
},
35+
undefined,
36+
);
37+
});
3338
});
3439
});

src/app/vaccines/rsv/page.test.tsx

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
1-
import { configProvider } from "@src/utils/config";
21
import { render, screen } from "@testing-library/react";
32
import VaccineRsv from "@src/app/vaccines/rsv/page";
43
import Vaccine from "@src/app/_components/vaccine/Vaccine";
4+
import { getContentForVaccine } from "@src/services/content-api/gateway/content-reader-service";
5+
import { mockStyledContent } from "@test-data/content-api/data";
56

6-
jest.mock("@src/utils/config");
7+
jest.mock("@src/services/content-api/gateway/content-reader-service");
78
jest.mock("@src/app/_components/vaccine/Vaccine", () => jest.fn(() => <div />));
89

910
describe("RSV vaccine page", () => {
10-
(configProvider as jest.Mock).mockImplementation(() => ({
11-
CONTENT_CACHE_PATH: "wiremock/__files/",
12-
PINO_LOG_LEVEL: "info",
13-
}));
14-
15-
it("should contain back link to vaccination schedule page", () => {
16-
const pathToSchedulePage = "/schedule";
17-
18-
render(VaccineRsv());
19-
20-
const linkToSchedulePage = screen.getByRole("link", { name: "Go back" });
21-
22-
expect(linkToSchedulePage.getAttribute("href")).toBe(pathToSchedulePage);
23-
});
24-
25-
it("should contain vaccine component", () => {
26-
render(VaccineRsv());
27-
28-
expect(Vaccine).toHaveBeenCalledWith(
29-
{
30-
name: "RSV",
31-
},
32-
undefined,
33-
);
11+
describe("when content loaded successfully", () => {
12+
beforeEach(() => {
13+
(getContentForVaccine as jest.Mock).mockResolvedValue(
14+
() => mockStyledContent,
15+
);
16+
(Vaccine as jest.Mock).mockImplementation(() => <div />);
17+
});
18+
19+
it("should contain back link to vaccination schedule page", () => {
20+
const pathToSchedulePage = "/schedule";
21+
22+
render(VaccineRsv());
23+
24+
const linkToSchedulePage = screen.getByRole("link", { name: "Go back" });
25+
26+
expect(linkToSchedulePage.getAttribute("href")).toBe(pathToSchedulePage);
27+
});
28+
29+
it("should contain vaccine component", () => {
30+
render(VaccineRsv());
31+
32+
expect(Vaccine).toHaveBeenCalledWith(
33+
{
34+
name: "RSV",
35+
},
36+
undefined,
37+
);
38+
});
3439
});
3540
});

0 commit comments

Comments
 (0)