Skip to content

Commit

Permalink
Add unit test coverage for utils; clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
mjac0bs committed Jan 23, 2025
1 parent f6790af commit d64b33d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
8 changes: 4 additions & 4 deletions packages/manager/src/hooks/usePendo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import React from 'react';
import { APP_ROOT, PENDO_API_KEY } from 'src/constants';
import { useAccount } from 'src/queries/account/account.js';
import { useProfile } from 'src/queries/profile/profile';

import { loadScript } from './useScript';
import {
ONE_TRUST_COOKIE_CATEGORIES,
checkOptanonConsent,
getCookie,
ONE_TRUST_COOKIE_CATEGORIES,
} from 'src/utilities/analytics/utils';

import { loadScript } from './useScript';

declare global {
interface Window {
pendo: any;
Expand Down Expand Up @@ -165,5 +165,5 @@ export const usePendo = () => {
});
});
}
}, [PENDO_URL, accountId, visitorId]);
}, [PENDO_URL, accountId, hasFunctionalCookieConsent, visitorId]);
};
74 changes: 74 additions & 0 deletions packages/manager/src/utilities/analytics/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,85 @@
import { generateTimeOfDay } from './customEventAnalytics';
import {
ONE_TRUST_COOKIE_CATEGORIES,
checkOptanonConsent,
getCookie,
getFormattedStringFromFormEventOptions,
waitForAdobeAnalyticsToBeLoaded,
} from './utils';

import type { FormEventOptions } from './types';

describe('getCookie', () => {
beforeAll(() => {
const cookies =
'mycookie=my-cookie-value; OptanonConsent=cookie-consent-here; mythirdcookie=my-third-cookie;';
vi.spyOn(document, 'cookie', 'get').mockReturnValue(cookies);
});

it('should return the value of a cookie from document.cookie given its name, given cookie in middle position', () => {
expect(getCookie('OptanonConsent')).toEqual('cookie-consent-here');
});

it('should return the value of a cookie from document.cookie given its name, given cookie in first position', () => {
expect(getCookie('mycookie')).toEqual('my-cookie-value');
});

it('should return the value of a cookie from document.cookie given its name, given cookie in last position', () => {
expect(getCookie('mythirdcookie')).toEqual('my-third-cookie');
});

it('should return undefined if the cookie does not exist in document.cookie', () => {
expect(getCookie('mysecondcookie')).toEqual(undefined);
});
});

describe('checkOptanonConsent', () => {
it('should return true if consent is enabled for the given Optanon cookie category', () => {
const mockFunctionalCookieConsentEnabled =
'somestuffhere&groups=C0001%3A1%2CC0002%3A1%2CC0003%3A1%2CC0004%3A1%2CC0005%3A1&intType=6';

expect(
checkOptanonConsent(
mockFunctionalCookieConsentEnabled,
ONE_TRUST_COOKIE_CATEGORIES['Functional Cookies']
)
).toEqual(true);
});

it('should return false if consent is disabled for the given Optanon cookie category', () => {
const mockFunctionalCookieConsentDisabled =
'somestuffhere&groups=C0001%3A1%2CC0002%3A1%2CC0003%3A0%2CC0004%3A1%2CC0005%3A1&intType=6';

expect(
checkOptanonConsent(
mockFunctionalCookieConsentDisabled,
ONE_TRUST_COOKIE_CATEGORIES['Functional Cookies']
)
).toEqual(false);
});

it('should return false if the consent category does not exist in the cookie', () => {
const mockNoFunctionalCookieCategory =
'somestuffhere&groups=C0001%3A1%2CC0002%3A1%2CC0004%3A1%2CC0005%3A1&intType=6';

expect(
checkOptanonConsent(
mockNoFunctionalCookieCategory,
ONE_TRUST_COOKIE_CATEGORIES['Functional Cookies']
)
).toEqual(false);
});

it('should return false if the cookie is undefined', () => {
expect(
checkOptanonConsent(
undefined,
ONE_TRUST_COOKIE_CATEGORIES['Functional Cookies']
)
).toEqual(false);
});
});

describe('generateTimeOfDay', () => {
it('should generate human-readable time of day', () => {
expect(generateTimeOfDay(0)).toBe('Early Morning');
Expand Down
5 changes: 3 additions & 2 deletions packages/manager/src/utilities/analytics/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ export const getCookie = (name: string): string | undefined => {
let selectedCookie: string | undefined = undefined;

cookies.forEach((cookie) => {
if (cookie.trim().startsWith(name + '=')) {
selectedCookie = cookie.substring(name.length + 1);
const trimmedCookie = cookie.trim(); // Remove whitespace so position in cookie string doesn't matter
if (trimmedCookie.startsWith(name + '=')) {
selectedCookie = trimmedCookie.substring(name.length + 1);
}
});
return selectedCookie;
Expand Down

0 comments on commit d64b33d

Please sign in to comment.