Skip to content
4 changes: 2 additions & 2 deletions ui/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ const createStorageMock = (): Storage => {

const sessionStorageMock = createStorageMock();

Object.defineProperty(window, "sessionStorage", {
Object.defineProperty(globalThis, "sessionStorage", {
value: sessionStorageMock,
configurable: true,
});

beforeEach(() => {
window.sessionStorage.clear();
globalThis.sessionStorage.clear();
});
Comment thread
cptiv2020 marked this conversation as resolved.

globalThis.TextEncoder = TextEncoder;
Expand Down
20 changes: 10 additions & 10 deletions ui/src/__tests__/lib/services/session-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sessionService, { SESSION_STORAGE_KEYS } from "@/lib/services/session-ser

describe("SessionService", () => {
beforeEach(() => {
window.sessionStorage.clear();
globalThis.sessionStorage.clear();
});

describe("auth user", () => {
Expand All @@ -18,21 +18,21 @@ describe("SessionService", () => {

sessionService.dehydrateAuthUser(user);

expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.authUser)).toBe(
expect(globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.authUser)).toBe(
JSON.stringify(user),
);
expect(sessionService.rehydrateAuthUser<typeof user>()).toEqual(user);
});

it("clears auth user when null is provided", () => {
window.sessionStorage.setItem(
globalThis.sessionStorage.setItem(
SESSION_STORAGE_KEYS.authUser,
JSON.stringify({ sub: "existing" }),
);

sessionService.dehydrateAuthUser(null);

expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.authUser)).toBeNull();
expect(globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.authUser)).toBeNull();
});
});

Expand All @@ -54,13 +54,13 @@ describe("SessionService", () => {

sessionService.dehydrateJourneyNavigation(navigation);

expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.journeyNavigation)).toBe(
expect(globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.journeyNavigation)).toBe(
JSON.stringify(navigation),
);

sessionService.clearJourneyNavigation();

expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.journeyNavigation)).toBeNull();
expect(globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.journeyNavigation)).toBeNull();
});
});

Expand All @@ -79,13 +79,13 @@ describe("SessionService", () => {

sessionService.dehydrateCreateOrderAnswers(answers);

expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.createOrderAnswers)).toBe(
expect(globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.createOrderAnswers)).toBe(
JSON.stringify(answers),
);

sessionService.clearCreateOrderAnswers();

expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.createOrderAnswers)).toBeNull();
expect(globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.createOrderAnswers)).toBeNull();
});
});

Expand Down Expand Up @@ -118,13 +118,13 @@ describe("SessionService", () => {

sessionService.dehydratePostcodeLookup(postcodeLookup);

expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.postcodeLookup)).toBe(
expect(globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.postcodeLookup)).toBe(
JSON.stringify(postcodeLookup),
);

sessionService.clearPostcodeLookup();

expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.postcodeLookup)).toBeNull();
expect(globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.postcodeLookup)).toBeNull();
});
});
});
14 changes: 7 additions & 7 deletions ui/src/__tests__/lib/services/session-storage-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sessionStorageService from "@/lib/services/session-storage-service";

describe("SessionStorageService", () => {
beforeEach(() => {
window.sessionStorage.clear();
globalThis.sessionStorage.clear();
});

describe("rehydrate", () => {
Expand All @@ -21,16 +21,16 @@ describe("SessionStorageService", () => {
});

it("returns parsed value when key exists with valid JSON", () => {
window.sessionStorage.setItem("my-key", JSON.stringify({ foo: "bar" }));
globalThis.sessionStorage.setItem("my-key", JSON.stringify({ foo: "bar" }));

expect(sessionStorageService.rehydrate("my-key", null)).toEqual({ foo: "bar" });
});

it("returns fallback and removes key when stored value is invalid JSON", () => {
window.sessionStorage.setItem("bad-key", "not-valid-json{");
globalThis.sessionStorage.setItem("bad-key", "not-valid-json{");

expect(sessionStorageService.rehydrate("bad-key", "default")).toBe("default");
expect(window.sessionStorage.getItem("bad-key")).toBeNull();
expect(globalThis.sessionStorage.getItem("bad-key")).toBeNull();
});
});

Expand All @@ -48,7 +48,7 @@ describe("SessionStorageService", () => {
it("stores value as JSON string", () => {
sessionStorageService.dehydrate("my-key", { a: 1, b: true });

expect(window.sessionStorage.getItem("my-key")).toBe(JSON.stringify({ a: 1, b: true }));
expect(globalThis.sessionStorage.getItem("my-key")).toBe(JSON.stringify({ a: 1, b: true }));
});
});

Expand All @@ -64,11 +64,11 @@ describe("SessionStorageService", () => {
});

it("removes the key from sessionStorage", () => {
window.sessionStorage.setItem("to-remove", "value");
globalThis.sessionStorage.setItem("to-remove", "value");

sessionStorageService.remove("to-remove");

expect(window.sessionStorage.getItem("to-remove")).toBeNull();
expect(globalThis.sessionStorage.getItem("to-remove")).toBeNull();
});
});
});
10 changes: 5 additions & 5 deletions ui/src/__tests__/state/AuthContext.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "@testing-library/jest-dom";
import { act, render, renderHook, screen } from "@testing-library/react";

import { AuthProvider, AuthUser, useAuth } from "@/state";
import { act, render, renderHook, screen } from "@testing-library/react";

const AUTH_STORAGE_KEY = "hometest:auth:user";

Expand All @@ -18,7 +18,7 @@ describe("AuthContext", () => {
};

beforeEach(() => {
window.sessionStorage.clear();
globalThis.sessionStorage.clear();
});

describe("AuthProvider", () => {
Expand All @@ -42,7 +42,7 @@ describe("AuthContext", () => {
});

it("rehydrates user state from session storage", () => {
window.sessionStorage.setItem(AUTH_STORAGE_KEY, JSON.stringify(mockUser));
globalThis.sessionStorage.setItem(AUTH_STORAGE_KEY, JSON.stringify(mockUser));

const { result } = renderHook(() => useAuth(), {
wrapper: AuthProvider,
Expand Down Expand Up @@ -94,7 +94,7 @@ describe("AuthContext", () => {
expect(result.current.user).toEqual(mockUser);
expect(result.current.user?.nhsNumber).toBe("9876543210");
expect(result.current.user?.birthdate).toBe("1985-05-15");
expect(window.sessionStorage.getItem(AUTH_STORAGE_KEY)).toBe(JSON.stringify(mockUser));
expect(globalThis.sessionStorage.getItem(AUTH_STORAGE_KEY)).toBe(JSON.stringify(mockUser));
});

it("clears user state when setUser is called with null", () => {
Expand All @@ -115,7 +115,7 @@ describe("AuthContext", () => {
});

expect(result.current.user).toBeNull();
expect(window.sessionStorage.getItem(AUTH_STORAGE_KEY)).toBeNull();
expect(globalThis.sessionStorage.getItem(AUTH_STORAGE_KEY)).toBeNull();
});

it("updates user correctly when setUser is called multiple times", () => {
Expand Down
16 changes: 9 additions & 7 deletions ui/src/__tests__/state/OrderContext.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import "@testing-library/jest-dom";
import { act, renderHook } from "@testing-library/react";

import { CreateOrderProvider, useCreateOrderContext } from "@/state/OrderContext";
import { SESSION_STORAGE_KEYS } from "@/lib/services/session-service";
import { act, renderHook } from "@testing-library/react";
import { CreateOrderProvider, useCreateOrderContext } from "@/state/OrderContext";

describe("OrderContext", () => {
beforeEach(() => {
window.sessionStorage.clear();
globalThis.sessionStorage.clear();
});

describe("CreateOrderProvider", () => {
Expand All @@ -24,7 +24,7 @@ describe("OrderContext", () => {
mobileNumber: "07700900123",
};

window.sessionStorage.setItem(
globalThis.sessionStorage.setItem(
SESSION_STORAGE_KEYS.createOrderAnswers,
JSON.stringify(persistedAnswers),
);
Expand Down Expand Up @@ -52,7 +52,7 @@ describe("OrderContext", () => {
postcodeSearch: "SW1A 1AA",
mobileNumber: "07700900123",
});
expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.createOrderAnswers)).toBe(
expect(globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.createOrderAnswers)).toBe(
JSON.stringify({
postcodeSearch: "SW1A 1AA",
mobileNumber: "07700900123",
Expand All @@ -69,14 +69,16 @@ describe("OrderContext", () => {
result.current.updateOrderAnswers({ postcodeSearch: "SW1A 1AA" });
});

expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.createOrderAnswers)).not.toBeNull();
expect(
globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.createOrderAnswers),
).not.toBeNull();

act(() => {
result.current.reset();
});

expect(result.current.orderAnswers).toEqual({});
expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.createOrderAnswers)).toBeNull();
expect(globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.createOrderAnswers)).toBeNull();
});
});

Expand Down
18 changes: 11 additions & 7 deletions ui/src/__tests__/state/PostcodeLookupContext.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "@testing-library/jest-dom";
import { act, renderHook, waitFor } from "@testing-library/react";

import { PostcodeLookupProvider, usePostcodeLookup } from "@/state/PostcodeLookupContext";
import { SESSION_STORAGE_KEYS } from "@/lib/services/session-service";
import { act, renderHook, waitFor } from "@testing-library/react";
import { PostcodeLookupProvider, usePostcodeLookup } from "@/state/PostcodeLookupContext";

jest.mock("@/settings", () => ({ backendUrl: "http://mock-backend" }));

Expand All @@ -11,7 +11,7 @@ globalThis.fetch = mockFetch as typeof fetch;

describe("PostcodeLookupContext", () => {
beforeEach(() => {
window.sessionStorage.clear();
globalThis.sessionStorage.clear();
jest.clearAllMocks();
});

Expand Down Expand Up @@ -51,7 +51,7 @@ describe("PostcodeLookupContext", () => {
error: null,
};

window.sessionStorage.setItem(
globalThis.sessionStorage.setItem(
SESSION_STORAGE_KEYS.postcodeLookup,
JSON.stringify(persistedState),
);
Expand Down Expand Up @@ -98,7 +98,9 @@ describe("PostcodeLookupContext", () => {
expect(result.current.error).toBeNull();

await waitFor(() => {
expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.postcodeLookup)).not.toBeNull();
expect(
globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.postcodeLookup),
).not.toBeNull();
});
});

Expand Down Expand Up @@ -164,7 +166,9 @@ describe("PostcodeLookupContext", () => {
});

await waitFor(() => {
expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.postcodeLookup)).not.toBeNull();
expect(
globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.postcodeLookup),
).not.toBeNull();
});

act(() => {
Expand All @@ -176,7 +180,7 @@ describe("PostcodeLookupContext", () => {
expect(result.current.selectedAddress).toBeNull();
expect(result.current.lookupResultsStatus).toBe("idle");
expect(result.current.error).toBeNull();
expect(window.sessionStorage.getItem(SESSION_STORAGE_KEYS.postcodeLookup)).toBeNull();
expect(globalThis.sessionStorage.getItem(SESSION_STORAGE_KEYS.postcodeLookup)).toBeNull();
});
});

Expand Down
12 changes: 7 additions & 5 deletions ui/src/routes/HomeTestPrivacyPolicyPage.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"use client";

import PageLayout from "@/layouts/PageLayout";
import { useContent } from "@/hooks";
import { useNavigate } from "react-router-dom";
import { renderTextWithLinks, cleanListItems, getListClass } from "@/utils/renderTextWithLinks";
import "@/styles/lists.css";

import { useNavigate } from "react-router-dom";

import { useContent } from "@/hooks";
import PageLayout from "@/layouts/PageLayout";
import { cleanListItems, getListClass, renderTextWithLinks } from "@/utils/renderTextWithLinks";

export default function HomeTestPrivacyPolicyPage() {
const navigate = useNavigate();
const { "home-test-privacy-policy": content } = useContent();

const renderHeading = (text: string) => {
const numberMatch = text.match(/^(\d+\.\s+)/);
const numberMatch = /^(\d+\.\s+)/.exec(text);
if (numberMatch) {
return (
<>
Expand Down
2 changes: 1 addition & 1 deletion ui/src/routes/HomeTestTermsOfUsePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const renderTableRow = (row: string[], rowIdx: number) => (
*/
const renderParagraphs = (paragraphs: string[]) =>
paragraphs.map((paragraph, index) => {
const numberMatch = paragraph.match(/^(\d+\.\d+\.?\s+)/);
const numberMatch = /^(\d+\.\d+\.?\s+)/.exec(paragraph);
if (numberMatch) {
const number = numberMatch[1];
const rest = paragraph.slice(number.length);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"use client";

import { Button, ErrorSummary, TextInput } from "nhsuk-react-components";
import { useAuth, useCreateOrderContext, useJourneyNavigationContext } from "@/state";
import { useState } from "react";

import type { ValidationMessages } from "@/content";
import { useContent } from "@/hooks";
import FormPageLayout from "@/layouts/FormPageLayout";
import { JourneyStepNames } from "@/lib/models/route-paths";
import type { ValidationMessages } from "@/content";
import laLookupService from "@/lib/services/la-lookup-service";
import { useContent } from "@/hooks";
import { useState } from "react";
import { isUnder18 } from "@/lib/utils/is-under-18";
import { useAuth, useCreateOrderContext, useJourneyNavigationContext } from "@/state";

const POSTCODE_REGEX = /^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/i;
const MAX_POSTCODE_LENGTH = 8;
Expand Down Expand Up @@ -211,7 +212,7 @@ export default function EnterAddressManuallyPage() {
try {
const postcode = postcodeValidation.value;
const laResponse = await laLookupService.getByPostcode(postcode);
if (!laResponse || !laResponse.suppliers || laResponse.suppliers.length === 0) {
if (!laResponse?.suppliers?.length) {
updateOrderAnswers({ postcodeSearch: postcode });
goToStep(JourneyStepNames.KitNotAvailableInArea);
return;
Expand Down
Loading
Loading