@springernature/global-javascript
Advanced tools
| import {checkConsent, isConsentBannerClosed} from "../../../src/helpers/util/onetrust"; | ||
| describe('checkConsent', () => { | ||
| const originalDocumentCookie = window.document.cookie; | ||
| beforeEach(() => { | ||
| Object.defineProperty(window.document, 'cookie', { | ||
| writable: true, | ||
| value: 'OptanonConsent=isIABGlobal=false&datestamp=Tue+Jul+14+2020+07%3A58%3A59+GMT%2B0100+(British+Summer+Time)&version=6.3.0&landingPath=NotLandingPage&groups=C0001%3A1%2CC0002%3A1%2CC0003%3A0%2CC0008%3A1%2CC0009%3A0&hosts=&legInt=&geolocation=%3B&AwaitingReconsent=false' | ||
| }); | ||
| }); | ||
| afterEach(() => window.document.cookie = originalDocumentCookie); | ||
| test('Should return boolean of each valid consent group set in the OptanonConsent cookie', () => { | ||
| expect(checkConsent('strictlyNecessary')).toBe(true); | ||
| expect(checkConsent('performance')).toBe(true); | ||
| expect(checkConsent('functional')).toBe(false); | ||
| expect(checkConsent('targetingFirstParty')).toBe(true); | ||
| expect(checkConsent('targetingThirdParty')).toBe(false); | ||
| }); | ||
| test('Should throw error if invalid group name passed in', () => { | ||
| expect(() => { | ||
| checkConsent('invalidGroupName'); | ||
| }).toThrowError(); | ||
| }); | ||
| }); | ||
| describe('isConsentBannerClosed', () => { | ||
| const originalDocumentCookie = window.document.cookie; | ||
| afterEach(() => window.document.cookie = originalDocumentCookie); | ||
| test('Should return true if OptanonAlertBoxClosed cookie is set', () => { | ||
| Object.defineProperty(window.document, 'cookie', { | ||
| writable: true, | ||
| value: 'OptanonAlertBoxClosed=2020-07-13T21:33:32.497Z' | ||
| }); | ||
| expect(isConsentBannerClosed()).toBe(true); | ||
| }); | ||
| test('Should return false if OptanonAlertBoxClosed cookie is not set', () => { | ||
| expect(isConsentBannerClosed()).toBe(false); | ||
| }); | ||
| }); |
| import {getCookie} from './get-cookie'; | ||
| /** | ||
| * onetrust.js | ||
| * @param {string} category - name of OneTrust category | ||
| * @return {boolean} | ||
| */ | ||
| const checkConsent = category => { | ||
| const validCategories = { | ||
| strictlyNecessary: 'C0001', | ||
| performance: 'C0002', | ||
| functional: 'C0003', | ||
| targetingFirstParty: 'C0008', | ||
| targetingThirdParty: 'C0009' | ||
| }; | ||
| if (!validCategories[category]) { | ||
| throw new Error('Invalid category: ' + category); | ||
| } | ||
| const consent = getCookie('OptanonConsent'); | ||
| const consentGroups = consent.split('groups=').pop().split('&')[0]; | ||
| const colonEncoding = '%3A'; | ||
| return consentGroups.includes(validCategories[category] + colonEncoding + '1'); | ||
| }; | ||
| /** | ||
| * isConsentBannerClosed | ||
| * @return {boolean} | ||
| */ | ||
| const isConsentBannerClosed = () => Boolean(getCookie('OptanonAlertBoxClosed')); | ||
| export {checkConsent, isConsentBannerClosed}; |
+4
-0
| # History | ||
| ## 2.3.0 (2020-07-14) | ||
| * FEATURE: checkConsent util | ||
| * FEATURE: isConsentBannerClosed util | ||
| ## 2.2.0 (2020-07-10) | ||
@@ -4,0 +8,0 @@ * FEATURE: getCookie util |
+1
-1
| { | ||
| "name": "@springernature/global-javascript", | ||
| "version": "2.2.0", | ||
| "version": "2.3.0", | ||
| "license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Globally shared Javascript helpers", |
+36
-3
@@ -21,8 +21,9 @@ # Global Javascript | ||
| - [getCookie](#getcookie) | ||
| - [getCookie](#debounce) | ||
| - [getCookie](#throttle) | ||
| - [debounce](#debounce) | ||
| - [throttle](#throttle) | ||
| - [onetrust](#onetrust) | ||
| **Dom** | ||
| - [getDataOptions](#getDataOptions) | ||
| - [getDataOptions](#getdataoptions) | ||
@@ -107,2 +108,34 @@ ### Util | ||
| #### onetrust | ||
| OneTrust is the cookie management tool we use in order to aid GDPR compliance. | ||
| This helper exports two named functions, `checkConsent` and `isConsentBannerClosed`. | ||
| ##### checkConsent | ||
| Takes a OneTrust category string and returns a boolean representing whether the category has been consent to (retrieved from the `OptanonConsent` cookie). | ||
| Valid categories are: | ||
| - "strictlyNecessary" | ||
| - "performance" | ||
| - "functional" | ||
| - "targetingFirstParty" | ||
| - "targetingThirdParty" | ||
| ```javascript | ||
| checkConsent('targetingThirdParty'); | ||
| ``` | ||
| An error will be thrown if an invalid category is passed in. | ||
| ##### isConsentBannerClosed | ||
| Returns a boolean representing whether the cookie consent banner has been closed (retrieved from the `OptanonAlertBoxClosed` cookie). | ||
| ```javascript | ||
| isConsentBannerClosed(); | ||
| ``` | ||
| ### Dom | ||
@@ -109,0 +142,0 @@ Dom helpers are used to help achieve JavaScript tasks that involve getting information from, or manipulating the DOM. |
@@ -7,2 +7,3 @@ // Util | ||
| import {getCookie} from './util/get-cookie'; | ||
| import {checkConsent, isConsentBannerClosed} from './util/onetrust'; | ||
@@ -12,2 +13,2 @@ // Dom | ||
| export {makeArray, createEvent, debounce, throttle, getCookie, getDataOptions}; | ||
| export {makeArray, createEvent, debounce, throttle, getCookie, getDataOptions, checkConsent, isConsentBannerClosed}; |
20251
21.74%18
12.5%430
18.46%179
22.6%