File size: 1,446 Bytes
246d201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import posthog from "posthog-js";
import { afterEach, describe, expect, it, vi } from "vitest";
import { handleCaptureConsent } from "#/utils/handle-capture-consent";

describe("handleCaptureConsent", () => {
  const optInSpy = vi.spyOn(posthog, "opt_in_capturing");
  const optOutSpy = vi.spyOn(posthog, "opt_out_capturing");
  const hasOptedInSpy = vi.spyOn(posthog, "has_opted_in_capturing");
  const hasOptedOutSpy = vi.spyOn(posthog, "has_opted_out_capturing");

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

  it("should opt out of of capturing", () => {
    handleCaptureConsent(false);

    expect(optOutSpy).toHaveBeenCalled();
    expect(optInSpy).not.toHaveBeenCalled();
  });

  it("should opt in to capturing if the user consents", () => {
    handleCaptureConsent(true);

    expect(optInSpy).toHaveBeenCalled();
    expect(optOutSpy).not.toHaveBeenCalled();
  });

  it("should not opt in to capturing if the user is already opted in", () => {
    hasOptedInSpy.mockReturnValueOnce(true);
    handleCaptureConsent(true);

    expect(optInSpy).not.toHaveBeenCalled();
    expect(optOutSpy).not.toHaveBeenCalled();
  });

  it("should not opt out of capturing if the user is already opted out", () => {
    hasOptedOutSpy.mockReturnValueOnce(true);
    handleCaptureConsent(false);

    expect(optOutSpy).not.toHaveBeenCalled();
    expect(optInSpy).not.toHaveBeenCalled();
  });
});