@servicetitan/datadog-rum 40.1.0 → 40.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/naming-convention -- RUM event fields are snake_case */
1
2
  // These are real error messages that are used to verify service and version attribution.
2
3
 
3
4
  import { RumEvent, RumEventDomainContext } from '@datadog/browser-rum';
@@ -138,6 +139,193 @@ const mfeResourceXhrRequest = {
138
139
  },
139
140
  };
140
141
 
142
+ const renderErrorStack = `
143
+ TypeError: null is not an object (evaluating 'le.current.useState')
144
+ at <anonymous> @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:294096
145
+ at PortalProvider @ https://go.servicetitan.com/scripts/build/desktop/shared/servicetitan.6c43a628f29ef9f51511.bundle.js:2:1137664
146
+ at Nh @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:217660
147
+ at Po @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:273405
148
+ at Uk @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:269025
149
+ at Tk @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:268953
150
+ at Ik @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:268806
151
+ at Nk @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:265121
152
+ at Gk @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:264074
153
+ at J @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor.8d7a7328d8d1c168f888.bundle.js:2:122247
154
+ at R @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor.8d7a7328d8d1c168f888.bundle.js:2:122611
155
+ at run @ https://go.servicetitan.com/scripts/build/desktop/vendor.15b48c67111ee07c5b30.bundle.js:2:5670964
156
+ at runIfPresent @ https://go.servicetitan.com/scripts/build/desktop/vendor.15b48c67111ee07c5b30.bundle.js:2:5671192
157
+ at onGlobalMessage @ https://go.servicetitan.com/scripts/build/desktop/vendor.15b48c67111ee07c5b30.bundle.js:2:5671715
158
+ `;
159
+
160
+ const hostErrorRenderUnhandled = {
161
+ error: {
162
+ message: "null is not an object (evaluating 'le.current.useState')",
163
+ stack: renderErrorStack,
164
+ },
165
+ context: {},
166
+ };
167
+
168
+ const hostErrorRenderConsole = {
169
+ error: {
170
+ message: "TypeError: null is not an object (evaluating 'le.current.useState')",
171
+ stack: renderErrorStack,
172
+ },
173
+ context: {
174
+ handlingStack: `
175
+ HandlingStack: console error
176
+ at e @ https://edge.fullstory.com/s/fs.js:2:34743
177
+ at <anonymous> @ https://unpkg.servicetitan.com/@servicetitan/ti-assist@0.0.0-master.626f98a/dist/bundle/light/vendor.78723d74cfa02e68895a.bundle.js:2:200079
178
+ at Li @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:228973
179
+ at <anonymous> @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:229190
180
+ at sh @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:216201
181
+ at kk @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:261182
182
+ at ik @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:260585
183
+ at hk @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:260117
184
+ at Wk @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:270178
185
+ at Pk @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:269520
186
+ at Gk @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js:2:264384
187
+ at J @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor.8d7a7328d8d1c168f888.bundle.js:2:122247
188
+ at R @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor.8d7a7328d8d1c168f888.bundle.js:2:122611
189
+ at run @ https://go.servicetitan.com/scripts/build/desktop/vendor.15b48c67111ee07c5b30.bundle.js:2:5670964
190
+ at runIfPresent @ https://go.servicetitan.com/scripts/build/desktop/vendor.15b48c67111ee07c5b30.bundle.js:2:5671192
191
+ at onGlobalMessage @ https://go.servicetitan.com/scripts/build/desktop/vendor.15b48c67111ee07c5b30.bundle.js:2:5671715
192
+ `,
193
+ },
194
+ };
195
+
196
+ const mfeErrorAxiosError401 = {
197
+ error: {
198
+ message: 'AxiosError: Request failed with status code 401',
199
+ stack: `
200
+ AxiosError: Request failed with status code 401
201
+ at settle @ https://unpkg.servicetitan.com/@servicetitan/dispatch-pro-console@0.0.0-master.8e05d88/dist/bundle/light/common.5178678e6dfc4f632bdb.bundle.js:2:31361
202
+ at XMLHttpRequest.onloadend @ https://unpkg.servicetitan.com/@servicetitan/dispatch-pro-console@0.0.0-master.8e05d88/dist/bundle/light/common.5178678e6dfc4f632bdb.bundle.js:2:40154
203
+ at Axios.request @ https://unpkg.servicetitan.com/@servicetitan/dispatch-pro-console@0.0.0-master.8e05d88/dist/bundle/light/common.5178678e6dfc4f632bdb.bundle.js:2:55248
204
+ at async makeCodedException @ https://unpkg.servicetitan.com/@servicetitan/dispatch-pro-console@0.0.0-master.8e05d88/dist/bundle/light/servicetitan.f1fe089245e6be8be424.bundle.js:2:76067
205
+ at async Object.queryFn @ https://unpkg.servicetitan.com/@servicetitan/dispatch-pro-console@0.0.0-master.8e05d88/dist/bundle/light/main.64019af64e0ecf97d613.bundle.js:2:579977
206
+ `,
207
+ },
208
+ context: {
209
+ handlingStack: `
210
+ HandlingStack: console error
211
+ at console.e @ https://edge.fullstory.com/s/fs.js:2:34738
212
+ at pe.e.<computed> [as error] @ https://unpkg.servicetitan.com/@servicetitan/ti-assist@0.0.0-master.4f470d3/dist/bundle/light/vendor.61a280ed9f0a0810f96c.bundle.js:2:381984
213
+ at <anonymous> @ https://unpkg.servicetitan.com/@servicetitan/dispatch-pro-console@0.0.0-master.8e05d88/dist/bundle/light/servicetitan.f1fe089245e6be8be424.bundle.js:2:792340
214
+ at executeAction @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.8048a9efb0bf24bfbf33.bundle.js:2:89974
215
+ at setQueryState @ https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.8048a9efb0bf24bfbf33.bundle.js:2:89747
216
+ at <anonymous> @ https://unpkg.servicetitan.com/@servicetitan/dispatch-pro-console@0.0.0-master.8e05d88/dist/bundle/light/main.64019af64e0ecf97d613.bundle.js:2:1032854
217
+ at Set.forEach @ <anonymous>
218
+ at <anonymous> @ https://unpkg.servicetitan.com/@servicetitan/dispatch-pro-console@0.0.0-master.8e05d88/dist/bundle/light/main.64019af64e0ecf97d613.bundle.js:2:1032835
219
+ `,
220
+ },
221
+ };
222
+
223
+ const hostErrorStringNoStack = {
224
+ error: {
225
+ message: '[Post loadGuide request failed: Unable to find global css after delay]',
226
+ },
227
+ context: {
228
+ handlingStack: `
229
+ HandlingStack: console error
230
+ at console.e @ https://edge.fullstory.com/s/fs.js:2:34738
231
+ at pe.e.<computed> [as error] @ https://unpkg.servicetitan.com/@servicetitan/ti-assist@0.0.0-master.4f470d3/dist/bundle/light/vendor.61a280ed9f0a0810f96c.bundle.js:2:381984
232
+ at Q0 @ https://cdn.pendo.io/agent/static/0359ffe9-310c-40b2-6e51-9ad6adde51e0/pendo.js:10:400978
233
+ at write @ https://cdn.pendo.io/agent/static/0359ffe9-310c-40b2-6e51-9ad6adde51e0/pendo.js:10:449241
234
+ at Ke @ https://cdn.pendo.io/agent/static/0359ffe9-310c-40b2-6e51-9ad6adde51e0/pendo.js:10:19797
235
+ at s @ https://cdn.pendo.io/agent/static/0359ffe9-310c-40b2-6e51-9ad6adde51e0/pendo.js:10:19998
236
+ at dispatch @ https://cdn.pendo.io/agent/static/0359ffe9-310c-40b2-6e51-9ad6adde51e0/pendo.js:10:79967
237
+ at Ke @ https://cdn.pendo.io/agent/static/0359ffe9-310c-40b2-6e51-9ad6adde51e0/pendo.js:10:19797
238
+ `,
239
+ },
240
+ };
241
+
242
+ const mfeHandlingStack = `
243
+ Error:
244
+ at addAction @ https://unpkg.servicetitan.com/@servicetitan/task-hub@1.4.0/dist/bundle/light/main.2a1f0c9d8e7b6a5f4e3d.bundle.js:2:10432
245
+ at onClick @ https://unpkg.servicetitan.com/@servicetitan/task-hub@1.4.0/dist/bundle/light/main.2a1f0c9d8e7b6a5f4e3d.bundle.js:2:88120
246
+ `;
247
+
248
+ const mfeManualView = {
249
+ view: { name: '/task-hub/{id}' },
250
+ context: { handlingStack: mfeHandlingStack },
251
+ };
252
+
253
+ /*
254
+ * A long animation frame that ran a short host script and a longer MFE script. The MFE script
255
+ * is listed second, so `scripts[0]` would name the host.
256
+ */
257
+ const mfeLongAnimationFrame = {
258
+ long_task: {
259
+ entry_type: 'long-animation-frame' as const,
260
+ duration: 180,
261
+ scripts: [
262
+ {
263
+ duration: 12,
264
+ source_url:
265
+ 'https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js',
266
+ },
267
+ {
268
+ duration: 140,
269
+ source_url:
270
+ 'https://unpkg.servicetitan.com/@servicetitan/task-hub@1.4.0/dist/bundle/light/main.2a1f0c9d8e7b6a5f4e3d.bundle.js',
271
+ },
272
+ ],
273
+ },
274
+ };
275
+
276
+ const hostLongAnimationFrame = {
277
+ long_task: {
278
+ entry_type: 'long-animation-frame' as const,
279
+ duration: 120,
280
+ scripts: [
281
+ {
282
+ duration: 10,
283
+ source_url:
284
+ 'https://unpkg.servicetitan.com/@servicetitan/task-hub@1.4.0/dist/bundle/light/main.2a1f0c9d8e7b6a5f4e3d.bundle.js',
285
+ },
286
+ {
287
+ duration: 95,
288
+ source_url:
289
+ 'https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js',
290
+ },
291
+ ],
292
+ },
293
+ };
294
+
295
+ const mfeLongAnimationFrameWithoutDurations = {
296
+ long_task: {
297
+ entry_type: 'long-animation-frame' as const,
298
+ duration: 60,
299
+ scripts: [
300
+ {
301
+ source_url:
302
+ 'https://unpkg.servicetitan.com/@servicetitan/task-hub@1.4.0/dist/bundle/light/main.2a1f0c9d8e7b6a5f4e3d.bundle.js',
303
+ },
304
+ {
305
+ source_url:
306
+ 'https://go.servicetitan.com/scripts/build/desktop/shared/vendor-core.bb011922ab999eb4885e.bundle.js',
307
+ },
308
+ ],
309
+ },
310
+ };
311
+
312
+ const longAnimationFrameWithoutScripts = {
313
+ long_task: { entry_type: 'long-animation-frame' as const, duration: 60 },
314
+ };
315
+
316
+ const legacyLongTask = {
317
+ long_task: { entry_type: 'long-task' as const, duration: 80 },
318
+ };
319
+
320
+ const mfeActionCustom = {
321
+ context: {
322
+ handlingStack: `
323
+ Error:
324
+ at addAction @ https://unpkg.servicetitan.com/@servicetitan/global-search-bar@0.0.0-HEAD.c8fa2a6/dist/bundle/light/main.a7d8d964f5d244d802a4.bundle.js:2:582407
325
+ `,
326
+ },
327
+ };
328
+
141
329
  type DeepPartial<T> = T extends object
142
330
  ? {
143
331
  [P in keyof T]?: DeepPartial<T[P]>;
@@ -263,4 +451,88 @@ export const rumEvents: {
263
451
  service: 'eh-user-management-client',
264
452
  version: '10.0.9-master-rc-eb9d8e988.0',
265
453
  },
454
+ {
455
+ event: {
456
+ type: 'error',
457
+ error: hostErrorRenderUnhandled.error,
458
+ },
459
+ context: hostErrorRenderUnhandled.context,
460
+ title: 'host render error is unhandled',
461
+ service: undefined,
462
+ version: undefined,
463
+ },
464
+ {
465
+ event: {
466
+ type: 'error',
467
+ error: hostErrorRenderConsole.error,
468
+ },
469
+ context: hostErrorRenderConsole.context,
470
+ title: 'host render error is logged through the ti-assist console wrapper',
471
+ service: undefined,
472
+ version: undefined,
473
+ },
474
+ {
475
+ event: {
476
+ type: 'error',
477
+ error: mfeErrorAxiosError401.error,
478
+ },
479
+ context: mfeErrorAxiosError401.context,
480
+ title: 'MFE request failed with status code 401 logged through the ti-assist console wrapper',
481
+ service: 'dispatch-pro-console',
482
+ version: '0.0.0-master.8e05d88',
483
+ },
484
+ {
485
+ event: {
486
+ type: 'error',
487
+ error: hostErrorStringNoStack.error,
488
+ },
489
+ context: hostErrorStringNoStack.context,
490
+ title: 'host logs a string with no error stack through the ti-assist console wrapper',
491
+ service: 'ti-assist',
492
+ version: '0.0.0-master.4f470d3',
493
+ },
494
+ {
495
+ event: { type: 'action' },
496
+ context: mfeActionCustom.context,
497
+ title: 'MFE adds a custom action',
498
+ service: 'global-search-bar',
499
+ version: '0.0.0-HEAD.c8fa2a6',
500
+ },
501
+ {
502
+ event: { type: 'view', view: mfeManualView.view },
503
+ context: mfeManualView.context,
504
+ title: 'MFE starts a manual view',
505
+ service: 'task-hub',
506
+ version: '1.4.0',
507
+ },
508
+ {
509
+ event: { type: 'long_task', long_task: mfeLongAnimationFrame.long_task },
510
+ title: 'long animation frame whose longest script is the MFE',
511
+ service: 'task-hub',
512
+ version: '1.4.0',
513
+ },
514
+ {
515
+ event: { type: 'long_task', long_task: hostLongAnimationFrame.long_task },
516
+ title: 'long animation frame whose longest script is the host',
517
+ service: undefined,
518
+ version: undefined,
519
+ },
520
+ {
521
+ event: { type: 'long_task', long_task: legacyLongTask.long_task },
522
+ title: 'legacy long task',
523
+ service: undefined,
524
+ version: undefined,
525
+ },
526
+ {
527
+ event: { type: 'long_task', long_task: mfeLongAnimationFrameWithoutDurations.long_task },
528
+ title: 'long animation frame whose scripts have no duration',
529
+ service: 'task-hub',
530
+ version: '1.4.0',
531
+ },
532
+ {
533
+ event: { type: 'long_task', long_task: longAnimationFrameWithoutScripts.long_task },
534
+ title: 'long animation frame with no scripts',
535
+ service: undefined,
536
+ version: undefined,
537
+ },
266
538
  ];
@@ -1,5 +1,6 @@
1
1
  export * from './before-send-data';
2
2
  export * from './init-config';
3
+ export * from './mfe-host';
3
4
  export * from './process-event';
4
5
  export * from './reset-shared-impl';
5
6
  export * from './tracing-urls';
@@ -0,0 +1,7 @@
1
+ export const mfeHost = (name: string, version: string): HTMLElement => {
2
+ const element = document.createElement(`${name}-abc123`);
3
+ element.setAttribute('data-mfe-name', name);
4
+ element.setAttribute('data-mfe-version', version);
5
+ element.attachShadow({ mode: 'open' });
6
+ return element;
7
+ };
@@ -4,6 +4,7 @@ import { buildAllowedTracingUrls } from '../build-allowed-tracing-urls';
4
4
  import { buildBeforeSend } from '../build-before-send';
5
5
  import { createImpl } from '../create-impl';
6
6
  import { installDdRumGuard } from '../dd-rum-guard';
7
+ import { elementServiceRecorder } from '../element-service-recorder';
7
8
  import { DATADOG_RUM_FOR_DEV, installDevConsole, isForceEnabled } from '../runtime-override';
8
9
  import type { SharedImpl } from '../shared-impl';
9
10
  import type { ApplicationConfig, InitRumOptions, State } from '../types';
@@ -12,6 +13,7 @@ jest.mock('@datadog/browser-rum');
12
13
  jest.mock('../build-before-send');
13
14
  jest.mock('../build-allowed-tracing-urls');
14
15
  jest.mock('../dd-rum-guard');
16
+ jest.mock('../element-service-recorder');
15
17
  jest.mock('../runtime-override', () => ({
16
18
  ...jest.requireActual('../runtime-override'),
17
19
  isForceEnabled: jest.fn(),
@@ -167,6 +169,12 @@ describe(`[datadog-rum] ${createImpl.name}`, () => {
167
169
  expect(installDdRumGuard).toHaveBeenCalledWith(datadogRum);
168
170
  });
169
171
 
172
+ test('installs the element service recorder', () => {
173
+ subject();
174
+
175
+ expect(elementServiceRecorder.install).toHaveBeenCalled();
176
+ });
177
+
170
178
  describe('when already called', () => {
171
179
  beforeEach(() => {
172
180
  subject();
@@ -0,0 +1,127 @@
1
+ import { mfeHost } from '../__mocks__';
2
+ import { createElementServiceRecorder } from '../element-service-recorder';
3
+
4
+ describe('[datadog-rum] createElementServiceRecorder', () => {
5
+ const version = '1.2.3';
6
+
7
+ const dispatchPointerUp = (target: HTMLElement) => {
8
+ const event = new MouseEvent('pointerup', { bubbles: true, composed: true });
9
+ target.dispatchEvent(event);
10
+ return event;
11
+ };
12
+
13
+ let recorder: ReturnType<typeof createElementServiceRecorder>;
14
+ let host: HTMLElement;
15
+ let button: HTMLElement;
16
+
17
+ beforeEach(() => {
18
+ jest.clearAllMocks();
19
+ recorder = createElementServiceRecorder();
20
+ host = document.body.appendChild(mfeHost('servicetitan-task-hub', version));
21
+ button = host.shadowRoot!.appendChild(document.createElement('button'));
22
+ });
23
+
24
+ afterEach(() => {
25
+ jest.restoreAllMocks();
26
+ document.body.innerHTML = '';
27
+ });
28
+
29
+ describe('serviceFor', () => {
30
+ let event: Event;
31
+
32
+ beforeEach(() => (event = dispatchPointerUp(button)));
33
+
34
+ const subject = () => recorder.serviceFor(event);
35
+
36
+ test('returns the service of the retargeted target', () => {
37
+ expect(subject()).toEqual({ service: 'task-hub', version });
38
+ });
39
+
40
+ describe('with a target that is not an element', () => {
41
+ beforeEach(() => (event = { target: window } as unknown as Event));
42
+
43
+ test('returns undefined', () => {
44
+ expect(subject()).toBeUndefined();
45
+ });
46
+ });
47
+
48
+ describe('when the recorder is installed', () => {
49
+ beforeEach(() => {
50
+ jest.spyOn(window, 'addEventListener');
51
+ recorder.install();
52
+ event = dispatchPointerUp(button);
53
+ });
54
+
55
+ afterEach(() => {
56
+ for (const args of jest.mocked(window.addEventListener).mock.calls) {
57
+ window.removeEventListener(...args);
58
+ }
59
+ });
60
+
61
+ test('returns the service resolved at dispatch', () => {
62
+ expect(subject()).toEqual({ service: 'task-hub', version });
63
+ });
64
+
65
+ describe('when the MFE unmounts before enrichment', () => {
66
+ beforeEach(() => host.remove());
67
+
68
+ test('returns the service resolved at dispatch', () => {
69
+ expect(subject()).toEqual({ service: 'task-hub', version });
70
+ });
71
+ });
72
+
73
+ describe('with a target outside any MFE', () => {
74
+ let outside: HTMLElement;
75
+
76
+ beforeEach(() => {
77
+ outside = document.body.appendChild(document.createElement('div'));
78
+ event = dispatchPointerUp(outside);
79
+ });
80
+
81
+ test('returns undefined', () => {
82
+ expect(subject()).toBeUndefined();
83
+ });
84
+
85
+ describe('when the target later moves into an MFE', () => {
86
+ beforeEach(() => host.appendChild(outside));
87
+
88
+ test('returns undefined', () => {
89
+ expect(subject()).toBeUndefined();
90
+ });
91
+ });
92
+ });
93
+ });
94
+ });
95
+
96
+ describe('install', () => {
97
+ beforeEach(() => jest.spyOn(window, 'addEventListener').mockImplementation(() => {}));
98
+
99
+ const subject = () => recorder.install();
100
+
101
+ test('adds a pointerup capture listener', () => {
102
+ subject();
103
+
104
+ expect(window.addEventListener).toHaveBeenCalledWith(
105
+ 'pointerup',
106
+ expect.any(Function),
107
+ {
108
+ capture: true,
109
+ passive: true,
110
+ }
111
+ );
112
+ });
113
+
114
+ describe('when already installed', () => {
115
+ beforeEach(() => {
116
+ subject();
117
+ jest.mocked(window.addEventListener).mockClear();
118
+ });
119
+
120
+ test('adds no listener', () => {
121
+ subject();
122
+
123
+ expect(window.addEventListener).not.toHaveBeenCalled();
124
+ });
125
+ });
126
+ });
127
+ });
@@ -1,12 +1,20 @@
1
+ /* eslint-disable @typescript-eslint/naming-convention -- RUM event fields are snake_case */
1
2
  import type { RumEvent, RumEventDomainContext } from '@datadog/browser-rum';
2
3
  import { type Mutable, rumEvents, type RumEventType } from '../__mocks__';
4
+ import { type ElementService, elementServiceRecorder } from '../element-service-recorder';
3
5
  import { enrichEvent } from '../enrich-event';
4
6
 
7
+ jest.mock('../element-service-recorder');
8
+
5
9
  describe(`[datadog-rum] ${enrichEvent.name}`, () => {
6
10
  let event: Mutable<RumEventType>;
7
11
  let context: RumEventDomainContext;
12
+ let elementService: ElementService | undefined;
8
13
 
9
14
  beforeEach(() => {
15
+ jest.clearAllMocks();
16
+ elementService = undefined;
17
+ jest.mocked(elementServiceRecorder.serviceFor).mockImplementation(() => elementService);
10
18
  event = { type: 'action' };
11
19
  context = {};
12
20
  });
@@ -19,16 +27,113 @@ describe(`[datadog-rum] ${enrichEvent.name}`, () => {
19
27
  context = rumEvent.context ?? {};
20
28
  });
21
29
 
22
- test('assigns the expected service', () => {
30
+ test('attributes the expected service and version', () => {
31
+ subject();
32
+
33
+ expect({ service: event.service, version: event.version }).toEqual({
34
+ service: rumEvent.service,
35
+ version: rumEvent.version,
36
+ });
37
+ });
38
+ });
39
+
40
+ describe('with a long animation frame', () => {
41
+ const unpkgUrl = (name: string) =>
42
+ `https://unpkg.servicetitan.com/@servicetitan/${name}@1.0.0/dist/main.js`;
43
+ const hostUrl = 'https://go.servicetitan.com/scripts/build/desktop/main.js';
44
+
45
+ const loafEvent = (scripts: { duration: number; source_url: string }[]) =>
46
+ ({
47
+ type: 'long_task',
48
+ long_task: { entry_type: 'long-animation-frame', scripts },
49
+ }) as Mutable<RumEventType>;
50
+
51
+ beforeEach(() => {
52
+ event = loafEvent([
53
+ { duration: 10, source_url: unpkgUrl('task-hub') },
54
+ { duration: 30, source_url: hostUrl },
55
+ { duration: 20, source_url: unpkgUrl('other-mfe') },
56
+ { duration: 5, source_url: unpkgUrl('task-hub') },
57
+ ]);
58
+ });
59
+
60
+ test('lists every MFE that ran a script, in run order', () => {
61
+ subject();
62
+
63
+ expect(event.context?.loafServices).toEqual(['task-hub', 'other-mfe']);
64
+ });
65
+
66
+ describe('with only host scripts', () => {
67
+ beforeEach(() => (event = loafEvent([{ duration: 60, source_url: hostUrl }])));
68
+
69
+ test('adds no list', () => {
70
+ subject();
71
+
72
+ expect(event.context?.loafServices).toBeUndefined();
73
+ });
74
+ });
75
+ });
76
+
77
+ describe('with a click action', () => {
78
+ const pointerUpEvent = new MouseEvent('pointerup');
79
+ const taskHubService = { service: 'task-hub', version: '1.4.0' };
80
+ const hostService = { service: 'desktop', version: '1.0.0' };
81
+
82
+ beforeEach(() => {
83
+ event = { type: 'action', action: { type: 'click' }, ...hostService };
84
+ context = { events: [pointerUpEvent] };
85
+ elementService = taskHubService;
86
+ });
87
+
88
+ test('reads the service from the DOM event', () => {
23
89
  subject();
24
90
 
25
- expect(event.service).toEqual(rumEvent.service);
91
+ expect(elementServiceRecorder.serviceFor).toHaveBeenCalledWith(pointerUpEvent);
26
92
  });
27
93
 
28
- test('assigns the expected version', () => {
94
+ test('assigns the element service and version', () => {
29
95
  subject();
30
96
 
31
- expect(event.version).toEqual(rumEvent.version);
97
+ expect({ service: event.service, version: event.version }).toEqual(taskHubService);
98
+ });
99
+
100
+ describe('with an MFE handling stack', () => {
101
+ beforeEach(() => {
102
+ context = {
103
+ events: [pointerUpEvent],
104
+ handlingStack:
105
+ 'at f @ https://unpkg.servicetitan.com/@servicetitan/other-mfe@2.0.0/dist/main.js:1:1',
106
+ };
107
+ });
108
+
109
+ test('assigns the service and version from the handling stack', () => {
110
+ subject();
111
+
112
+ expect({ service: event.service, version: event.version }).toEqual({
113
+ service: 'other-mfe',
114
+ version: '2.0.0',
115
+ });
116
+ });
117
+ });
118
+
119
+ describe('without an MFE element on the path', () => {
120
+ beforeEach(() => (elementService = undefined));
121
+
122
+ test('keeps the application service', () => {
123
+ subject();
124
+
125
+ expect({ service: event.service, version: event.version }).toEqual(hostService);
126
+ });
127
+ });
128
+
129
+ describe('without DOM events', () => {
130
+ beforeEach(() => (context = {}));
131
+
132
+ test('keeps the application service', () => {
133
+ subject();
134
+
135
+ expect({ service: event.service, version: event.version }).toEqual(hostService);
136
+ });
32
137
  });
33
138
  });
34
139
  });