Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/modules/shared/scripts/ScriptCrisp.test.tsx
T

61 lines
2.0 KiB
TypeScript

// @vitest-environment jsdom
import { cleanup, render, waitFor } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import ScriptCrisp from "./ScriptCrisp";
const { hasConsent, isAppOrAdminSurface } = vi.hoisted(() => ({
hasConsent: vi.fn(() => false),
isAppOrAdminSurface: vi.fn(() => false),
}));
vi.mock("react-router", () => ({
useLocation: () => ({ pathname: "/" }),
}));
vi.mock("~/utils/data/useRootData", () => ({
useRootData: () => ({ chatWebsiteId: "site-id", userSession: { cookies: [] } }),
}));
vi.mock("~/utils/helpers/CookieHelper", () => ({
default: { hasConsent },
}));
vi.mock("~/utils/app/HostAwareSurface", () => ({
isAppOrAdminSurface,
}));
afterEach(() => {
cleanup();
document.querySelectorAll('script[src="https://client.crisp.chat/l.js"]').forEach((node) => node.remove());
delete window.$crisp;
delete window.CRISP_WEBSITE_ID;
hasConsent.mockReset();
hasConsent.mockReturnValue(false);
isAppOrAdminSurface.mockReset();
isAppOrAdminSurface.mockReturnValue(false);
});
describe("ScriptCrisp privacy gate", () => {
it("does not contact Crisp without functional-cookie consent", () => {
render(<ScriptCrisp nonce="aBcDeFgHiJkLmNoPqRsTuVwX" />);
expect(document.querySelector('script[src="https://client.crisp.chat/l.js"]')).toBeNull();
});
it("loads Crisp on a public surface after consent and carries the CSP nonce", async () => {
hasConsent.mockReturnValue(true);
render(<ScriptCrisp nonce="aBcDeFgHiJkLmNoPqRsTuVwX" />);
await waitFor(() => {
const script = document.querySelector<HTMLScriptElement>('script[src="https://client.crisp.chat/l.js"]');
expect(script?.nonce).toBe("aBcDeFgHiJkLmNoPqRsTuVwX");
});
});
it("never loads the public chat script on app or admin surfaces", () => {
hasConsent.mockReturnValue(true);
isAppOrAdminSurface.mockReturnValue(true);
render(<ScriptCrisp nonce="aBcDeFgHiJkLmNoPqRsTuVwX" />);
expect(document.querySelector('script[src="https://client.crisp.chat/l.js"]')).toBeNull();
});
});