@servicetitan/datadog-rum 36.3.0 → 36.4.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.
- package/dist/before-send.d.ts +11 -1
- package/dist/before-send.d.ts.map +1 -1
- package/dist/before-send.js +2 -56
- package/dist/before-send.js.map +1 -1
- package/dist/build-allowed-tracing-urls.d.ts +9 -0
- package/dist/build-allowed-tracing-urls.d.ts.map +1 -0
- package/dist/build-allowed-tracing-urls.js +54 -0
- package/dist/build-allowed-tracing-urls.js.map +1 -0
- package/dist/build-before-send.d.ts +4 -0
- package/dist/build-before-send.d.ts.map +1 -0
- package/dist/build-before-send.js +64 -0
- package/dist/build-before-send.js.map +1 -0
- package/dist/bundle-state.d.ts +10 -0
- package/dist/bundle-state.d.ts.map +1 -0
- package/dist/bundle-state.js +10 -0
- package/dist/bundle-state.js.map +1 -0
- package/dist/create-impl.d.ts +8 -0
- package/dist/create-impl.d.ts.map +1 -0
- package/dist/create-impl.js +65 -0
- package/dist/create-impl.js.map +1 -0
- package/dist/datadog-provider.d.ts +2 -5
- package/dist/datadog-provider.d.ts.map +1 -1
- package/dist/datadog-provider.js +2 -2
- package/dist/datadog-provider.js.map +1 -1
- package/dist/enrich-event.d.ts +6 -0
- package/dist/enrich-event.d.ts.map +1 -0
- package/dist/enrich-event.js +33 -0
- package/dist/enrich-event.js.map +1 -0
- package/dist/get-shared.d.ts +10 -0
- package/dist/get-shared.d.ts.map +1 -0
- package/dist/get-shared.js +16 -0
- package/dist/get-shared.js.map +1 -0
- package/dist/handle-resource.d.ts +3 -0
- package/dist/handle-resource.d.ts.map +1 -0
- package/dist/handle-resource.js +27 -0
- package/dist/handle-resource.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +17 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +28 -0
- package/dist/init.js.map +1 -0
- package/dist/set-user.d.ts +13 -0
- package/dist/set-user.d.ts.map +1 -0
- package/dist/set-user.js +22 -0
- package/dist/set-user.js.map +1 -0
- package/dist/shared-impl.d.ts +11 -0
- package/dist/shared-impl.d.ts.map +1 -0
- package/dist/shared-impl.js +3 -0
- package/dist/shared-impl.js.map +1 -0
- package/dist/types.d.ts +50 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/use-rum-service.d.ts +12 -0
- package/dist/use-rum-service.d.ts.map +1 -0
- package/dist/use-rum-service.js +25 -0
- package/dist/use-rum-service.js.map +1 -0
- package/package.json +3 -3
- package/src/__mocks__/index.ts +6 -0
- package/src/__mocks__/init-config.ts +6 -0
- package/src/__mocks__/process-event.ts +20 -0
- package/src/__mocks__/reset-shared-impl.ts +8 -0
- package/src/__mocks__/tracing-urls.ts +30 -0
- package/src/__mocks__/types.ts +1 -0
- package/src/__tests__/before-send.test.ts +39 -87
- package/src/__tests__/build-allowed-tracing-urls.test.ts +120 -0
- package/src/__tests__/build-before-send.test.ts +193 -0
- package/src/__tests__/create-impl.test.ts +211 -0
- package/src/__tests__/datadog-provider.test.tsx +55 -25
- package/src/__tests__/enrich-event.test.ts +34 -0
- package/src/__tests__/get-shared.test.ts +56 -0
- package/src/__tests__/handle-resource.test.ts +76 -0
- package/src/__tests__/init.test.ts +69 -0
- package/src/__tests__/integration.test.ts +147 -0
- package/src/__tests__/set-user.test.ts +61 -0
- package/src/__tests__/use-rum-service.test.tsx +82 -0
- package/src/before-send.ts +15 -63
- package/src/build-allowed-tracing-urls.ts +53 -0
- package/src/build-before-send.ts +81 -0
- package/src/bundle-state.ts +9 -0
- package/src/create-impl.ts +71 -0
- package/src/datadog-provider.tsx +4 -6
- package/src/enrich-event.ts +38 -0
- package/src/get-shared.ts +24 -0
- package/src/handle-resource.ts +30 -0
- package/src/index.ts +3 -0
- package/src/init.ts +39 -0
- package/src/set-user.ts +25 -0
- package/src/shared-impl.ts +9 -0
- package/src/types.ts +61 -0
- package/src/use-rum-service.ts +28 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import type { RumEvent, RumEventDomainContext } from '@datadog/browser-rum';
|
|
2
|
+
import { type Mutable } from '../__mocks__';
|
|
3
|
+
import { buildBeforeSend } from '../build-before-send';
|
|
4
|
+
import { enrichEvent } from '../enrich-event';
|
|
5
|
+
import { handleResource } from '../handle-resource';
|
|
6
|
+
import type { State } from '../types';
|
|
7
|
+
|
|
8
|
+
jest.mock('../enrich-event');
|
|
9
|
+
jest.mock('../handle-resource');
|
|
10
|
+
|
|
11
|
+
describe(`[datadog-rum] ${buildBeforeSend.name}`, () => {
|
|
12
|
+
const service = 'task-hub';
|
|
13
|
+
|
|
14
|
+
let state: State;
|
|
15
|
+
let event: Mutable<RumEvent>;
|
|
16
|
+
let context: RumEventDomainContext;
|
|
17
|
+
let serviceHandler: jest.Mock;
|
|
18
|
+
let applicationBeforeSend: jest.Mock;
|
|
19
|
+
|
|
20
|
+
function makeEvent(overrides: Record<string, unknown> = {}) {
|
|
21
|
+
return { type: 'error', service, ...overrides } as unknown as Mutable<RumEvent>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
jest.clearAllMocks();
|
|
26
|
+
jest.mocked(enrichEvent).mockImplementation(jest.fn());
|
|
27
|
+
jest.mocked(handleResource).mockImplementation(jest.fn());
|
|
28
|
+
serviceHandler = jest.fn(() => {});
|
|
29
|
+
applicationBeforeSend = jest.fn(() => true);
|
|
30
|
+
state = { services: new Map(), applicationBeforeSend };
|
|
31
|
+
state.services.set(service, { beforeSend: serviceHandler });
|
|
32
|
+
event = makeEvent();
|
|
33
|
+
context = {};
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const subject = () => buildBeforeSend(state)(event as RumEvent, context);
|
|
37
|
+
|
|
38
|
+
function itReturns(value: boolean) {
|
|
39
|
+
test(`returns ${value}`, () => {
|
|
40
|
+
expect(subject()).toBe(value);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function itCallsServiceHandler() {
|
|
45
|
+
test('calls service handler', () => {
|
|
46
|
+
subject();
|
|
47
|
+
|
|
48
|
+
expect(serviceHandler).toHaveBeenCalledWith(event, context);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function itDoesNotCallServiceHandler() {
|
|
53
|
+
test('does not call service handler', () => {
|
|
54
|
+
subject();
|
|
55
|
+
|
|
56
|
+
expect(serviceHandler).not.toHaveBeenCalled();
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
test('enriches the event', () => {
|
|
61
|
+
subject();
|
|
62
|
+
|
|
63
|
+
expect(enrichEvent).toHaveBeenCalledWith(event, context);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('calls application handler', () => {
|
|
67
|
+
subject();
|
|
68
|
+
|
|
69
|
+
expect(applicationBeforeSend).toHaveBeenCalledWith(event, context);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
itCallsServiceHandler();
|
|
73
|
+
|
|
74
|
+
itReturns(true);
|
|
75
|
+
|
|
76
|
+
describe('when enrichment throws', () => {
|
|
77
|
+
beforeEach(() => {
|
|
78
|
+
jest.mocked(enrichEvent).mockImplementation(() => {
|
|
79
|
+
throw new Error('boom');
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
itReturns(true);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('when application handler throws', () => {
|
|
87
|
+
beforeEach(() => {
|
|
88
|
+
jest.mocked(applicationBeforeSend).mockImplementation(() => {
|
|
89
|
+
throw new Error('Nope!');
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
itCallsServiceHandler();
|
|
94
|
+
|
|
95
|
+
itReturns(true);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('when service handler throws', () => {
|
|
99
|
+
beforeEach(() => {
|
|
100
|
+
jest.mocked(serviceHandler).mockImplementation(() => {
|
|
101
|
+
throw new Error('Nope!');
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
itReturns(true);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('does not call handleResource', () => {
|
|
109
|
+
subject();
|
|
110
|
+
|
|
111
|
+
expect(handleResource).not.toHaveBeenCalled();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('when application handler returns false', () => {
|
|
115
|
+
beforeEach(() => jest.mocked(applicationBeforeSend).mockReturnValue(false));
|
|
116
|
+
|
|
117
|
+
itReturns(false);
|
|
118
|
+
|
|
119
|
+
itDoesNotCallServiceHandler();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('when service handler returns false', () => {
|
|
123
|
+
beforeEach(() => jest.mocked(serviceHandler).mockReturnValue(false));
|
|
124
|
+
|
|
125
|
+
itReturns(true);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('when event.service does not match', () => {
|
|
129
|
+
beforeEach(() => (event.service = 'unknown'));
|
|
130
|
+
|
|
131
|
+
itDoesNotCallServiceHandler();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe('when event.service is undefined', () => {
|
|
135
|
+
beforeEach(() => delete event.service);
|
|
136
|
+
|
|
137
|
+
itDoesNotCallServiceHandler();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe('with a service context', () => {
|
|
141
|
+
const serviceContext = { orgId: '42', env: 'prod' };
|
|
142
|
+
|
|
143
|
+
beforeEach(() => (state.services.get(service)!.context = serviceContext));
|
|
144
|
+
|
|
145
|
+
test('adds context to event', () => {
|
|
146
|
+
subject();
|
|
147
|
+
|
|
148
|
+
expect(event.context).toEqual(serviceContext);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
describe('when event.service does not match', () => {
|
|
152
|
+
beforeEach(() => (event.service = 'unknown'));
|
|
153
|
+
|
|
154
|
+
test('does not add context', () => {
|
|
155
|
+
subject();
|
|
156
|
+
|
|
157
|
+
expect(event.context).toBeUndefined();
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('with pre-existing event.context', () => {
|
|
162
|
+
const eventContext = { foo: 'bar' };
|
|
163
|
+
|
|
164
|
+
beforeEach(() => (event.context = eventContext));
|
|
165
|
+
|
|
166
|
+
test('merges service and event contexts', () => {
|
|
167
|
+
subject();
|
|
168
|
+
|
|
169
|
+
expect(event.context).toEqual({ ...eventContext, ...serviceContext });
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe('with a "resource" event', () => {
|
|
175
|
+
beforeEach(() => (event = makeEvent({ type: 'resource' })));
|
|
176
|
+
|
|
177
|
+
test('calls handleResource with the event and context', () => {
|
|
178
|
+
subject();
|
|
179
|
+
|
|
180
|
+
expect(handleResource).toHaveBeenCalledWith(event, context);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
describe('when handleResource throws', () => {
|
|
184
|
+
beforeEach(() => {
|
|
185
|
+
jest.mocked(handleResource).mockImplementation(() => {
|
|
186
|
+
throw new Error('Nope!');
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
itReturns(true);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
});
|
|
@@ -0,0 +1,211 @@
|
|
|
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 type { SharedImpl } from '../shared-impl';
|
|
7
|
+
import type { InitRumOptions, State } from '../types';
|
|
8
|
+
|
|
9
|
+
jest.mock('@datadog/browser-rum');
|
|
10
|
+
jest.mock('../build-before-send');
|
|
11
|
+
jest.mock('../build-allowed-tracing-urls');
|
|
12
|
+
|
|
13
|
+
describe(`[datadog-rum] ${createImpl.name}`, () => {
|
|
14
|
+
const mockBeforeSend = jest.fn();
|
|
15
|
+
const mockAllowedTracingUrls = jest.fn();
|
|
16
|
+
let impl: SharedImpl;
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
jest.clearAllMocks();
|
|
20
|
+
jest.mocked(datadogRum.getInitConfiguration).mockReturnValue(undefined);
|
|
21
|
+
jest.mocked(buildBeforeSend).mockReturnValue(mockBeforeSend);
|
|
22
|
+
jest.mocked(buildAllowedTracingUrls).mockReturnValue(mockAllowedTracingUrls as any);
|
|
23
|
+
impl = createImpl();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/*
|
|
27
|
+
* createImpl passes the same live state object to both collaborators, so the
|
|
28
|
+
* captured argument reflects later mutations from init/setServiceContext/cleanup.
|
|
29
|
+
*/
|
|
30
|
+
const capturedState = () => jest.mocked(buildBeforeSend).mock.calls[0][0] as State;
|
|
31
|
+
|
|
32
|
+
describe('init', () => {
|
|
33
|
+
let options: InitRumOptions;
|
|
34
|
+
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
options = { service: { name: 'desktop' }, application: { ...minimalApplication } };
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const subject = () => impl.init(options);
|
|
40
|
+
|
|
41
|
+
test('calls datadogRum.init', () => {
|
|
42
|
+
subject();
|
|
43
|
+
|
|
44
|
+
expect(datadogRum.init).toHaveBeenCalledWith(
|
|
45
|
+
expect.objectContaining({
|
|
46
|
+
...options.application,
|
|
47
|
+
sessionSampleRate: 100,
|
|
48
|
+
sessionReplaySampleRate: 0,
|
|
49
|
+
defaultPrivacyLevel: 'mask-user-input',
|
|
50
|
+
service: options.service.name,
|
|
51
|
+
beforeSend: mockBeforeSend,
|
|
52
|
+
allowedTracingUrls: mockAllowedTracingUrls,
|
|
53
|
+
})
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('reports it initialized the SDK', () => {
|
|
58
|
+
expect(subject().initializedSdk).toBe(true);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('registers the service', () => {
|
|
62
|
+
subject();
|
|
63
|
+
|
|
64
|
+
expect(capturedState().services.has(options.service.name)).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe('when config overrides defaults', () => {
|
|
68
|
+
const overrides = {
|
|
69
|
+
sessionSampleRate: 25,
|
|
70
|
+
sessionReplaySampleRate: 10,
|
|
71
|
+
defaultPrivacyLevel: 'allow',
|
|
72
|
+
} as const;
|
|
73
|
+
|
|
74
|
+
beforeEach(() => {
|
|
75
|
+
options.application = { ...minimalApplication, ...overrides };
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('calls datadogRum.init with overridden values', () => {
|
|
79
|
+
subject();
|
|
80
|
+
|
|
81
|
+
expect(datadogRum.init).toHaveBeenCalledWith(expect.objectContaining(overrides));
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('when config contains extra values', () => {
|
|
86
|
+
const excludedActivityUrls = [/pendo\.io/];
|
|
87
|
+
|
|
88
|
+
beforeEach(() => {
|
|
89
|
+
options.application = { ...minimalApplication, excludedActivityUrls };
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('calls datadogRum.init with extra values', () => {
|
|
93
|
+
subject();
|
|
94
|
+
|
|
95
|
+
expect(datadogRum.init).toHaveBeenCalledWith(
|
|
96
|
+
expect.objectContaining({ excludedActivityUrls })
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('with an application beforeSend handler', () => {
|
|
102
|
+
const appBeforeSend = jest.fn();
|
|
103
|
+
|
|
104
|
+
beforeEach(() => {
|
|
105
|
+
options.application = { ...minimalApplication, beforeSend: appBeforeSend };
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('registers the handler', () => {
|
|
109
|
+
subject();
|
|
110
|
+
|
|
111
|
+
expect(capturedState().applicationBeforeSend).toBe(appBeforeSend);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
function itDoesNotCallDatadogRumInit() {
|
|
116
|
+
test('does not call datadogRum.init', () => {
|
|
117
|
+
subject();
|
|
118
|
+
|
|
119
|
+
expect(datadogRum.init).not.toHaveBeenCalled();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test('reports it did not initialize the SDK', () => {
|
|
123
|
+
expect(subject().initializedSdk).toBe(false);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
describe('when already called', () => {
|
|
128
|
+
beforeEach(() => {
|
|
129
|
+
subject();
|
|
130
|
+
jest.clearAllMocks();
|
|
131
|
+
jest.spyOn(console, 'info').mockImplementation(() => {});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
itDoesNotCallDatadogRumInit();
|
|
135
|
+
|
|
136
|
+
test('logs an info message', () => {
|
|
137
|
+
const infoSpy = jest.spyOn(console, 'info');
|
|
138
|
+
|
|
139
|
+
subject();
|
|
140
|
+
|
|
141
|
+
expect(infoSpy).toHaveBeenCalledWith(
|
|
142
|
+
expect.stringContaining('application config ignored')
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe('when datadogRum.init was already called directly', () => {
|
|
148
|
+
beforeEach(() => {
|
|
149
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
150
|
+
jest.mocked(datadogRum.getInitConfiguration).mockReturnValue({} as any);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
itDoesNotCallDatadogRumInit();
|
|
154
|
+
|
|
155
|
+
test('warns the SDK was initialized outside the facade', () => {
|
|
156
|
+
const warnSpy = jest.spyOn(console, 'warn');
|
|
157
|
+
|
|
158
|
+
subject();
|
|
159
|
+
|
|
160
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
161
|
+
expect.stringContaining('SDK was initialized outside the facade')
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe('with service only (no application)', () => {
|
|
167
|
+
beforeEach(() => {
|
|
168
|
+
delete options.application;
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
itDoesNotCallDatadogRumInit();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe('cleanup', () => {
|
|
175
|
+
beforeEach(() => {
|
|
176
|
+
subject().cleanup();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('unregisters the service', () => {
|
|
180
|
+
expect(capturedState().services.has(options.service.name)).toBe(false);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
describe('setServiceContext', () => {
|
|
186
|
+
const name = 'task-hub';
|
|
187
|
+
const context = { orgId: '42' };
|
|
188
|
+
|
|
189
|
+
beforeEach(() => {
|
|
190
|
+
impl.init({ service: { name }, application: minimalApplication });
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const subject = () => impl.setServiceContext(name, context);
|
|
194
|
+
|
|
195
|
+
test('registers service context', () => {
|
|
196
|
+
subject();
|
|
197
|
+
|
|
198
|
+
expect(capturedState().services.get(name)?.context).toEqual(context);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe('when already called', () => {
|
|
202
|
+
beforeEach(() => subject());
|
|
203
|
+
|
|
204
|
+
test('init() preserves context', () => {
|
|
205
|
+
impl.init({ service: { name, beforeSend: jest.fn() } });
|
|
206
|
+
|
|
207
|
+
expect(capturedState().services.get(name)?.context).toEqual(context);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
});
|
|
@@ -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
|
-
|
|
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<
|
|
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
|
-
|
|
38
|
-
isEnabled = true;
|
|
39
|
-
options = {};
|
|
40
|
-
jest.clearAllMocks();
|
|
41
|
-
});
|
|
42
|
-
test('initializes user', () => {
|
|
51
|
+
test('initializes the user on mount', () => {
|
|
43
52
|
subject();
|
|
44
|
-
|
|
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(
|
|
63
|
+
setUser(nextUser);
|
|
52
64
|
});
|
|
53
65
|
});
|
|
54
66
|
|
|
55
|
-
test('updates user', () => {
|
|
56
|
-
expect(datadogRum.setUser).toHaveBeenCalledWith(
|
|
67
|
+
test('updates the SDK user', () => {
|
|
68
|
+
expect(datadogRum.setUser).toHaveBeenCalledWith(nextUser);
|
|
57
69
|
});
|
|
58
70
|
|
|
59
|
-
describe('
|
|
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
|
-
|
|
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
|
-
|
|
85
|
-
|
|
96
|
+
describe('without options.hashRouting', () => {
|
|
97
|
+
test('does not render <HashRouting />', async () => {
|
|
98
|
+
subject();
|
|
86
99
|
|
|
87
|
-
|
|
100
|
+
await waitFor(() =>
|
|
101
|
+
expect(screen.queryByText('<HashRouting />')).not.toBeInTheDocument()
|
|
102
|
+
);
|
|
103
|
+
});
|
|
88
104
|
});
|
|
89
105
|
|
|
90
|
-
describe('
|
|
106
|
+
describe('with options.hashRouting=true', () => {
|
|
91
107
|
beforeEach(() => {
|
|
92
|
-
options =
|
|
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
|
+
});
|