@datadog/mobile-react-navigation 1.0.0-rc5 → 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 (33) hide show
  1. package/lib/commonjs/index.js.map +1 -1
  2. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js +70 -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 +66 -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 +14 -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/DdRumResourceTracking.d.ts +10 -9
  20. package/lib/typescript/core/src/rum/instrumentation/DdRumUserInteractionTracking.d.ts +3 -3
  21. package/lib/typescript/core/src/rum/instrumentation/DdRumXhr.d.ts +13 -3
  22. package/lib/typescript/core/src/rum/instrumentation/ShallowObjectEqualityChecker.d.ts +4 -0
  23. package/lib/typescript/core/src/version.d.ts +1 -1
  24. package/lib/typescript/react-navigation/src/rum/instrumentation/DdRumReactNavigationTracking.d.ts +8 -3
  25. package/lib/typescript/react-navigation/src/rum/instrumentation/react-navigation.d.ts +116 -0
  26. package/package.json +11 -6
  27. package/src/__tests__/rum/instrumentation/DdRumReactNavigationTracking.test.tsx +467 -384
  28. package/src/__tests__/rum/instrumentation/__utils__/AppStateMock.ts +56 -0
  29. package/src/__tests__/rum/instrumentation/__utils__/Navigators/NavigatorsV5.tsx +105 -0
  30. package/src/__tests__/rum/instrumentation/__utils__/Navigators/NavigatorsV6.tsx +105 -0
  31. package/src/index.tsx +4 -1
  32. package/src/rum/instrumentation/DdRumReactNavigationTracking.tsx +136 -64
  33. package/src/rum/instrumentation/react-navigation.ts +158 -0
@@ -1,411 +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
- let baseConsoleErrorCalled = false;
46
- let baseConsoleError = (...params: unknown) => {
47
- baseConsoleErrorCalled = true
48
- }
49
- let originalConsoleError = undefined
50
-
51
71
  beforeEach(() => {
52
-
72
+ mocked(InternalLog.log).mockClear();
53
73
  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;
61
-
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
62
82
  DdRumReactNavigationTracking.registeredContainer = null;
83
+ // @ts-ignore
63
84
  DdRumReactNavigationTracking.navigationStateChangeListener = null;
64
- DdRumReactNavigationTracking.appStateListener = null;
65
- })
66
-
67
- afterEach(() => {
68
- console.error = originalConsoleError
69
- })
85
+ });
70
86
 
71
87
  // 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
- }
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
+ );