@datadog/mobile-react-native 3.2.0 → 3.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.
Files changed (142) hide show
  1. package/DatadogSDKReactNative.podspec +7 -7
  2. package/android/build.gradle +1 -1
  3. package/android/src/main/kotlin/com/datadog/reactnative/DdRumImplementation.kt +5 -0
  4. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkImplementation.kt +9 -0
  5. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkNativeInitialization.kt +13 -6
  6. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkSessionStartedListener.kt +93 -22
  7. package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
  8. package/android/src/newarch/kotlin/com/datadog/reactnative/DdRum.kt +5 -1
  9. package/android/src/oldarch/kotlin/com/datadog/reactnative/DdRum.kt +5 -1
  10. package/android/src/test/kotlin/com/datadog/reactnative/DdRumTest.kt +9 -1
  11. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkNativeInitializationTest.kt +68 -0
  12. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkSessionStartedListenerTest.kt +38 -1
  13. package/ios/Sources/Attributes.swift +25 -0
  14. package/ios/Sources/DdRum.mm +6 -4
  15. package/ios/Sources/DdRumImplementation.swift +82 -7
  16. package/ios/Sources/DdSdkNativeInitialization.swift +24 -17
  17. package/ios/Sources/DdSdkSessionStartedListener.swift +28 -1
  18. package/ios/Sources/RNViewResolution.swift +88 -0
  19. package/ios/Sources/RUMMonitorInternalProtocol.swift +8 -0
  20. package/ios/Sources/SdkVersion.swift +1 -1
  21. package/lib/commonjs/DdAttributes.js +7 -1
  22. package/lib/commonjs/DdAttributes.js.map +1 -1
  23. package/lib/commonjs/DdSdkReactNative.js +16 -3
  24. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  25. package/lib/commonjs/config/DatadogProviderConfiguration.js +3 -1
  26. package/lib/commonjs/config/DatadogProviderConfiguration.js.map +1 -1
  27. package/lib/commonjs/index.js +12 -0
  28. package/lib/commonjs/index.js.map +1 -1
  29. package/lib/commonjs/metro/debugIdResolver.js +5 -3
  30. package/lib/commonjs/metro/debugIdResolver.js.map +1 -1
  31. package/lib/commonjs/metro/plugin/metroSerializer.js +4 -3
  32. package/lib/commonjs/metro/plugin/metroSerializer.js.map +1 -1
  33. package/lib/commonjs/rum/DdRum.js +14 -1
  34. package/lib/commonjs/rum/DdRum.js.map +1 -1
  35. package/lib/commonjs/rum/eventMappers/actionEventMapper.js.map +1 -1
  36. package/lib/commonjs/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.js +2 -2
  37. package/lib/commonjs/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.js.map +1 -1
  38. package/lib/commonjs/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.js +4 -1
  39. package/lib/commonjs/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.js.map +1 -1
  40. package/lib/commonjs/rum/instrumentation/resourceTracking/graphql/graphqlUtils.js +51 -0
  41. package/lib/commonjs/rum/instrumentation/resourceTracking/graphql/graphqlUtils.js.map +1 -0
  42. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js +6 -0
  43. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js.map +1 -1
  44. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.js +35 -1
  45. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.js.map +1 -1
  46. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.js +170 -0
  47. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.js.map +1 -0
  48. package/lib/commonjs/sdk/AttributesEncoding/defaultEncoders.js +6 -1
  49. package/lib/commonjs/sdk/AttributesEncoding/defaultEncoders.js.map +1 -1
  50. package/lib/commonjs/specs/NativeDdRum.js.map +1 -1
  51. package/lib/commonjs/version.js +1 -1
  52. package/lib/module/DdAttributes.js +7 -1
  53. package/lib/module/DdAttributes.js.map +1 -1
  54. package/lib/module/DdSdkReactNative.js +16 -3
  55. package/lib/module/DdSdkReactNative.js.map +1 -1
  56. package/lib/module/config/DatadogProviderConfiguration.js +3 -1
  57. package/lib/module/config/DatadogProviderConfiguration.js.map +1 -1
  58. package/lib/module/index.js +2 -2
  59. package/lib/module/index.js.map +1 -1
  60. package/lib/module/metro/debugIdResolver.js +5 -3
  61. package/lib/module/metro/debugIdResolver.js.map +1 -1
  62. package/lib/module/metro/plugin/metroSerializer.js +4 -3
  63. package/lib/module/metro/plugin/metroSerializer.js.map +1 -1
  64. package/lib/module/rum/DdRum.js +14 -1
  65. package/lib/module/rum/DdRum.js.map +1 -1
  66. package/lib/module/rum/eventMappers/actionEventMapper.js.map +1 -1
  67. package/lib/module/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.js +2 -2
  68. package/lib/module/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.js.map +1 -1
  69. package/lib/module/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.js +3 -2
  70. package/lib/module/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.js.map +1 -1
  71. package/lib/module/rum/instrumentation/resourceTracking/graphql/graphqlUtils.js +45 -0
  72. package/lib/module/rum/instrumentation/resourceTracking/graphql/graphqlUtils.js.map +1 -0
  73. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js +6 -0
  74. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js.map +1 -1
  75. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.js +36 -2
  76. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.js.map +1 -1
  77. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.js +161 -0
  78. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.js.map +1 -0
  79. package/lib/module/sdk/AttributesEncoding/defaultEncoders.js +6 -1
  80. package/lib/module/sdk/AttributesEncoding/defaultEncoders.js.map +1 -1
  81. package/lib/module/specs/NativeDdRum.js.map +1 -1
  82. package/lib/module/version.js +1 -1
  83. package/lib/typescript/DdAttributes.d.ts +6 -0
  84. package/lib/typescript/DdAttributes.d.ts.map +1 -1
  85. package/lib/typescript/DdSdkReactNative.d.ts.map +1 -1
  86. package/lib/typescript/config/DatadogProviderConfiguration.d.ts +3 -1
  87. package/lib/typescript/config/DatadogProviderConfiguration.d.ts.map +1 -1
  88. package/lib/typescript/index.d.ts +2 -2
  89. package/lib/typescript/index.d.ts.map +1 -1
  90. package/lib/typescript/metro/debugIdResolver.d.ts.map +1 -1
  91. package/lib/typescript/metro/plugin/metroSerializer.d.ts +1 -1
  92. package/lib/typescript/metro/plugin/metroSerializer.d.ts.map +1 -1
  93. package/lib/typescript/nativeModulesTypes.d.ts +2 -1
  94. package/lib/typescript/nativeModulesTypes.d.ts.map +1 -1
  95. package/lib/typescript/rum/DdRum.d.ts.map +1 -1
  96. package/lib/typescript/rum/eventMappers/actionEventMapper.d.ts +1 -0
  97. package/lib/typescript/rum/eventMappers/actionEventMapper.d.ts.map +1 -1
  98. package/lib/typescript/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.d.ts.map +1 -1
  99. package/lib/typescript/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.d.ts +2 -0
  100. package/lib/typescript/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.d.ts.map +1 -1
  101. package/lib/typescript/rum/instrumentation/resourceTracking/graphql/graphqlUtils.d.ts +7 -0
  102. package/lib/typescript/rum/instrumentation/resourceTracking/graphql/graphqlUtils.d.ts.map +1 -0
  103. package/lib/typescript/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.d.ts.map +1 -1
  104. package/lib/typescript/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.d.ts +36 -0
  105. package/lib/typescript/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.d.ts.map +1 -0
  106. package/lib/typescript/rum/instrumentation/resourceTracking/requestProxy/interfaces/RumResource.d.ts +11 -0
  107. package/lib/typescript/rum/instrumentation/resourceTracking/requestProxy/interfaces/RumResource.d.ts.map +1 -1
  108. package/lib/typescript/sdk/AttributesEncoding/defaultEncoders.d.ts.map +1 -1
  109. package/lib/typescript/specs/NativeDdRum.d.ts +2 -1
  110. package/lib/typescript/specs/NativeDdRum.d.ts.map +1 -1
  111. package/lib/typescript/version.d.ts +1 -1
  112. package/package.json +2 -2
  113. package/src/DdAttributes.ts +8 -1
  114. package/src/DdSdkReactNative.tsx +18 -2
  115. package/src/__tests__/DdSdkReactNative.test.tsx +1 -0
  116. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +8 -0
  117. package/src/config/DatadogProviderConfiguration.ts +3 -1
  118. package/src/index.tsx +5 -1
  119. package/src/metro/__tests__/metro.test.ts +13 -0
  120. package/src/metro/debugIdResolver.ts +7 -3
  121. package/src/metro/plugin/metroSerializer.ts +11 -3
  122. package/src/nativeModulesTypes.ts +2 -0
  123. package/src/rum/DdRum.ts +25 -0
  124. package/src/rum/__tests__/DdRum.test.ts +33 -0
  125. package/src/rum/eventMappers/actionEventMapper.ts +1 -0
  126. package/src/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.ts +3 -2
  127. package/src/rum/instrumentation/interactionTracking/__tests__/DdBabelInteractionTracking.test.ts +97 -0
  128. package/src/rum/instrumentation/resourceTracking/graphql/__tests__/graphqlUtils.test.ts +143 -0
  129. package/src/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.ts +4 -2
  130. package/src/rum/instrumentation/resourceTracking/graphql/graphqlUtils.ts +61 -0
  131. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.ts +10 -0
  132. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.ts +46 -5
  133. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts +787 -0
  134. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.ts +175 -0
  135. package/src/rum/instrumentation/resourceTracking/requestProxy/interfaces/RumResource.ts +9 -0
  136. package/src/sdk/AttributesEncoding/__tests__/defaultEncoders.test.ts +25 -1
  137. package/src/sdk/AttributesEncoding/defaultEncoders.ts +11 -1
  138. package/src/sdk/DatadogProvider/__tests__/__utils__/renderWithProvider.tsx +24 -7
  139. package/src/sdk/DatadogProvider/__tests__/initialization.test.tsx +19 -15
  140. package/src/sdk/DatadogProvider/__tests__/initializationModes.test.tsx +85 -54
  141. package/src/specs/NativeDdRum.ts +2 -0
  142. package/src/version.ts +1 -1
@@ -0,0 +1,175 @@
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 { InternalLog } from '../../../../../InternalLog';
8
+ import { SdkVerbosity } from '../../../../../config/types';
9
+
10
+ /**
11
+ * Safely parses a JSON string.
12
+ *
13
+ * @param text - JSON string to parse.
14
+ * @returns Parsed value or `null` if parsing fails.
15
+ */
16
+ export function safeJsonParse<T = any>(
17
+ text: string | null | undefined
18
+ ): T | null {
19
+ if (!text) {
20
+ return null;
21
+ }
22
+
23
+ try {
24
+ return JSON.parse(text) as T;
25
+ } catch {
26
+ return null;
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Safely reads `responseText` from an XHR object.
32
+ *
33
+ * @param xhr - XHR-like object.
34
+ * @returns Response text or `null` if unavailable.
35
+ */
36
+ export function safeGetResponseText(xhr: any): string | null {
37
+ // RN can throw if responseType is "blob"
38
+ try {
39
+ return typeof xhr?.responseText === 'string' ? xhr.responseText : null;
40
+ } catch {
41
+ return null;
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Converts a Blob-like object to text.
47
+ *
48
+ * @param blob - Blob or Blob-like value.
49
+ * @returns Text content or `null` if conversion fails.
50
+ */
51
+ export async function blobToText(blob: any): Promise<string | null> {
52
+ if (!blob) {
53
+ return null;
54
+ }
55
+
56
+ // The 'data' field is sometimes available in a blob, use that if available
57
+ const data = blob?._data;
58
+ if (typeof data === 'string') {
59
+ return data;
60
+ }
61
+
62
+ if (typeof blob.text === 'function') {
63
+ try {
64
+ return await blob.text();
65
+ } catch (error) {
66
+ const errorData = getErrorData(error);
67
+ if (errorData) {
68
+ InternalLog.log(
69
+ `Unable to transform blob to text using 'blob.text()': ${errorData}`,
70
+ SdkVerbosity.WARN
71
+ );
72
+ }
73
+ }
74
+ }
75
+
76
+ if (typeof FileReader !== 'undefined') {
77
+ try {
78
+ return await new Promise<string>((resolve, reject) => {
79
+ const reader = new FileReader();
80
+ reader.onload = () => resolve(String(reader.result ?? ''));
81
+ reader.onerror = () => reject(new Error('FileReader error'));
82
+ reader.readAsText(blob);
83
+ });
84
+ } catch (error) {
85
+ const errorData = getErrorData(error);
86
+ if (errorData) {
87
+ InternalLog.log(
88
+ `Unable to transform blob to text using 'FileReader': ${errorData}`,
89
+ SdkVerbosity.WARN
90
+ );
91
+ }
92
+ }
93
+ }
94
+
95
+ return null;
96
+ }
97
+
98
+ /**
99
+ * Reads and parses an XHR response body as JSON.
100
+ *
101
+ * @param xhr - XHR-like object.
102
+ * @returns Parsed JSON or `null` if not readable.
103
+ */
104
+ export async function readXhrJsonBody(xhr: any): Promise<any | null> {
105
+ // If responseType allows it, responseText is the simplest
106
+ if (xhr?.responseType === '' || xhr?.responseType === 'text') {
107
+ const t = safeGetResponseText(xhr);
108
+ const parsed = safeJsonParse(t);
109
+
110
+ if (parsed !== null) {
111
+ return parsed;
112
+ }
113
+ }
114
+
115
+ // Use xhr.response
116
+ const response = xhr?.response;
117
+ if (response == null) {
118
+ return null;
119
+ }
120
+
121
+ // String
122
+ if (typeof response === 'string') {
123
+ return safeJsonParse(response);
124
+ }
125
+
126
+ // Blob-like object (RN blobs have _data, standard Blobs have .text())
127
+ if (typeof response === 'object' && isBlob(response)) {
128
+ const text = await blobToText(response);
129
+ return safeJsonParse(text);
130
+ }
131
+
132
+ // Already parsed JSON (e.g. responseType is 'json')
133
+ if (typeof response === 'object') {
134
+ return response;
135
+ }
136
+
137
+ return null;
138
+ }
139
+
140
+ /**
141
+ * Detects whether a value is a Blob or a React Native Blob-like object.
142
+ */
143
+ function isBlob(value: any): boolean {
144
+ // React Native Blob-like object
145
+ if ('_data' in value) {
146
+ return true;
147
+ }
148
+ // Standard Blob
149
+ if (typeof value.text === 'function') {
150
+ return true;
151
+ }
152
+ // Blob constructor available in environment
153
+ if (typeof Blob !== 'undefined' && value instanceof Blob) {
154
+ return true;
155
+ }
156
+ return false;
157
+ }
158
+
159
+ /**
160
+ * Extracts basic error data for logging.
161
+ *
162
+ * @param error - Unknown error value.
163
+ * @returns Serialized error info or `null`.
164
+ */
165
+ export function getErrorData(error: unknown) {
166
+ if (error instanceof Error) {
167
+ return JSON.stringify({
168
+ name: error.name,
169
+ message: error.message,
170
+ stack: error.stack
171
+ });
172
+ }
173
+
174
+ return null;
175
+ }
@@ -28,8 +28,17 @@ export interface RUMResource {
28
28
  resourceContext?: XMLHttpRequest;
29
29
  }
30
30
 
31
+ export type DdRumResourceGraphqlError = {
32
+ message: string;
33
+ code?: string;
34
+ locations?: Array<{ line: number; column: number }>;
35
+ path?: Array<string | number>;
36
+ };
37
+
31
38
  export type DdRumResourceGraphqlAttributes = {
32
39
  operationType?: string;
33
40
  operationName?: string;
34
41
  variables?: string;
42
+ payload?: string;
43
+ errors?: DdRumResourceGraphqlError[];
35
44
  };
@@ -123,12 +123,36 @@ describe('default encoders', () => {
123
123
  expect(result).toHaveProperty('componentStack');
124
124
  });
125
125
 
126
+ it('does not cause infinite recursion', () => {
127
+ const error = new Error('stack overflow test');
128
+ errorEncoder.encode(error);
129
+ expect(warn).not.toHaveBeenCalledWith(
130
+ expect.stringContaining('Encoder error')
131
+ );
132
+ });
133
+
134
+ it('does not cause infinite recursion with nested Error cause', () => {
135
+ const inner = new Error('inner');
136
+ const outer: any = new Error('outer');
137
+ outer.cause = inner;
138
+ errorEncoder.encode(outer);
139
+ expect(warn).not.toHaveBeenCalledWith(
140
+ expect.stringContaining('Encoder error')
141
+ );
142
+ });
143
+
126
144
  it('encodes error with cause', () => {
127
145
  const cause = new Error('inner');
128
146
  const err: any = new Error('outer');
129
147
  err.cause = cause;
130
148
  const result = errorEncoder.encode(err) as Record<string, any>;
131
- expect(result.cause).toBe(cause);
149
+ expect(result.cause).toEqual(
150
+ expect.objectContaining({
151
+ name: 'Error',
152
+ message: 'inner'
153
+ })
154
+ );
155
+ expect(result.cause.stack).toContain('Error: inner');
132
156
  });
133
157
  });
134
158
 
@@ -77,7 +77,17 @@ export const errorEncoder: AttributeEncoder<any> = {
77
77
  ...DdSdk.attributeEncoders,
78
78
  ...builtInEncoders
79
79
  ];
80
- encodeAttributesInPlace(e, extraAttributes, [], allEncoders);
80
+
81
+ // We encode each enumerable property individually to avoid potential infinite recursion issues
82
+ // such as those caused by having nested errors
83
+ for (const [key, val] of Object.entries(e)) {
84
+ encodeAttributesInPlace(
85
+ val,
86
+ extraAttributes,
87
+ [key],
88
+ allEncoders
89
+ );
90
+ }
81
91
  }
82
92
 
83
93
  // Remove fields that are duplicated in the dedicated fields below
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  import { render } from '@testing-library/react-native';
8
- import { Animated, Button, InteractionManager, Text, View } from 'react-native';
8
+ import { Animated, Button, Text, View } from 'react-native';
9
9
  import React, { useState } from 'react';
10
10
 
11
11
  import { DatadogProviderConfiguration } from '../../../../config/DatadogProviderConfiguration';
@@ -109,14 +109,31 @@ export const renderWithProvider = (params?: {
109
109
  };
110
110
 
111
111
  /**
112
- * Mocks an animation for InteractionManager.runAfterInteractions. Returns
113
- * a function to be called to finish the animation
112
+ * Mocks requestIdleCallback so that scheduled callbacks only run when
113
+ * {@link flushIdleCallbacks} is called. Restores the original value on
114
+ * {@link restore}.
114
115
  */
115
- export const mockAnimation = () => {
116
- const fakeAnimationHandle = InteractionManager.createInteractionHandle();
116
+ export const mockIdleCallback = () => {
117
+ const callbacks: Array<() => void> = [];
118
+ const original = (globalThis as Record<string, unknown>)
119
+ .requestIdleCallback;
120
+
121
+ (globalThis as Record<string, unknown>).requestIdleCallback = (
122
+ cb: () => void
123
+ ) => {
124
+ callbacks.push(cb);
125
+ return callbacks.length;
126
+ };
117
127
 
118
128
  return {
119
- finishAnimation: () =>
120
- InteractionManager.clearInteractionHandle(fakeAnimationHandle)
129
+ flushIdleCallbacks: () => {
130
+ callbacks.splice(0).forEach(cb => cb());
131
+ },
132
+ restore: () => {
133
+ (globalThis as Record<
134
+ string,
135
+ unknown
136
+ >).requestIdleCallback = original;
137
+ }
121
138
  };
122
139
  };
@@ -22,7 +22,7 @@ import {
22
22
 
23
23
  import {
24
24
  getDefaultConfiguration,
25
- mockAnimation,
25
+ mockIdleCallback,
26
26
  renderWithProvider
27
27
  } from './__utils__/renderWithProvider';
28
28
 
@@ -190,20 +190,24 @@ describe('DatadogProvider', () => {
190
190
 
191
191
  it('runs after initialization when ASYNC initialization', async () => {
192
192
  const onInitialization = jest.fn();
193
- const { finishAnimation } = mockAnimation();
194
- const configuration = getDefaultConfiguration();
195
- configuration.initializationMode = InitializationMode.ASYNC;
196
- const { getByText } = renderWithProvider({
197
- onInitialization,
198
- configuration
199
- });
200
- getByText('I am a test application');
201
- await flushPromises();
202
- expect(onInitialization).not.toHaveBeenCalled();
203
-
204
- finishAnimation();
205
- await flushPromises();
206
- expect(onInitialization).toHaveBeenCalledTimes(1);
193
+ const idle = mockIdleCallback();
194
+ try {
195
+ const configuration = getDefaultConfiguration();
196
+ configuration.initializationMode = InitializationMode.ASYNC;
197
+ const { getByText } = renderWithProvider({
198
+ onInitialization,
199
+ configuration
200
+ });
201
+ getByText('I am a test application');
202
+ await flushPromises();
203
+ expect(onInitialization).not.toHaveBeenCalled();
204
+
205
+ idle.flushIdleCallbacks();
206
+ await flushPromises();
207
+ expect(onInitialization).toHaveBeenCalledTimes(1);
208
+ } finally {
209
+ idle.restore();
210
+ }
207
211
  });
208
212
  it('runs after initialization when partial initialization', async () => {
209
213
  const onInitialization = jest.fn();
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  import { fireEvent } from '@testing-library/react-native';
8
- import { NativeModules } from 'react-native';
8
+ import { InteractionManager, NativeModules } from 'react-native';
9
9
 
10
10
  import { DdSdkReactNative } from '../../../DdSdkReactNative';
11
11
  import { InitializationMode } from '../../../config/types';
@@ -22,7 +22,7 @@ import {
22
22
 
23
23
  import {
24
24
  getDefaultConfiguration,
25
- mockAnimation,
25
+ mockIdleCallback,
26
26
  renderWithProvider,
27
27
  renderWithProviderAndAnimation
28
28
  } from './__utils__/renderWithProvider';
@@ -70,70 +70,101 @@ describe('DatadogProvider', () => {
70
70
 
71
71
  expect(NativeModules.DdRum.addAction).toHaveBeenCalledTimes(1);
72
72
  });
73
- it('initializes the SDK before animations are done', async () => {
74
- const { finishAnimation } = mockAnimation();
75
- renderWithProvider();
73
+ it('initializes the SDK without waiting for idle callback', async () => {
74
+ const idle = mockIdleCallback();
75
+ try {
76
+ const configuration = getDefaultConfiguration();
77
+ configuration.initializationMode = InitializationMode.SYNC;
78
+ renderWithProvider({ configuration });
76
79
 
77
- await flushPromises();
78
- expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
79
- finishAnimation();
80
+ await flushPromises();
81
+
82
+ expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
83
+
84
+ idle.flushIdleCallbacks();
85
+ await flushPromises();
86
+
87
+ expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
88
+ } finally {
89
+ idle.restore();
90
+ }
80
91
  });
81
92
  });
82
93
 
83
94
  describe('initializationMode ASYNC', () => {
84
- beforeEach(() => {
85
- jest.useFakeTimers();
86
- });
87
- afterEach(() => {
88
- jest.useRealTimers();
89
- });
90
- it('starts auto-instrumentation after animations are done (with real Animation)', async () => {
91
- const configuration = getDefaultConfiguration();
92
- configuration.initializationMode = InitializationMode.ASYNC;
93
-
94
- const { getByText } = renderWithProviderAndAnimation({
95
- configuration
96
- });
97
- await flushPromises();
98
- expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(0);
99
- jest.advanceTimersByTime(700);
100
- await flushPromises();
101
- expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
102
- const button = getByText('test button');
103
- fireEvent(button, 'press', {
104
- _targetInst: {
105
- props: {
106
- 'dd-action-name': 'press button'
95
+ it('initializes the SDK when the idle callback fires', async () => {
96
+ const idle = mockIdleCallback();
97
+ try {
98
+ const configuration = getDefaultConfiguration();
99
+ configuration.initializationMode = InitializationMode.ASYNC;
100
+
101
+ const { getByText } = renderWithProvider({ configuration });
102
+ await flushPromises();
103
+ expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(0);
104
+
105
+ idle.flushIdleCallbacks();
106
+ await flushPromises();
107
+
108
+ expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
109
+ const button = getByText('test button');
110
+ fireEvent(button, 'press', {
111
+ _targetInst: {
112
+ props: {
113
+ 'dd-action-name': 'press button'
114
+ }
107
115
  }
108
- }
109
- });
110
- expect(NativeModules.DdRum.addAction).toHaveBeenCalledTimes(1);
116
+ });
117
+ expect(NativeModules.DdRum.addAction).toHaveBeenCalledTimes(1);
118
+ } finally {
119
+ idle.restore();
120
+ }
111
121
  });
112
122
 
113
- it('starts auto-instrumentation after animations are done (with InteractionManager)', async () => {
114
- jest.useRealTimers();
115
- const configuration = getDefaultConfiguration();
116
- configuration.initializationMode = InitializationMode.ASYNC;
123
+ it('defers initialization while animations are running', async () => {
124
+ const idle = mockIdleCallback();
125
+ try {
126
+ const configuration = getDefaultConfiguration();
127
+ configuration.initializationMode = InitializationMode.ASYNC;
117
128
 
118
- const { finishAnimation } = mockAnimation();
119
- const { getByText } = renderWithProvider({ configuration });
129
+ renderWithProviderAndAnimation({ configuration });
130
+ await flushPromises();
131
+ expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(0);
120
132
 
121
- await flushPromises();
122
- expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(0);
133
+ idle.flushIdleCallbacks();
134
+ await flushPromises();
123
135
 
124
- finishAnimation();
125
- await flushPromises();
136
+ expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
137
+ } finally {
138
+ idle.restore();
139
+ }
140
+ });
126
141
 
127
- expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
128
- const button = getByText('test button');
129
- fireEvent(button, 'press', {
130
- _targetInst: {
131
- props: {
132
- 'dd-action-name': 'press button'
133
- }
134
- }
135
- });
136
- expect(NativeModules.DdRum.addAction).toHaveBeenCalledTimes(1);
142
+ it('falls back to InteractionManager when requestIdleCallback is unavailable', async () => {
143
+ const original = (globalThis as Record<string, unknown>)
144
+ .requestIdleCallback;
145
+ (globalThis as Record<
146
+ string,
147
+ unknown
148
+ >).requestIdleCallback = undefined;
149
+ try {
150
+ const configuration = getDefaultConfiguration();
151
+ configuration.initializationMode = InitializationMode.ASYNC;
152
+
153
+ const handle = InteractionManager.createInteractionHandle();
154
+ renderWithProvider({ configuration });
155
+ await flushPromises();
156
+ expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(0);
157
+
158
+ InteractionManager.clearInteractionHandle(handle);
159
+ await flushPromises();
160
+
161
+ expect(NativeModules.DdSdk.initialize).toHaveBeenCalledTimes(1);
162
+ } finally {
163
+ (globalThis as Record<
164
+ string,
165
+ unknown
166
+ >).requestIdleCallback = original;
167
+ }
137
168
  });
138
169
  });
139
170
 
@@ -68,12 +68,14 @@ export interface Spec extends TurboModule {
68
68
  * Add a RUM Action.
69
69
  * @param type: The action type (tap, scroll, swipe, back, custom).
70
70
  * @param name: The action name.
71
+ * @param touch: The native touch data for tap actions, or null for other action types.
71
72
  * @param context: The additional context to send.
72
73
  * @param timestampMs: The timestamp when the action occurred (in milliseconds). If not provided, current timestamp will be used.
73
74
  */
74
75
  addAction(
75
76
  type: string,
76
77
  name: string,
78
+ touch: Object | null,
77
79
  context: Object,
78
80
  timestampMs: number
79
81
  ): Promise<void>;
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // generated by genversion
2
- export const version = '3.2.0';
2
+ export const version = '3.4.0';