UserId + Formatting

This commit is contained in:
Chloe Herd 2023-12-05 11:35:51 -05:00
parent f45d73e297
commit aa59d955cb
2 changed files with 60 additions and 4 deletions

View file

@ -5,6 +5,8 @@ import {
getSessionIdValue,
setCookieProperties,
regenerateDeviceId,
getUserIdValue,
regenerateUserId,
} from "@/helpers/heritage-integration/cookie-helper.js";
import { cookieNames } from "@/constants/cookie-names";
import { removeAllTestCookies, setupCookies, setupCrypto } from "@/helpers/unit-test-helper";
@ -139,9 +141,13 @@ describe("cookies", () => {
test("Should return id even if cookie contains other data", () => {
//Arrange
setCookieProperties({
[cookieNames.DXDEV]: "did=f4a1a9e8-b3f3-4936-8c30-2f06a98644af&tz=-300&tzd=1",
}, {});
setCookieProperties(
{
[cookieNames.DXDEV]:
"did=f4a1a9e8-b3f3-4936-8c30-2f06a98644af&tz=-300&tzd=1",
},
{}
);
//Act
const result = getDeviceIdValue();
@ -178,6 +184,56 @@ describe("cookies", () => {
});
});
describe("User Id", () => {
describe("getUserIdValue", () => {
test("Should return GUID", () => {
// Arrange
setupCookies({});
// Act
const result = getUserIdValue();
//Assert
expect(result).toBe("11aec5e8-92ba-4dc9-a8b6-179a916d8d7a");
});
test("Should return 0s if unset", () => {
//Arrange
//Act
const result = getUserIdValue();
//Assert
expect(result).toBe("00000000-0000-0000-0000-000000000000");
});
});
describe("regenerateUserId", () => {
test("Generates a new id if unset", () => {
// Arrange
// Act
regenerateUserId();
const result = getUserIdValue();
// Assert
expect(result).not.toBe("00000000-0000-0000-0000-000000000000");
expect(global.crypto.randomUUID).toBeCalled();
});
test("Does not create a new id if already set", () => {
// Arrange
setupCookies({});
// Act
regenerateUserId();
const result = getUserIdValue();
// Assert
expect(result).toBe("11aec5e8-92ba-4dc9-a8b6-179a916d8d7a");
expect(global.crypto.randomUUID).not.toBeCalled();
});
});
});
test("getSessionKeyValue, should return session key int", () => {
// Arrange
setupCookies({});

View file

@ -141,7 +141,7 @@ export function setupCookies({ funnelCookieValue = "", includeHeritageCookie = t
export function setupCrypto(mockValue) {
global.crypto = {
randomUUID: jest.fn()
randomUUID: jest.fn(),
};
global.crypto.randomUUID.mockImplementation(() => mockValue);