// @vitest-environment jsdom import { cleanup, render, waitFor } from "@testing-library/react"; import { afterEach, describe, expect, it, vi } from "vitest"; import ScriptSentryReplay from "./ScriptSentryReplay"; const mocks = vi.hoisted(() => ({ disableSentryReplay: vi.fn(async () => true), enableSentryReplay: vi.fn(() => true), hasConsent: vi.fn(() => false), readClientSentryConfig: vi.fn(() => ({ dsn: "https://public@example.test/1", replaysOnErrorSampleRate: 1, replaysSessionSampleRate: 0.1, })), })); vi.mock("~/utils/data/useRootData", () => ({ useRootData: () => ({ userSession: { cookies: [] } }), })); vi.mock("~/utils/helpers/CookieHelper", () => ({ default: { hasConsent: mocks.hasConsent }, })); vi.mock("~/sentry.client", () => ({ readClientSentryConfig: mocks.readClientSentryConfig, })); vi.mock("~/sentry-replay.client", () => ({ disableSentryReplay: mocks.disableSentryReplay, enableSentryReplay: mocks.enableSentryReplay, })); afterEach(() => { cleanup(); vi.clearAllMocks(); mocks.hasConsent.mockReturnValue(false); }); describe("ScriptSentryReplay consent gate", () => { it("starts Replay only after analytics consent and stops it on revocation", async () => { const view = render(); expect(mocks.readClientSentryConfig).not.toHaveBeenCalled(); expect(mocks.enableSentryReplay).not.toHaveBeenCalled(); mocks.hasConsent.mockReturnValue(true); view.rerender(); await waitFor(() => { expect(mocks.enableSentryReplay).toHaveBeenCalledWith({ dsn: "https://public@example.test/1", replaysOnErrorSampleRate: 1, replaysSessionSampleRate: 0.1, }); }); mocks.hasConsent.mockReturnValue(false); view.rerender(); await waitFor(() => { expect(mocks.disableSentryReplay).toHaveBeenCalled(); }); }); });