@datadog/mobile-react-navigation 1.0.0-rc3 → 1.0.0-rc7

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