From 51167c060021f3cffce9d44407c8a394b65d0dcd Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Tue, 29 Oct 2024 11:52:37 -0500 Subject: [PATCH] Add signals.find test (#1174) --- .../src/tests/signals-vanilla/index-page.ts | 22 ++++++++++------- .../signals-vanilla/signals-find.test.ts | 24 +++++++++++++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 packages/signals/signals-integration-tests/src/tests/signals-vanilla/signals-find.test.ts diff --git a/packages/signals/signals-integration-tests/src/tests/signals-vanilla/index-page.ts b/packages/signals/signals-integration-tests/src/tests/signals-vanilla/index-page.ts index 99b82ee39..40a4bbb8c 100644 --- a/packages/signals/signals-integration-tests/src/tests/signals-vanilla/index-page.ts +++ b/packages/signals/signals-integration-tests/src/tests/signals-vanilla/index-page.ts @@ -23,15 +23,19 @@ export class IndexPage extends BasePage { return promiseTimeout(p, 2000, 'analytics.on("track") did not resolve') } - addUserDefinedSignal() { - return this.page.evaluate(() => { - window.signalsPlugin.addSignal({ - type: 'userDefined', - data: { - foo: 'bar', - }, - }) - }) + addUserDefinedSignal(data?: Record) { + return this.page.evaluate( + (args) => { + window.signalsPlugin.addSignal({ + type: 'userDefined', + data: { + foo: 'bar', + ...args.data, + }, + }) + }, + { data } + ) } async clickButton() { diff --git a/packages/signals/signals-integration-tests/src/tests/signals-vanilla/signals-find.test.ts b/packages/signals/signals-integration-tests/src/tests/signals-vanilla/signals-find.test.ts new file mode 100644 index 000000000..5018db7f0 --- /dev/null +++ b/packages/signals/signals-integration-tests/src/tests/signals-vanilla/signals-find.test.ts @@ -0,0 +1,24 @@ +import { test, expect } from '@playwright/test' +import { IndexPage } from './index-page' + +const indexPage = new IndexPage() + +test('should find the most recent signal', async ({ page }) => { + const basicEdgeFn = `const processSignal = (signal) => { + if (signal.type === 'interaction' && signal.data.target.id === 'complex-button') { + const mostRecentSignal = signals.find(signal, 'userDefined') + if (mostRecentSignal.data.num === 2) { + analytics.track('correct signal found') + } + } +}` + + await indexPage.loadAndWait(page, basicEdgeFn) + const tapiFlush = indexPage.waitForTrackingApiFlush() + await indexPage.addUserDefinedSignal({ num: 1 }) + await indexPage.addUserDefinedSignal({ num: 2 }) + await indexPage.clickComplexButton() + await tapiFlush + const lastEvent = indexPage.trackingAPI.lastEvent() + expect(lastEvent.event).toEqual('correct signal found') +})