Skip to content

Commit

Permalink
Refactor: Extract pushToDataLayer to standalone and tested module
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraM committed Oct 21, 2024
1 parent 9cad93a commit 63659a4
Show file tree
Hide file tree
Showing 6 changed files with 433 additions and 20 deletions.
1 change: 1 addition & 0 deletions config/jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"/node_modules/",
"/dist/"
],
"testEnvironment": "jsdom",
"modulePathIgnorePatterns": ["<rootDir>/dist"]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"eslint-plugin-prettier": "5.2.1",
"husky": "9.1.6",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"nodemon": "3.1.7",
"npm-run-all2": "6.2.3",
"postcss": "8.4.47",
Expand Down
14 changes: 1 addition & 13 deletions src/LmcCookieConsentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from './types';
import { CookieConsentCategory, DisplayMode, SecondaryButtonMode } from './constants';
import { assembleLanguagesConfig } from './languages/loader';
import { pushToDataLayer } from './dataLayer';

/* eslint-disable-next-line no-unused-vars, @typescript-eslint/no-empty-function */
const noopAcceptCallback: OnAcceptCallback = (cookieConsent) => {};
Expand Down Expand Up @@ -167,17 +168,4 @@ const LmcCookieConsentManager: CookieConsentManager = (serviceName, args) => {
return cookieConsent;
};

function pushToDataLayer(cookie: VanillaCookieConsent.Cookie<CookieConsentCategoryValues>) {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'CookieConsent-update',
'CookieConsent.necessary': cookie.level.includes(CookieConsentCategory.NECESSARY),
'CookieConsent.analytics': cookie.level.includes(CookieConsentCategory.ANALYTICS),
'CookieConsent.ad': cookie.level.includes(CookieConsentCategory.AD),
'CookieConsent.functionality': cookie.level.includes(CookieConsentCategory.FUNCTIONALITY),
'CookieConsent.personalization': cookie.level.includes(CookieConsentCategory.PERSONALIZATION),
'CookieConsent.revision': cookie.revision,
});
}

export { CookieConsentCategory, DisplayMode, SecondaryButtonMode, VanillaCookieConsent, LmcCookieConsentManager };
62 changes: 62 additions & 0 deletions src/__tests__/dataLayer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { pushToDataLayer } from '../dataLayer';
import { CookieConsentCategoryValues, VanillaCookieConsent } from '../types';
import { CookieConsentCategory } from '../constants';

describe('dataLayer', () => {
describe('pushToDataLayer', () => {
it('should pushToDataLayer', () => {
expect(true).toBe(true);
});

beforeEach(() => {
// Reset the dataLayer before each test
window.dataLayer = [];
});

it('should push the correct event to the dataLayer', () => {
const cookie: VanillaCookieConsent.Cookie<CookieConsentCategoryValues> = {
level: [CookieConsentCategory.NECESSARY, CookieConsentCategory.ANALYTICS],
data: { uid: '123', serviceName: 'foo-service' },
revision: 1,
rfc_cookie: true,
};

pushToDataLayer(cookie);

expect(window.dataLayer).toEqual([
{
event: 'CookieConsent-update',
'CookieConsent.necessary': true,
'CookieConsent.analytics': true,
'CookieConsent.ad': false,
'CookieConsent.functionality': false,
'CookieConsent.personalization': false,
'CookieConsent.revision': 1,
},
]);
});

it('should handle empty levels correctly', () => {
const cookie: VanillaCookieConsent.Cookie<CookieConsentCategoryValues> = {
level: [],
data: { uid: '123', serviceName: 'foo-service' },
revision: 333,
rfc_cookie: true,
};

pushToDataLayer(cookie);

expect(window.dataLayer).toEqual([
{
event: 'CookieConsent-update',
'CookieConsent.necessary': false,
'CookieConsent.analytics': false,
'CookieConsent.ad': false,
'CookieConsent.functionality': false,
'CookieConsent.personalization': false,
'CookieConsent.revision': 333,
},
]);
});
});
});
15 changes: 15 additions & 0 deletions src/dataLayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CookieConsentCategoryValues, VanillaCookieConsent } from './types';
import { CookieConsentCategory } from './constants';

export const pushToDataLayer = (cookie: VanillaCookieConsent.Cookie<CookieConsentCategoryValues>) => {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'CookieConsent-update',
'CookieConsent.necessary': cookie.level.includes(CookieConsentCategory.NECESSARY),
'CookieConsent.analytics': cookie.level.includes(CookieConsentCategory.ANALYTICS),
'CookieConsent.ad': cookie.level.includes(CookieConsentCategory.AD),
'CookieConsent.functionality': cookie.level.includes(CookieConsentCategory.FUNCTIONALITY),
'CookieConsent.personalization': cookie.level.includes(CookieConsentCategory.PERSONALIZATION),
'CookieConsent.revision': cookie.revision,
});
};
Loading

0 comments on commit 63659a4

Please sign in to comment.