-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✅ skip queue for zaraz.consent methods, increase tests
- Loading branch information
1 parent
ada8793
commit 9ae9283
Showing
25 changed files
with
346 additions
and
35 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
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 |
---|---|---|
@@ -1,8 +1,21 @@ | ||
import { getZaraz } from '../helpers/get-zaraz'; | ||
import { showWarning } from '../helpers/show-warning'; | ||
|
||
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 { | ||
const zaraz = getZaraz({ skipQueue: true }); | ||
|
||
if (typeof zaraz?.consent?.getAllCheckboxes === 'function') { | ||
return zaraz.consent.getAllCheckboxes(); | ||
} | ||
|
||
showWarning(`getAllCheckBoxes()`); | ||
|
||
return {}; | ||
} |
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 |
---|---|---|
@@ -1,8 +1,30 @@ | ||
import { getZaraz } from '../helpers/get-zaraz'; | ||
import { showWarning } from '../helpers/show-warning'; | ||
|
||
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 { | ||
const zaraz = getZaraz({ skipQueue: true }); | ||
|
||
if (typeof zaraz?.consent?.getAll === 'function') { | ||
return zaraz.consent.getAll(); | ||
} | ||
|
||
showWarning(`getAll()`); | ||
|
||
return {}; | ||
} |
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,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); | ||
}); | ||
}); |
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,22 @@ | ||
import { getZaraz } from '../helpers/get-zaraz'; | ||
import { showWarning } from '../helpers/show-warning'; | ||
|
||
/** | ||
* 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 { | ||
const zaraz = getZaraz({ skipQueue: true }); | ||
|
||
if (typeof zaraz?.consent?.APIReady === 'boolean') { | ||
return zaraz.consent.APIReady; | ||
} | ||
|
||
showWarning(`getAPIReady()`); | ||
|
||
return false; | ||
} |
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 |
---|---|---|
@@ -1,14 +1,31 @@ | ||
import { getZaraz } from '../helpers/get-zaraz'; | ||
import { showWarning } from '../helpers/show-warning'; | ||
|
||
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 { | ||
const zaraz = getZaraz({ skipQueue: true }); | ||
|
||
if (typeof zaraz?.consent?.purposes === 'object') { | ||
return zaraz.consent.purposes; | ||
} | ||
|
||
showWarning(`getPurposes()`); | ||
|
||
return {}; | ||
} |
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
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 |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import { getZaraz } from '../helpers/get-zaraz'; | ||
import { showWarning } from '../helpers/show-warning'; | ||
|
||
/** | ||
* 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(); | ||
const zaraz = getZaraz({ skipQueue: true }); | ||
|
||
if (typeof zaraz?.consent?.sendQueuedEvents === 'function') { | ||
zaraz.consent.sendQueuedEvents(); | ||
} else { | ||
showWarning(`sendQueuedEvents()`); | ||
} | ||
} |
Oops, something went wrong.