|
1 | 1 | /** |
2 | | - * Tests targeting @segment/analytics-consent-tools |
| 2 | + * Playwright Tests targeting @segment/analytics-consent-tools |
| 3 | + * |
| 4 | + * This test verifies that: |
| 5 | + * - Tracking events do not go through before explicit consent. |
| 6 | + * - After giving consent, appropriate events are sent with the correct consent context. |
3 | 7 | */ |
4 | 8 |
|
5 | 9 | import { ConsentToolsVanillaOptIn } from '../page-objects/consent-tools-vanilla' |
6 | | -import { expect } from 'expect' |
| 10 | +import { test, expect } from '@playwright/test' |
7 | 11 |
|
8 | | -const page = new ConsentToolsVanillaOptIn() |
| 12 | +let pageObject: ConsentToolsVanillaOptIn |
9 | 13 |
|
10 | | -afterEach(async () => { |
11 | | - await page.cleanup() |
| 14 | +test.beforeEach(async ({ page }) => { |
| 15 | + // Initialize the test page object and load the test HTML fixture |
| 16 | + pageObject = new ConsentToolsVanillaOptIn(page) |
| 17 | + await pageObject.load() |
12 | 18 | }) |
13 | 19 |
|
14 | | -it('should send a track call after waiting for explicit consent', async () => { |
15 | | - await page.load() |
| 20 | +test.afterEach(async () => { |
| 21 | + // Cleanup local/session storage and reset intercepted requests after each test |
| 22 | + await pageObject.cleanup() |
| 23 | +}) |
16 | 24 |
|
17 | | - void browser.execute(() => { |
18 | | - return window.analytics.track('buffered') |
| 25 | +test('should send a track call after waiting for explicit consent', async ({ |
| 26 | + page, |
| 27 | +}) => { |
| 28 | + // Attempt to track an event before consent is given — this should be buffered/blocked |
| 29 | + await page.evaluate(() => { |
| 30 | + void window.analytics.track('buffered') |
19 | 31 | }) |
20 | 32 |
|
21 | | - try { |
22 | | - // we don't expect any tracking events to go through yet |
23 | | - await browser.waitUntil(() => page.getAllTrackingEvents().length, { |
24 | | - timeout: 7000, |
25 | | - }) |
26 | | - throw new Error('Expected no track calls to be made') |
27 | | - } catch (err) { |
28 | | - // expected to fail |
29 | | - } |
| 33 | + // Optional delay to let the network pipeline settle (useful in debugging) |
| 34 | + await page.waitForTimeout(2000) |
30 | 35 |
|
31 | | - await page.clickGiveConsent() |
| 36 | + // Assert that no events have been tracked yet |
| 37 | + await expect.poll(() => pageObject.getAllTrackingEvents().length).toBe(0) |
32 | 38 |
|
33 | | - await browser.waitUntil(() => page.getConsentChangedEvents().length, { |
34 | | - timeout: 20000, |
35 | | - timeoutMsg: 'Expected a consent change call to be made', |
36 | | - }) |
| 39 | + // Ensure analytics is loaded before interacting |
| 40 | + await page.waitForFunction(() => typeof window.analytics !== 'undefined') |
37 | 41 |
|
38 | | - const consentChangeEvents = page.getConsentChangedEvents() |
39 | | - expect(consentChangeEvents.length).toBe(1) |
40 | | - expect(consentChangeEvents[0].event).toEqual( |
41 | | - 'Segment Consent Preference Updated' |
42 | | - ) |
| 42 | + // Simulate user giving consent |
| 43 | + await pageObject.clickGiveConsent() |
43 | 44 |
|
44 | | - await browser.waitUntil(() => page.fetchIntegrationReqs.length, { |
45 | | - timeout: 20000, |
46 | | - timeoutMsg: 'Expected integrations/destinations to be fetched', |
47 | | - }) |
| 45 | + // Wait until the consent change event is tracked |
| 46 | + await expect |
| 47 | + .poll(() => pageObject.getConsentChangedEvents().length, { |
| 48 | + timeout: 10_000, |
| 49 | + }) |
| 50 | + .toBeGreaterThan(0) |
48 | 51 |
|
49 | | - await browser.execute(() => { |
50 | | - return window.analytics.track('hello') |
| 52 | + // Validate that the expected consent change event was emitted |
| 53 | + const events = pageObject.getConsentChangedEvents() |
| 54 | + expect(events.length).toBe(1) |
| 55 | + expect(events[0].event).toBe('Segment Consent Preference Updated') |
| 56 | + |
| 57 | + // Now track a new event "hello" after consent has been given |
| 58 | + await page.evaluate(() => { |
| 59 | + void window.analytics.track('hello') |
51 | 60 | }) |
52 | 61 |
|
53 | | - const getHelloTrackEvents = () => |
54 | | - page.getAllTrackingEvents().find((el) => el.event === 'hello') |
| 62 | + // Helper to retrieve the "hello" tracking event |
| 63 | + const getHelloEvent = () => |
| 64 | + pageObject.getAllTrackingEvents().find((e) => e.event === 'hello') |
55 | 65 |
|
56 | | - await browser.waitUntil(() => getHelloTrackEvents(), { |
57 | | - timeout: 20000, |
58 | | - timeoutMsg: 'Expected a hello track call to be made', |
59 | | - }) |
| 66 | + // Wait until "hello" event is tracked |
| 67 | + await expect.poll(getHelloEvent).not.toBe(undefined) |
60 | 68 |
|
61 | | - expect(getHelloTrackEvents()!.context?.consent).toEqual({ |
| 69 | + // Validate that the "hello" event includes the correct consent context |
| 70 | + expect((await getHelloEvent())?.context?.consent).toEqual({ |
62 | 71 | categoryPreferences: { |
63 | 72 | FooCategory1: true, |
64 | 73 | FooCategory2: true, |
65 | 74 | }, |
66 | 75 | }) |
67 | 76 |
|
68 | | - const getBufferTrackEvents = () => |
69 | | - page.getAllTrackingEvents().find((el) => el.event === 'buffered') |
| 77 | + // Now re-check the previously buffered event "buffered" |
| 78 | + const getBufferedEvent = () => |
| 79 | + pageObject.getAllTrackingEvents().find((e) => e.event === 'buffered') |
70 | 80 |
|
71 | | - await browser.waitUntil(() => getBufferTrackEvents(), { |
72 | | - timeout: 20000, |
73 | | - timeoutMsg: 'Expected a "BUFFER" track call to be made', |
74 | | - }) |
| 81 | + // Wait until the "buffered" event is finally flushed and sent |
| 82 | + await expect.poll(getBufferedEvent).not.toBe(undefined) |
75 | 83 |
|
76 | | - expect(getBufferTrackEvents()!.context?.consent).toEqual({ |
| 84 | + // Assert that the flushed "buffered" event also includes the consent context |
| 85 | + expect((await getBufferedEvent())?.context?.consent).toEqual({ |
77 | 86 | categoryPreferences: { |
78 | 87 | FooCategory1: true, |
79 | 88 | FooCategory2: true, |
80 | 89 | }, |
81 | 90 | }) |
| 91 | + |
| 92 | + // Optional: print all tracking events for debug purposes |
| 93 | + //console.log('All tracking events so far:', pageObject.getAllTrackingEvents()) |
82 | 94 | }) |
0 commit comments