-
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 8de4b54
Showing
17 changed files
with
158 additions
and
25 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,18 @@ | ||
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. | ||
* | ||
* - When the value is undefined, the consent hasn't been given yet | ||
* - When the value is true, the consent has been given | ||
* - When the value is false, the consent has been denied | ||
* | ||
* 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() || {}; | ||
} |
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,38 @@ | ||
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; | ||
|
||
beforeAll(() => { | ||
windowObj = window; | ||
}); | ||
|
||
afterAll(() => { | ||
window = windowObj; | ||
}); | ||
|
||
describe('getAPIReady()', () => { | ||
it('should return false when the Zaraz consent API is not ready', () => { | ||
const apiReady = getAPIReady(); | ||
|
||
expect(apiReady).toEqual(false); | ||
}); | ||
|
||
it('should return true when the Zaraz consent API is ready', () => { | ||
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,8 @@ | ||
import { getZaraz } from '../helpers/get-zaraz'; | ||
|
||
/** | ||
* Indicates whether the Consent API is currently available on the page. | ||
*/ | ||
export function getAPIReady(): boolean { | ||
return getZaraz({ skipQueue: true })?.consent?.APIReady || 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,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 || {}; | ||
} |
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
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'; | ||
|
||
export type ConsentPreferences = { [key: string]: boolean }; | ||
|
||
/** | ||
* Set the consent status for some purposes using the purpose ID. | ||
* Set the consent status for a specific purpose using the purpose ID. | ||
* | ||
* ``` | ||
* true: The consent was granted. | ||
* false: The consent was not granted. | ||
* ``` | ||
*/ | ||
export function set(consentPreferences: { [key: string]: boolean }): void { | ||
getZaraz().consent.set(consentPreferences); | ||
export function set(consentPreferences: ConsentPreferences): void { | ||
getZaraz({ skipQueue: true })?.consent?.set(consentPreferences); | ||
} |
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