|
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | +import { waitForCondition } from '../../helpers/playwright-utils' |
| 3 | +import { IndexPage } from './index-page' |
| 4 | + |
| 5 | +const basicEdgeFn = ` |
| 6 | + // this is a process signal function |
| 7 | + const processSignal = (signal) => { |
| 8 | + if (signal.type === 'interaction') { |
| 9 | + analytics.track('hello', { myAnonId: signal.anonymousId, myTimestamp: signal.timestamp }) |
| 10 | + } |
| 11 | + } |
| 12 | +` |
| 13 | + |
| 14 | +let indexPage: IndexPage |
| 15 | + |
| 16 | +const isoDateRegEx = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/ |
| 17 | + |
| 18 | +test.beforeEach(async ({ page }) => { |
| 19 | + indexPage = await new IndexPage().loadAndWait(page, basicEdgeFn) |
| 20 | +}) |
| 21 | + |
| 22 | +test('Signals should have anonymousId and timestamp at top level', async () => { |
| 23 | + await indexPage.network.mockTestRoute() |
| 24 | + await indexPage.network.makeFetchCall() |
| 25 | + await Promise.all([ |
| 26 | + indexPage.clickButton(), |
| 27 | + indexPage.makeAnalyticsPageCall(), |
| 28 | + indexPage.waitForSignalsApiFlush(), |
| 29 | + indexPage.waitForTrackingApiFlush(), |
| 30 | + ]) |
| 31 | + |
| 32 | + const types = [ |
| 33 | + 'network', |
| 34 | + 'interaction', |
| 35 | + 'instrumentation', |
| 36 | + 'navigation', |
| 37 | + ] as const |
| 38 | + |
| 39 | + const evs = types.map((type) => ({ |
| 40 | + type, |
| 41 | + networkCalls: indexPage.signalsAPI.getEvents(type), |
| 42 | + })) |
| 43 | + |
| 44 | + evs.forEach((events) => { |
| 45 | + if (!events.networkCalls.length) { |
| 46 | + throw new Error(`No events found for type ${events.type}`) |
| 47 | + } |
| 48 | + events.networkCalls.forEach((event) => { |
| 49 | + const expected = { |
| 50 | + anonymousId: event.anonymousId, |
| 51 | + timestamp: expect.stringMatching(isoDateRegEx), |
| 52 | + type: event.properties!.type, |
| 53 | + } |
| 54 | + expect(event.properties).toMatchObject(expected) |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + const getCreatedEvent = () => |
| 59 | + indexPage.trackingAPI |
| 60 | + .getEvents() |
| 61 | + .find((el) => el.type === 'track' && el.event === 'hello') |
| 62 | + |
| 63 | + await waitForCondition(() => !!getCreatedEvent(), { |
| 64 | + errorMessage: 'No track events found, should have an event', |
| 65 | + }) |
| 66 | + const event = getCreatedEvent()! |
| 67 | + expect(event.properties).toEqual({ |
| 68 | + myAnonId: event.anonymousId, |
| 69 | + myTimestamp: expect.stringMatching(isoDateRegEx), |
| 70 | + }) |
| 71 | +}) |
0 commit comments