Skip to content

Commit

Permalink
Add OneTrust wrapper integration tests (#944)
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky authored Sep 6, 2023
1 parent ee855ba commit e1cb8f2
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 1,102 deletions.
2 changes: 0 additions & 2 deletions packages/consent/consent-tools-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
"@wdio/cli": "^8.10.6",
"@wdio/local-runner": "^8.10.6",
"@wdio/mocha-framework": "^8.10.6",
"@wdio/sauce-service": "^8.10.6",
"@wdio/spec-reporter": "^8.10.6",
"@wdio/static-server-service": "^8.10.6",
"@wdio/types": "8",
"expect": "^29.4.1",
"globby": "^11.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ export abstract class BasePage {
const baseURL = browser.options.baseUrl
assert(baseURL)
await waitUntilReady()

await browser.url(baseURL + '/' + this.page)
}

async clearStorage() {
await browser.deleteAllCookies()
await browser.execute(() => localStorage.clear())
}

/**
* Hard reload the page
*/
reload() {
return browser.execute(() => window.location.reload())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ class OneTrustPage extends BasePage {
clickAcceptButtonAndClosePopup() {
return $('#onetrust-accept-btn-handler').click()
}

/**
* Whenever a consent event is sent, increment a counter
*/
detectConsentChanged() {
const getConsentChangedCallCount = (): Promise<number> =>
browser.execute(() => window._segmentConsentCalls)

void browser.execute(() => {
window._segmentConsentCalls ??= 0
window.analytics.on('track', (name) => {
if (name.includes('Segment Consent')) {
window._segmentConsentCalls += 1
}
})
})
return { getConsentChangedCallCount }
}
}

export default new OneTrustPage()
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
import page from '../page-objects/onetrust'
import { expect } from 'expect'
import { Context } from '@segment/analytics-next'

declare global {
interface Window {
_segmentConsentCalls: number
}
}

afterEach(async () => {
await page.clearStorage()
})

it('should stamp each event', async () => {
await page.load()

const commands = [
`analytics.track("hello world")`,
`analytics.alias("foo", "bar")`,
Expand All @@ -13,15 +23,31 @@ it('should stamp each event', async () => {
`analytics.identify("bar", { bar: 123 })`,
]

const r = Promise.all<Context>(commands.map((cmd) => browser.execute(cmd)))
const eventsP = Promise.all<Context>(
commands.map((cmd) => browser.execute(cmd))
)

await page.clickAcceptButtonAndClosePopup()

const responses = await r

responses.forEach((ctx) => {
;(await eventsP).forEach((ctx) => {
expect(
Object.keys((ctx.event.context as any).consent.categoryPreferences).length
).toBeGreaterThan(0)
})
})

it('should send a consent changed event when user clicks accept on popup', async () => {
await page.load()

const { getConsentChangedCallCount } = await page.detectConsentChanged()

await browser.pause(1000)
await expect(getConsentChangedCallCount()).resolves.toBe(0)

// make a consent selection in the OneTrust popup
await page.clickAcceptButtonAndClosePopup()

// 1 consent changed event should now be sent
await browser.waitUntil(
async () => (await getConsentChangedCallCount()) === 1
)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { AnalyticsSnippet } from '@segment/analytics-next'

declare global {
interface Window {
analytics: AnalyticsSnippet
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"node",
"expect-webdriverio",
"@wdio/mocha-framework",
"@wdio/sauce-service"
"wdio-intercept-service"
]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import type { Options } from '@wdio/types'

export const config: Options.Testrunner = {
afterTest(test, _, { error, passed }) {
console.log(
[
`${passed ? '✅ Passed' : '❌ Failed'}! ${test.title}`,
test.file,
error ? error : '',
].join('\n')
)
},
services: ['intercept'],
// services: [ /* Using webpack-dev-server instead */
// [
Expand Down
2 changes: 1 addition & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"**/*.tsx",
":!**/__tests__/**"
],
"outputs": ["dist/**", ".next/**"]
"outputs": ["**/dist/**", ".next/**"]
},
"test": {
"dependsOn": ["^build"]
Expand Down
Loading

0 comments on commit e1cb8f2

Please sign in to comment.