@datadog/mobile-react-navigation 1.0.0-rc5 → 1.0.0-rc9

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 (46) hide show
  1. package/lib/commonjs/index.js.map +1 -1
  2. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js +77 -51
  3. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js.map +1 -1
  4. package/lib/commonjs/rum/instrumentation/react-navigation.js +2 -0
  5. package/lib/commonjs/rum/instrumentation/react-navigation.js.map +1 -0
  6. package/lib/module/index.js.map +1 -1
  7. package/lib/module/rum/instrumentation/DdRumReactNavigationTracking.js +74 -52
  8. package/lib/module/rum/instrumentation/DdRumReactNavigationTracking.js.map +1 -1
  9. package/lib/module/rum/instrumentation/react-navigation.js +2 -0
  10. package/lib/module/rum/instrumentation/react-navigation.js.map +1 -0
  11. package/lib/typescript/core/src/DdSdkReactNative.d.ts +6 -6
  12. package/lib/typescript/core/src/DdSdkReactNativeConfiguration.d.ts +15 -3
  13. package/lib/typescript/core/src/InternalLog.d.ts +5 -0
  14. package/lib/typescript/core/src/TimeProvider.d.ts +20 -0
  15. package/lib/typescript/core/src/Timer.d.ts +3 -2
  16. package/lib/typescript/core/src/index.d.ts +4 -3
  17. package/lib/typescript/core/src/rum/instrumentation/DdEventsInterceptor.d.ts +1 -0
  18. package/lib/typescript/core/src/rum/instrumentation/DdRumErrorTracking.d.ts +2 -2
  19. package/lib/typescript/core/src/rum/instrumentation/DdRumUserInteractionTracking.d.ts +3 -3
  20. package/lib/typescript/core/src/rum/instrumentation/ShallowObjectEqualityChecker.d.ts +4 -0
  21. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +15 -0
  22. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +18 -0
  23. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.d.ts +6 -0
  24. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.d.ts +8 -0
  25. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +19 -0
  26. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.d.ts +8 -0
  27. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.d.ts +6 -0
  28. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.d.ts +17 -0
  29. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/URLHostParser.d.ts +6 -0
  30. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/XHRProxy.d.ts +25 -0
  31. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/responseSize.d.ts +3 -0
  32. package/lib/typescript/core/src/version.d.ts +1 -1
  33. package/lib/typescript/react-navigation/src/rum/instrumentation/DdRumReactNavigationTracking.d.ts +9 -3
  34. package/lib/typescript/react-navigation/src/rum/instrumentation/react-navigation.d.ts +116 -0
  35. package/package.json +11 -6
  36. package/src/__tests__/rum/instrumentation/DdRumReactNavigationTracking.test.tsx +481 -385
  37. package/src/__tests__/rum/instrumentation/__utils__/AppStateMock.ts +75 -0
  38. package/src/__tests__/rum/instrumentation/__utils__/AppStateMockLegacy.ts +61 -0
  39. package/src/__tests__/rum/instrumentation/__utils__/Navigators/NavigatorsV5.tsx +105 -0
  40. package/src/__tests__/rum/instrumentation/__utils__/Navigators/NavigatorsV6.tsx +105 -0
  41. package/src/index.tsx +4 -1
  42. package/src/rum/instrumentation/DdRumReactNavigationTracking.tsx +145 -64
  43. package/src/rum/instrumentation/react-navigation.ts +158 -0
  44. package/lib/typescript/core/src/rum/instrumentation/DdRumResourceTracking.d.ts +0 -30
  45. package/lib/typescript/core/src/rum/instrumentation/DdRumXhr.d.ts +0 -13
  46. package/lib/typescript/core/src/rum/instrumentation/TraceIdentifier.d.ts +0 -1
@@ -1,411 +1,507 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
1
2
  /*
2
3
  * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
4
  * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
5
  * Copyright 2016-Present Datadog, Inc.
5
6
  */
6
7
 
7
- import React from 'react';
8
- import { View, Text, Button, AppState } from 'react-native';
9
- import { NavigationContainer, NavigationContainerRef, Route } from '@react-navigation/native';
10
- import { DdRum } from '@datadog/mobile-react-native';
11
- import { DdRumReactNavigationTracking, ViewNamePredicate} from '../../../rum/instrumentation/DdRumReactNavigationTracking';
8
+ import { DdRum, InternalLog } from '@datadog/mobile-react-native';
9
+ import type { Route } from '@react-navigation/native-v5';
12
10
  import { render, fireEvent } from '@testing-library/react-native';
13
- import { createStackNavigator } from '@react-navigation/stack';
11
+ import mockBackHandler from 'react-native/Libraries/Utilities/__mocks__/BackHandler.js';
12
+ import { AppState, BackHandler } from 'react-native';
13
+ import React, { createRef } from 'react';
14
+
15
+ import type { ViewNamePredicate } from '../../../rum/instrumentation/DdRumReactNavigationTracking';
16
+ import { DdRumReactNavigationTracking } from '../../../rum/instrumentation/DdRumReactNavigationTracking';
17
+
18
+ import { AppStateMockLegacy } from './__utils__/AppStateMockLegacy';
19
+ import { AppStateMock } from './__utils__/AppStateMock';
20
+ import {
21
+ FakeNavigator1 as FakeNavigator1v5,
22
+ FakeNavigator2 as FakeNavigator2v5,
23
+ FakeNestedNavigator as FakeNestedNavigatorv5
24
+ } from './__utils__/Navigators/NavigatorsV5';
25
+ import {
26
+ FakeNavigator1 as FakeNavigator1v6,
27
+ FakeNavigator2 as FakeNavigator2v6,
28
+ FakeNestedNavigator as FakeNestedNavigatorv6
29
+ } from './__utils__/Navigators/NavigatorsV6';
30
+
31
+ // TODO: inject this as a global
32
+ function mocked<T extends (...args: any[]) => any>(item: T) {
33
+ return (item as unknown) as jest.MockedFunction<typeof item>;
34
+ }
35
+
36
+ jest.mock(
37
+ 'react-native/Libraries/Utilities/BackHandler',
38
+ () => mockBackHandler
39
+ );
14
40
 
15
41
  jest.mock('@datadog/mobile-react-native', () => {
16
42
  return {
17
43
  DdRum: {
18
44
  // eslint-disable-next-line @typescript-eslint/no-empty-function
19
- startView: jest.fn().mockImplementation(() => { }),
20
- stopView: jest.fn().mockImplementation(() => { })
45
+ startView: jest.fn().mockImplementation(() => {}),
46
+ stopView: jest.fn().mockImplementation(() => {})
47
+ },
48
+ SdkVerbosity: {
49
+ DEBUG: 'debug',
50
+ INFO: 'info',
51
+ WARN: 'warn',
52
+ ERROR: 'error'
21
53
  },
54
+ InternalLog: {
55
+ log: jest.fn()
56
+ }
22
57
  };
23
58
  });
24
59
 
25
- let capturedAppStateChangeCallback = null;
26
-
27
- jest.mock('react-native/Libraries/AppState/AppState', () => ({
28
- addEventListener: jest.fn((event, callback) => {
29
- if (event === 'change') {
30
- capturedAppStateChangeCallback = callback;
31
- }
32
- }),
33
- removeEventListener: jest.fn().mockImplementation(() => { })
34
- }));
35
-
36
- const { Screen, Navigator } = createStackNavigator();
37
- const navigationRef1: React.RefObject<NavigationContainerRef> = React.createRef();
38
- const navigationRef2: React.RefObject<NavigationContainerRef> = React.createRef();
39
- const navigationRef3: React.RefObject<NavigationContainerRef> = React.createRef();
40
-
41
60
  // Silence the warning https://github.com/facebook/react-native/issues/11094#issuecomment-263240420
42
- jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');
61
+ jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
43
62
  jest.useFakeTimers();
44
63
 
45
- let baseConsoleErrorCalled = false;
46
- let baseConsoleError = (...params: unknown) => {
47
- baseConsoleErrorCalled = true
48
- }
49
- let originalConsoleError = undefined
50
-
51
64
  beforeEach(() => {
52
-
65
+ mocked(InternalLog.log).mockClear();
53
66
  jest.setTimeout(20000);
54
- DdRum.startView.mockClear();
55
- DdRum.stopView.mockClear();
56
- AppState.addEventListener.mockClear();
57
- AppState.removeEventListener.mockClear();
58
- baseConsoleErrorCalled = false;
59
- originalConsoleError = console.error;
60
- console.error = baseConsoleError;
67
+ mocked(DdRum.startView).mockClear();
68
+ mocked(DdRum.stopView).mockClear();
69
+ mocked(AppState.addEventListener).mockClear();
70
+ mocked(BackHandler.exitApp).mockClear();
61
71
 
72
+ // @ts-ignore
62
73
  DdRumReactNavigationTracking.registeredContainer = null;
74
+ // @ts-ignore
63
75
  DdRumReactNavigationTracking.navigationStateChangeListener = null;
64
- DdRumReactNavigationTracking.appStateListener = null;
65
- })
66
-
67
- afterEach(() => {
68
- console.error = originalConsoleError
69
- })
76
+ });
70
77
 
71
78
  // Unit tests
72
-
73
- it('M send a RUM ViewEvent W startTrackingViews', async () => {
74
-
75
- // GIVEN
76
- render(<FakeNavigator1 />);
77
-
78
- // WHEN
79
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current);
80
-
81
- // THEN
82
- expect(DdRum.startView.mock.calls.length).toBe(1);
83
- expect(DdRum.startView.mock.calls[0][0]).toBe(navigationRef1.current?.getCurrentRoute()?.key);
84
- expect(DdRum.startView.mock.calls[0][1]).toBe(navigationRef1.current?.getCurrentRoute()?.name);
85
- expect(DdRum.startView.mock.calls[0][2]).toBeUndefined();
86
- })
87
-
88
- it('M send a related RUM ViewEvent W switching screens { navigationContainer listener attached }', async () => {
89
-
90
- // GIVEN
91
- const { getByText } = render(<FakeNavigator1 />);
92
- const goToAboutButton = getByText('Go to About');
93
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current);
94
-
95
- // WHEN
96
- expect(goToAboutButton).toBeTruthy();
97
- fireEvent(goToAboutButton, "press");
98
-
99
- // THEN
100
- expect(DdRum.startView.mock.calls.length).toBe(2);
101
- expect(DdRum.startView.mock.calls[1][0]).toBe(navigationRef1.current?.getCurrentRoute()?.key);
102
- expect(DdRum.startView.mock.calls[1][1]).toBe(navigationRef1.current?.getCurrentRoute()?.name);
103
- expect(DdRum.startView.mock.calls[1][2]).toBeUndefined();
104
- })
105
-
106
- it('M send a related RUM ViewEvent W switching screens { viewPredicate provided }', async () => {
107
- // GIVEN
108
- const { getByText } = render(<FakeNavigator1 />);
109
- const goToAboutButton = getByText('Go to About');
110
- const customViewName = "custom_view_name"
111
- const predicate: ViewNamePredicate = function (_route: Route<string, any | undefined>, _trackedName: string) {
112
- return customViewName
113
- };
114
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current, predicate);
115
-
116
- // WHEN
117
- expect(goToAboutButton).toBeTruthy();
118
- fireEvent(goToAboutButton, "press");
119
-
120
- // THEN
121
- expect(DdRum.startView.mock.calls.length).toBe(2);
122
- expect(DdRum.startView.mock.calls[1][0]).toBe(navigationRef1.current?.getCurrentRoute()?.key);
123
- expect(DdRum.startView.mock.calls[1][1]).toBe(customViewName);
124
- expect(DdRum.startView.mock.calls[1][2]).toBeUndefined();
125
- })
126
-
127
- it('M only register once W startTrackingViews{ multiple times }', async () => {
128
-
129
- // GIVEN
130
- const { getByText } = render(<FakeNavigator1 />);
131
- const goToAboutButton = getByText('Go to About');
132
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current);
133
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current);
134
-
135
- // WHEN
136
- expect(goToAboutButton).toBeTruthy();
137
- fireEvent(goToAboutButton, "press");
138
-
139
- // THEN
140
- expect(DdRum.startView.mock.calls.length).toBe(2);
141
- expect(DdRum.startView.mock.calls[1][0]).toBe(navigationRef1.current?.getCurrentRoute()?.key);
142
- expect(DdRum.startView.mock.calls[1][1]).toBe(navigationRef1.current?.getCurrentRoute()?.name);
143
- expect(DdRum.startView.mock.calls[1][2]).toBeUndefined();
144
- })
145
-
146
- it('M do nothing W switching screens { navigationContainer listener detached }', async () => {
147
-
148
- // GIVEN
149
- const { getByText } = render(<FakeNavigator1 />);
150
- const goToAboutButton = getByText('Go to About');
151
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current);
152
-
153
- // WHEN
154
- DdRumReactNavigationTracking.stopTrackingViews(navigationRef1.current);
155
- expect(goToAboutButton).toBeTruthy();
156
- fireEvent(goToAboutButton, "press");
157
-
158
- // THEN
159
- expect(DdRum.startView.mock.calls.length).toBe(1);
160
- })
161
-
162
- it('M do nothing W startTrackingViews { undefined NavigationContainerRef ', async () => {
163
-
164
- // WHEN
165
- DdRumReactNavigationTracking.startTrackingViews(null);
166
-
167
- // THEN
168
- expect(DdRum.startView.mock.calls.length).toBe(0);
169
- })
170
-
171
- it('M send a RUM ViewEvent for each W startTrackingViews { multiple navigation containers w first not detached }', async () => {
172
-
173
- // GIVEN
174
- const testUtils1: { getByText } = render(<FakeNavigator1 />);
175
- const goToAboutButton1 = testUtils1.getByText('Go to About');
176
- const testUtils2: { getByText } = render(<FakeNavigator2 />);
177
- const goToAboutButton2 = testUtils2.getByText('Go to About');
178
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current);
179
- // this call will be ignored, because only one NavigationContainer tracking is supported at the time
180
- DdRumReactNavigationTracking.startTrackingViews(navigationRef2.current);
181
-
182
- // WHEN
183
- expect(goToAboutButton1).toBeTruthy();
184
- expect(goToAboutButton2).toBeTruthy();
185
- fireEvent(goToAboutButton1, "press");
186
- fireEvent(goToAboutButton2, "press");
187
-
188
- // THEN
189
- expect(DdRum.startView.mock.calls.length).toBe(2);
190
- expect(DdRum.startView.mock.calls[1][0]).toBe(navigationRef1.current?.getCurrentRoute()?.key);
191
- expect(DdRum.startView.mock.calls[1][1]).toBe(navigationRef1.current?.getCurrentRoute()?.name);
192
- expect(DdRum.startView.mock.calls[1][2]).toBeUndefined();
193
- expect(baseConsoleErrorCalled).toBe(true);
194
- })
195
-
196
- it('M send a RUM ViewEvent for each W startTrackingViews { multiple navigation containers w first is detached }', async () => {
197
-
198
- // GIVEN
199
- const testUtils1: { getByText } = render(<FakeNavigator1 />);
200
- const goToAboutButton1 = testUtils1.getByText('Go to About');
201
- const testUtils2: { getByText } = render(<FakeNavigator2 />);
202
- const goToAboutButton2 = testUtils2.getByText('Go to About');
203
-
204
- // WHEN
205
- expect(goToAboutButton1).toBeTruthy();
206
- expect(goToAboutButton2).toBeTruthy();
207
-
208
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current);
209
- fireEvent(goToAboutButton1, "press");
210
- DdRumReactNavigationTracking.stopTrackingViews(navigationRef1.current);
211
-
212
- DdRumReactNavigationTracking.startTrackingViews(navigationRef2.current);
213
-
214
- const navigationRef2StartRoute = navigationRef2.current.getCurrentRoute();
215
-
216
- fireEvent(goToAboutButton2, "press");
217
-
218
- // THEN
219
- expect(DdRum.startView.mock.calls.length).toBe(4);
220
- expect(DdRum.startView.mock.calls[2][0]).toBe(navigationRef2StartRoute.key);
221
- expect(DdRum.startView.mock.calls[2][1]).toBe(navigationRef2StartRoute.name);
222
- expect(DdRum.startView.mock.calls[2][2]).toBeUndefined();
223
- expect(DdRum.startView.mock.calls[3][0]).toBe(navigationRef2.current?.getCurrentRoute()?.key);
224
- expect(DdRum.startView.mock.calls[3][1]).toBe(navigationRef2.current?.getCurrentRoute()?.name);
225
- expect(DdRum.startView.mock.calls[3][2]).toBeUndefined();
226
- })
227
-
228
- it('M send a RUM ViewEvent for each W switching screens { multiple navigation containers }', async () => {
229
-
230
- // GIVEN
231
- render(<FakeNavigator1 />);
232
- render(<FakeNavigator2 />);
233
-
234
- // WHEN
235
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current);
236
- DdRumReactNavigationTracking.startTrackingViews(navigationRef2.current);
237
-
238
- // THEN
239
- expect(DdRum.startView.mock.calls.length).toBe(1);
240
- expect(DdRum.startView.mock.calls[0][0]).toBe(navigationRef1.current?.getCurrentRoute()?.key);
241
- expect(DdRum.startView.mock.calls[0][1]).toBe(navigationRef1.current?.getCurrentRoute()?.name);
242
- expect(DdRum.startView.mock.calls[0][2]).toBeUndefined();
243
- expect(baseConsoleErrorCalled).toBe(true);
244
- })
245
-
246
- it('M register AppState listener only once', async () => {
247
-
248
- // GIVEN
249
- render(<FakeNavigator1 />);
250
- render(<FakeNavigator2 />);
251
-
252
- // WHEN
253
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current);
254
- DdRumReactNavigationTracking.stopTrackingViews(navigationRef1.current);
255
- DdRumReactNavigationTracking.startTrackingViews(navigationRef2.current);
256
-
257
- // THEN
258
- expect(AppState.addEventListener.mock.calls.length).toBe(1);
259
- expect(AppState.removeEventListener.mock.calls.length).toBe(0);
260
- })
261
-
262
- it('M stop active view W app goes into background', async () => {
263
-
264
- // GIVEN
265
- render(<FakeNavigator1 />);
266
-
267
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current);
268
-
269
- // WHEN
270
- capturedAppStateChangeCallback('background');
271
-
272
- // THEN
273
- expect(DdRum.stopView.mock.calls.length).toBe(1);
274
- expect(DdRum.stopView.mock.calls[0][0]).toBe(navigationRef1.current?.getCurrentRoute()?.key);
275
- expect(DdRum.stopView.mock.calls[0][1]).toBeUndefined();
276
-
277
- })
278
-
279
- it('M start last view W app goes into foreground', async () => {
280
-
281
- // GIVEN
282
- render(<FakeNavigator1 />);
283
-
284
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current);
285
-
286
- // WHEN
287
- capturedAppStateChangeCallback('background');
288
- capturedAppStateChangeCallback('active');
289
-
290
- // THEN
291
- expect(DdRum.stopView.mock.calls.length).toBe(1);
292
- expect(DdRum.startView.mock.calls.length).toBe(2);
293
- expect(DdRum.startView.mock.calls[0][0]).toBe(navigationRef1.current?.getCurrentRoute()?.key);
294
- expect(DdRum.startView.mock.calls[0][1]).toBe(navigationRef1.current?.getCurrentRoute()?.name);
295
- expect(DdRum.startView.mock.calls[0][2]).toBeUndefined();
296
- })
297
-
298
- it('M not stop view W no navigator attached', async () => {
299
-
300
- // GIVEN
301
- render(<FakeNavigator1 />);
302
-
303
- DdRumReactNavigationTracking.startTrackingViews(navigationRef1.current);
304
- DdRumReactNavigationTracking.stopTrackingViews(navigationRef1.current);
305
-
306
- // WHEN
307
- capturedAppStateChangeCallback('background');
308
-
309
- // THEN
310
- expect(DdRum.stopView.mock.calls.length).toBe(0);
311
- })
312
-
313
- it('M send a RUM ViewEvent for each W switching screens { nested navigation containers }', async () => {
314
-
315
- // GIVEN
316
- const testUtils: { getByText } = render(<FakeNestedNavigator />);
317
- DdRumReactNavigationTracking.startTrackingViews(navigationRef3.current);
318
- const goToAboutButton = testUtils.getByText('Go to About');
319
- let initialRoute = navigationRef3.current?.getCurrentRoute()
320
-
321
- // WHEN
322
- expect(goToAboutButton).toBeTruthy();
323
- fireEvent(goToAboutButton, "press");
324
-
325
- // THEN
326
- expect(DdRum.startView.mock.calls.length).toBe(2);
327
- expect(DdRum.startView.mock.calls[0][0]).toBe(initialRoute?.key);
328
- expect(DdRum.startView.mock.calls[0][1]).toBe(initialRoute?.name);
329
- expect(DdRum.startView.mock.calls[0][2]).toBeUndefined();
330
- expect(DdRum.startView.mock.calls[1][0]).toBe(navigationRef3.current?.getCurrentRoute()?.key);
331
- expect(DdRum.startView.mock.calls[1][1]).toBe(navigationRef3.current?.getCurrentRoute()?.name);
332
- expect(DdRum.startView.mock.calls[1][2]).toBeUndefined();
333
- })
334
-
335
-
336
- // Internals
337
-
338
- function FakeAboutScreen({ navigation }) {
339
-
340
- return (
341
- <View>
342
- <Text>Welcome to About</Text>
343
- </View>
344
-
345
- )
346
- }
347
-
348
- function FakeHomeScreen({ navigation }) {
349
-
350
- return (
351
- <View>
352
- <Text>Welcome to Home</Text>
353
- <Button title="Go to About" onPress={() => {
354
- navigation.navigate("About");
355
- }}></Button>
356
- </View>
357
-
358
- )
359
- }
360
-
361
- function FakeSettingsScreen({ navigation }) {
362
-
363
- return (
364
- <View>
365
- <Text>Welcome to About</Text>
366
- </View>
367
-
368
- )
369
- }
370
-
371
- function FakeProfileScreen({navigation}) {
372
- return (
373
- <Navigator>
374
- <Screen name="Home" component={FakeHomeScreen} />
375
- <Screen name="About" component={FakeAboutScreen} />
376
- </Navigator>
377
- )
378
- }
379
-
380
- function FakeNavigator1() {
381
- return (
382
- <NavigationContainer ref={navigationRef1}>
383
- <Navigator>
384
- <Screen name="Home" component={FakeHomeScreen} />
385
- <Screen name="About" component={FakeAboutScreen} />
386
- </Navigator>
387
- </NavigationContainer>
388
- )
389
- }
390
-
391
- function FakeNavigator2() {
392
- return (
393
- <NavigationContainer ref={navigationRef2}>
394
- <Navigator>
395
- <Screen name="Home" component={FakeHomeScreen} />
396
- <Screen name="About" component={FakeAboutScreen} />
397
- </Navigator>
398
- </NavigationContainer>
399
- )
400
- }
401
-
402
- function FakeNestedNavigator() {
403
- return (
404
- <NavigationContainer ref={navigationRef3}>
405
- <Navigator>
406
- <Screen name="Profile" component= {FakeProfileScreen} />
407
- <Screen name ="Settings" component= {FakeSettingsScreen}/>
408
- </Navigator>
409
- </NavigationContainer>
410
- )
411
- }
79
+ describe.each([
80
+ [
81
+ '5',
82
+ {
83
+ FakeNavigator1: FakeNavigator1v5,
84
+ FakeNavigator2: FakeNavigator2v5,
85
+ FakeNestedNavigator: FakeNestedNavigatorv5
86
+ }
87
+ ],
88
+ [
89
+ '6',
90
+ {
91
+ FakeNavigator1: FakeNavigator1v6,
92
+ FakeNavigator2: FakeNavigator2v6,
93
+ FakeNestedNavigator: FakeNestedNavigatorv6
94
+ }
95
+ ]
96
+ ])(
97
+ 'DdRumReactNavigationTracking on react-navigation v%s',
98
+ (version, { FakeNavigator1, FakeNavigator2, FakeNestedNavigator }) => {
99
+ describe('startTrackingViews', () => {
100
+ it('sends a related RUM ViewEvent when switching screens { navigationContainer listener attached }', async () => {
101
+ // GIVEN
102
+ const navigationRef = createRef<any>();
103
+ const { getByText } = render(
104
+ <FakeNavigator1 navigationRef={navigationRef} />
105
+ );
106
+ const goToAboutButton = getByText('Go to About');
107
+ DdRumReactNavigationTracking.startTrackingViews(
108
+ navigationRef.current
109
+ );
110
+
111
+ // WHEN
112
+ expect(goToAboutButton).toBeTruthy();
113
+ fireEvent(goToAboutButton, 'press');
114
+
115
+ // THEN
116
+ expect(DdRum.startView).toHaveBeenCalledTimes(2);
117
+ expect(DdRum.startView).toHaveBeenCalledWith(
118
+ expect.any(String),
119
+ 'Home'
120
+ );
121
+ expect(DdRum.startView).toHaveBeenCalledWith(
122
+ expect.any(String),
123
+ 'About'
124
+ );
125
+ });
126
+
127
+ it('sends a related RUM ViewEvent when switching screens { viewPredicate provided }', async () => {
128
+ // GIVEN
129
+ const navigationRef = createRef<any>();
130
+ const { getByText } = render(
131
+ <FakeNavigator1 navigationRef={navigationRef} />
132
+ );
133
+ const goToAboutButton = getByText('Go to About');
134
+ const customViewName = 'custom_view_name';
135
+ const predicate: ViewNamePredicate = function (
136
+ _route: Route<string, any | undefined>,
137
+ _trackedName: string
138
+ ) {
139
+ return customViewName;
140
+ };
141
+ DdRumReactNavigationTracking.startTrackingViews(
142
+ navigationRef.current,
143
+ predicate
144
+ );
145
+
146
+ // WHEN
147
+ expect(goToAboutButton).toBeTruthy();
148
+ fireEvent(goToAboutButton, 'press');
149
+
150
+ // THEN
151
+ expect(DdRum.startView).toHaveBeenCalledWith(
152
+ expect.any(String),
153
+ 'custom_view_name'
154
+ );
155
+ });
156
+
157
+ it('only registers once when startTrackingViews{ multiple times }', async () => {
158
+ // GIVEN
159
+ const navigationRef = createRef<any>();
160
+ const { getByText } = render(
161
+ <FakeNavigator1 navigationRef={navigationRef} />
162
+ );
163
+ const goToAboutButton = getByText('Go to About');
164
+ DdRumReactNavigationTracking.startTrackingViews(
165
+ navigationRef.current
166
+ );
167
+ DdRumReactNavigationTracking.startTrackingViews(
168
+ navigationRef.current
169
+ );
170
+
171
+ // WHEN
172
+ expect(goToAboutButton).toBeTruthy();
173
+ fireEvent(goToAboutButton, 'press');
174
+
175
+ // THEN
176
+ expect(DdRum.startView).toHaveBeenCalledTimes(2);
177
+ });
178
+
179
+ it('does nothing when startTrackingViews { undefined any ', async () => {
180
+ // WHEN
181
+ DdRumReactNavigationTracking.startTrackingViews(null);
182
+
183
+ // THEN
184
+ expect(DdRum.startView).toHaveBeenCalledTimes(0);
185
+ expect(InternalLog.log).toHaveBeenCalledTimes(1);
186
+ expect(InternalLog.log).toHaveBeenCalledWith(
187
+ DdRumReactNavigationTracking.NULL_NAVIGATION_REF_ERROR_MESSAGE,
188
+ 'error'
189
+ );
190
+ });
191
+
192
+ it('sends a RUM ViewEvent for each when startTrackingViews { multiple navigation containers when first not detached }', async () => {
193
+ // GIVEN
194
+ const navigationRef1 = createRef<any>();
195
+ const testUtils1: { getByText } = render(
196
+ <FakeNavigator1 navigationRef={navigationRef1} />
197
+ );
198
+ const goToAboutButton1 = testUtils1.getByText('Go to About');
199
+ const navigationRef2 = createRef<any>();
200
+ const testUtils2: { getByText } = render(
201
+ <FakeNavigator2 navigationRef={navigationRef2} />
202
+ );
203
+ const goToAboutButton2 = testUtils2.getByText('Go to About');
204
+ DdRumReactNavigationTracking.startTrackingViews(
205
+ navigationRef1.current
206
+ );
207
+ // this call will be ignored, because only one NavigationContainer tracking is supported at the time
208
+ DdRumReactNavigationTracking.startTrackingViews(
209
+ navigationRef2.current
210
+ );
211
+
212
+ // WHEN
213
+ expect(goToAboutButton1).toBeTruthy();
214
+ expect(goToAboutButton2).toBeTruthy();
215
+ fireEvent(goToAboutButton1, 'press');
216
+ fireEvent(goToAboutButton2, 'press');
217
+
218
+ // THEN
219
+ expect(DdRum.startView).toHaveBeenCalledTimes(2);
220
+ expect(InternalLog.log).toHaveBeenCalledWith(
221
+ DdRumReactNavigationTracking.NAVIGATION_REF_IN_USE_ERROR_MESSAGE,
222
+ 'error'
223
+ );
224
+ });
225
+
226
+ it('sends a RUM ViewEvent for each when switching screens { nested navigation containers }', async () => {
227
+ // GIVEN
228
+ const navigationRef = createRef<any>();
229
+ const testUtils: { getByText } = render(
230
+ <FakeNestedNavigator navigationRef={navigationRef} />
231
+ );
232
+ DdRumReactNavigationTracking.startTrackingViews(
233
+ navigationRef.current
234
+ );
235
+ const goToAboutButton = testUtils.getByText('Go to About');
236
+
237
+ // WHEN
238
+ expect(goToAboutButton).toBeTruthy();
239
+ fireEvent(goToAboutButton, 'press');
240
+
241
+ // THEN
242
+ expect(DdRum.startView).toHaveBeenCalledTimes(2);
243
+ expect(DdRum.startView).toHaveBeenCalledWith(
244
+ expect.any(String),
245
+ 'Home'
246
+ );
247
+ expect(DdRum.startView).toHaveBeenCalledWith(
248
+ expect.any(String),
249
+ 'About'
250
+ );
251
+
252
+ // WHEN
253
+ const goToNestedHome = testUtils.getByText('Go to Nested Home');
254
+ fireEvent(goToNestedHome, 'press');
255
+
256
+ // THEN
257
+ expect(DdRum.startView).toHaveBeenCalledWith(
258
+ expect.any(String),
259
+ 'NestedHome'
260
+ );
261
+ });
262
+ });
263
+
264
+ describe('stopTrackingViews', () => {
265
+ it('does nothing when switching screens { navigationContainer listener detached }', async () => {
266
+ // GIVEN
267
+ const navigationRef = createRef<any>();
268
+ const { getByText } = render(
269
+ <FakeNavigator1 navigationRef={navigationRef} />
270
+ );
271
+ const goToAboutButton = getByText('Go to About');
272
+ DdRumReactNavigationTracking.startTrackingViews(
273
+ navigationRef.current
274
+ );
275
+
276
+ // WHEN
277
+ DdRumReactNavigationTracking.stopTrackingViews(
278
+ navigationRef.current
279
+ );
280
+ expect(goToAboutButton).toBeTruthy();
281
+ fireEvent(goToAboutButton, 'press');
282
+
283
+ // THEN
284
+ expect(DdRum.startView).toHaveBeenCalledTimes(1);
285
+ });
286
+
287
+ it('sends a RUM ViewEvent for each when startTrackingViews { multiple navigation containers when first is detached }', async () => {
288
+ // GIVEN
289
+ const navigationRef1 = createRef<any>();
290
+ const testUtils1: { getByText } = render(
291
+ <FakeNavigator1 navigationRef={navigationRef1} />
292
+ );
293
+ const goToAboutButton1 = testUtils1.getByText('Go to About');
294
+ const navigationRef2 = createRef<any>();
295
+ const testUtils2: { getByText } = render(
296
+ <FakeNavigator2 navigationRef={navigationRef2} />
297
+ );
298
+ const goToAboutButton2 = testUtils2.getByText('Go to About');
299
+
300
+ // WHEN
301
+ expect(goToAboutButton1).toBeTruthy();
302
+ expect(goToAboutButton2).toBeTruthy();
303
+
304
+ DdRumReactNavigationTracking.startTrackingViews(
305
+ navigationRef1.current
306
+ );
307
+ fireEvent(goToAboutButton1, 'press');
308
+ DdRumReactNavigationTracking.stopTrackingViews(
309
+ navigationRef1.current
310
+ );
311
+
312
+ DdRumReactNavigationTracking.startTrackingViews(
313
+ navigationRef2.current
314
+ );
315
+
316
+ const navigationRef2StartRoute = navigationRef2.current.getCurrentRoute();
317
+
318
+ fireEvent(goToAboutButton2, 'press');
319
+
320
+ // THEN
321
+ expect(DdRum.startView).toHaveBeenCalledTimes(4);
322
+ expect(DdRum.startView).toHaveBeenCalledWith(
323
+ navigationRef2StartRoute.key,
324
+ 'Home'
325
+ );
326
+ expect(DdRum.startView).toHaveBeenCalledWith(
327
+ navigationRef2.current?.getCurrentRoute()?.key,
328
+ 'About'
329
+ );
330
+ });
331
+ });
332
+
333
+ describe.each([
334
+ ['react-native 0.63-0.64', AppStateMockLegacy],
335
+ ['react-native 0.65+', AppStateMock]
336
+ ])(
337
+ 'AppState listener on %s',
338
+ (reactNativeVersion, AppStateMockVersion) => {
339
+ let appStateMock;
340
+ beforeEach(() => {
341
+ appStateMock = new AppStateMockVersion();
342
+ mocked(AppState.addEventListener).mockImplementation(
343
+ // @ts-ignore
344
+ appStateMock.addEventListener
345
+ );
346
+ // @ts-ignore
347
+ if (appStateMock.removeEventListener) {
348
+ AppState.removeEventListener = jest.fn(
349
+ appStateMock.removeEventListener
350
+ );
351
+ }
352
+ });
353
+ it('registers and unregisters AppState', async () => {
354
+ // GIVEN
355
+ const navigationRef1 = createRef<any>();
356
+ render(<FakeNavigator1 navigationRef={navigationRef1} />);
357
+ const navigationRef2 = createRef<any>();
358
+ render(<FakeNavigator2 navigationRef={navigationRef2} />);
359
+
360
+ // WHEN
361
+ DdRumReactNavigationTracking.startTrackingViews(
362
+ navigationRef1.current
363
+ );
364
+ DdRumReactNavigationTracking.stopTrackingViews(
365
+ navigationRef1.current
366
+ );
367
+ expect(appStateMock.listeners.change).toHaveLength(0);
368
+
369
+ DdRumReactNavigationTracking.startTrackingViews(
370
+ navigationRef2.current
371
+ );
372
+
373
+ // THEN
374
+ expect(AppState.addEventListener).toHaveBeenCalledTimes(2);
375
+ expect(appStateMock.listeners.change).toHaveLength(1);
376
+
377
+ // WHEN we go in background mode
378
+ appStateMock.changeValue('background');
379
+
380
+ // THEN the listener is only called once
381
+ expect(DdRum.stopView).toHaveBeenCalledTimes(1);
382
+ });
383
+
384
+ it('does not log AppState changes when tracking is stopped', async () => {
385
+ // GIVEN
386
+ const navigationRef = createRef<any>();
387
+ render(<FakeNavigator1 navigationRef={navigationRef} />);
388
+
389
+ // WHEN
390
+ DdRumReactNavigationTracking.startTrackingViews(
391
+ navigationRef.current
392
+ );
393
+ DdRumReactNavigationTracking.stopTrackingViews(
394
+ navigationRef.current
395
+ );
396
+ appStateMock.changeValue('background');
397
+
398
+ // THEN
399
+ expect(DdRum.stopView).not.toHaveBeenCalled();
400
+ expect(InternalLog.log).not.toHaveBeenCalledWith(
401
+ 'We could not determine the route when changing the application state to: background. No RUM View event will be sent in this case.',
402
+ 'error'
403
+ );
404
+ });
405
+
406
+ it('stops active view when app goes into background', async () => {
407
+ // GIVEN
408
+ const navigationRef = createRef<any>();
409
+ render(<FakeNavigator1 navigationRef={navigationRef} />);
410
+
411
+ DdRumReactNavigationTracking.startTrackingViews(
412
+ navigationRef.current
413
+ );
414
+
415
+ // WHEN
416
+ appStateMock.changeValue('background');
417
+
418
+ // THEN
419
+ expect(DdRum.stopView).toHaveBeenCalledTimes(1);
420
+ expect(DdRum.stopView).toHaveBeenCalledWith(
421
+ navigationRef.current?.getCurrentRoute()?.key
422
+ );
423
+ expect(
424
+ typeof navigationRef.current?.getCurrentRoute()?.key
425
+ ).toBe('string');
426
+ });
427
+
428
+ it('starts last view when app goes into foreground', async () => {
429
+ // GIVEN
430
+ const navigationRef = createRef<any>();
431
+ render(<FakeNavigator1 navigationRef={navigationRef} />);
432
+
433
+ DdRumReactNavigationTracking.startTrackingViews(
434
+ navigationRef.current
435
+ );
436
+
437
+ // WHEN
438
+ appStateMock.changeValue('background');
439
+ appStateMock.changeValue('active');
440
+
441
+ // THEN
442
+ expect(DdRum.stopView).toHaveBeenCalledTimes(1);
443
+ expect(DdRum.startView).toHaveBeenCalledTimes(2);
444
+ });
445
+
446
+ it('does not stop view when no navigator attached', async () => {
447
+ // GIVEN
448
+ const navigationRef = createRef<any>();
449
+ render(<FakeNavigator1 navigationRef={navigationRef} />);
450
+
451
+ DdRumReactNavigationTracking.startTrackingViews(
452
+ navigationRef.current
453
+ );
454
+ DdRumReactNavigationTracking.stopTrackingViews(
455
+ navigationRef.current
456
+ );
457
+
458
+ // WHEN
459
+ appStateMock.changeValue('background');
460
+
461
+ // THEN
462
+ expect(DdRum.stopView).not.toHaveBeenCalled();
463
+ });
464
+ }
465
+ );
466
+
467
+ describe('Android back handler', () => {
468
+ it('does not send an error when the app closes with Android back button', async () => {
469
+ // GIVEN
470
+ const navigationRef1 = createRef<any>();
471
+ const { unmount } = render(
472
+ <FakeNavigator1 navigationRef={navigationRef1} />
473
+ );
474
+ DdRumReactNavigationTracking.startTrackingViews(
475
+ navigationRef1.current
476
+ );
477
+
478
+ // WHEN back is pressed
479
+ mockBackHandler.mockPressBack();
480
+ // THEN app is closed
481
+ expect(BackHandler.exitApp).toHaveBeenCalled();
482
+
483
+ // WHEN app is restarted and we navigate
484
+ unmount();
485
+ const navigationRef2 = createRef<any>();
486
+ const { getByText } = render(
487
+ <FakeNavigator2 navigationRef={navigationRef2} />
488
+ );
489
+ DdRumReactNavigationTracking.startTrackingViews(
490
+ navigationRef2.current
491
+ );
492
+ const goToAboutButton = getByText('Go to About');
493
+ fireEvent(goToAboutButton, 'press');
494
+
495
+ // THEN new navigation is attached, no error and message is sent
496
+ expect(DdRum.startView).toHaveBeenLastCalledWith(
497
+ expect.any(String),
498
+ 'About'
499
+ );
500
+ expect(InternalLog.log).not.toHaveBeenCalledWith(
501
+ DdRumReactNavigationTracking.NAVIGATION_REF_IN_USE_ERROR_MESSAGE,
502
+ 'error'
503
+ );
504
+ });
505
+ });
506
+ }
507
+ );