From 19455be8aeee058555cd41f038925b7da63c9c9a Mon Sep 17 00:00:00 2001 From: "Gustavo Veloso (CW)" Date: Thu, 21 Aug 2025 09:55:16 -0300 Subject: [PATCH 1/2] Taking warning off of globalSession --- vitestSetup.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vitestSetup.js b/vitestSetup.js index a183ea4b..fd1aea9d 100644 --- a/vitestSetup.js +++ b/vitestSetup.js @@ -3,7 +3,10 @@ import { sdkClientTestGlobals } from "./test/globals"; const applyEnvs = () => { Object.keys(sdkClientTestGlobals).forEach((k) => { - window[k] = sdkClientTestGlobals[k]; + // Skip crypto property as it cannot be directly assigned + if (k !== "crypto") { + window[k] = sdkClientTestGlobals[k]; + } }); }; From 035d7dda6d46084d8bd4d711d07f3a5b8bf17d52 Mon Sep 17 00:00:00 2001 From: "Gustavo Veloso (CW)" Date: Fri, 22 Aug 2025 15:33:43 -0300 Subject: [PATCH 2/2] Checking if globalSession storage exists in order to don't create a new empty one --- src/globalSession.js | 15 +++++++++----- src/globalSession.test.js | 41 +++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/globalSession.js b/src/globalSession.js index 7aafadc9..069a6cee 100644 --- a/src/globalSession.js +++ b/src/globalSession.js @@ -20,12 +20,17 @@ function getGlobalSessionStorage(): Storage { return getStorage({ name: getGlobalSessionName() }); } -function getGlobalStorageState(handler: (storage: Object) => T): T { - return getGlobalSessionStorage().getState(handler); -} - export function getGlobalSessionID(): string | void { - const globalSessionID = getGlobalStorageState((state) => { + const storage = getGlobalSessionStorage(); + + if ( + !storage || + (storage.checkIfStorageExists && !storage.checkIfStorageExists()) + ) { + return undefined; + } + + const globalSessionID = storage.getState((state) => { if ( !state || typeof state !== "object" || diff --git a/src/globalSession.test.js b/src/globalSession.test.js index ff171f17..9ecc5b54 100644 --- a/src/globalSession.test.js +++ b/src/globalSession.test.js @@ -8,22 +8,24 @@ import { } from "./globalSession"; let storageState = {}; +let storageExistsMock = vi.fn(() => true); -vi.mock("@krakenjs/belter/src", async () => { - const actual = await vi.importActual("@krakenjs/belter/src"); +vi.mock("./global", async () => { + const actual = await vi.importActual("./global"); return { ...actual, - getStorage: vi.fn(() => ({ - getState: (handler) => handler(storageState), - })), + getVersion: vi.fn(() => "5.0.500"), }; }); -vi.mock("./global", async () => { - const actual = await vi.importActual("./global"); +vi.mock("@krakenjs/belter/src", async () => { + const actual = await vi.importActual("@krakenjs/belter/src"); return { ...actual, - getVersion: vi.fn(() => "5.0.500"), + getStorage: vi.fn(() => ({ + getState: (handler) => handler(storageState), + checkIfStorageExists: () => storageExistsMock, + })), }; }); @@ -80,4 +82,27 @@ describe("globalSession", () => { const globalSessionID = getGlobalSessionID(); expect(globalSessionID).toBeUndefined(); }); + + it("should return undefined if storage does not exist", () => { + storageExistsMock = vi.fn(() => false); + const globalSessionID = getGlobalSessionID(); + expect(globalSessionID).toBeUndefined(); + storageExistsMock = vi.fn(() => true); + }); + + it("should return undefined if storage is undefined", async () => { + const { getStorage } = await import("@krakenjs/belter/src"); + // $FlowFixMe + getStorage.mockImplementation(() => undefined); + const globalSessionID = getGlobalSessionID(); + expect(globalSessionID).toBeUndefined(); + }); + + it("should return unknown version if getVersion returns null", async () => { + const { getVersion } = await import("./global"); + // $FlowFixMe + getVersion.mockImplementation(() => undefined); + const name = getGlobalSessionName(); + expect(name).toBe("paypal_global_unknown"); + }); });