-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
18 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
106 changes: 97 additions & 9 deletions
106
...s/consent/consent-tools-integration-tests/src/page-bundles/consent-tools-vanilla/index.ts
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,29 +1,117 @@ | ||
import { AnalyticsBrowser } from '@segment/analytics-next' | ||
import { createWrapper } from '@segment/analytics-consent-tools' | ||
import { createWrapper, resolveWhen } from '@segment/analytics-consent-tools' | ||
|
||
const fakeCategories = { FooCategory1: true, FooCategory2: true } | ||
const constants = { | ||
giveConsentId: 'give-consent', | ||
denyConsentId: 'deny-consent', | ||
} | ||
/** | ||
* This is a mock consent manager that simulates a consent manager that is loaded asynchronously. | ||
* Similar to OneTrust, TrustArc, etc. | ||
*/ | ||
export const initMockConsentManager = (settings: { isOptIn: boolean }) => { | ||
const categories = {} | ||
let onConsentChange = (_categories: Record<string, boolean>) => | ||
undefined as void | ||
|
||
const createAlertBox = () => { | ||
const container = document.createElement('div') | ||
container.id = 'alert-box' | ||
container.innerHTML = ` | ||
<button id="${constants.giveConsentId}">Give consent</button> | ||
<button id="${constants.denyConsentId}">Deny consent</button> | ||
` | ||
return container | ||
} | ||
|
||
const alertBox = createAlertBox() | ||
let loaded = false | ||
setTimeout(() => { | ||
loaded = true | ||
mockCMPPublicAPI.openAlertBox() | ||
}, 300) | ||
|
||
/** | ||
* similar to window.OneTrust | ||
*/ | ||
const mockCMPPublicAPI = { | ||
get isLoaded() { | ||
return loaded | ||
}, | ||
get isOptIn() { | ||
return settings.isOptIn | ||
}, | ||
setCategories: (newCategories: Record<string, boolean>) => { | ||
const c = { ...categories, ...newCategories } | ||
onConsentChange(c) | ||
return c | ||
}, | ||
getCategories: () => ({ FooCategory1: true, FooCategory2: true }), | ||
openAlertBox: () => { | ||
document.body.appendChild(alertBox) | ||
document | ||
.getElementById(constants.giveConsentId)! | ||
.addEventListener('click', () => { | ||
mockCMPPublicAPI.setCategories({ | ||
FooCategory1: true, | ||
FooCategory2: true, | ||
}) | ||
mockCMPPublicAPI.closeAlertBox() | ||
}) | ||
document | ||
.getElementById(constants.denyConsentId)! | ||
.addEventListener('click', () => { | ||
mockCMPPublicAPI.setCategories({ | ||
FooCategory1: false, | ||
FooCategory2: false, | ||
}) | ||
mockCMPPublicAPI.closeAlertBox() | ||
}) | ||
}, | ||
closeAlertBox: () => { | ||
document.body.removeChild(alertBox) | ||
}, | ||
onConsentChange: (fn: (categories: Record<string, boolean>) => void) => { | ||
onConsentChange = fn | ||
}, | ||
} | ||
|
||
return mockCMPPublicAPI | ||
} | ||
|
||
const consentManager = initMockConsentManager({ isOptIn: true }) | ||
|
||
const withCMP = createWrapper({ | ||
getCategories: () => fakeCategories, | ||
shouldLoadWrapper: async () => { | ||
await resolveWhen(() => consentManager.isLoaded, 500) | ||
}, | ||
getCategories: () => consentManager.getCategories(), | ||
registerOnConsentChanged: (fn) => { | ||
consentManager.onConsentChange(fn) | ||
}, | ||
shouldLoadSegment: async (ctx) => { | ||
return new Promise((resolve, reject) => { | ||
if (!consentManager.isOptIn) { | ||
// if opt out, load immediately | ||
return ctx.load({ optIn: false }) | ||
} | ||
return new Promise((resolve) => { | ||
document.getElementById('give-consent')!.addEventListener('click', () => { | ||
ctx.load({ optIn: true }) | ||
ctx.load({ optIn: false }) | ||
resolve() | ||
}) | ||
|
||
document.getElementById('deny-consent')!.addEventListener('click', () => { | ||
ctx.abort() | ||
reject() | ||
resolve() | ||
}) | ||
}) | ||
}, | ||
}) | ||
|
||
const analytics = new AnalyticsBrowser() | ||
// for testing | ||
;(window as any).analytics = analytics | ||
|
||
withCMP(analytics).load({ | ||
writeKey: 'foo', | ||
}) | ||
|
||
// for testing | ||
;(window as any).analytics = analytics |
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