Tests for deviceID

This commit is contained in:
Chloe Herd 2023-12-05 10:49:05 -05:00
parent 78bdc5cb86
commit f45d73e297
2 changed files with 83 additions and 16 deletions

View file

@ -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

View file

@ -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) {