|
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | +import { standaloneMock } from './helpers/standalone-mock' |
| 3 | +import { extractWriteKeyFromUrl } from './helpers/extract-writekey' |
| 4 | +import { CDNSettingsBuilder } from '@internal/test-helpers' |
| 5 | + |
| 6 | +test.describe('Segment with custom global key', () => { |
| 7 | + test.beforeEach(standaloneMock) |
| 8 | + test.beforeEach(async ({ context }) => { |
| 9 | + await context.route( |
| 10 | + 'https://cdn.segment.com/v1/projects/*/settings', |
| 11 | + (route, request) => { |
| 12 | + if (request.method().toLowerCase() !== 'get') { |
| 13 | + return route.continue() |
| 14 | + } |
| 15 | + |
| 16 | + const writeKey = extractWriteKeyFromUrl(request.url()) || 'writeKey' |
| 17 | + return route.fulfill({ |
| 18 | + status: 200, |
| 19 | + headers: { |
| 20 | + 'Content-Type': 'application/json', |
| 21 | + }, |
| 22 | + body: JSON.stringify(new CDNSettingsBuilder({ writeKey }).build()), |
| 23 | + }) |
| 24 | + } |
| 25 | + ) |
| 26 | + }) |
| 27 | + |
| 28 | + test('supports using a custom global key', async ({ page }) => { |
| 29 | + // Load analytics.js |
| 30 | + await page.goto('/standalone-custom-key.html') |
| 31 | + await page.evaluate(() => |
| 32 | + (window as any).segment_analytics.load('fake-key') |
| 33 | + ) |
| 34 | + |
| 35 | + const req = await page.waitForRequest('https://api.segment.io/v1/t') |
| 36 | + |
| 37 | + // confirm that any events triggered from within snippet have been sent |
| 38 | + expect(req.postDataJSON().event).toBe('track from snippet') |
| 39 | + |
| 40 | + const contextObj = await page.evaluate(() => |
| 41 | + (window as any).segment_analytics.track('track after load') |
| 42 | + ) |
| 43 | + |
| 44 | + // confirm that any events triggered after load return a regular context object |
| 45 | + expect(contextObj).toMatchObject( |
| 46 | + expect.objectContaining({ |
| 47 | + attempts: expect.anything(), |
| 48 | + event: expect.objectContaining({ event: 'track after load' }), |
| 49 | + stats: expect.objectContaining({ metrics: expect.anything() }), |
| 50 | + }) |
| 51 | + ) |
| 52 | + }) |
| 53 | +}) |
0 commit comments