handle-capture-consent.test.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import posthog from "posthog-js";
  2. import { afterEach, describe, expect, it, vi } from "vitest";
  3. import { handleCaptureConsent } from "#/utils/handle-capture-consent";
  4. describe("handleCaptureConsent", () => {
  5. const optInSpy = vi.spyOn(posthog, "opt_in_capturing");
  6. const optOutSpy = vi.spyOn(posthog, "opt_out_capturing");
  7. const hasOptedInSpy = vi.spyOn(posthog, "has_opted_in_capturing");
  8. const hasOptedOutSpy = vi.spyOn(posthog, "has_opted_out_capturing");
  9. afterEach(() => {
  10. vi.clearAllMocks();
  11. });
  12. it("should opt out of of capturing", () => {
  13. handleCaptureConsent(false);
  14. expect(optOutSpy).toHaveBeenCalled();
  15. expect(optInSpy).not.toHaveBeenCalled();
  16. });
  17. it("should opt in to capturing if the user consents", () => {
  18. handleCaptureConsent(true);
  19. expect(optInSpy).toHaveBeenCalled();
  20. expect(optOutSpy).not.toHaveBeenCalled();
  21. });
  22. it("should not opt in to capturing if the user is already opted in", () => {
  23. hasOptedInSpy.mockReturnValueOnce(true);
  24. handleCaptureConsent(true);
  25. expect(optInSpy).not.toHaveBeenCalled();
  26. expect(optOutSpy).not.toHaveBeenCalled();
  27. });
  28. it("should not opt out of capturing if the user is already opted out", () => {
  29. hasOptedOutSpy.mockReturnValueOnce(true);
  30. handleCaptureConsent(false);
  31. expect(optOutSpy).not.toHaveBeenCalled();
  32. expect(optInSpy).not.toHaveBeenCalled();
  33. });
  34. });