|
| 1 | +import { getUsagePulsePayload } from "./utils"; |
| 2 | +import AnalyticsUtil from "ee/utils/AnalyticsUtil"; |
| 3 | +import { FALLBACK_KEY } from "ee/constants/UsagePulse"; |
| 4 | +import { APP_MODE } from "entities/App"; |
| 5 | + |
| 6 | +jest.mock("store", () => ({ |
| 7 | + __esModule: true, |
| 8 | + default: { getState: jest.fn(() => ({})) }, |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock("ee/selectors/entitiesSelector", () => ({ |
| 12 | + getAppMode: jest.fn(() => APP_MODE.EDIT), |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock("ee/utils/AnalyticsUtil", () => ({ |
| 16 | + __esModule: true, |
| 17 | + default: { getAnonymousId: jest.fn() }, |
| 18 | +})); |
| 19 | + |
| 20 | +const getAnonymousIdMock = AnalyticsUtil.getAnonymousId as jest.Mock; |
| 21 | + |
| 22 | +describe("getUsagePulsePayload", () => { |
| 23 | + beforeEach(() => { |
| 24 | + localStorage.clear(); |
| 25 | + getAnonymousIdMock.mockReset(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("does not attach an anonymousUserId for logged-in users", () => { |
| 29 | + getAnonymousIdMock.mockReturnValue("segment-id"); |
| 30 | + |
| 31 | + const payload = getUsagePulsePayload(true, false); |
| 32 | + |
| 33 | + expect(payload).not.toHaveProperty("anonymousUserId"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("uses Segment's anonymous id when telemetry is enabled and it is available", () => { |
| 37 | + getAnonymousIdMock.mockReturnValue("segment-id"); |
| 38 | + |
| 39 | + const payload = getUsagePulsePayload(true, true); |
| 40 | + |
| 41 | + expect(payload["anonymousUserId"]).toBe("segment-id"); |
| 42 | + // Segment id should not be persisted as the local fallback. |
| 43 | + expect(localStorage.getItem(FALLBACK_KEY)).toBeNull(); |
| 44 | + }); |
| 45 | + |
| 46 | + // "Unavailable" is defined as null or undefined — the two values |
| 47 | + // AnalyticsUtil.getAnonymousId() returns when no Segment user exists. |
| 48 | + it.each([undefined, null])( |
| 49 | + "falls back to a locally persisted id when telemetry is enabled but Segment's id is %s", |
| 50 | + (segmentId) => { |
| 51 | + getAnonymousIdMock.mockReturnValue(segmentId); |
| 52 | + |
| 53 | + const payload = getUsagePulsePayload(true, true); |
| 54 | + |
| 55 | + const fallback = localStorage.getItem(FALLBACK_KEY); |
| 56 | + |
| 57 | + expect(fallback).toBeTruthy(); |
| 58 | + expect(payload["anonymousUserId"]).toBe(fallback); |
| 59 | + }, |
| 60 | + ); |
| 61 | + |
| 62 | + it("uses the local fallback id when telemetry is disabled", () => { |
| 63 | + getAnonymousIdMock.mockReturnValue("segment-id"); |
| 64 | + |
| 65 | + const payload = getUsagePulsePayload(false, true); |
| 66 | + |
| 67 | + const fallback = localStorage.getItem(FALLBACK_KEY); |
| 68 | + |
| 69 | + expect(fallback).toBeTruthy(); |
| 70 | + expect(payload["anonymousUserId"]).toBe(fallback); |
| 71 | + // Segment's id must not be used on the telemetry-disabled path. |
| 72 | + expect(payload["anonymousUserId"]).not.toBe("segment-id"); |
| 73 | + // getAnonymousId is Segment-coupled and should not be consulted here. |
| 74 | + expect(getAnonymousIdMock).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("still returns an id when localStorage is unavailable", () => { |
| 78 | + getAnonymousIdMock.mockReturnValue(undefined); |
| 79 | + |
| 80 | + const setItemSpy = jest |
| 81 | + .spyOn(Storage.prototype, "setItem") |
| 82 | + .mockImplementation(() => { |
| 83 | + throw new Error("QuotaExceededError"); |
| 84 | + }); |
| 85 | + |
| 86 | + try { |
| 87 | + const payload = getUsagePulsePayload(true, true); |
| 88 | + |
| 89 | + expect(payload["anonymousUserId"]).toBeTruthy(); |
| 90 | + } finally { |
| 91 | + setItemSpy.mockRestore(); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it("reuses the same fallback id across pulses", () => { |
| 96 | + getAnonymousIdMock.mockReturnValue(undefined); |
| 97 | + |
| 98 | + const first = getUsagePulsePayload(true, true); |
| 99 | + const second = getUsagePulsePayload(false, true); |
| 100 | + |
| 101 | + expect(first["anonymousUserId"]).toBe(second["anonymousUserId"]); |
| 102 | + }); |
| 103 | +}); |
0 commit comments