From f45d73e297dfaf979a75dc40ca8beb584263f7fd Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Tue, 5 Dec 2023 10:49:05 -0500 Subject: [PATCH] Tests for deviceID --- .../cookie-helper.spec.js | 91 +++++++++++++++---- src/helpers/unit-test-helper.js | 8 ++ 2 files changed, 83 insertions(+), 16 deletions(-) diff --git a/src/helpers/heritage-integration/cookie-helper.spec.js b/src/helpers/heritage-integration/cookie-helper.spec.js index 5df2ae925..8abd265e5 100644 --- a/src/helpers/heritage-integration/cookie-helper.spec.js +++ b/src/helpers/heritage-integration/cookie-helper.spec.js @@ -3,10 +3,19 @@ import { getDeviceIdValue, getSessionKeyValue, getSessionIdValue, + setCookieProperties, + regenerateDeviceId, } from "@/helpers/heritage-integration/cookie-helper.js"; -import { removeAllTestCookies, setupCookies } from "@/helpers/unit-test-helper"; +import { cookieNames } from "@/constants/cookie-names"; +import { removeAllTestCookies, setupCookies, setupCrypto } from "@/helpers/unit-test-helper"; + +const randomUUID = "68d89736-c277-46f1-8fee-c3dacdb23c08"; describe("cookies", () => { + beforeEach(() => { + setupCrypto(randomUUID); + }); + afterEach(() => { removeAllTestCookies(); }); @@ -106,30 +115,80 @@ describe("cookies", () => { }); }); - describe("getDeviceIdValue", () => { - test("getDeviceIdValue, should return GUID", () => { - // Arrange - setupCookies({}); + describe("Device Id", () => { + describe("getDeviceIdValue", () => { + test("getDeviceIdValue, should return GUID", () => { + // Arrange + setupCookies({}); - // Act - const result = getDeviceIdValue(); + // Act + const result = getDeviceIdValue(); - //Assert - expect(result).toBe("21b9b94a-ec23-42c1-aaac-e2ae4e4dbffe"); + //Assert + expect(result).toBe("21b9b94a-ec23-42c1-aaac-e2ae4e4dbffe"); + }); + + test("Should return 0s if unset", () => { + //Arrange + //Act + const result = getDeviceIdValue(); + + //Assert + expect(result).toBe("00000000-0000-0000-0000-000000000000"); + }); + + test("Should return id even if cookie contains other data", () => { + //Arrange + setCookieProperties({ + [cookieNames.DXDEV]: "did=f4a1a9e8-b3f3-4936-8c30-2f06a98644af&tz=-300&tzd=1", + }, {}); + + //Act + const result = getDeviceIdValue(); + + //Assert + expect(result).toBe("f4a1a9e8-b3f3-4936-8c30-2f06a98644af"); + }); }); - test("getSessionKeyValue, should return session key int", () => { - // Arrange - setupCookies({}); + describe("regenerateDeviceId", () => { + test("Generates a new id if unset", () => { + // Arrange + // Act + regenerateDeviceId(); + const result = getDeviceIdValue(); - // Act - const result = getSessionKeyValue(); + // Assert + expect(result).not.toBe("00000000-0000-0000-0000-000000000000"); + expect(global.crypto.randomUUID).toBeCalled(); + }); - //Assert - expect(result).toBe("12345"); + test("Does not create a new id if already set", () => { + // Arrange + setupCookies({}); + + // Act + regenerateDeviceId(); + const result = getDeviceIdValue(); + + // Assert + expect(result).toBe("21b9b94a-ec23-42c1-aaac-e2ae4e4dbffe"); + expect(global.crypto.randomUUID).not.toBeCalled(); + }); }); }); + test("getSessionKeyValue, should return session key int", () => { + // Arrange + setupCookies({}); + + // Act + const result = getSessionKeyValue(); + + //Assert + expect(result).toBe("12345"); + }); + describe("getSessionIdValue", () => { test("getSessionIdValue, should return GUID", () => { // Arrange diff --git a/src/helpers/unit-test-helper.js b/src/helpers/unit-test-helper.js index 123dc9d40..457dd47fb 100644 --- a/src/helpers/unit-test-helper.js +++ b/src/helpers/unit-test-helper.js @@ -139,6 +139,14 @@ export function setupCookies({ funnelCookieValue = "", includeHeritageCookie = t }); } +export function setupCrypto(mockValue) { + global.crypto = { + randomUUID: jest.fn() + }; + + global.crypto.randomUUID.mockImplementation(() => mockValue); +} + // Private methods function setupBaseMixinDispatchStoreAction(mockData) { if (mockData.actionList !== undefined) {