Skip to content

Commit 846863a

Browse files
VIA-254 Refactor getToken test to extract shared beforeEach blocks
1 parent dfc6ff5 commit 846863a

File tree

1 file changed

+25
-44
lines changed

1 file changed

+25
-44
lines changed

src/utils/auth/callbacks/get-token.test.ts

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Account, Profile } from "next-auth";
88
import { JWT } from "next-auth/jwt";
99
import { ReadonlyHeaders } from "next/dist/server/web/spec-extension/adapters/headers";
1010
import { headers } from "next/headers";
11+
import { ApimHttpError } from "@src/utils/auth/apim/exceptions";
1112

1213
jest.mock("@project/auth", () => ({
1314
auth: jest.fn(),
@@ -33,39 +34,39 @@ describe("getToken", () => {
3334
(headers as jest.Mock).mockResolvedValue(fakeHeaders);
3435
});
3536

36-
describe("when AUTH APIM is available", () => {
37-
const oldNEXT_RUNTIME = process.env.NEXT_RUNTIME;
37+
const oldNEXT_RUNTIME = process.env.NEXT_RUNTIME;
3838

39-
const mockConfig: AppConfig = appConfigBuilder()
40-
.withNHS_LOGIN_URL("https://mock.nhs.login")
41-
.andNHS_LOGIN_CLIENT_ID("mock-client-id")
42-
.andNHS_LOGIN_PRIVATE_KEY("mock-private-key")
43-
.build();
39+
const mockConfig: AppConfig = appConfigBuilder()
40+
.withNHS_LOGIN_URL("https://mock.nhs.login")
41+
.andNHS_LOGIN_CLIENT_ID("mock-client-id")
42+
.andNHS_LOGIN_PRIVATE_KEY("mock-private-key")
43+
.build();
4444

45-
const nowInSeconds = 1749052001;
45+
const nowInSeconds = 1749052001;
4646

47-
beforeEach(() => {
48-
jest.clearAllMocks();
49-
jest.useFakeTimers().setSystemTime(nowInSeconds * 1000);
50-
process.env.NEXT_RUNTIME = "nodejs";
51-
});
47+
beforeEach(() => {
48+
jest.clearAllMocks();
49+
jest.useFakeTimers().setSystemTime(nowInSeconds * 1000);
50+
process.env.NEXT_RUNTIME = "nodejs";
51+
});
52+
53+
afterEach(() => {
54+
jest.resetAllMocks();
55+
process.env.NEXT_RUNTIME = oldNEXT_RUNTIME;
56+
});
5257

58+
afterAll(() => {
59+
jest.useRealTimers();
60+
});
61+
62+
describe("when AUTH APIM is available", () => {
5363
beforeEach(async () => {
5464
(getOrRefreshApimCredentials as jest.Mock).mockResolvedValue({
5565
accessToken: "new-apim-access-token",
5666
expiresAt: nowInSeconds + 1111,
5767
});
5868
});
5969

60-
afterEach(() => {
61-
jest.resetAllMocks();
62-
process.env.NEXT_RUNTIME = oldNEXT_RUNTIME;
63-
});
64-
65-
afterAll(() => {
66-
jest.useRealTimers();
67-
});
68-
6970
it("should return null and logs error if token is falsy", async () => {
7071
const result = await getToken(null as unknown as JWT, null, undefined, mockConfig, 300 as MaxAgeInSeconds);
7172
expect(result).toBeNull();
@@ -173,32 +174,12 @@ describe("getToken", () => {
173174
});
174175
});
175176

176-
// this condition is now not relevant at this layer
177177
describe("when AUTH APIM is not available", () => {
178-
(getOrRefreshApimCredentials as jest.Mock).mockResolvedValue(undefined);
179-
180-
const mockConfig: AppConfig = appConfigBuilder()
181-
.withNHS_LOGIN_URL("https://mock.nhs.login")
182-
.andNHS_LOGIN_CLIENT_ID("mock-client-id")
183-
.andNHS_LOGIN_PRIVATE_KEY("mock-private-key")
184-
.build();
185-
186-
const nowInSeconds = 1749052001;
187-
188178
beforeEach(() => {
189-
jest.clearAllMocks();
190-
jest.useFakeTimers().setSystemTime(nowInSeconds * 1000);
191-
});
192-
193-
afterEach(() => {
194-
jest.resetAllMocks();
195-
});
196-
197-
afterAll(() => {
198-
jest.useRealTimers();
179+
(getOrRefreshApimCredentials as jest.Mock).mockResolvedValue(undefined);
199180
});
200181

201-
it("should return updated token on initial login with account profile, and default APIM credentials", async () => {
182+
it("should return updated token on initial login with account profile, and default empty APIM credentials", async () => {
202183
(jwtDecode as jest.Mock).mockReturnValue({
203184
jti: "jti_test",
204185
});

0 commit comments

Comments
 (0)