From 9b9001d67a36b9f00fc0302fe6b3f0394a448eed Mon Sep 17 00:00:00 2001 From: Chloe Herd Date: Tue, 5 Dec 2023 18:06:12 -0500 Subject: [PATCH] Tests for initsession --- src/mixins/analytics-mixin.spec.js | 133 ++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 3 deletions(-) diff --git a/src/mixins/analytics-mixin.spec.js b/src/mixins/analytics-mixin.spec.js index 7e04215af..567d9154e 100644 --- a/src/mixins/analytics-mixin.spec.js +++ b/src/mixins/analytics-mixin.spec.js @@ -1,5 +1,10 @@ import analyticsMixin from "@/mixins/analytics-mixin"; -import { setupMocksForJsFiles, setupCookies, setupCrypto } from "@/helpers/unit-test-helper.js"; +import { + setupMocksForJsFiles, + setupCookies, + setupCrypto, + removeAllTestCookies, +} from "@/helpers/unit-test-helper.js"; import { storeActions } from "@/constants/store-actions"; import { analyticsPageEvents, @@ -10,11 +15,17 @@ import { ValueToLogTypes, } from "@/constants/analytics"; import store from "@/store"; - +import { + getDeviceIdValue, + getSessionIdValue, + getSessionKeyValue, + getUserIdValue, +} from "@/helpers/heritage-integration/cookie-helper"; describe("analyticsMixin.js", () => { beforeEach(() => { - setupCrypto(); + removeAllTestCookies(); + setupCrypto("7e4727f3-9a3d-4c59-9cb3-6f4121b5ea94"); }); test("logPageView: calls dispatch with type and payload", async () => { @@ -341,4 +352,120 @@ describe("analyticsMixin.js", () => { //Assert expect(gaLabels).toEqual(GaLabels); }); + describe("initSession", () => { + test("Generates random values for userId and deviceId if not present", async () => { + // Arrange + const mockData = { + actionList: [ + { + actionName: storeActions.LOG_CUSTOM_EVENT, + }, + { + actionName: storeActions.INITIALIZE_SESSION, + data: { + sessionKey: "12345", + sessionId: "4f1eba4a-4dd5-4144-9a6b-1363c1e5e54f", + }, + }, + ], + }; + const mocks = setupMocksForJsFiles(mockData); + + // Act + await analyticsMixin.methods.initSession(); + const userId = getUserIdValue(); + const deviceId = getDeviceIdValue(); + + // Assert + expect(userId).not.toBe("00000000-0000-0000-0000-000000000000"); + expect(deviceId).not.toBe("00000000-0000-0000-0000-000000000000"); + }); + + test("Pulls sessionId and sessionKey from api if not set", async () => { + // Arrange + const mockData = { + actionList: [ + { + actionName: storeActions.LOG_CUSTOM_EVENT, + }, + { + actionName: storeActions.INITIALIZE_SESSION, + data: { + sessionKey: "54321", + sessionId: "4f1eba4a-4dd5-4144-9a6b-1363c1e5e54f", + }, + }, + ], + }; + const mocks = setupMocksForJsFiles(mockData); + + // Act + await analyticsMixin.methods.initSession(); + const sessionId = getSessionIdValue(); + const sessionKey = getSessionKeyValue(); + + // Assert + expect(sessionId).toBe("4f1eba4a-4dd5-4144-9a6b-1363c1e5e54f"); + expect(sessionKey).toBe("54321"); + }); + + test("Does not overwrite values that are already set", async () => { + // Arrange + const mockData = { + actionList: [ + { + actionName: storeActions.LOG_CUSTOM_EVENT, + }, + { + actionName: storeActions.INITIALIZE_SESSION, + data: { + sessionKey: "54321", + sessionId: "4f1eba4a-4dd5-4144-9a6b-1363c1e5e54f", + }, + }, + ], + }; + const mocks = setupMocksForJsFiles(mockData); + setupCookies({}); + + // Act + await analyticsMixin.methods.initSession(); + + const userId = getUserIdValue(); + const deviceId = getDeviceIdValue(); + const sessionId = getSessionIdValue(); + const sessionKey = getSessionKeyValue(); + + // Assert + expect(userId).toBe("11aec5e8-92ba-4dc9-a8b6-179a916d8d7a"); + expect(deviceId).toBe("21b9b94a-ec23-42c1-aaac-e2ae4e4dbffe"); + expect(sessionId).toBe("cba0c3d1-3c1b-4305-bb56-31aa50f58e27"); + expect(sessionKey).toBe("12345"); + }); + + test("Calls dispatchStoreAction", async () => { + // Arrange + const mockData = { + actionList: [ + { + actionName: storeActions.LOG_CUSTOM_EVENT, + }, + { + actionName: storeActions.INITIALIZE_SESSION, + data: { + sessionKey: "12345", + sessionId: "4f1eba4a-4dd5-4144-9a6b-1363c1e5e54f", + }, + }, + ], + }; + const mocks = setupMocksForJsFiles(mockData); + + // Act + await analyticsMixin.methods.initSession(); + + // Assert + expect(mocks.baseMixin.methods.dispatchStoreAction).toBeCalled(); + }); + }); });