Skip to content

Commit

Permalink
feat: ✅ skip queue for zaraz.consent methods, increase tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davevanhoorn committed Apr 18, 2024
1 parent ada8793 commit 4be784d
Show file tree
Hide file tree
Showing 24 changed files with 251 additions and 35 deletions.
12 changes: 11 additions & 1 deletion src/consent/get-all-checkboxes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@ declare global {

let windowObj: Window & typeof globalThis;

const warnSpy = jest.spyOn(console, 'warn');

beforeAll(() => {
windowObj = window;
warnSpy.mockImplementation();
});

afterAll(() => {
window = windowObj;
warnSpy.mockRestore();
});

describe('getAllCheckboxes()', () => {
it('should return all checkboxes from zaraz consent', () => {
it("should return an empty object when the Zaraz consent API hasn't been initialised", () => {
const checkboxes = getAllCheckboxes();
expect(checkboxes).toEqual({});
});

it('should return an object of checkboxes when the Zaraz consent API has been initialised', () => {
const checkboxesMock = { key1: true, key2: false };

window.zaraz = {
consent: {
getAllCheckboxes: jest.fn().mockReturnValue(checkboxesMock),
Expand Down
8 changes: 6 additions & 2 deletions src/consent/get-all-checkboxes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { getZaraz } from '../helpers/get-zaraz';

export type CheckboxStatuses = {
[key: string]: undefined | boolean; // The key is equal to the ID in Zaraz, e.g. "OVAL" or "DVEA".
};

/**
* Returns an object with the checkbox status of all purposes.
*/
export function getAllCheckboxes(): { [key: string]: boolean } {
return getZaraz().consent.getAllCheckboxes();
export function getAllCheckboxes(): CheckboxStatuses {
return getZaraz({ skipQueue: true })?.consent?.getAllCheckboxes() || {};
}
18 changes: 16 additions & 2 deletions src/consent/get-all.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,31 @@ declare global {

let windowObj: Window & typeof globalThis;

const warnSpy = jest.spyOn(console, 'warn');

beforeAll(() => {
windowObj = window;
warnSpy.mockImplementation();
});

afterAll(() => {
window = windowObj;
warnSpy.mockRestore();
});

describe('getAll()', () => {
it('should return all consents from zaraz consent', () => {
const consentsMock = { key1: true, key2: false };
it("should return an empty object when the Zaraz consent API hasn't been initialised", () => {
const consents = getAll();
expect(consents).toEqual({});
});

it('should return an object of consents when the Zaraz consent API has been initialised', () => {
const consentsMock = {
key1: true,
key2: false,
key3: undefined,
};

window.zaraz = {
consent: {
getAll: jest.fn().mockReturnValue(consentsMock),
Expand Down
19 changes: 16 additions & 3 deletions src/consent/get-all.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { getZaraz } from '../helpers/get-zaraz';

export type PurposeStatuses = {
[key: string]: undefined | boolean; // The key is equal to the ID in Zaraz, e.g. "OVAL" or "DVEA".
};

/**
* Returns an object with the consent status of all purposes.
* Returns an object with the status of all purposes.
*
* ```
* true: the consent has been given
* false: the consent has been denied
* undefined: the consent has never been given or seen
* ```
*
* When Zaraz hasn't been initalised, it returns an empty object.
*/
export function getAll(): { [key: string]: boolean } {
return getZaraz().consent.getAll();

export function getAll(): PurposeStatuses {
return getZaraz({ skipQueue: true })?.consent?.getAll() || {};
}
41 changes: 41 additions & 0 deletions src/consent/get-api-ready.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { getAPIReady } from './get-api-ready';

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zaraz: any;
}
}

let windowObj: Window & typeof globalThis;

const warnSpy = jest.spyOn(console, 'warn');

beforeAll(() => {
windowObj = window;
warnSpy.mockImplementation();
});

afterAll(() => {
window = windowObj;
warnSpy.mockRestore();
});

describe('getAPIReady()', () => {
it("should return false when the Zaraz consent API hasn't been initialised", () => {
const apiReady = getAPIReady();
expect(apiReady).toEqual(false);
});

it('should return true when the Zaraz consent API has been initialised', () => {
window.zaraz = {
consent: {
APIReady: true,
},
};

const apiReady = getAPIReady();

expect(apiReady).toEqual(true);
});
});
13 changes: 13 additions & 0 deletions src/consent/get-api-ready.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getZaraz } from '../helpers/get-zaraz';

/**
* Indicates whether the Consent API is currently available on the page.
*
* ```
* true: the Zaraz consent API is ready to be used with e.g. getAll(), getAllCheckboxes() etc.
* false: the Zaraz consent API is not ready to be used
* ```
*/
export function getAPIReady(): boolean {
return getZaraz({ skipQueue: true })?.consent?.APIReady || false;
}
28 changes: 23 additions & 5 deletions src/consent/get-purposes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPurposes } from './get-purposes';
import { getPurposes, Purposes } from './get-purposes';

declare global {
interface Window {
Expand All @@ -9,20 +9,38 @@ declare global {

let windowObj: Window & typeof globalThis;

const warnSpy = jest.spyOn(console, 'warn');

beforeAll(() => {
windowObj = window;
warnSpy.mockImplementation();
});

afterAll(() => {
window = windowObj;
warnSpy.mockRestore();
});

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 },
it("should return an empty object when the Zaraz consent API isn't initialised", () => {
const purposes = getPurposes();
expect(purposes).toEqual({});
});

it('should return an object of purposes from the Zaraz consent API when Zaraz is initialised', () => {
const purposesMock: Purposes = {
ORFL: {
name: { en: 'Name 1' },
description: { en: 'Description 1' },
order: 100,
},
OAVL: {
name: { en: 'Name 2' },
description: { en: 'Description 2' },
order: 200,
},
};

window.zaraz = {
consent: {
purposes: purposesMock,
Expand Down
18 changes: 13 additions & 5 deletions src/consent/get-purposes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { getZaraz } from '../helpers/get-zaraz';

type Purpose = {
name: string;
description: string;
export type Purpose = {
order: number;
name: {
[key: string]: string; // the key is the language set in Zaraz (e.g. "en")
};
description: {
[key: string]: string; // the key is the language set in Zaraz (e.g. "en")
};
};

export type Purposes = {
[key: string]: Purpose; // the key is also the ID visible in Zaraz (e.g. "OrGL" or "OAVL")
};

/**
* An object containing all configured purposes, with their ID, name, description, and order.
*/
export function getPurposes(): { [key: string]: Purpose } {
return getZaraz().consent.purposes;
export function getPurposes(): Purposes {
return getZaraz({ skipQueue: true })?.consent?.purposes || {};
}
12 changes: 11 additions & 1 deletion src/consent/get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@ declare global {

let windowObj: Window & typeof globalThis;

const warnSpy = jest.spyOn(console, 'warn');

beforeAll(() => {
windowObj = window;
warnSpy.mockImplementation();
});

afterAll(() => {
window = windowObj;
warnSpy.mockRestore();
});

describe('get()', () => {
it('should return the consent status for a purpose from zaraz consent', () => {
it("should return undefined when the Zaraz consent API hasn't been initialised", () => {
const consentStatus = get('key');
expect(consentStatus).toBe(undefined);
});

it('should return the consent status for a purpose when the Zaraz consent API has been initialised', () => {
const getMock = jest.fn().mockReturnValue(true);

window.zaraz = {
consent: {
get: getMock,
Expand Down
4 changes: 2 additions & 2 deletions src/consent/get.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getZaraz } from '../helpers/get-zaraz';

/**
* Get the current consent status for a purpose using the purpose ID.
* Get the consent status for a specific purpose using the purpose ID.
*
* ```
* true: The consent was granted.
Expand All @@ -10,5 +10,5 @@ import { getZaraz } from '../helpers/get-zaraz';
* ```
*/
export function get(purposeId: string): boolean | undefined {
return getZaraz().consent.get(purposeId);
return getZaraz({ skipQueue: true })?.consent?.get(purposeId) || undefined;
}
1 change: 1 addition & 0 deletions src/consent/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('consent', () => {
setAll: expect.any(Function),
getAllCheckboxes: expect.any(Function),
getPurposes: expect.any(Function),
getAPIReady: expect.any(Function),
setCheckboxes: expect.any(Function),
setAllCheckboxes: expect.any(Function),
sendQueuedEvents: expect.any(Function),
Expand Down
2 changes: 2 additions & 0 deletions src/consent/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { get } from './get';
import { getAll } from './get-all';
import { getAllCheckboxes } from './get-all-checkboxes';
import { getAPIReady } from './get-api-ready';
import { getPurposes } from './get-purposes';
import { sendQueuedEvents } from './send-queued-events';
import { set } from './set';
Expand All @@ -14,6 +15,7 @@ export const consent = {
getAll,
setAll,
getPurposes,
getAPIReady,
getAllCheckboxes,
setCheckboxes,
setAllCheckboxes,
Expand Down
13 changes: 12 additions & 1 deletion src/consent/send-queued-event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@ declare global {

let windowObj: Window & typeof globalThis;

const warnSpy = jest.spyOn(console, 'warn');

beforeAll(() => {
windowObj = window;
warnSpy.mockImplementation();
});

afterAll(() => {
window = windowObj;
warnSpy.mockRestore();
});

describe('sendQueuedEvents()', () => {
it('should call sendQueuedEvents method on zaraz consent', () => {
it("should not break when the Zaraz consent API hasn't been initialised", () => {
const sendQueuedEventsMock = jest.fn();
sendQueuedEvents();
expect(sendQueuedEventsMock).toHaveBeenCalledTimes(0);
});

it('should call sendQueuedEventsMock when the Zaraz consent API has been initialised', () => {
const sendQueuedEventsMock = jest.fn();

window.zaraz = {
consent: {
sendQueuedEvents: sendQueuedEventsMock,
Expand Down
2 changes: 1 addition & 1 deletion src/consent/send-queued-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ 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();
getZaraz({ skipQueue: true })?.consent?.sendQueuedEvents();
}
13 changes: 12 additions & 1 deletion src/consent/set-all-checkboxes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@ declare global {

let windowObj: Window & typeof globalThis;

const warnSpy = jest.spyOn(console, 'warn');

beforeAll(() => {
windowObj = window;
warnSpy.mockImplementation();
});

afterAll(() => {
window = windowObj;
warnSpy.mockRestore();
});

describe('setAllCheckboxes()', () => {
it('should call setAllCheckboxes method on zaraz consent with the correct argument', () => {
it("should not break when the Zaraz consent API hasn't been initialised", () => {
const setAllCheckboxesMock = jest.fn();
setAllCheckboxes(true);
expect(setAllCheckboxesMock).toHaveBeenCalledTimes(0);
});

it('should call setAllCheckboxes method on the Zaraz consent API with the correct argument', () => {
const setAllCheckboxesMock = jest.fn();

window.zaraz = {
consent: {
setAllCheckboxes: setAllCheckboxesMock,
Expand Down
2 changes: 1 addition & 1 deletion src/consent/set-all-checkboxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ 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);
getZaraz({ skipQueue: true })?.consent?.setAllCheckboxes(checkboxStatus);
}
Loading

0 comments on commit 4be784d

Please sign in to comment.