Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/test/services/gtag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { loadGtag } from "@/services/analytics/gtag";

describe("loadGtag", () => {
beforeEach(() => {
vi.stubGlobal("gtag", undefined);
vi.stubGlobal("dataLayer", undefined);
});

afterEach(() => {
vi.unstubAllGlobals();
});

it("does nothing on localhost", () => {
const spy = vi.spyOn(document.head, "appendChild");
loadGtag();
expect(spy).not.toHaveBeenCalled();
expect(window.gtag).toBeUndefined();
spy.mockRestore();
});

it("does nothing if gtag is already defined", () => {
vi.stubGlobal("location", { hostname: "dmaman86.github.io" });
vi.stubGlobal("gtag", vi.fn());
const spy = vi.spyOn(document.head, "appendChild");

loadGtag();

expect(spy).not.toHaveBeenCalled();
spy.mockRestore();
});

it("injects async GA script when not on localhost", () => {
vi.stubGlobal("location", { hostname: "dmaman86.github.io" });
const spy = vi.spyOn(document.head, "appendChild");

loadGtag();

expect(spy).toHaveBeenCalledOnce();
const script = spy.mock.calls[0][0] as HTMLScriptElement;
expect(script.src).toContain("googletagmanager.com/gtag/js");
expect(script.async).toBe(true);
spy.mockRestore();
});

it("sets up window.gtag as a function and initializes dataLayer", () => {
vi.stubGlobal("location", { hostname: "dmaman86.github.io" });
const spy = vi.spyOn(document.head, "appendChild").mockImplementation(() => document.head);

loadGtag();

expect(typeof window.gtag).toBe("function");
expect(Array.isArray(window.dataLayer)).toBe(true);
spy.mockRestore();
});

it("pushes js and config commands to dataLayer after loading", () => {
vi.stubGlobal("location", { hostname: "dmaman86.github.io" });
const spy = vi.spyOn(document.head, "appendChild").mockImplementation(() => document.head);

loadGtag();

expect(window.dataLayer).toHaveLength(2);
const [jsCall, configCall] = window.dataLayer as unknown[][];
expect(jsCall[0]).toBe("js");
expect(jsCall[1]).toBeInstanceOf(Date);
expect(configCall[0]).toBe("config");
expect(configCall[1]).toBe("G-G19J1209M6");
spy.mockRestore();
});

it("is idempotent: second call does nothing if gtag already loaded", () => {
vi.stubGlobal("location", { hostname: "dmaman86.github.io" });
const spy = vi.spyOn(document.head, "appendChild").mockImplementation(() => document.head);

loadGtag();
loadGtag();

expect(spy).toHaveBeenCalledOnce();
spy.mockRestore();
});
});
85 changes: 85 additions & 0 deletions src/test/ui/hooks/useCookieConsent.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from "vitest";
import { renderHook, act } from "@testing-library/react";

vi.mock("@/services", () => ({
loadGtag: vi.fn(),
}));

import { loadGtag } from "@/services";
import { useCookieConsent } from "@/hooks/useCookieConsent";

describe("useCookieConsent", () => {
let store: Record<string, string> = {};

beforeAll(() => {
vi.stubGlobal("localStorage", {
getItem: (key: string) => store[key] ?? null,
setItem: (key: string, value: string) => { store[key] = value; },
removeItem: (key: string) => { delete store[key]; },
clear: () => { store = {}; },
});
});

afterAll(() => {
vi.unstubAllGlobals();
});

beforeEach(() => {
store = {};
vi.clearAllMocks();
});

it("returns null when no consent is stored", () => {
const { result } = renderHook(() => useCookieConsent());
expect(result.current.consent).toBeNull();
});

it("returns true when consent was previously accepted", () => {
store["cookie_consent"] = "true";
const { result } = renderHook(() => useCookieConsent());
expect(result.current.consent).toBe(true);
});

it("returns false when consent was previously rejected", () => {
store["cookie_consent"] = "false";
const { result } = renderHook(() => useCookieConsent());
expect(result.current.consent).toBe(false);
});

it("accept() sets consent to true and persists to localStorage", () => {
const { result } = renderHook(() => useCookieConsent());
act(() => result.current.accept());
expect(result.current.consent).toBe(true);
expect(store["cookie_consent"]).toBe("true");
});

it("reject() sets consent to false and persists to localStorage", () => {
const { result } = renderHook(() => useCookieConsent());
act(() => result.current.reject());
expect(result.current.consent).toBe(false);
expect(store["cookie_consent"]).toBe("false");
});

it("calls loadGtag when user accepts", () => {
const { result } = renderHook(() => useCookieConsent());
act(() => result.current.accept());
expect(loadGtag).toHaveBeenCalledTimes(1);
});

it("does not call loadGtag when consent is null on mount", () => {
renderHook(() => useCookieConsent());
expect(loadGtag).not.toHaveBeenCalled();
});

it("does not call loadGtag when user rejects", () => {
const { result } = renderHook(() => useCookieConsent());
act(() => result.current.reject());
expect(loadGtag).not.toHaveBeenCalled();
});

it("calls loadGtag on mount when previously accepted", () => {
store["cookie_consent"] = "true";
renderHook(() => useCookieConsent());
expect(loadGtag).toHaveBeenCalledTimes(1);
});
});
Loading