From 15c66924f009f299ddce072604ec173d4d26d286 Mon Sep 17 00:00:00 2001 From: Ural Date: Wed, 10 Apr 2024 10:58:04 +0200 Subject: [PATCH 01/10] feat(add-consent): add zaraz consent for cookies --- README.md | 10 +++++----- src/consent.spec.ts | 32 ++++++++++++++++++++++++++++++++ src/consent.ts | 10 ++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/consent.spec.ts create mode 100644 src/consent.ts diff --git a/README.md b/README.md index 4e69a50..edfed53 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ A type-safe wrapper around the Cloudflare Zaraz Web API. - Basic types for `zaraz.track()`; - Basic types for `zaraz.set()`; +- Basic types for `zaraz.consent()`; - Extensive types for `zaraz.ecommerce()`; - Cheks for `zaraz` being available in the window. @@ -17,13 +18,13 @@ Import zaraz and call the desired method. That's it! import { zaraz } from 'zaraz-ts'; // Track custom events on your website, that might happen in real time. -await zaraz.track("button clicked", { userId: "ABC-123", value: 200 }) +await zaraz.track('button clicked', { userId: 'ABC-123', value: 200 }); ``` ```ts import { zaraz } from 'zaraz-ts'; -// Make a variable available in all your events without manually setting it +// Make a variable available in all your events without manually setting it // every time you are using zaraz.track(). zaraz.set('user_id', '123456'); ``` @@ -31,8 +32,8 @@ zaraz.set('user_id', '123456'); ```ts import { zaraz } from 'zaraz-ts'; -// Track common events of the e-commerce user journey, such as when a user adds -// a product to cart, starts the checkout funnel or completes an order. +// Track common events of the e-commerce user journey, such as when a user adds +// a product to cart, starts the checkout funnel or completes an order. await zaraz.ecommerce('Order Completed', { checkout_id: '616727740', order_id: '817286897056801', @@ -72,7 +73,6 @@ Checkout the official Cloudflare docs for more details: https://developers.cloud This package is maintained and actively used by [Expatfile.tax][expatfile-site]. The #1 US expat tax e-filing software. 🇺🇸 - [build-url]: https://img.shields.io/github/checks-status/expatfile/zaraz-ts/main [cov-img]: https://codecov.io/gh/expatfile/zaraz-ts/branch/main/graph/badge.svg?token=mbGgsweFuP [cov-url]: https://codecov.io/gh/expatfile/zaraz-ts diff --git a/src/consent.spec.ts b/src/consent.spec.ts new file mode 100644 index 0000000..533bd6c --- /dev/null +++ b/src/consent.spec.ts @@ -0,0 +1,32 @@ +import { setConsent } from './consent'; + +const setConsentMock = jest.fn(); + +declare global { + interface Window { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + zaraz: any; + } +} + +let windowObj: Window & typeof globalThis; + +beforeAll(() => { + windowObj = window; +}); + +afterAll(() => { + window = windowObj; +}); + +describe('setConsent()', () => { + it('should call zaraz consent when called', () => { + window.zaraz = { + consent: setConsentMock, + }; + + setConsent('key', true); + + expect(setConsentMock).toHaveBeenCalledWith('key', true); + }); +}); diff --git a/src/consent.ts b/src/consent.ts new file mode 100644 index 0000000..27a6500 --- /dev/null +++ b/src/consent.ts @@ -0,0 +1,10 @@ +import { getZaraz } from './helpers/get-zaraz'; + +export function setConsent(key: string, value: boolean): void { + const zaraz = getZaraz(); + if (zaraz && zaraz.consent) { + zaraz.consent[key] = value; + } else { + console.error('Zaraz consent API is not available'); + } +} From fabd6ac05335dddae2965bc83bd9ad084293b219 Mon Sep 17 00:00:00 2001 From: Ural Date: Wed, 10 Apr 2024 11:04:26 +0200 Subject: [PATCH 02/10] feat(add-consent): fix test --- src/consent.spec.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/consent.spec.ts b/src/consent.spec.ts index 533bd6c..c9a9936 100644 --- a/src/consent.spec.ts +++ b/src/consent.spec.ts @@ -1,7 +1,5 @@ import { setConsent } from './consent'; -const setConsentMock = jest.fn(); - declare global { interface Window { // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -20,13 +18,14 @@ afterAll(() => { }); describe('setConsent()', () => { - it('should call zaraz consent when called', () => { + it('should set zaraz consent when called', () => { + const consentMock: { [key: string]: boolean } = {}; window.zaraz = { - consent: setConsentMock, + consent: consentMock, }; setConsent('key', true); - expect(setConsentMock).toHaveBeenCalledWith('key', true); + expect(consentMock.key).toBe(true); }); }); From 43d5aaf85ebaa1650732509444f8b7938e7509db Mon Sep 17 00:00:00 2001 From: Ural Date: Wed, 10 Apr 2024 11:10:40 +0200 Subject: [PATCH 03/10] feat(add-consent): remove error handling --- src/consent.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/consent.ts b/src/consent.ts index 27a6500..bdb6433 100644 --- a/src/consent.ts +++ b/src/consent.ts @@ -2,9 +2,5 @@ import { getZaraz } from './helpers/get-zaraz'; export function setConsent(key: string, value: boolean): void { const zaraz = getZaraz(); - if (zaraz && zaraz.consent) { - zaraz.consent[key] = value; - } else { - console.error('Zaraz consent API is not available'); - } + zaraz.consent[key] = value; } From 06d2c4c400df6abb3eac087a35f68b737fbaf49c Mon Sep 17 00:00:00 2001 From: Zino Hofmann Date: Wed, 10 Apr 2024 10:53:37 +0000 Subject: [PATCH 04/10] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20consent=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/consent.spec.ts | 31 ------------------------------- src/consent.ts | 6 ------ src/consent/get-all-checkboxes.ts | 8 ++++++++ src/consent/get-all.ts | 8 ++++++++ src/consent/get-purposes.ts | 14 ++++++++++++++ src/consent/get.ts | 14 ++++++++++++++ src/consent/index.ts | 19 +++++++++++++++++++ src/consent/send-queued-events.ts | 8 ++++++++ src/consent/set-all-checkboxes.ts | 8 ++++++++ src/consent/set-all.ts | 8 ++++++++ src/consent/set-checkboxes.ts | 10 ++++++++++ src/consent/set.ts | 8 ++++++++ src/index.ts | 3 +++ 13 files changed, 108 insertions(+), 37 deletions(-) delete mode 100644 src/consent.spec.ts delete mode 100644 src/consent.ts create mode 100644 src/consent/get-all-checkboxes.ts create mode 100644 src/consent/get-all.ts create mode 100644 src/consent/get-purposes.ts create mode 100644 src/consent/get.ts create mode 100644 src/consent/index.ts create mode 100644 src/consent/send-queued-events.ts create mode 100644 src/consent/set-all-checkboxes.ts create mode 100644 src/consent/set-all.ts create mode 100644 src/consent/set-checkboxes.ts create mode 100644 src/consent/set.ts diff --git a/src/consent.spec.ts b/src/consent.spec.ts deleted file mode 100644 index c9a9936..0000000 --- a/src/consent.spec.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { setConsent } from './consent'; - -declare global { - interface Window { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - zaraz: any; - } -} - -let windowObj: Window & typeof globalThis; - -beforeAll(() => { - windowObj = window; -}); - -afterAll(() => { - window = windowObj; -}); - -describe('setConsent()', () => { - it('should set zaraz consent when called', () => { - const consentMock: { [key: string]: boolean } = {}; - window.zaraz = { - consent: consentMock, - }; - - setConsent('key', true); - - expect(consentMock.key).toBe(true); - }); -}); diff --git a/src/consent.ts b/src/consent.ts deleted file mode 100644 index bdb6433..0000000 --- a/src/consent.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { getZaraz } from './helpers/get-zaraz'; - -export function setConsent(key: string, value: boolean): void { - const zaraz = getZaraz(); - zaraz.consent[key] = value; -} diff --git a/src/consent/get-all-checkboxes.ts b/src/consent/get-all-checkboxes.ts new file mode 100644 index 0000000..e7301c4 --- /dev/null +++ b/src/consent/get-all-checkboxes.ts @@ -0,0 +1,8 @@ +import { getZaraz } from '../helpers/get-zaraz'; + +/** + * Returns an object with the checkbox status of all purposes. + */ +export function getAllCheckboxes(): { [key: string]: boolean } { + return getZaraz().consent.getAllCheckboxes(); +} diff --git a/src/consent/get-all.ts b/src/consent/get-all.ts new file mode 100644 index 0000000..0324706 --- /dev/null +++ b/src/consent/get-all.ts @@ -0,0 +1,8 @@ +import { getZaraz } from '../helpers/get-zaraz'; + +/** + * Returns an object with the consent status of all purposes. + */ +export function getAll(): { [key: string]: boolean } { + return getZaraz().consent.getAll(); +} diff --git a/src/consent/get-purposes.ts b/src/consent/get-purposes.ts new file mode 100644 index 0000000..382b3fa --- /dev/null +++ b/src/consent/get-purposes.ts @@ -0,0 +1,14 @@ +import { getZaraz } from '../helpers/get-zaraz'; + +type Purpose = { + name: string; + description: string; + order: number; +}; + +/** + * An object containing all configured purposes, with their ID, name, description, and order. + */ +export function getPurposes(): { [key: string]: Purpose } { + return getZaraz().consent.purposes; +} diff --git a/src/consent/get.ts b/src/consent/get.ts new file mode 100644 index 0000000..7f11ebc --- /dev/null +++ b/src/consent/get.ts @@ -0,0 +1,14 @@ +import { getZaraz } from '../helpers/get-zaraz'; + +/** + * Get the current consent status for a purpose using the purpose ID. + * + * ``` + * true: The consent was granted. + * false: The consent was not granted. + * undefined: The purpose does not exist. + * ``` + */ +export function get(purposeId: string): boolean | undefined { + return getZaraz().consent.get(purposeId); +} diff --git a/src/consent/index.ts b/src/consent/index.ts new file mode 100644 index 0000000..f3fff63 --- /dev/null +++ b/src/consent/index.ts @@ -0,0 +1,19 @@ +import { get } from './get'; +import { getAll } from './get-all'; +import { getAllCheckboxes } from './get-all-checkboxes'; +import { sendQueuedEvents } from './send-queued-events'; +import { set } from './set'; +import { setAll } from './set-all'; +import { setAllCheckboxes } from './set-all-checkboxes'; +import { setCheckboxes } from './set-checkboxes'; + +export const consent = { + get, + set, + getAll, + setAll, + getAllCheckboxes, + setCheckboxes, + setAllCheckboxes, + sendQueuedEvents, +}; diff --git a/src/consent/send-queued-events.ts b/src/consent/send-queued-events.ts new file mode 100644 index 0000000..70ef5d7 --- /dev/null +++ b/src/consent/send-queued-events.ts @@ -0,0 +1,8 @@ +import { getZaraz } from '../helpers/get-zaraz'; + +/** + * If some Pageview-based events were not sent due to a lack of consent, they can be sent using this method after consent was granted. + */ +export function sendQueuedEvents(): void { + getZaraz().consent.sendQueuedEvents(); +} diff --git a/src/consent/set-all-checkboxes.ts b/src/consent/set-all-checkboxes.ts new file mode 100644 index 0000000..d238d9e --- /dev/null +++ b/src/consent/set-all-checkboxes.ts @@ -0,0 +1,8 @@ +import { getZaraz } from '../helpers/get-zaraz'; + +/** + * Set the checkboxStatus status for all purposes in the consent modal at once. + */ +export function setAllCheckboxes(checkboxStatus: boolean): void { + getZaraz().consent.setAllCheckboxes(checkboxStatus); +} diff --git a/src/consent/set-all.ts b/src/consent/set-all.ts new file mode 100644 index 0000000..54e5e2b --- /dev/null +++ b/src/consent/set-all.ts @@ -0,0 +1,8 @@ +import { getZaraz } from '../helpers/get-zaraz'; + +/** + * Set the consent status for all purposes at once. + */ +export function setAll(consentStatus: boolean): void { + getZaraz().consent.setAll(consentStatus); +} diff --git a/src/consent/set-checkboxes.ts b/src/consent/set-checkboxes.ts new file mode 100644 index 0000000..b78b485 --- /dev/null +++ b/src/consent/set-checkboxes.ts @@ -0,0 +1,10 @@ +import { getZaraz } from '../helpers/get-zaraz'; + +/** + * Set the consent status for some purposes using the purpose ID. + */ +export function setCheckboxes(checkboxesStatus: { + [key: string]: boolean; +}): void { + getZaraz().consent.setCheckboxes(checkboxesStatus); +} diff --git a/src/consent/set.ts b/src/consent/set.ts new file mode 100644 index 0000000..0476980 --- /dev/null +++ b/src/consent/set.ts @@ -0,0 +1,8 @@ +import { getZaraz } from '../helpers/get-zaraz'; + +/** + * Set the consent status for some purposes using the purpose ID. + */ +export function set(consentPreferences: { [key: string]: boolean }): void { + getZaraz().consent.set(consentPreferences); +} diff --git a/src/index.ts b/src/index.ts index 5575590..ce63cb2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ /* istanbul ignore file */ +import { consent } from './consent'; import { ecommerce } from './ecommerce'; import { set } from './set'; import { track } from './track'; @@ -15,9 +16,11 @@ export const zaraz = { track, set, ecommerce, + consent, }; // Export typing etc. +export * from './consent'; export * from './ecommerce'; export * from './set'; export * from './track'; From c5b80a19894c80ca80c660e39b112faaca645a37 Mon Sep 17 00:00:00 2001 From: Ural Date: Wed, 10 Apr 2024 16:13:00 +0200 Subject: [PATCH 05/10] feat(add-consent): add missing tests for consent --- src/consent/get-all-checkboxes.spec.ts | 33 +++++++++++++++++++++++ src/consent/get-all.spec.ts | 33 +++++++++++++++++++++++ src/consent/get-purposes.spec.ts | 36 +++++++++++++++++++++++++ src/consent/get.spec.ts | 34 +++++++++++++++++++++++ src/consent/send-queued-event.spec.ts | 33 +++++++++++++++++++++++ src/consent/set-all-checkboxes.spect.ts | 33 +++++++++++++++++++++++ src/consent/set-all.spec.ts | 33 +++++++++++++++++++++++ src/consent/set-checkboxes.spec.ts | 34 +++++++++++++++++++++++ src/consent/set.spec.ts | 34 +++++++++++++++++++++++ 9 files changed, 303 insertions(+) create mode 100644 src/consent/get-all-checkboxes.spec.ts create mode 100644 src/consent/get-all.spec.ts create mode 100644 src/consent/get-purposes.spec.ts create mode 100644 src/consent/get.spec.ts create mode 100644 src/consent/send-queued-event.spec.ts create mode 100644 src/consent/set-all-checkboxes.spect.ts create mode 100644 src/consent/set-all.spec.ts create mode 100644 src/consent/set-checkboxes.spec.ts create mode 100644 src/consent/set.spec.ts diff --git a/src/consent/get-all-checkboxes.spec.ts b/src/consent/get-all-checkboxes.spec.ts new file mode 100644 index 0000000..95616b0 --- /dev/null +++ b/src/consent/get-all-checkboxes.spec.ts @@ -0,0 +1,33 @@ +import { getAllCheckboxes } from './get-all-checkboxes'; + +declare global { + interface Window { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + zaraz: any; + } +} + +let windowObj: Window & typeof globalThis; + +beforeAll(() => { + windowObj = window; +}); + +afterAll(() => { + window = windowObj; +}); + +describe('getAllCheckboxes()', () => { + it('should return all checkboxes from zaraz consent', () => { + const checkboxesMock = { key1: true, key2: false }; + window.zaraz = { + consent: { + getAllCheckboxes: jest.fn().mockReturnValue(checkboxesMock), + }, + }; + + const checkboxes = getAllCheckboxes(); + + expect(checkboxes).toEqual(checkboxesMock); + }); +}); diff --git a/src/consent/get-all.spec.ts b/src/consent/get-all.spec.ts new file mode 100644 index 0000000..e808f99 --- /dev/null +++ b/src/consent/get-all.spec.ts @@ -0,0 +1,33 @@ +import { getAll } from './get-all'; + +declare global { + interface Window { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + zaraz: any; + } +} + +let windowObj: Window & typeof globalThis; + +beforeAll(() => { + windowObj = window; +}); + +afterAll(() => { + window = windowObj; +}); + +describe('getAll()', () => { + it('should return all consents from zaraz consent', () => { + const consentsMock = { key1: true, key2: false }; + window.zaraz = { + consent: { + getAll: jest.fn().mockReturnValue(consentsMock), + }, + }; + + const consents = getAll(); + + expect(consents).toEqual(consentsMock); + }); +}); diff --git a/src/consent/get-purposes.spec.ts b/src/consent/get-purposes.spec.ts new file mode 100644 index 0000000..63de7a4 --- /dev/null +++ b/src/consent/get-purposes.spec.ts @@ -0,0 +1,36 @@ +import { getPurposes } from './get-purposes'; + +declare global { + interface Window { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + zaraz: any; + } +} + +let windowObj: Window & typeof globalThis; + +beforeAll(() => { + windowObj = window; +}); + +afterAll(() => { + window = windowObj; +}); + +describe('getPurposes()', () => { + it('should return all purposes from zaraz consent', () => { + const purposesMock = { + key1: { name: 'Name 1', description: 'Description 1', order: 1 }, + key2: { name: 'Name 2', description: 'Description 2', order: 2 }, + }; + window.zaraz = { + consent: { + purposes: purposesMock, + }, + }; + + const purposes = getPurposes(); + + expect(purposes).toEqual(purposesMock); + }); +}); diff --git a/src/consent/get.spec.ts b/src/consent/get.spec.ts new file mode 100644 index 0000000..db1b3cc --- /dev/null +++ b/src/consent/get.spec.ts @@ -0,0 +1,34 @@ +import { get } from './get'; + +declare global { + interface Window { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + zaraz: any; + } +} + +let windowObj: Window & typeof globalThis; + +beforeAll(() => { + windowObj = window; +}); + +afterAll(() => { + window = windowObj; +}); + +describe('get()', () => { + it('should return the consent status for a purpose from zaraz consent', () => { + const getMock = jest.fn().mockReturnValue(true); + window.zaraz = { + consent: { + get: getMock, + }, + }; + + const consentStatus = get('key'); + + expect(consentStatus).toBe(true); + expect(getMock).toHaveBeenCalledWith('key'); + }); +}); diff --git a/src/consent/send-queued-event.spec.ts b/src/consent/send-queued-event.spec.ts new file mode 100644 index 0000000..12c6656 --- /dev/null +++ b/src/consent/send-queued-event.spec.ts @@ -0,0 +1,33 @@ +import { sendQueuedEvents } from './send-queued-events'; + +declare global { + interface Window { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + zaraz: any; + } +} + +let windowObj: Window & typeof globalThis; + +beforeAll(() => { + windowObj = window; +}); + +afterAll(() => { + window = windowObj; +}); + +describe('sendQueuedEvents()', () => { + it('should call sendQueuedEvents method on zaraz consent', () => { + const sendQueuedEventsMock = jest.fn(); + window.zaraz = { + consent: { + sendQueuedEvents: sendQueuedEventsMock, + }, + }; + + sendQueuedEvents(); + + expect(sendQueuedEventsMock).toHaveBeenCalled(); + }); +}); diff --git a/src/consent/set-all-checkboxes.spect.ts b/src/consent/set-all-checkboxes.spect.ts new file mode 100644 index 0000000..29c3acc --- /dev/null +++ b/src/consent/set-all-checkboxes.spect.ts @@ -0,0 +1,33 @@ +import { setAllCheckboxes } from './set-all-checkboxes'; + +declare global { + interface Window { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + zaraz: any; + } +} + +let windowObj: Window & typeof globalThis; + +beforeAll(() => { + windowObj = window; +}); + +afterAll(() => { + window = windowObj; +}); + +describe('setAllCheckboxes()', () => { + it('should call setAllCheckboxes method on zaraz consent with the correct argument', () => { + const setAllCheckboxesMock = jest.fn(); + window.zaraz = { + consent: { + setAllCheckboxes: setAllCheckboxesMock, + }, + }; + + setAllCheckboxes(true); + + expect(setAllCheckboxesMock).toHaveBeenCalledWith(true); + }); +}); diff --git a/src/consent/set-all.spec.ts b/src/consent/set-all.spec.ts new file mode 100644 index 0000000..35e086b --- /dev/null +++ b/src/consent/set-all.spec.ts @@ -0,0 +1,33 @@ +import { setAll } from './set-all'; + +declare global { + interface Window { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + zaraz: any; + } +} + +let windowObj: Window & typeof globalThis; + +beforeAll(() => { + windowObj = window; +}); + +afterAll(() => { + window = windowObj; +}); + +describe('setAll()', () => { + it('should call setAll method on zaraz consent with the correct argument', () => { + const setAllMock = jest.fn(); + window.zaraz = { + consent: { + setAll: setAllMock, + }, + }; + + setAll(true); + + expect(setAllMock).toHaveBeenCalledWith(true); + }); +}); diff --git a/src/consent/set-checkboxes.spec.ts b/src/consent/set-checkboxes.spec.ts new file mode 100644 index 0000000..f02f2ca --- /dev/null +++ b/src/consent/set-checkboxes.spec.ts @@ -0,0 +1,34 @@ +import { setCheckboxes } from './set-checkboxes'; + +declare global { + interface Window { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + zaraz: any; + } +} + +let windowObj: Window & typeof globalThis; + +beforeAll(() => { + windowObj = window; +}); + +afterAll(() => { + window = windowObj; +}); + +describe('setCheckboxes()', () => { + it('should call setCheckboxes method on zaraz consent with the correct argument', () => { + const setCheckboxesMock = jest.fn(); + window.zaraz = { + consent: { + setCheckboxes: setCheckboxesMock, + }, + }; + + const checkboxesStatus = { key1: true, key2: false }; + setCheckboxes(checkboxesStatus); + + expect(setCheckboxesMock).toHaveBeenCalledWith(checkboxesStatus); + }); +}); diff --git a/src/consent/set.spec.ts b/src/consent/set.spec.ts new file mode 100644 index 0000000..febc75f --- /dev/null +++ b/src/consent/set.spec.ts @@ -0,0 +1,34 @@ +import { set } from './set'; + +declare global { + interface Window { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + zaraz: any; + } +} + +let windowObj: Window & typeof globalThis; + +beforeAll(() => { + windowObj = window; +}); + +afterAll(() => { + window = windowObj; +}); + +describe('set()', () => { + it('should call set method on zaraz consent with the correct argument', () => { + const setMock = jest.fn(); + window.zaraz = { + consent: { + set: setMock, + }, + }; + + const consentPreferences = { key1: true, key2: false }; + set(consentPreferences); + + expect(setMock).toHaveBeenCalledWith(consentPreferences); + }); +}); From 29c7d455ed8bc5578fa2d168ad6ba8a3c961d07c Mon Sep 17 00:00:00 2001 From: Ural Date: Wed, 10 Apr 2024 16:23:10 +0200 Subject: [PATCH 06/10] feat(add-consent): check for set-all-checkboxes coverage error --- src/consent/set-all-checkboxes.spect.ts | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/consent/set-all-checkboxes.spect.ts b/src/consent/set-all-checkboxes.spect.ts index 29c3acc..9000850 100644 --- a/src/consent/set-all-checkboxes.spect.ts +++ b/src/consent/set-all-checkboxes.spect.ts @@ -1,30 +1,16 @@ +import { getZaraz } from '../helpers/get-zaraz'; import { setAllCheckboxes } from './set-all-checkboxes'; -declare global { - interface Window { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - zaraz: any; - } -} - -let windowObj: Window & typeof globalThis; - -beforeAll(() => { - windowObj = window; -}); - -afterAll(() => { - window = windowObj; -}); +jest.mock('../helpers/get-zaraz'); describe('setAllCheckboxes()', () => { it('should call setAllCheckboxes method on zaraz consent with the correct argument', () => { const setAllCheckboxesMock = jest.fn(); - window.zaraz = { + (getZaraz as jest.Mock).mockReturnValue({ consent: { setAllCheckboxes: setAllCheckboxesMock, }, - }; + }); setAllCheckboxes(true); From 2db9bf2e8ffc2c9a432207ff8c804a465d378407 Mon Sep 17 00:00:00 2001 From: Ural Date: Wed, 10 Apr 2024 16:34:29 +0200 Subject: [PATCH 07/10] feat(add-consent): fix typo --- ...es.spect.ts => set-all-checkboxes.spec.ts} | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) rename src/consent/{set-all-checkboxes.spect.ts => set-all-checkboxes.spec.ts} (58%) diff --git a/src/consent/set-all-checkboxes.spect.ts b/src/consent/set-all-checkboxes.spec.ts similarity index 58% rename from src/consent/set-all-checkboxes.spect.ts rename to src/consent/set-all-checkboxes.spec.ts index 9000850..29c3acc 100644 --- a/src/consent/set-all-checkboxes.spect.ts +++ b/src/consent/set-all-checkboxes.spec.ts @@ -1,16 +1,30 @@ -import { getZaraz } from '../helpers/get-zaraz'; import { setAllCheckboxes } from './set-all-checkboxes'; -jest.mock('../helpers/get-zaraz'); +declare global { + interface Window { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + zaraz: any; + } +} + +let windowObj: Window & typeof globalThis; + +beforeAll(() => { + windowObj = window; +}); + +afterAll(() => { + window = windowObj; +}); describe('setAllCheckboxes()', () => { it('should call setAllCheckboxes method on zaraz consent with the correct argument', () => { const setAllCheckboxesMock = jest.fn(); - (getZaraz as jest.Mock).mockReturnValue({ + window.zaraz = { consent: { setAllCheckboxes: setAllCheckboxesMock, }, - }); + }; setAllCheckboxes(true); From efc14ed9fefb156af1e79a6307953d1a31e74c0b Mon Sep 17 00:00:00 2001 From: Ural Date: Wed, 10 Apr 2024 16:40:40 +0200 Subject: [PATCH 08/10] feat(add-consent): test for index.ts --- src/consent/index.spec.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/consent/index.spec.ts diff --git a/src/consent/index.spec.ts b/src/consent/index.spec.ts new file mode 100644 index 0000000..9c7a386 --- /dev/null +++ b/src/consent/index.spec.ts @@ -0,0 +1,16 @@ +import * as consentModule from './index'; + +describe('consent', () => { + it('should have the expected methods', () => { + expect(consentModule.consent).toEqual({ + get: expect.any(Function), + set: expect.any(Function), + getAll: expect.any(Function), + setAll: expect.any(Function), + getAllCheckboxes: expect.any(Function), + setCheckboxes: expect.any(Function), + setAllCheckboxes: expect.any(Function), + sendQueuedEvents: expect.any(Function), + }); + }); +}); From 9b5ab98b3997ee1fad13eaf83e1ec7ef292422e5 Mon Sep 17 00:00:00 2001 From: Ural Date: Wed, 10 Apr 2024 17:42:55 +0200 Subject: [PATCH 09/10] feat(add-consent): expose getPurposes under consent --- src/consent/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/consent/index.ts b/src/consent/index.ts index f3fff63..65ff013 100644 --- a/src/consent/index.ts +++ b/src/consent/index.ts @@ -1,6 +1,7 @@ import { get } from './get'; import { getAll } from './get-all'; import { getAllCheckboxes } from './get-all-checkboxes'; +import { getPurposes } from './get-purposes'; import { sendQueuedEvents } from './send-queued-events'; import { set } from './set'; import { setAll } from './set-all'; @@ -12,6 +13,7 @@ export const consent = { set, getAll, setAll, + getPurposes, getAllCheckboxes, setCheckboxes, setAllCheckboxes, From 47bdfe03528068ab263a88ec7361673a7ddfd765 Mon Sep 17 00:00:00 2001 From: Ural Date: Wed, 10 Apr 2024 17:44:22 +0200 Subject: [PATCH 10/10] feat(add-consent): update index.spec --- src/consent/index.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/consent/index.spec.ts b/src/consent/index.spec.ts index 9c7a386..0e3271f 100644 --- a/src/consent/index.spec.ts +++ b/src/consent/index.spec.ts @@ -8,6 +8,7 @@ describe('consent', () => { getAll: expect.any(Function), setAll: expect.any(Function), getAllCheckboxes: expect.any(Function), + getPurposes: expect.any(Function), setCheckboxes: expect.any(Function), setAllCheckboxes: expect.any(Function), sendQueuedEvents: expect.any(Function),