|
| 1 | +/* @flow */ |
| 2 | +import { describe, it, afterEach, expect, vi } from "vitest"; |
| 3 | + |
| 4 | +import { |
| 5 | + getGlobalSessionName, |
| 6 | + getGlobalSessionID, |
| 7 | + setGlobalSessionID, |
| 8 | +} from "./globalSession"; |
| 9 | + |
| 10 | +let storageState = {}; |
| 11 | + |
| 12 | +vi.mock("@krakenjs/belter/src", async () => { |
| 13 | + const actual = await vi.importActual("@krakenjs/belter/src"); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + getStorage: vi.fn(() => ({ |
| 17 | + getState: (handler) => handler(storageState), |
| 18 | + setState: (newState) => { |
| 19 | + storageState = { ...storageState, ...newState }; |
| 20 | + }, |
| 21 | + })), |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +vi.mock("./global", () => ({ |
| 26 | + getVersion: vi.fn(() => "5.0.500"), |
| 27 | +})); |
| 28 | + |
| 29 | +describe("globalSession", () => { |
| 30 | + afterEach(() => { |
| 31 | + vi.clearAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should generate the correct global session storage name", () => { |
| 35 | + const expectedName = "paypal_global_5_0_500"; |
| 36 | + |
| 37 | + const name = getGlobalSessionName(); |
| 38 | + |
| 39 | + expect(name).toBe(expectedName); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should return undefined if globalSessionID is not set", () => { |
| 43 | + const globalSessionID = getGlobalSessionID(); |
| 44 | + |
| 45 | + expect(globalSessionID).toBeUndefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should set and get the globalSessionID", () => { |
| 49 | + const testID = "uid_123456"; |
| 50 | + setGlobalSessionID(testID); |
| 51 | + |
| 52 | + const globalSessionID = getGlobalSessionID(); |
| 53 | + |
| 54 | + expect(globalSessionID).toBe(testID); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should overwrite the globalSessionID if set multiple times", () => { |
| 58 | + const firstID = "uid_first"; |
| 59 | + const secondID = "uid_second"; |
| 60 | + |
| 61 | + setGlobalSessionID(firstID); |
| 62 | + setGlobalSessionID(secondID); |
| 63 | + const globalSessionID = getGlobalSessionID(); |
| 64 | + |
| 65 | + expect(globalSessionID).toBe(secondID); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should handle setting globalSessionID to an empty string", () => { |
| 69 | + const emptyID = ""; |
| 70 | + |
| 71 | + setGlobalSessionID(emptyID); |
| 72 | + const globalSessionID = getGlobalSessionID(); |
| 73 | + |
| 74 | + expect(globalSessionID).toBe(emptyID); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should return undefined if state is not an object", () => { |
| 78 | + storageState = null; |
| 79 | + const globalSessionID = getGlobalSessionID(); |
| 80 | + expect(globalSessionID).toBeUndefined(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should warn if state is not an object when setting globalSessionID", async () => { |
| 84 | + const logger = await import("./logger"); |
| 85 | + storageState = null; |
| 86 | + const warnSpy = vi.spyOn(logger, "getLogger").mockReturnValue({ |
| 87 | + warn: vi.fn(), |
| 88 | + }); |
| 89 | + |
| 90 | + setGlobalSessionID("uid_error"); |
| 91 | + |
| 92 | + expect(warnSpy().warn).toHaveBeenCalledWith( |
| 93 | + "global_session_no_storage_state_found" |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should warn if version is not defined", async () => { |
| 98 | + const logger = await import("./logger"); |
| 99 | + const { getVersion } = await import("./global"); |
| 100 | + // $FlowFixMe |
| 101 | + getVersion.mockImplementation(() => undefined); |
| 102 | + |
| 103 | + const warnSpy = vi.spyOn(logger, "getLogger").mockReturnValue({ |
| 104 | + warn: vi.fn(), |
| 105 | + }); |
| 106 | + |
| 107 | + getGlobalSessionName(); |
| 108 | + |
| 109 | + expect(warnSpy().warn).toHaveBeenCalledWith( |
| 110 | + "global_session_no_sdk_version" |
| 111 | + ); |
| 112 | + }); |
| 113 | +}); |
0 commit comments