@datadog/mobile-react-native 1.0.0-beta4 → 1.0.0-rc4

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 (103) hide show
  1. package/DatadogSDKReactNative.podspec +1 -1
  2. package/README.md +73 -1
  3. package/android/build.gradle +2 -3
  4. package/android/src/main/java/com/datadog/reactnative/DdRum.kt +25 -24
  5. package/android/src/main/java/com/datadog/reactnative/DdTrace.kt +6 -6
  6. package/ios/Sources/DdRum.m +10 -9
  7. package/ios/Sources/DdRum.swift +24 -24
  8. package/ios/Sources/DdTrace.m +2 -2
  9. package/ios/Sources/DdTrace.swift +6 -6
  10. package/ios/Sources/QueueHolder.swift +1 -1
  11. package/lib/commonjs/DdSdkReactNative.js +57 -0
  12. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  13. package/lib/commonjs/DdSdkReactNativeConfiguration.js +8 -0
  14. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  15. package/lib/commonjs/NativeSdkVerbosity.js +26 -0
  16. package/lib/commonjs/NativeSdkVerbosity.js.map +1 -0
  17. package/lib/commonjs/ProxyConfiguration.js +53 -0
  18. package/lib/commonjs/ProxyConfiguration.js.map +1 -0
  19. package/lib/commonjs/Timer.js +99 -0
  20. package/lib/commonjs/Timer.js.map +1 -0
  21. package/lib/commonjs/TrackingConsent.js +6 -0
  22. package/lib/commonjs/TrackingConsent.js.map +1 -1
  23. package/lib/commonjs/foundation.js +88 -8
  24. package/lib/commonjs/foundation.js.map +1 -1
  25. package/lib/commonjs/index.js +14 -0
  26. package/lib/commonjs/index.js.map +1 -1
  27. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +51 -11
  28. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  29. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +38 -6
  30. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  31. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +103 -18
  32. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  33. package/lib/commonjs/types.js.map +1 -1
  34. package/lib/commonjs/version.js +10 -0
  35. package/lib/commonjs/version.js.map +1 -0
  36. package/lib/module/DdSdkReactNative.js +56 -1
  37. package/lib/module/DdSdkReactNative.js.map +1 -1
  38. package/lib/module/DdSdkReactNativeConfiguration.js +9 -1
  39. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  40. package/lib/module/NativeSdkVerbosity.js +18 -0
  41. package/lib/module/NativeSdkVerbosity.js.map +1 -0
  42. package/lib/module/ProxyConfiguration.js +46 -0
  43. package/lib/module/ProxyConfiguration.js.map +1 -0
  44. package/lib/module/Timer.js +90 -0
  45. package/lib/module/Timer.js.map +1 -0
  46. package/lib/module/TrackingConsent.js +6 -0
  47. package/lib/module/TrackingConsent.js.map +1 -1
  48. package/lib/module/foundation.js +89 -3
  49. package/lib/module/foundation.js.map +1 -1
  50. package/lib/module/index.js +2 -1
  51. package/lib/module/index.js.map +1 -1
  52. package/lib/module/rum/instrumentation/DdEventsInterceptor.js +51 -11
  53. package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  54. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +37 -6
  55. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  56. package/lib/module/rum/instrumentation/DdRumResourceTracking.js +96 -18
  57. package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  58. package/lib/module/types.js.map +1 -1
  59. package/lib/module/version.js +3 -0
  60. package/lib/module/version.js.map +1 -0
  61. package/lib/typescript/DdSdkReactNative.d.ts +11 -0
  62. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +6 -0
  63. package/lib/typescript/NativeSdkVerbosity.d.ts +9 -0
  64. package/lib/typescript/ProxyConfiguration.d.ts +56 -0
  65. package/lib/typescript/Timer.d.ts +20 -0
  66. package/lib/typescript/index.d.ts +2 -1
  67. package/lib/typescript/rum/instrumentation/DdEventsInterceptor.d.ts +2 -0
  68. package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +2 -0
  69. package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +4 -0
  70. package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +2 -2
  71. package/lib/typescript/types.d.ts +25 -24
  72. package/lib/typescript/version.d.ts +1 -0
  73. package/package.json +12 -13
  74. package/src/DdSdkReactNative.tsx +47 -0
  75. package/src/DdSdkReactNativeConfiguration.tsx +7 -1
  76. package/src/NativeSdkVerbosity.tsx +15 -0
  77. package/src/ProxyConfiguration.tsx +37 -0
  78. package/src/Timer.ts +96 -0
  79. package/src/TrackingConsent.tsx +6 -0
  80. package/src/__tests__/DdRumWrapper.test.tsx +91 -0
  81. package/src/__tests__/DdSdkReactNative.test.tsx +541 -0
  82. package/src/__tests__/Timer.test.ts +145 -0
  83. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +222 -0
  84. package/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx +314 -0
  85. package/src/__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx +677 -0
  86. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +188 -0
  87. package/src/__tests__/rum/instrumentation/TraceIdentifier.test.tsx +33 -0
  88. package/src/foundation.tsx +83 -3
  89. package/src/index.tsx +3 -2
  90. package/src/rum/instrumentation/DdEventsInterceptor.tsx +40 -10
  91. package/src/rum/instrumentation/DdRumErrorTracking.tsx +56 -25
  92. package/src/rum/instrumentation/DdRumResourceTracking.tsx +92 -22
  93. package/src/rum/instrumentation/DdRumXhr.tsx +6 -5
  94. package/src/types.tsx +25 -24
  95. package/src/version.ts +2 -0
  96. package/android/.project +0 -17
  97. package/android/.settings/org.eclipse.buildship.core.prefs +0 -13
  98. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  99. package/android/gradle/wrapper/gradle-wrapper.properties +0 -6
  100. package/android/gradlew +0 -185
  101. package/android/gradlew.bat +0 -104
  102. package/ios/DdSdkReactNative.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  103. package/ios/Tests/ModulesTest.swift +0 -43
@@ -0,0 +1,677 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import {
8
+ DdRumResourceTracking,
9
+ PARENT_ID_HEADER_KEY,
10
+ TRACE_ID_HEADER_KEY,
11
+ ORIGIN_RUM,
12
+ ORIGIN_HEADER_KEY,
13
+ SAMPLING_PRIORITY_HEADER_KEY,
14
+ SAMPLED_HEADER_KEY,
15
+ calculateResponseSize
16
+ } from '../../../rum/instrumentation/DdRumResourceTracking'
17
+ import { DdRum } from '../../../index';
18
+ import { Platform } from 'react-native';
19
+
20
+ jest.useFakeTimers()
21
+
22
+ jest.mock('../../../foundation', () => {
23
+ return {
24
+ DdRum: {
25
+ startResource: jest.fn().mockResolvedValue(() => {
26
+ return Promise.resolve()
27
+ }),
28
+
29
+ stopResource: jest.fn().mockResolvedValue(() => {
30
+ return Promise.resolve()
31
+ })
32
+ },
33
+ };
34
+ });
35
+
36
+ function randomInt(max: number): number {
37
+ return Math.floor(Math.random() * max)
38
+ }
39
+
40
+ class XMLHttpRequest {
41
+
42
+ static readonly UNSENT = 0;
43
+ static readonly OPENED = 1;
44
+ static readonly HEADERS_RECEIVED = 2;
45
+ static readonly LOADING = 3;
46
+ static readonly DONE = 4;
47
+
48
+ public response?: any;
49
+ public responseType?: string;
50
+ public status: number = 0;
51
+ public readyState: number = XMLHttpRequest.UNSENT;
52
+ public requestHeaders: Map<String, String> = new Map();
53
+ public responseHeaders: Map<String, String> = new Map();
54
+
55
+ constructor() {
56
+
57
+ }
58
+
59
+ public originalOpenCalled: boolean = false
60
+ public originalSendCalled: boolean = false
61
+ public originalOnReadyStateChangeCalled: boolean = false
62
+
63
+ open(method: string, url: string) {
64
+ this.originalOpenCalled = true;
65
+ }
66
+ send() {
67
+ this.originalSendCalled = true;
68
+ }
69
+ onreadystatechange() {
70
+ this.originalOnReadyStateChangeCalled = true
71
+ }
72
+
73
+ abort() {
74
+ this.status = 0
75
+ }
76
+
77
+ notifyResponseArrived() {
78
+ this.readyState = XMLHttpRequest.HEADERS_RECEIVED
79
+ this.onreadystatechange()
80
+ }
81
+
82
+ complete(status: number, response?: any, responseType?: string) {
83
+ this.response = response
84
+ if (response) {
85
+ this.responseType = responseType ?? 'text'
86
+ }
87
+ this.status = status
88
+ this.readyState = XMLHttpRequest.DONE
89
+ this.onreadystatechange()
90
+ }
91
+
92
+ setRequestHeader(header: string, value: string): void {
93
+ this.requestHeaders[header] = value
94
+ }
95
+
96
+ setResponseHeader(header: string, value: string): void {
97
+ this.responseHeaders[header] = value
98
+ }
99
+
100
+ getResponseHeader(header: string): string | null {
101
+ return this.responseHeaders[header];
102
+ }
103
+ }
104
+
105
+ const flushPromises = () => new Promise(setImmediate);
106
+
107
+ beforeEach(() => {
108
+ DdRum.startResource.mockClear();
109
+ DdRum.stopResource.mockClear();
110
+
111
+ // we need this because with ms precision between Date.now() calls we can get 0, so we advance
112
+ // it manually with each call
113
+ let now = Date.now()
114
+ jest.spyOn(Date, 'now').mockImplementation(() => {
115
+ now += 5
116
+ return now
117
+ })
118
+
119
+ jest.setTimeout(20000);
120
+ })
121
+
122
+ afterEach(() => {
123
+ DdRumResourceTracking.stopTracking();
124
+ Date.now.mockClear();
125
+ })
126
+
127
+
128
+ it('M intercept XHR request W startTracking() + XHR.open() + XHR.send()', async () => {
129
+ // GIVEN
130
+ let method = "GET"
131
+ let url = "https://api.example.com/v2/user"
132
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
133
+
134
+ // WHEN
135
+ const xhr = new XMLHttpRequest();
136
+ xhr.open(method, url);
137
+ xhr.send();
138
+ xhr.notifyResponseArrived();
139
+ xhr.complete(200, 'ok');
140
+ await flushPromises();
141
+
142
+ // THEN
143
+ expect(DdRum.startResource.mock.calls.length).toBe(1);
144
+ expect(DdRum.startResource.mock.calls[0][1]).toBe(method);
145
+ expect(DdRum.startResource.mock.calls[0][2]).toBe(url);
146
+
147
+ expect(DdRum.stopResource.mock.calls.length).toBe(1);
148
+ expect(DdRum.stopResource.mock.calls[0][0]).toBe(DdRum.startResource.mock.calls[0][0]);
149
+ expect(DdRum.stopResource.mock.calls[0][1]).toBe(200);
150
+ expect(DdRum.stopResource.mock.calls[0][2]).toBe('xhr');
151
+ expect(DdRum.stopResource.mock.calls[0][3]).toBeGreaterThan(0);
152
+
153
+ expect(xhr.originalOpenCalled).toBe(true);
154
+ expect(xhr.originalSendCalled).toBe(true);
155
+ expect(xhr.originalOnReadyStateChangeCalled).toBe(true);
156
+ })
157
+
158
+ it('M intercept failing XHR request W startTracking() + XHR.open() + XHR.send()', async () => {
159
+ // GIVEN
160
+ let method = "GET"
161
+ let url = "https://api.example.com/v2/user"
162
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
163
+
164
+ // WHEN
165
+ const xhr = new XMLHttpRequest();
166
+ xhr.open(method, url);
167
+ xhr.send();
168
+ xhr.notifyResponseArrived();
169
+ xhr.complete(500, 'error');
170
+ await flushPromises();
171
+
172
+ // THEN
173
+ expect(DdRum.startResource.mock.calls.length).toBe(1);
174
+ expect(DdRum.startResource.mock.calls[0][1]).toBe(method);
175
+ expect(DdRum.startResource.mock.calls[0][2]).toBe(url);
176
+
177
+ expect(DdRum.stopResource.mock.calls.length).toBe(1);
178
+ expect(DdRum.stopResource.mock.calls[0][0]).toBe(DdRum.startResource.mock.calls[0][0]);
179
+ expect(DdRum.stopResource.mock.calls[0][1]).toBe(500);
180
+ expect(DdRum.stopResource.mock.calls[0][2]).toBe('xhr');
181
+ expect(DdRum.stopResource.mock.calls[0][3]).toBeGreaterThan(0);
182
+
183
+ expect(xhr.originalOpenCalled).toBe(true);
184
+ expect(xhr.originalSendCalled).toBe(true);
185
+ expect(xhr.originalOnReadyStateChangeCalled).toBe(true);
186
+ })
187
+
188
+ it('M intercept aborted XHR request W startTracking() + XHR.open() + XHR.send() + XHR.abort()', async () => {
189
+ // GIVEN
190
+ let method = "GET"
191
+ let url = "https://api.example.com/v2/user"
192
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
193
+
194
+ // WHEN
195
+ const xhr = new XMLHttpRequest();
196
+ xhr.open(method, url);
197
+ xhr.send();
198
+ xhr.abort();
199
+ xhr.complete(0, undefined);
200
+ await flushPromises();
201
+
202
+ // THEN
203
+ expect(DdRum.startResource.mock.calls.length).toBe(1);
204
+ expect(DdRum.startResource.mock.calls[0][1]).toBe(method);
205
+ expect(DdRum.startResource.mock.calls[0][2]).toBe(url);
206
+
207
+ expect(DdRum.stopResource.mock.calls.length).toBe(1);
208
+ expect(DdRum.stopResource.mock.calls[0][0]).toBe(DdRum.startResource.mock.calls[0][0]);
209
+ expect(DdRum.stopResource.mock.calls[0][1]).toBe(0);
210
+ expect(DdRum.stopResource.mock.calls[0][2]).toBe('xhr');
211
+ expect(DdRum.stopResource.mock.calls[0][3]).toBe(-1);
212
+
213
+ expect(xhr.originalOpenCalled).toBe(true);
214
+ expect(xhr.originalSendCalled).toBe(true);
215
+ expect(xhr.originalOnReadyStateChangeCalled).toBe(true);
216
+ })
217
+
218
+
219
+ it('M add the span id in the request headers W startTracking() + XHR.open() + XHR.send()', async () => {
220
+ // GIVEN
221
+ let method = "GET"
222
+ let url = "https://api.example.com/v2/user"
223
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
224
+
225
+ // WHEN
226
+ const xhr = new XMLHttpRequest();
227
+ xhr.open(method, url);
228
+ xhr.send();
229
+ xhr.notifyResponseArrived();
230
+ xhr.complete(200, 'ok');
231
+ await flushPromises();
232
+
233
+ // THEN
234
+ const spanId = xhr.requestHeaders[PARENT_ID_HEADER_KEY];
235
+ expect(spanId).toBeDefined();
236
+ expect(spanId).toMatch(/[1-9].+/);
237
+ })
238
+
239
+ it('M add the trace id in the request headers W startTracking() + XHR.open() + XHR.send()', async () => {
240
+ // GIVEN
241
+ let method = "GET"
242
+ let url = "https://api.example.com/v2/user"
243
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
244
+
245
+ // WHEN
246
+ const xhr = new XMLHttpRequest();
247
+ xhr.open(method, url);
248
+ xhr.send();
249
+ xhr.notifyResponseArrived();
250
+ xhr.complete(200, 'ok');
251
+ await flushPromises();
252
+
253
+ // THEN
254
+ const traceId = xhr.requestHeaders[TRACE_ID_HEADER_KEY];
255
+ expect(traceId).toBeDefined();
256
+ expect(traceId).toMatch(/[1-9].+/);
257
+ })
258
+
259
+ it('M generate different ids for spanId and traceId in request headers', async () => {
260
+ // GIVEN
261
+ let method = "GET"
262
+ let url = "https://api.example.com/v2/user"
263
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
264
+
265
+ // WHEN
266
+ const xhr = new XMLHttpRequest();
267
+ xhr.open(method, url);
268
+ xhr.send();
269
+ xhr.notifyResponseArrived();
270
+ xhr.complete(200, 'ok');
271
+ await flushPromises();
272
+
273
+ // THEN
274
+ const traceId = xhr.requestHeaders[TRACE_ID_HEADER_KEY];
275
+ const spanId = xhr.requestHeaders[PARENT_ID_HEADER_KEY];
276
+ expect(traceId !== spanId).toBeTruthy()
277
+ })
278
+
279
+ it('M add origin as RUM in the request headers W startTracking() + XHR.open() + XHR.send()', async () => {
280
+ // GIVEN
281
+ let method = "GET"
282
+ let url = "https://api.example.com/v2/user"
283
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
284
+
285
+ // WHEN
286
+ const xhr = new XMLHttpRequest();
287
+ xhr.open(method, url);
288
+ xhr.send();
289
+ xhr.notifyResponseArrived();
290
+ xhr.complete(200, 'ok');
291
+ await flushPromises();
292
+
293
+ // THEN
294
+ expect(xhr.requestHeaders[ORIGIN_HEADER_KEY]).toBe(ORIGIN_RUM)
295
+ })
296
+
297
+ it('M force the agent to keep the request generated trace W startTracking() + XHR.open() + XHR.send()', async () => {
298
+ // GIVEN
299
+ let method = "GET"
300
+ let url = "https://api.example.com/v2/user"
301
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
302
+
303
+ // WHEN
304
+ const xhr = new XMLHttpRequest();
305
+ xhr.open(method, url);
306
+ xhr.send();
307
+ xhr.notifyResponseArrived();
308
+ xhr.complete(200, 'ok');
309
+ await flushPromises();
310
+
311
+ // THEN
312
+ expect(xhr.requestHeaders[SAMPLING_PRIORITY_HEADER_KEY]).toBe("1")
313
+ })
314
+
315
+ it('M mark the request generated trace for sampling W startTracking() + XHR.open() + XHR.send()', async () => {
316
+ // GIVEN
317
+ let method = "GET"
318
+ let url = "https://api.example.com/v2/user"
319
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
320
+
321
+ // WHEN
322
+ const xhr = new XMLHttpRequest();
323
+ xhr.open(method, url);
324
+ xhr.send();
325
+ xhr.notifyResponseArrived();
326
+ xhr.complete(200, 'ok');
327
+ await flushPromises();
328
+
329
+ // THEN
330
+ expect(xhr.requestHeaders[SAMPLED_HEADER_KEY]).toBe("1")
331
+ })
332
+
333
+ it('M add the span id as resource attributes W startTracking() + XHR.open() + XHR.send()', async () => {
334
+ // GIVEN
335
+ let method = "GET"
336
+ let url = "https://api.example.com/v2/user"
337
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
338
+
339
+ // WHEN
340
+ const xhr = new XMLHttpRequest();
341
+ xhr.open(method, url);
342
+ xhr.send();
343
+ xhr.notifyResponseArrived();
344
+ xhr.complete(200, 'ok');
345
+ await flushPromises();
346
+
347
+ // THEN
348
+ const spanId = DdRum.startResource.mock.calls[0][3]["_dd.span_id"];
349
+ expect(spanId).toBeDefined();
350
+ expect(spanId).toMatch(/[1-9].+/);
351
+ })
352
+
353
+ it('M add the trace id as resource attributes W startTracking() + XHR.open() + XHR.send()', async () => {
354
+ // GIVEN
355
+ let method = "GET"
356
+ let url = "https://api.example.com/v2/user"
357
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
358
+
359
+ // WHEN
360
+ const xhr = new XMLHttpRequest();
361
+ xhr.open(method, url);
362
+ xhr.send();
363
+ xhr.notifyResponseArrived();
364
+ xhr.complete(200, 'ok');
365
+ await flushPromises();
366
+
367
+ // THEN
368
+ const traceId = DdRum.startResource.mock.calls[0][3]["_dd.trace_id"];
369
+ expect(traceId).toBeDefined();
370
+ expect(traceId).toMatch(/[1-9].+/);
371
+ })
372
+
373
+ it('M generate different ids for spanId and traceId for resource attributes', async () => {
374
+ // GIVEN
375
+ let method = "GET"
376
+ let url = "https://api.example.com/v2/user"
377
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
378
+
379
+ // WHEN
380
+ const xhr = new XMLHttpRequest();
381
+ xhr.open(method, url);
382
+ xhr.send();
383
+ xhr.notifyResponseArrived();
384
+ xhr.complete(200, 'ok');
385
+ await flushPromises();
386
+
387
+ // THEN
388
+ const traceId = DdRum.startResource.mock.calls[0][3]["_dd.trace_id"];
389
+ const spanId = DdRum.startResource.mock.calls[0][3]["_dd.span_id"];
390
+ expect(traceId !== spanId).toBeTruthy();
391
+ })
392
+
393
+ describe.each([['android'], ['ios']])('timings test', (platform) => {
394
+ it(`M generate resource timings W startTracking() + XHR.open() + XHR.send(), platform=${platform}`, async () => {
395
+ // GIVEN
396
+ let method = "GET"
397
+ let url = "https://api.example.com/v2/user"
398
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
399
+
400
+ jest.doMock('react-native/Libraries/Utilities/Platform', () => ({
401
+ OS: platform
402
+ }));
403
+
404
+ // WHEN
405
+ const xhr = new XMLHttpRequest();
406
+ xhr.open(method, url);
407
+ xhr.send();
408
+ xhr.notifyResponseArrived();
409
+ xhr.complete(200, 'ok');
410
+ await flushPromises();
411
+
412
+ // THEN
413
+ const timings = DdRum.stopResource.mock.calls[0][4]["_dd.resource_timings"];
414
+
415
+ if (Platform.OS === 'ios') {
416
+ expect(timings['firstByte']['startTime']).toBeGreaterThan(0)
417
+ } else {
418
+ expect(timings['firstByte']['startTime']).toBe(0)
419
+ }
420
+ expect(timings['firstByte']['duration']).toBeGreaterThan(0)
421
+
422
+ expect(timings['download']['startTime']).toBeGreaterThan(0)
423
+ expect(timings['download']['duration']).toBeGreaterThan(0)
424
+
425
+ if (Platform.OS === 'ios') {
426
+ expect(timings['fetch']['startTime']).toBeGreaterThan(0)
427
+ } else {
428
+ expect(timings['fetch']['startTime']).toBe(0)
429
+ }
430
+ expect(timings['fetch']['duration']).toBeGreaterThan(0)
431
+ })
432
+
433
+ it(`M generate resource timings W startTracking() + XHR.open() + XHR.send() + XHR.abort(), platform=${platform}`, async () => {
434
+ // GIVEN
435
+ let method = "GET"
436
+ let url = "https://api.example.com/v2/user"
437
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
438
+
439
+ jest.doMock('react-native/Libraries/Utilities/Platform', () => ({
440
+ OS: platform
441
+ }));
442
+
443
+ // WHEN
444
+ const xhr = new XMLHttpRequest();
445
+ xhr.open(method, url);
446
+ xhr.send();
447
+ xhr.notifyResponseArrived();
448
+ xhr.abort();
449
+ xhr.complete(0, undefined);
450
+ await flushPromises();
451
+
452
+ // THEN
453
+ const timings = DdRum.stopResource.mock.calls[0][4]["_dd.resource_timings"];
454
+
455
+ if (Platform.OS === 'ios') {
456
+ expect(timings['firstByte']['startTime']).toBeGreaterThan(0)
457
+ } else {
458
+ expect(timings['firstByte']['startTime']).toBe(0)
459
+ }
460
+ expect(timings['firstByte']['duration']).toBeGreaterThan(0)
461
+
462
+ expect(timings['download']['startTime']).toBeGreaterThan(0)
463
+ expect(timings['download']['duration']).toBeGreaterThan(0)
464
+
465
+ if (Platform.OS === 'ios') {
466
+ expect(timings['fetch']['startTime']).toBeGreaterThan(0)
467
+ } else {
468
+ expect(timings['fetch']['startTime']).toBe(0)
469
+ }
470
+ expect(timings['fetch']['duration']).toBeGreaterThan(0)
471
+ })
472
+ })
473
+
474
+ it('M not generate resource timings W startTracking() + XHR.open() + XHR.send() + XHR.abort() before load started', async () => {
475
+ // GIVEN
476
+ let method = "GET"
477
+ let url = "https://api.example.com/v2/user"
478
+ DdRumResourceTracking.startTrackingInternal(XMLHttpRequest);
479
+
480
+ // WHEN
481
+ const xhr = new XMLHttpRequest();
482
+ xhr.open(method, url);
483
+ xhr.send();
484
+ xhr.abort();
485
+ xhr.complete(0, undefined);
486
+ await flushPromises();
487
+
488
+ // THEN
489
+ const attributes = DdRum.stopResource.mock.calls[0][4];
490
+
491
+ expect(attributes['_dd.resource_timings']).toBeNull()
492
+ })
493
+
494
+ describe.each([
495
+ 'blob',
496
+ 'arraybuffer',
497
+ 'text',
498
+ '',
499
+ 'json'
500
+ ].map((responseType) => {
501
+ const xhr = new XMLHttpRequest();
502
+ xhr.readyState = XMLHttpRequest.DONE;
503
+ xhr.responseType = responseType;
504
+ xhr.response = {};
505
+
506
+ const contentLength = randomInt(1_000_000_000)
507
+ xhr.setResponseHeader('Content-Length', contentLength.toString());
508
+ return [xhr, responseType, contentLength]
509
+ }))('Response size from response header', (xhr, responseType, expectedSize) => {
510
+ if (responseType === '') {
511
+ responseType = '_empty_'
512
+ }
513
+ it(`M calculate response size W calculateResponseSize(), responseType=${responseType}`, () => {
514
+ // WHEN
515
+ // @ts-ignore
516
+ const size = calculateResponseSize(xhr);
517
+
518
+ // THEN
519
+ expect(size).toEqual(expectedSize);
520
+ })
521
+ })
522
+
523
+ it('M calculate response size W calculateResponseSize() { responseType=blob }', () => {
524
+ // GIVEN
525
+ const xhr = new XMLHttpRequest();
526
+ xhr.readyState = XMLHttpRequest.DONE;
527
+ xhr.responseType = 'blob';
528
+
529
+ const expectedSize = randomInt(1_000_000)
530
+ xhr.response = { get size() { return expectedSize } }
531
+
532
+ // WHEN
533
+ // @ts-ignore
534
+ const size = calculateResponseSize(xhr);
535
+
536
+ // THEN
537
+ expect(size).toEqual(expectedSize);
538
+ })
539
+
540
+ it('M calculate response size W calculateResponseSize() { responseType=arraybuffer }', () => {
541
+ // GIVEN
542
+ const xhr = new XMLHttpRequest();
543
+ xhr.readyState = XMLHttpRequest.DONE;
544
+ xhr.responseType = 'arraybuffer';
545
+
546
+ const expectedSize = randomInt(100_000);
547
+ xhr.response = new ArrayBuffer(expectedSize);
548
+
549
+ // WHEN
550
+ // @ts-ignore
551
+ const size = calculateResponseSize(xhr);
552
+
553
+ // THEN
554
+ expect(size).toEqual(expectedSize);
555
+ })
556
+
557
+ it('M calculate response size W calculateResponseSize() { responseType=text }', () => {
558
+ // GIVEN
559
+ const xhr = new XMLHttpRequest();
560
+ xhr.readyState = XMLHttpRequest.DONE;
561
+ xhr.responseType = 'text';
562
+
563
+ // size per char is 24, but in bytes it is 33.
564
+ const expectedSize = 33;
565
+ xhr.response = "{\"foo\": \"bar+úñïçôδè ℓ\"}";
566
+
567
+ // WHEN
568
+ // @ts-ignore
569
+ const size = calculateResponseSize(xhr);
570
+
571
+ // THEN
572
+ expect(size).toEqual(expectedSize);
573
+ })
574
+
575
+ it('M calculate response size W calculateResponseSize() { responseType=_empty_ }', () => {
576
+ // GIVEN
577
+ const xhr = new XMLHttpRequest();
578
+ xhr.readyState = XMLHttpRequest.DONE;
579
+ xhr.responseType = '';
580
+
581
+ // size per char is 24, but in bytes it is 33.
582
+ const expectedSize = 33;
583
+ xhr.response = "{\"foo\": \"bar+úñïçôδè ℓ\"}";
584
+
585
+ // WHEN
586
+ // @ts-ignore
587
+ const size = calculateResponseSize(xhr);
588
+
589
+ // THEN
590
+ expect(size).toEqual(expectedSize);
591
+ })
592
+
593
+ it('M calculate response size W calculateResponseSize() { responseType=json }', () => {
594
+ // GIVEN
595
+ const xhr = new XMLHttpRequest();
596
+ xhr.readyState = XMLHttpRequest.DONE;
597
+ xhr.responseType = 'json';
598
+
599
+ const expectedSize = 24;
600
+ xhr.response = { "foo": { "bar": "foobar" } }
601
+
602
+ // WHEN
603
+ // @ts-ignore
604
+ const size = calculateResponseSize(xhr);
605
+
606
+ // THEN
607
+ expect(size).toEqual(expectedSize);
608
+ })
609
+
610
+ it('M not calculate response size W calculateResponseSize() { responseType=document }', () => {
611
+ // GIVEN
612
+ const xhr = new XMLHttpRequest();
613
+ xhr.readyState = XMLHttpRequest.DONE;
614
+ // document type is not supported by RN, so there are no classes to handle it
615
+ xhr.responseType = 'document';
616
+ xhr.response = {};
617
+
618
+ // WHEN
619
+ // @ts-ignore
620
+ const size = calculateResponseSize(xhr);
621
+
622
+ // THEN
623
+ expect(size).toEqual(-1);
624
+ })
625
+
626
+ it('M return 0 W calculateResponseSize() { error is thrown }', () => {
627
+ // GIVEN
628
+ const consoleErrorMock = jest.spyOn(console, 'error').mockImplementation()
629
+ const xhr = new XMLHttpRequest();
630
+ xhr.readyState = XMLHttpRequest.DONE;
631
+ xhr.responseType = 'blob';
632
+ xhr.response = {
633
+ get size() { throw new Error() }
634
+ };
635
+
636
+ // WHEN
637
+ // @ts-ignore
638
+ const size = calculateResponseSize(xhr);
639
+
640
+ // THEN
641
+ expect(size).toEqual(-1);
642
+ expect(consoleErrorMock).toHaveBeenCalled();
643
+
644
+ consoleErrorMock.mockRestore();
645
+ })
646
+
647
+ it('M return 0 W calculateResponseSize() { size is not a number }', () => {
648
+ // GIVEN
649
+ const xhr = new XMLHttpRequest();
650
+ xhr.readyState = XMLHttpRequest.DONE;
651
+ xhr.responseType = 'blob';
652
+
653
+ // we pass empty object, so that .size property is missing, we will get undefined
654
+ xhr.response = {};
655
+
656
+ // WHEN
657
+ // @ts-ignore
658
+ const size = calculateResponseSize(xhr);
659
+
660
+ // THEN
661
+ expect(size).toEqual(-1);
662
+ })
663
+
664
+ it('M return 0 W calculateResponseSize() { no response }', () => {
665
+ // GIVEN
666
+ const xhr = new XMLHttpRequest();
667
+ xhr.readyState = XMLHttpRequest.DONE;
668
+ xhr.responseType = 'blob';
669
+ xhr.response = null;
670
+
671
+ // WHEN
672
+ // @ts-ignore
673
+ const size = calculateResponseSize(xhr);
674
+
675
+ // THEN
676
+ expect(size).toEqual(-1);
677
+ })