-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Extract pushToDataLayer to standalone and tested module
- Loading branch information
Showing
6 changed files
with
433 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,6 @@ | |
"/node_modules/", | ||
"/dist/" | ||
], | ||
"testEnvironment": "jsdom", | ||
"modulePathIgnorePatterns": ["<rootDir>/dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}; |
Oops, something went wrong.