Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/globalSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ function getGlobalSessionStorage(): Storage {
return getStorage({ name: getGlobalSessionName() });
}

function getGlobalStorageState<T>(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" ||
Expand Down
41 changes: 33 additions & 8 deletions src/globalSession.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})),
};
});

Expand Down Expand Up @@ -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");
});
});
5 changes: 4 additions & 1 deletion vitestSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
});
};

Expand Down
Loading