Skip to content

Commit 127f683

Browse files
Added Opt out test case
1 parent 5a31945 commit 127f683

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

packages/consent/consent-tools-integration-tests/public/consent-tools-vanilla-opt-out.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<h1>Hello World - Serving Analytics (Consent Tools Vanilla Opt Out)</h1>
66
<h2>Please Check Network tab</h2>
77
<p>This page can used as playground or run by webdriver.io</p>
8-
<script src="/public/dist/consent-tools-vanilla-opt-out.bundle.js"></script>
8+
<script src="dist/consent-tools-vanilla-opt-out.bundle.js"></script>
99
</body>
1010

1111
</html>

packages/consent/consent-tools-integration-tests/src/page-bundles/consent-tools-vanilla-opt-out/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const analytics = new AnalyticsBrowser()
1010

1111
withMockCMP(analytics).load(
1212
{
13-
writeKey: '9lSrez3BlfLAJ7NOChrqWtILiATiycoc',
13+
writeKey: 'foo',
1414
},
1515
{ initialPageview: true }
1616
)

packages/consent/consent-tools-integration-tests/src/tests/consent-tools-vanilla-opt-in.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test.afterEach(async () => {
2222
await pageObject.cleanup()
2323
})
2424

25-
test('should send a track call after waiting for explicit consent', async ({
25+
test('Consent Tools Vanilla Opt-in: Should send a track call after waiting for explicit consent', async ({
2626
page,
2727
}) => {
2828
// Attempt to track an event before consent is given — this should be buffered/blocked
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Playwright Tests targeting @segment/analytics-consent-tools
3+
*
4+
* This test verifies that:
5+
* - Tracking events are sent before consent (opt-out model).
6+
* - Consent change updates are reflected in later events.
7+
*/
8+
9+
import { ConsentToolsVanillaOptOut } from '../page-objects/consent-tools-vanilla'
10+
import { test, expect } from '@playwright/test'
11+
12+
let pageObject: ConsentToolsVanillaOptOut
13+
14+
test.beforeEach(async ({ page }) => {
15+
pageObject = new ConsentToolsVanillaOptOut(page)
16+
await pageObject.load()
17+
})
18+
19+
test.afterEach(async () => {
20+
await pageObject.cleanup()
21+
})
22+
23+
test('Consent Tools Vanilla Opt-out: Should not wait for consent before sending track call', async ({
24+
page,
25+
}) => {
26+
// Track an event immediately before any consent is given
27+
await page.evaluate(() => {
28+
void window.analytics.track('hello')
29+
})
30+
31+
// Consent change should not have happened yet
32+
let consentChangeEvents = pageObject.getConsentChangedEvents()
33+
expect(consentChangeEvents.length).toBe(0)
34+
35+
// Wait for the "hello" event to be sent
36+
const getHelloTrackEvent = () =>
37+
pageObject.getAllTrackingEvents().find((e) => e.event === 'hello')
38+
39+
await expect
40+
.poll(getHelloTrackEvent, {
41+
timeout: 30000,
42+
})
43+
.not.toBe(undefined)
44+
45+
expect(
46+
(await getHelloTrackEvent())?.context?.consent?.categoryPreferences
47+
).toEqual({
48+
FooCategory1: false,
49+
FooCategory2: false,
50+
})
51+
52+
// Simulate user giving consent
53+
await pageObject.clickGiveConsent()
54+
55+
// Wait for the consent update to be tracked
56+
await expect
57+
.poll(() => pageObject.getConsentChangedEvents().length, {
58+
timeout: 20000,
59+
})
60+
.toBeGreaterThan(0)
61+
62+
consentChangeEvents = pageObject.getConsentChangedEvents()
63+
expect(consentChangeEvents.length).toBe(1)
64+
expect(consentChangeEvents[0].event).toBe(
65+
'Segment Consent Preference Updated'
66+
)
67+
68+
// Track another event after giving consent
69+
await page.evaluate(() => {
70+
void window.analytics.track('sup')
71+
})
72+
73+
const getSupTrackEvent = () =>
74+
pageObject.getAllTrackingEvents().find((e) => e.event === 'sup')
75+
76+
await expect
77+
.poll(getSupTrackEvent, {
78+
timeout: 20000,
79+
})
80+
.not.toBe(undefined)
81+
82+
expect(
83+
(await getSupTrackEvent())?.context?.consent?.categoryPreferences
84+
).toEqual({
85+
FooCategory1: true,
86+
FooCategory2: true,
87+
})
88+
})

0 commit comments

Comments
 (0)