From 83f9694839f79b173d105f045bea00bd0609b18b Mon Sep 17 00:00:00 2001 From: cy-moi Date: Fri, 20 Sep 2024 11:24:10 +0200 Subject: [PATCH 1/3] Add e2e test for init configuration --- test/e2e/scenario/rum/init.scenario.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/e2e/scenario/rum/init.scenario.ts b/test/e2e/scenario/rum/init.scenario.ts index 3d3a4e6d9e..8ca639e6e6 100644 --- a/test/e2e/scenario/rum/init.scenario.ts +++ b/test/e2e/scenario/rum/init.scenario.ts @@ -120,6 +120,17 @@ describe('API calls and events around init', () => { { name: 'after manual view', viewId: initialView.view.id, viewName: 'after manual view' } ) }) + + createTest('should be able to set view context') + .withRum({ enableExperimentalFeatures: ['view_specific_context'] }) + .withRumSlim() + .withRumInit((configuration) => { + window.DD_RUM!.init(configuration) + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + ;(window.DD_RUM as any).setViewContext({ foo: 'bar' }) + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + ;(window.DD_RUM as any).setViewContextProperty('foo', 'baz') + }) }) describe('beforeSend', () => { From 16f1a2ab924597c2bab8f2b13905a839fec8dce6 Mon Sep 17 00:00:00 2001 From: cy-moi Date: Fri, 20 Sep 2024 11:55:24 +0200 Subject: [PATCH 2/3] Check the view contexts are added correctly --- test/e2e/scenario/rum/init.scenario.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/e2e/scenario/rum/init.scenario.ts b/test/e2e/scenario/rum/init.scenario.ts index 8ca639e6e6..745e9f8364 100644 --- a/test/e2e/scenario/rum/init.scenario.ts +++ b/test/e2e/scenario/rum/init.scenario.ts @@ -129,7 +129,13 @@ describe('API calls and events around init', () => { // eslint-disable-next-line @typescript-eslint/no-unsafe-call ;(window.DD_RUM as any).setViewContext({ foo: 'bar' }) // eslint-disable-next-line @typescript-eslint/no-unsafe-call - ;(window.DD_RUM as any).setViewContextProperty('foo', 'baz') + ;(window.DD_RUM as any).setViewContextProperty('bar', 'foo') + }) + .run(async ({ intakeRegistry }) => { + await flushEvents() + + const initialView = intakeRegistry.rumViewEvents[0] + expect(initialView.context).toEqual(jasmine.objectContaining({ foo: 'bar', bar: 'foo' })) }) }) From 75786ac5f3e779a9d81acbc0addedb847dfa48e8 Mon Sep 17 00:00:00 2001 From: cy-moi Date: Mon, 23 Sep 2024 14:29:03 +0200 Subject: [PATCH 3/3] Add e2e test for view context population --- test/e2e/scenario/rum/init.scenario.ts | 53 +++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/test/e2e/scenario/rum/init.scenario.ts b/test/e2e/scenario/rum/init.scenario.ts index 745e9f8364..14e77cd3b9 100644 --- a/test/e2e/scenario/rum/init.scenario.ts +++ b/test/e2e/scenario/rum/init.scenario.ts @@ -1,3 +1,4 @@ +import type { Context } from '@datadog/browser-core' import type { IntakeRegistry } from '../../lib/framework' import { flushEvents, createTest } from '../../lib/framework' import { withBrowserLogs } from '../../lib/helpers/browser' @@ -130,12 +131,51 @@ describe('API calls and events around init', () => { ;(window.DD_RUM as any).setViewContext({ foo: 'bar' }) // eslint-disable-next-line @typescript-eslint/no-unsafe-call ;(window.DD_RUM as any).setViewContextProperty('bar', 'foo') + + // context should populate the context of the children events + window.DD_RUM!.addAction('custom action') + window.DD_RUM!.addError('custom error') + + // context should not populate the context of the next view + setTimeout(() => window.DD_RUM!.startView('manual view'), 10) + setTimeout(() => { + window.DD_RUM!.addAction('after manual view') + window.DD_RUM!.addError('after manual view') + }, 20) }) .run(async ({ intakeRegistry }) => { await flushEvents() const initialView = intakeRegistry.rumViewEvents[0] + const nextView = intakeRegistry.rumViewEvents[1] + expect(initialView.context).toEqual(jasmine.objectContaining({ foo: 'bar', bar: 'foo' })) + expect(nextView.context!.foo).toBeUndefined() + + expectToHaveActions( + intakeRegistry, + { + name: 'custom action', + viewId: initialView.view.id, + context: { foo: 'bar', bar: 'foo' }, + }, + { + name: 'after manual view', + viewId: nextView.view.id, + } + ) + expectToHaveErrors( + intakeRegistry, + { + message: 'Provided "custom error"', + viewId: initialView.view.id, + context: { foo: 'bar', bar: 'foo' }, + }, + { + message: 'Provided "after manual view"', + viewId: nextView.view.id, + } + ) }) }) @@ -182,19 +222,25 @@ describe('beforeSend', () => { }) }) -function expectToHaveErrors(events: IntakeRegistry, ...errors: Array<{ message: string; viewId: string }>) { +function expectToHaveErrors( + events: IntakeRegistry, + ...errors: Array<{ message: string; viewId: string; context?: Context }> +) { expect(events.rumErrorEvents.length).toBe(errors.length) for (let i = 0; i < errors.length; i++) { const registryError = events.rumErrorEvents[i] const expectedError = errors[i] expect(registryError.error.message).toBe(expectedError.message) expect(registryError.view.id).toBe(expectedError.viewId) + if (expectedError.context) { + expect(registryError.context).toEqual(jasmine.objectContaining(expectedError.context)) + } } } function expectToHaveActions( events: IntakeRegistry, - ...actions: Array<{ name: string; viewId: string; viewName?: string }> + ...actions: Array<{ name: string; viewId: string; viewName?: string; context?: Context }> ) { expect(events.rumActionEvents.length).toBe(actions.length) for (let i = 0; i < actions.length; i++) { @@ -205,5 +251,8 @@ function expectToHaveActions( if (i === 0 && expectedAction.viewName) { expect(registryAction.view.name).toBe(expectedAction.viewName) } + if (expectedAction.context) { + expect(registryAction.context).toEqual(jasmine.objectContaining(expectedAction.context)) + } } }