@servicetitan/datadog-rum 36.3.1 → 37.0.0

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.
Files changed (116) hide show
  1. package/dist/before-send.d.ts +11 -1
  2. package/dist/before-send.d.ts.map +1 -1
  3. package/dist/before-send.js +2 -56
  4. package/dist/before-send.js.map +1 -1
  5. package/dist/build-allowed-tracing-urls.d.ts +9 -0
  6. package/dist/build-allowed-tracing-urls.d.ts.map +1 -0
  7. package/dist/build-allowed-tracing-urls.js +54 -0
  8. package/dist/build-allowed-tracing-urls.js.map +1 -0
  9. package/dist/build-before-send.d.ts +4 -0
  10. package/dist/build-before-send.d.ts.map +1 -0
  11. package/dist/build-before-send.js +64 -0
  12. package/dist/build-before-send.js.map +1 -0
  13. package/dist/bundle-state.d.ts +10 -0
  14. package/dist/bundle-state.d.ts.map +1 -0
  15. package/dist/bundle-state.js +10 -0
  16. package/dist/bundle-state.js.map +1 -0
  17. package/dist/create-impl.d.ts +8 -0
  18. package/dist/create-impl.d.ts.map +1 -0
  19. package/dist/create-impl.js +81 -0
  20. package/dist/create-impl.js.map +1 -0
  21. package/dist/datadog-provider.d.ts +2 -5
  22. package/dist/datadog-provider.d.ts.map +1 -1
  23. package/dist/datadog-provider.js +2 -2
  24. package/dist/datadog-provider.js.map +1 -1
  25. package/dist/enrich-event.d.ts +6 -0
  26. package/dist/enrich-event.d.ts.map +1 -0
  27. package/dist/enrich-event.js +33 -0
  28. package/dist/enrich-event.js.map +1 -0
  29. package/dist/get-shared.d.ts +10 -0
  30. package/dist/get-shared.d.ts.map +1 -0
  31. package/dist/get-shared.js +16 -0
  32. package/dist/get-shared.js.map +1 -0
  33. package/dist/handle-resource.d.ts +3 -0
  34. package/dist/handle-resource.d.ts.map +1 -0
  35. package/dist/handle-resource.js +27 -0
  36. package/dist/handle-resource.js.map +1 -0
  37. package/dist/index.d.ts +3 -0
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +3 -0
  40. package/dist/index.js.map +1 -1
  41. package/dist/init.d.ts +17 -0
  42. package/dist/init.d.ts.map +1 -0
  43. package/dist/init.js +28 -0
  44. package/dist/init.js.map +1 -0
  45. package/dist/runtime-override/constants.d.ts +2 -0
  46. package/dist/runtime-override/constants.d.ts.map +1 -0
  47. package/dist/runtime-override/constants.js +3 -0
  48. package/dist/runtime-override/constants.js.map +1 -0
  49. package/dist/runtime-override/dev-console.d.ts +12 -0
  50. package/dist/runtime-override/dev-console.d.ts.map +1 -0
  51. package/dist/runtime-override/dev-console.js +40 -0
  52. package/dist/runtime-override/dev-console.js.map +1 -0
  53. package/dist/runtime-override/index.d.ts +4 -0
  54. package/dist/runtime-override/index.d.ts.map +1 -0
  55. package/dist/runtime-override/index.js +5 -0
  56. package/dist/runtime-override/index.js.map +1 -0
  57. package/dist/runtime-override/override.d.ts +4 -0
  58. package/dist/runtime-override/override.d.ts.map +1 -0
  59. package/dist/runtime-override/override.js +35 -0
  60. package/dist/runtime-override/override.js.map +1 -0
  61. package/dist/set-user.d.ts +13 -0
  62. package/dist/set-user.d.ts.map +1 -0
  63. package/dist/set-user.js +22 -0
  64. package/dist/set-user.js.map +1 -0
  65. package/dist/shared-impl.d.ts +11 -0
  66. package/dist/shared-impl.d.ts.map +1 -0
  67. package/dist/shared-impl.js +3 -0
  68. package/dist/shared-impl.js.map +1 -0
  69. package/dist/types.d.ts +51 -0
  70. package/dist/types.d.ts.map +1 -0
  71. package/dist/types.js +3 -0
  72. package/dist/types.js.map +1 -0
  73. package/dist/use-rum-service.d.ts +12 -0
  74. package/dist/use-rum-service.d.ts.map +1 -0
  75. package/dist/use-rum-service.js +25 -0
  76. package/dist/use-rum-service.js.map +1 -0
  77. package/package.json +3 -3
  78. package/src/__mocks__/index.ts +6 -0
  79. package/src/__mocks__/init-config.ts +6 -0
  80. package/src/__mocks__/process-event.ts +20 -0
  81. package/src/__mocks__/reset-shared-impl.ts +8 -0
  82. package/src/__mocks__/tracing-urls.ts +30 -0
  83. package/src/__mocks__/types.ts +1 -0
  84. package/src/__tests__/before-send.test.ts +39 -87
  85. package/src/__tests__/build-allowed-tracing-urls.test.ts +120 -0
  86. package/src/__tests__/build-before-send.test.ts +193 -0
  87. package/src/__tests__/create-impl.test.ts +293 -0
  88. package/src/__tests__/datadog-provider.test.tsx +55 -25
  89. package/src/__tests__/enrich-event.test.ts +34 -0
  90. package/src/__tests__/get-shared.test.ts +56 -0
  91. package/src/__tests__/handle-resource.test.ts +76 -0
  92. package/src/__tests__/init.test.ts +69 -0
  93. package/src/__tests__/integration.test.ts +147 -0
  94. package/src/__tests__/set-user.test.ts +61 -0
  95. package/src/__tests__/use-rum-service.test.tsx +82 -0
  96. package/src/before-send.ts +13 -60
  97. package/src/build-allowed-tracing-urls.ts +53 -0
  98. package/src/build-before-send.ts +81 -0
  99. package/src/bundle-state.ts +9 -0
  100. package/src/create-impl.ts +94 -0
  101. package/src/datadog-provider.tsx +4 -6
  102. package/src/enrich-event.ts +38 -0
  103. package/src/get-shared.ts +24 -0
  104. package/src/handle-resource.ts +30 -0
  105. package/src/index.ts +3 -0
  106. package/src/init.ts +39 -0
  107. package/src/runtime-override/__tests__/dev-console.test.ts +158 -0
  108. package/src/runtime-override/__tests__/override.test.ts +109 -0
  109. package/src/runtime-override/constants.ts +1 -0
  110. package/src/runtime-override/dev-console.ts +56 -0
  111. package/src/runtime-override/index.ts +3 -0
  112. package/src/runtime-override/override.ts +41 -0
  113. package/src/set-user.ts +25 -0
  114. package/src/shared-impl.ts +9 -0
  115. package/src/types.ts +62 -0
  116. package/src/use-rum-service.ts +28 -0
@@ -0,0 +1,293 @@
1
+ import { datadogRum } from '@datadog/browser-rum';
2
+ import { minimalApplication } from '../__mocks__';
3
+ import { buildAllowedTracingUrls } from '../build-allowed-tracing-urls';
4
+ import { buildBeforeSend } from '../build-before-send';
5
+ import { createImpl } from '../create-impl';
6
+ import { DATADOG_RUM_FOR_DEV, installDevConsole, isForceEnabled } from '../runtime-override';
7
+ import type { SharedImpl } from '../shared-impl';
8
+ import type { InitRumOptions, State } from '../types';
9
+
10
+ jest.mock('@datadog/browser-rum');
11
+ jest.mock('../build-before-send');
12
+ jest.mock('../build-allowed-tracing-urls');
13
+ jest.mock('../runtime-override', () => ({
14
+ ...jest.requireActual('../runtime-override'),
15
+ isForceEnabled: jest.fn(),
16
+ installDevConsole: jest.fn(),
17
+ }));
18
+
19
+ describe(`[datadog-rum] ${createImpl.name}`, () => {
20
+ const mockBeforeSend = jest.fn();
21
+ const mockAllowedTracingUrls = jest.fn();
22
+ let impl: SharedImpl;
23
+
24
+ beforeEach(() => {
25
+ jest.clearAllMocks();
26
+ jest.mocked(datadogRum.getInitConfiguration).mockReturnValue(undefined);
27
+ jest.mocked(isForceEnabled).mockReturnValue(false);
28
+ jest.mocked(buildBeforeSend).mockReturnValue(mockBeforeSend);
29
+ jest.mocked(buildAllowedTracingUrls).mockReturnValue(mockAllowedTracingUrls as any);
30
+ impl = createImpl();
31
+ });
32
+
33
+ /*
34
+ * createImpl passes the same live state object to both collaborators, so the
35
+ * captured argument reflects later mutations from init/setServiceContext/cleanup.
36
+ */
37
+ const capturedState = () => jest.mocked(buildBeforeSend).mock.calls[0][0] as State;
38
+
39
+ describe('init', () => {
40
+ let options: InitRumOptions;
41
+
42
+ beforeEach(() => {
43
+ options = { service: { name: 'desktop' }, application: { ...minimalApplication } };
44
+ });
45
+
46
+ const subject = () => impl.init(options);
47
+
48
+ test('calls datadogRum.init', () => {
49
+ subject();
50
+
51
+ expect(datadogRum.init).toHaveBeenCalledWith(
52
+ expect.objectContaining({
53
+ ...options.application,
54
+ sessionSampleRate: 100,
55
+ sessionReplaySampleRate: 0,
56
+ defaultPrivacyLevel: 'mask-user-input',
57
+ service: options.service.name,
58
+ beforeSend: mockBeforeSend,
59
+ allowedTracingUrls: mockAllowedTracingUrls,
60
+ })
61
+ );
62
+ });
63
+
64
+ test('reports it initialized the SDK', () => {
65
+ expect(subject().initializedSdk).toBe(true);
66
+ });
67
+
68
+ test('registers the service', () => {
69
+ subject();
70
+
71
+ expect(capturedState().services.has(options.service.name)).toBe(true);
72
+ });
73
+
74
+ itDoesNotInstallDevConsole();
75
+
76
+ itDoesNotTagTheSession();
77
+
78
+ describe('with a stale override present', () => {
79
+ beforeEach(() => {
80
+ jest.mocked(isForceEnabled).mockReturnValue(true);
81
+ });
82
+
83
+ itDoesNotTagTheSession();
84
+ });
85
+
86
+ describe('when config overrides defaults', () => {
87
+ const overrides = {
88
+ sessionSampleRate: 25,
89
+ sessionReplaySampleRate: 10,
90
+ defaultPrivacyLevel: 'allow',
91
+ } as const;
92
+
93
+ beforeEach(() => {
94
+ options.application = { ...minimalApplication, ...overrides };
95
+ });
96
+
97
+ test('calls datadogRum.init with overridden values', () => {
98
+ subject();
99
+
100
+ expect(datadogRum.init).toHaveBeenCalledWith(expect.objectContaining(overrides));
101
+ });
102
+ });
103
+
104
+ describe('when config contains extra values', () => {
105
+ const excludedActivityUrls = [/pendo\.io/];
106
+
107
+ beforeEach(() => {
108
+ options.application = { ...minimalApplication, excludedActivityUrls };
109
+ });
110
+
111
+ test('calls datadogRum.init with extra values', () => {
112
+ subject();
113
+
114
+ expect(datadogRum.init).toHaveBeenCalledWith(
115
+ expect.objectContaining({ excludedActivityUrls })
116
+ );
117
+ });
118
+ });
119
+
120
+ describe('with an application beforeSend handler', () => {
121
+ const appBeforeSend = jest.fn();
122
+
123
+ beforeEach(() => {
124
+ options.application = { ...minimalApplication, beforeSend: appBeforeSend };
125
+ });
126
+
127
+ test('registers the handler', () => {
128
+ subject();
129
+
130
+ expect(capturedState().applicationBeforeSend).toBe(appBeforeSend);
131
+ });
132
+ });
133
+
134
+ function itDoesNotCallDatadogRumInit() {
135
+ test('does not call datadogRum.init', () => {
136
+ subject();
137
+
138
+ expect(datadogRum.init).not.toHaveBeenCalled();
139
+ });
140
+
141
+ test('reports it did not initialize the SDK', () => {
142
+ expect(subject().initializedSdk).toBe(false);
143
+ });
144
+ }
145
+
146
+ function itDoesNotInstallDevConsole() {
147
+ test('does not install the dev console handle', () => {
148
+ subject();
149
+
150
+ expect(installDevConsole).not.toHaveBeenCalled();
151
+ });
152
+ }
153
+
154
+ function itDoesNotTagTheSession() {
155
+ test('does not tag the session', () => {
156
+ subject();
157
+
158
+ expect(datadogRum.setGlobalContextProperty).not.toHaveBeenCalled();
159
+ });
160
+ }
161
+
162
+ describe('when already called', () => {
163
+ beforeEach(() => {
164
+ subject();
165
+ jest.clearAllMocks();
166
+ jest.spyOn(console, 'info').mockImplementation(() => {});
167
+ });
168
+
169
+ itDoesNotCallDatadogRumInit();
170
+
171
+ test('logs an info message', () => {
172
+ const infoSpy = jest.spyOn(console, 'info');
173
+
174
+ subject();
175
+
176
+ expect(infoSpy).toHaveBeenCalledWith(
177
+ expect.stringContaining('application config ignored')
178
+ );
179
+ });
180
+ });
181
+
182
+ describe('when datadogRum.init was already called directly', () => {
183
+ beforeEach(() => {
184
+ jest.spyOn(console, 'warn').mockImplementation(() => {});
185
+ jest.mocked(datadogRum.getInitConfiguration).mockReturnValue({} as any);
186
+ });
187
+
188
+ itDoesNotCallDatadogRumInit();
189
+
190
+ test('warns the SDK was initialized outside the facade', () => {
191
+ const warnSpy = jest.spyOn(console, 'warn');
192
+
193
+ subject();
194
+
195
+ expect(warnSpy).toHaveBeenCalledWith(
196
+ expect.stringContaining('SDK was initialized outside the facade')
197
+ );
198
+ });
199
+ });
200
+
201
+ describe('with service only (no application)', () => {
202
+ beforeEach(() => {
203
+ delete options.application;
204
+ });
205
+
206
+ itDoesNotCallDatadogRumInit();
207
+ });
208
+
209
+ describe('when enabled is false', () => {
210
+ beforeEach(() => {
211
+ options.enabled = false;
212
+ });
213
+
214
+ test('installs the dev console handle', () => {
215
+ subject();
216
+
217
+ expect(installDevConsole).toHaveBeenCalled();
218
+ });
219
+
220
+ itDoesNotCallDatadogRumInit();
221
+
222
+ describe('with an active override', () => {
223
+ beforeEach(() => {
224
+ jest.mocked(isForceEnabled).mockReturnValue(true);
225
+ });
226
+
227
+ test('initializes the SDK', () => {
228
+ subject();
229
+
230
+ expect(datadogRum.init).toHaveBeenCalled();
231
+ });
232
+
233
+ test('reports it initialized the SDK', () => {
234
+ expect(subject().initializedSdk).toBe(true);
235
+ });
236
+
237
+ test('tags the session as override-driven', () => {
238
+ subject();
239
+
240
+ expect(datadogRum.setGlobalContextProperty).toHaveBeenCalledWith(
241
+ DATADOG_RUM_FOR_DEV,
242
+ true
243
+ );
244
+ });
245
+ });
246
+
247
+ describe('with no application', () => {
248
+ beforeEach(() => {
249
+ delete options.application;
250
+ });
251
+
252
+ itDoesNotInstallDevConsole();
253
+ });
254
+ });
255
+
256
+ describe('cleanup', () => {
257
+ beforeEach(() => {
258
+ subject().cleanup();
259
+ });
260
+
261
+ test('unregisters the service', () => {
262
+ expect(capturedState().services.has(options.service.name)).toBe(false);
263
+ });
264
+ });
265
+ });
266
+
267
+ describe('setServiceContext', () => {
268
+ const name = 'task-hub';
269
+ const context = { orgId: '42' };
270
+
271
+ beforeEach(() => {
272
+ impl.init({ service: { name }, application: minimalApplication });
273
+ });
274
+
275
+ const subject = () => impl.setServiceContext(name, context);
276
+
277
+ test('registers service context', () => {
278
+ subject();
279
+
280
+ expect(capturedState().services.get(name)?.context).toEqual(context);
281
+ });
282
+
283
+ describe('when already called', () => {
284
+ beforeEach(() => subject());
285
+
286
+ test('init() preserves context', () => {
287
+ impl.init({ service: { name, beforeSend: jest.fn() } });
288
+
289
+ expect(capturedState().services.get(name)?.context).toEqual(context);
290
+ });
291
+ });
292
+ });
293
+ });
@@ -2,7 +2,11 @@ import { datadogRum } from '@datadog/browser-rum';
2
2
  import '@testing-library/jest-dom';
3
3
  import { act, render, screen, waitFor } from '@testing-library/react';
4
4
  import { FC, Fragment, useState } from 'react';
5
+ import { minimalApplication, resetSharedImpl } from '../__mocks__';
6
+ import { bundleState } from '../bundle-state';
5
7
  import { DatadogProvider, Options } from '../datadog-provider';
8
+ import { init } from '../init';
9
+ import type { RumUser } from '../set-user';
6
10
 
7
11
  jest.mock('@datadog/browser-rum');
8
12
  jest.mock('../hash-routing', () => mockComponent('HashRouting'));
@@ -18,52 +22,61 @@ function mockComponent(name: string) {
18
22
  };
19
23
  }
20
24
 
21
- type User = Omit<import('@datadog/browser-core').User, 'id'> & { id: string };
22
-
23
25
  describe(`[datadog-rum] ${DatadogProvider.name}`, () => {
24
- let setUser: (user?: User) => void;
26
+ const initialUser: RumUser = { id: '1' };
27
+
25
28
  let isEnabled: boolean;
26
29
  let options: Options;
30
+ let setUser: (user?: RumUser) => void;
31
+
32
+ beforeEach(() => {
33
+ jest.clearAllMocks();
34
+ resetSharedImpl();
35
+ isEnabled = true;
36
+ options = {};
37
+ // The test bundle takes the Datadog host role by calling init.
38
+ init({ service: { name: 'host' }, application: minimalApplication });
39
+ jest.clearAllMocks();
40
+ });
27
41
 
28
42
  const subject = () => {
29
43
  const Component: FC = () => {
30
- const [user, setUserInner] = useState<User | undefined>({ id: '1' });
44
+ const [user, setUserInner] = useState<RumUser | undefined>(initialUser);
31
45
  setUser = setUserInner;
32
46
  return <DatadogProvider isEnabled={isEnabled} user={user} options={options} />;
33
47
  };
34
48
  return render(<Component />);
35
49
  };
36
50
 
37
- beforeEach(() => {
38
- isEnabled = true;
39
- options = {};
40
- jest.clearAllMocks();
41
- });
42
- test('initializes user', () => {
51
+ test('initializes the user on mount', () => {
43
52
  subject();
44
- expect(datadogRum.setUser).toHaveBeenCalledWith({ id: '1' });
53
+
54
+ expect(datadogRum.setUser).toHaveBeenCalledWith(initialUser);
45
55
  });
46
56
 
47
- describe('when user is changed', () => {
57
+ describe('when the user is changed', () => {
58
+ const nextUser: RumUser = { id: '2' };
59
+
48
60
  beforeEach(() => {
49
61
  subject();
50
62
  act(() => {
51
- setUser({ id: '2' });
63
+ setUser(nextUser);
52
64
  });
53
65
  });
54
66
 
55
- test('updates user', () => {
56
- expect(datadogRum.setUser).toHaveBeenCalledWith({ id: '2' });
67
+ test('updates the SDK user', () => {
68
+ expect(datadogRum.setUser).toHaveBeenCalledWith(nextUser);
57
69
  });
58
70
 
59
- describe('to undefined', () => {
71
+ describe('when the user is then unset', () => {
60
72
  beforeEach(() => {
61
73
  jest.clearAllMocks();
62
74
  act(() => {
63
75
  setUser(undefined);
64
76
  });
65
77
  });
66
- test("doesn't update user", () => {
78
+
79
+ test('does not call setUser', () => {
67
80
  expect(datadogRum.setUser).not.toHaveBeenCalled();
68
81
  });
69
82
  });
@@ -72,24 +85,27 @@ describe(`[datadog-rum] ${DatadogProvider.name}`, () => {
72
85
  describe('with isEnabled=false', () => {
73
86
  beforeEach(() => {
74
87
  isEnabled = false;
75
- });
76
-
77
- test("doesn't initialize user", () => {
78
88
  subject();
89
+ });
79
90
 
91
+ test('does not call setUser', () => {
80
92
  expect(datadogRum.setUser).not.toHaveBeenCalled();
81
93
  });
82
94
  });
83
95
 
84
- test("doesn't render <HashRouting />", async () => {
85
- subject();
96
+ describe('without options.hashRouting', () => {
97
+ test('does not render <HashRouting />', async () => {
98
+ subject();
86
99
 
87
- await waitFor(() => expect(screen.queryByText('<HashRouting />')).not.toBeInTheDocument());
100
+ await waitFor(() =>
101
+ expect(screen.queryByText('<HashRouting />')).not.toBeInTheDocument()
102
+ );
103
+ });
88
104
  });
89
105
 
90
- describe('when options.hashRouting=true', () => {
106
+ describe('with options.hashRouting=true', () => {
91
107
  beforeEach(() => {
92
- options = { hashRouting: true };
108
+ options.hashRouting = true;
93
109
  });
94
110
 
95
111
  test('renders <HashRouting />', async () => {
@@ -98,4 +114,18 @@ describe(`[datadog-rum] ${DatadogProvider.name}`, () => {
98
114
  await waitFor(() => expect(screen.queryByText('<HashRouting />')).toBeInTheDocument());
99
115
  });
100
116
  });
117
+
118
+ describe('when mounted from a non-host bundle', () => {
119
+ beforeEach(() => {
120
+ bundleState.isSdkInitializer = false;
121
+ jest.clearAllMocks();
122
+ });
123
+
124
+ test('does not call setUser', () => {
125
+ const Component: FC = () => <DatadogProvider isEnabled user={{ id: 'non-host' }} />;
126
+ render(<Component />);
127
+
128
+ expect(datadogRum.setUser).not.toHaveBeenCalled();
129
+ });
130
+ });
101
131
  });
@@ -0,0 +1,34 @@
1
+ import type { RumEvent, RumEventDomainContext } from '@datadog/browser-rum';
2
+ import { type Mutable, rumEvents, type RumEventType } from '../__mocks__';
3
+ import { enrichEvent } from '../enrich-event';
4
+
5
+ describe(`[datadog-rum] ${enrichEvent.name}`, () => {
6
+ let event: Mutable<RumEventType>;
7
+ let context: RumEventDomainContext;
8
+
9
+ beforeEach(() => {
10
+ event = { type: 'action' };
11
+ context = {};
12
+ });
13
+
14
+ const subject = () => enrichEvent(event as RumEvent, context);
15
+
16
+ describe.each(rumEvents)('when event.type=$event.type and $title', rumEvent => {
17
+ beforeEach(() => {
18
+ event = { ...rumEvent.event };
19
+ context = rumEvent.context ?? {};
20
+ });
21
+
22
+ test('assigns the expected service', () => {
23
+ subject();
24
+
25
+ expect(event.service).toEqual(rumEvent.service);
26
+ });
27
+
28
+ test('assigns the expected version', () => {
29
+ subject();
30
+
31
+ expect(event.version).toEqual(rumEvent.version);
32
+ });
33
+ });
34
+ });
@@ -0,0 +1,56 @@
1
+ import { resetSharedImpl } from '../__mocks__/reset-shared-impl';
2
+ import { getOrCreateSharedImpl, getSharedImpl, STATE_KEY } from '../get-shared';
3
+
4
+ describe(`[datadog-rum] ${getOrCreateSharedImpl.name}`, () => {
5
+ beforeEach(() => {
6
+ resetSharedImpl();
7
+ });
8
+
9
+ const subject = () => getOrCreateSharedImpl();
10
+
11
+ test('stores the instance under the well-known Symbol on globalThis', () => {
12
+ const impl = subject();
13
+
14
+ expect((globalThis as any)[STATE_KEY]).toBe(impl);
15
+ });
16
+
17
+ test('resolves the well-known Symbol from the package key', () => {
18
+ expect(Symbol.for('@servicetitan/datadog-rum')).toBe(STATE_KEY);
19
+ });
20
+
21
+ describe('when an instance already exists', () => {
22
+ let first: ReturnType<typeof getOrCreateSharedImpl>;
23
+
24
+ beforeEach(() => {
25
+ first = subject();
26
+ });
27
+
28
+ test('returns the same instance', () => {
29
+ expect(subject()).toBe(first);
30
+ });
31
+ });
32
+ });
33
+
34
+ describe(`[datadog-rum] ${getSharedImpl.name}`, () => {
35
+ beforeEach(() => {
36
+ resetSharedImpl();
37
+ });
38
+
39
+ const subject = () => getSharedImpl();
40
+
41
+ test('returns undefined', () => {
42
+ expect(subject()).toBeUndefined();
43
+ });
44
+
45
+ describe('when an instance exists', () => {
46
+ let impl: ReturnType<typeof getOrCreateSharedImpl>;
47
+
48
+ beforeEach(() => {
49
+ impl = getOrCreateSharedImpl();
50
+ });
51
+
52
+ test('returns existing instance', () => {
53
+ expect(subject()).toBe(impl);
54
+ });
55
+ });
56
+ });
@@ -0,0 +1,76 @@
1
+ import type { RumResourceEvent } from '@datadog/browser-rum';
2
+ import { type Mutable } from '../__mocks__';
3
+ import { handleResource } from '../handle-resource';
4
+
5
+ describe(`[datadog-rum] ${handleResource.name}`, () => {
6
+ const cfRay = 'ray-1';
7
+ const headers = new Map([['cf-ray', cfRay]]);
8
+
9
+ let event: Mutable<RumResourceEvent>;
10
+ let context: any;
11
+
12
+ function makeResourceEvent(type: 'fetch' | 'xhr', url: string) {
13
+ return {
14
+ type: 'resource',
15
+ resource: { type, url },
16
+ } as unknown as Mutable<RumResourceEvent>;
17
+ }
18
+
19
+ const subject = () => handleResource(event, context);
20
+
21
+ function itAddsCfRayToContext() {
22
+ test('adds the cf-ray value to event context', () => {
23
+ subject();
24
+
25
+ expect(event.context?.cfRay).toBe(cfRay);
26
+ });
27
+ }
28
+
29
+ function itDoesNotAddCfRay() {
30
+ test('does not add a cf-ray value', () => {
31
+ subject();
32
+
33
+ expect(event.context?.cfRay).toBeUndefined();
34
+ });
35
+ }
36
+
37
+ describe('with a "fetch" resource', () => {
38
+ beforeEach(() => {
39
+ event = makeResourceEvent('fetch', window.location.toString());
40
+ context = { response: { headers } };
41
+ });
42
+
43
+ itAddsCfRayToContext();
44
+
45
+ describe('when the request targets a different domain', () => {
46
+ beforeEach(() => (event = makeResourceEvent('fetch', 'https://example.com/')));
47
+
48
+ itDoesNotAddCfRay();
49
+ });
50
+
51
+ describe('without a cf-ray response header', () => {
52
+ beforeEach(() => (context = { response: { headers: new Map() } }));
53
+
54
+ itDoesNotAddCfRay();
55
+ });
56
+
57
+ describe('with pre-existing event context', () => {
58
+ beforeEach(() => (event.context = { existing: 'keep' }));
59
+
60
+ test('merges the cf-ray value without dropping existing keys', () => {
61
+ subject();
62
+
63
+ expect(event.context).toEqual({ existing: 'keep', cfRay });
64
+ });
65
+ });
66
+ });
67
+
68
+ describe('with an "xhr" resource', () => {
69
+ beforeEach(() => {
70
+ event = makeResourceEvent('xhr', window.location.toString());
71
+ context = { xhr: { getResponseHeader: (key: string) => headers.get(key) } };
72
+ });
73
+
74
+ itAddsCfRayToContext();
75
+ });
76
+ });
@@ -0,0 +1,69 @@
1
+ import { minimalApplication } from '../__mocks__/init-config';
2
+ import { bundleState } from '../bundle-state';
3
+ import { getOrCreateSharedImpl } from '../get-shared';
4
+ import { init } from '../init';
5
+ import type { InitRumOptions } from '../types';
6
+
7
+ jest.mock('../get-shared');
8
+
9
+ describe(`[datadog-rum] ${init.name}`, () => {
10
+ const cleanup = jest.fn();
11
+ const setServiceContext = jest.fn();
12
+ let initializedSdk: boolean;
13
+ let options: InitRumOptions;
14
+
15
+ beforeEach(() => {
16
+ jest.clearAllMocks();
17
+ bundleState.isSdkInitializer = false;
18
+ initializedSdk = false;
19
+ jest.mocked(getOrCreateSharedImpl).mockReturnValue({
20
+ init: jest.fn(() => ({ cleanup, initializedSdk })),
21
+ setServiceContext,
22
+ });
23
+ options = { service: { name: 'desktop' }, application: minimalApplication };
24
+ });
25
+
26
+ const subject = () => init(options);
27
+
28
+ test('forwards to the shared implementation', () => {
29
+ subject();
30
+
31
+ expect(getOrCreateSharedImpl().init).toHaveBeenCalledWith(options);
32
+ });
33
+
34
+ test('returns the cleanup from the implementation', () => {
35
+ expect(subject().cleanup).toBe(cleanup);
36
+ });
37
+
38
+ test('returns a setServiceContext bound to the service name', () => {
39
+ const context = { orgId: '42' };
40
+
41
+ subject().setServiceContext(context);
42
+
43
+ expect(setServiceContext).toHaveBeenCalledWith(options.service.name, context);
44
+ });
45
+
46
+ describe('when the implementation initialized the SDK', () => {
47
+ beforeEach(() => {
48
+ initializedSdk = true;
49
+ });
50
+
51
+ test('marks this bundle as the SDK initializer', () => {
52
+ subject();
53
+
54
+ expect(bundleState.isSdkInitializer).toBe(true);
55
+ });
56
+ });
57
+
58
+ describe('when the implementation did not initialize the SDK', () => {
59
+ beforeEach(() => {
60
+ initializedSdk = false;
61
+ });
62
+
63
+ test('does not mark this bundle as the SDK initializer', () => {
64
+ subject();
65
+
66
+ expect(bundleState.isSdkInitializer).toBe(false);
67
+ });
68
+ });
69
+ });