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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/README.md +51 -1
  2. package/lib/commonjs/index.js +8 -4
  3. package/lib/commonjs/index.js.map +1 -1
  4. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js +36 -18
  5. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js.map +1 -1
  6. package/lib/module/index.js +2 -2
  7. package/lib/module/index.js.map +1 -1
  8. package/lib/module/rum/instrumentation/DdRumReactNavigationTracking.js +35 -17
  9. package/lib/module/rum/instrumentation/DdRumReactNavigationTracking.js.map +1 -1
  10. package/lib/typescript/core/src/DdSdkReactNative.d.ts +11 -0
  11. package/lib/typescript/core/src/DdSdkReactNativeConfiguration.d.ts +6 -0
  12. package/lib/typescript/core/src/NativeSdkVerbosity.d.ts +9 -0
  13. package/lib/typescript/core/src/ProxyConfiguration.d.ts +56 -0
  14. package/lib/typescript/core/src/Timer.d.ts +20 -0
  15. package/lib/typescript/core/src/index.d.ts +2 -1
  16. package/lib/typescript/core/src/rum/instrumentation/DdEventsInterceptor.d.ts +2 -0
  17. package/lib/typescript/core/src/rum/instrumentation/DdRumErrorTracking.d.ts +2 -0
  18. package/lib/typescript/core/src/rum/instrumentation/DdRumResourceTracking.d.ts +4 -0
  19. package/lib/typescript/core/src/rum/instrumentation/DdRumXhr.d.ts +2 -2
  20. package/lib/typescript/core/src/types.d.ts +25 -24
  21. package/lib/typescript/core/src/version.d.ts +1 -0
  22. package/lib/typescript/react-navigation/src/index.d.ts +2 -2
  23. package/lib/typescript/react-navigation/src/rum/instrumentation/DdRumReactNavigationTracking.d.ts +5 -3
  24. package/package.json +6 -10
  25. package/src/__tests__/rum/instrumentation/DdRumReactNavigationTracking.test.tsx +397 -0
  26. package/src/index.tsx +2 -2
  27. package/src/rum/instrumentation/DdRumReactNavigationTracking.tsx +44 -17
@@ -46,25 +46,25 @@ export declare type DdLogsType = {
46
46
  * @param message: The message to send.
47
47
  * @param context: The additional context to send.
48
48
  */
49
- debug(message: string, context: object): Promise<void>;
49
+ debug(message: string, context?: object): Promise<void>;
50
50
  /**
51
51
  * Send a log with level info.
52
52
  * @param message: The message to send.
53
53
  * @param context: The additional context to send.
54
54
  */
55
- info(message: string, context: object): Promise<void>;
55
+ info(message: string, context?: object): Promise<void>;
56
56
  /**
57
57
  * Send a log with level warn.
58
58
  * @param message: The message to send.
59
59
  * @param context: The additional context to send.
60
60
  */
61
- warn(message: string, context: object): Promise<void>;
61
+ warn(message: string, context?: object): Promise<void>;
62
62
  /**
63
63
  * Send a log with level error.
64
64
  * @param message: The message to send.
65
65
  * @param context: The additional context to send.
66
66
  */
67
- error(message: string, context: object): Promise<void>;
67
+ error(message: string, context?: object): Promise<void>;
68
68
  };
69
69
  /**
70
70
  * The entry point to use Datadog's Trace feature.
@@ -73,17 +73,17 @@ export declare type DdTraceType = {
73
73
  /**
74
74
  * Start a span, and returns a unique identifier for the span.
75
75
  * @param operation: The operation name of the span.
76
- * @param timestampMs: The timestamp when the operation started (in milliseconds).
77
76
  * @param context: The additional context to send.
77
+ * @param timestampMs: The timestamp when the operation started (in milliseconds). If not provided, current timestamp will be used.
78
78
  */
79
- startSpan(operation: string, timestampMs: number, context: object): Promise<string>;
79
+ startSpan(operation: string, context?: object, timestampMs?: number): Promise<string>;
80
80
  /**
81
81
  * Finish a started span.
82
82
  * @param spanId: The unique identifier of the span.
83
- * @param timestampMs: The timestamp when the operation stopped (in milliseconds).
84
83
  * @param context: The additional context to send.
84
+ * @param timestampMs: The timestamp when the operation stopped (in milliseconds). If not provided, current timestamp will be used.
85
85
  */
86
- finishSpan(spanId: string, timestampMs: number, context: object): Promise<void>;
86
+ finishSpan(spanId: string, context?: object, timestampMs?: number): Promise<void>;
87
87
  };
88
88
  /**
89
89
  * The entry point to use Datadog's RUM feature.
@@ -93,66 +93,67 @@ export declare type DdRumType = {
93
93
  * Start tracking a RUM View.
94
94
  * @param key: The view unique key identifier.
95
95
  * @param name: The view name.
96
- * @param timestampMs: The timestamp when the view started (in milliseconds).
97
96
  * @param context: The additional context to send.
97
+ * @param timestampMs: The timestamp when the view started (in milliseconds). If not provided, current timestamp will be used.
98
98
  */
99
- startView(key: string, name: string, timestampMs: number, context: object): Promise<void>;
99
+ startView(key: string, name: string, context?: object, timestampMs?: number): Promise<void>;
100
100
  /**
101
101
  * Stop tracking a RUM View.
102
102
  * @param key: The view unique key identifier.
103
- * @param timestampMs: The timestamp when the view stopped (in milliseconds).
104
103
  * @param context: The additional context to send.
104
+ * @param timestampMs: The timestamp when the view stopped (in milliseconds). If not provided, current timestamp will be used.
105
105
  */
106
- stopView(key: string, timestampMs: number, context: object): Promise<void>;
106
+ stopView(key: string, context?: object, timestampMs?: number): Promise<void>;
107
107
  /**
108
108
  * Start tracking a RUM Action.
109
109
  * @param type: The action type (tap, scroll, swipe, click, custom).
110
110
  * @param name: The action name.
111
- * @param timestampMs: The timestamp when the action started (in milliseconds).
112
111
  * @param context: The additional context to send.
112
+ * @param timestampMs: The timestamp when the action started (in milliseconds). If not provided, current timestamp will be used.
113
113
  */
114
- startAction(type: string, name: string, timestampMs: number, context: object): Promise<void>;
114
+ startAction(type: string, name: string, context?: object, timestampMs?: number): Promise<void>;
115
115
  /**
116
116
  * Stop tracking the ongoing RUM Action.
117
- * @param timestampMs: The timestamp when the action stopped (in milliseconds).
118
117
  * @param context: The additional context to send.
118
+ * @param timestampMs: The timestamp when the action stopped (in milliseconds). If not provided, current timestamp will be used.
119
119
  */
120
- stopAction(timestampMs: number, context: object): Promise<void>;
120
+ stopAction(context?: object, timestampMs?: number): Promise<void>;
121
121
  /**
122
122
  * Add a RUM Action.
123
123
  * @param type: The action type (tap, scroll, swipe, click, custom).
124
124
  * @param name: The action name.
125
- * @param timestampMs: The timestamp when the action occurred (in milliseconds).
126
125
  * @param context: The additional context to send.
126
+ * @param timestampMs: The timestamp when the action occurred (in milliseconds). If not provided, current timestamp will be used.
127
127
  */
128
- addAction(type: string, name: string, timestampMs: number, context: object): Promise<void>;
128
+ addAction(type: string, name: string, context?: object, timestampMs?: number): Promise<void>;
129
129
  /**
130
130
  * Start tracking a RUM Resource.
131
131
  * @param key: The resource unique key identifier.
132
132
  * @param method: The resource method (GET, POST, …).
133
133
  * @param url: The resource url.
134
- * @param timestampMs: The timestamp when the resource started (in milliseconds).
135
134
  * @param context: The additional context to send.
135
+ * @param timestampMs: The timestamp when the resource started (in milliseconds). If not provided, current timestamp will be used.
136
136
  */
137
- startResource(key: string, method: string, url: string, timestampMs: number, context: object): Promise<void>;
137
+ startResource(key: string, method: string, url: string, context?: object, timestampMs?: number): Promise<void>;
138
138
  /**
139
139
  * Stop tracking a RUM Resource.
140
140
  * @param key: The resource unique key identifier.
141
141
  * @param statusCode: The resource status code.
142
142
  * @param kind: The resource's kind (xhr, document, image, css, font, …).
143
- * @param timestampMs: The timestamp when the resource stopped (in milliseconds).
143
+ * @param size: The resource size in bytes. If size is unknown, use -1.
144
144
  * @param context: The additional context to send.
145
+ * @param timestampMs: The timestamp when the resource stopped (in milliseconds). If not provided, current timestamp will be used.
145
146
  */
146
- stopResource(key: string, statusCode: number, kind: string, timestampMs: number, context: object): Promise<void>;
147
+ stopResource(key: string, statusCode: number, kind: string, size?: number, context?: object, timestampMs?: number): Promise<void>;
147
148
  /**
148
149
  * Add a RUM Error.
149
150
  * @param message: The error message.
150
151
  * @param source: The error source (network, source, console, logger, …).
151
152
  * @param stacktrace: The error stacktrace.
152
- * @param timestampMs: The timestamp when the error occurred (in milliseconds).
153
153
  * @param context: The additional context to send.
154
+ * @param timestampMs: The timestamp when the error occurred (in milliseconds). If not provided, current timestamp will be used.
154
155
  */
155
- addError(message: string, source: string, stacktrace: string, timestampMs: number, context: object): Promise<void>;
156
+ addError(message: string, source: string, stacktrace: string, context?: object, timestampMs?: number): Promise<void>;
156
157
  /**
157
158
  * Adds a specific timing in the active View. The timing duration will be computed as the difference between the time the View was started and the time this function was called.
158
159
  * @param name: The name of the new custom timing attribute. Timings can be nested up to 8 levels deep. Names using more than 8 levels will be sanitized by SDK.
@@ -0,0 +1 @@
1
+ export declare const version = "1.0.0-rc4";
@@ -1,2 +1,2 @@
1
- import DdRumReactNavigationTracking from './rum/instrumentation/DdRumReactNavigationTracking';
2
- export { DdRumReactNavigationTracking };
1
+ import { DdRumReactNavigationTracking, ViewNamePredicate } from './rum/instrumentation/DdRumReactNavigationTracking';
2
+ export { DdRumReactNavigationTracking, ViewNamePredicate };
@@ -1,16 +1,18 @@
1
- import type { NavigationContainerRef } from "@react-navigation/native";
1
+ import type { NavigationContainerRef, Route } from "@react-navigation/native";
2
+ export declare type ViewNamePredicate = (route: Route<string, any | undefined>, trackedName: string) => string;
2
3
  /**
3
4
  * Provides RUM integration for the [ReactNavigation](https://reactnavigation.org/) API.
4
5
  */
5
- export default class DdRumReactNavigationTracking {
6
+ export declare class DdRumReactNavigationTracking {
6
7
  private static registeredContainer;
7
8
  private static navigationStateChangeListener;
8
9
  private static appStateListener;
10
+ private static viewNamePredicate;
9
11
  /**
10
12
  * Starts tracking the NavigationContainer and sends a RUM View event every time the navigation route changed.
11
13
  * @param navigationRef the reference to the real NavigationContainer.
12
14
  */
13
- static startTrackingViews(navigationRef: NavigationContainerRef | null): void;
15
+ static startTrackingViews(navigationRef: NavigationContainerRef | null, viewNamePredicate?: ViewNamePredicate): void;
14
16
  /**
15
17
  * Stops tracking the NavigationContainer.
16
18
  * @param navigationRef the reference to the real NavigationContainer.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datadog/mobile-react-navigation",
3
- "version": "1.0.0-beta4",
3
+ "version": "1.0.0-rc4",
4
4
  "description": "A client-side React Native module to interact with Datadog",
5
5
  "keywords": [
6
6
  "datadog",
@@ -21,11 +21,7 @@
21
21
  "main": "lib/commonjs/index",
22
22
  "files": [
23
23
  "src",
24
- "lib",
25
- "!lib/typescript/example",
26
- "!**/__tests__",
27
- "!**/__fixtures__",
28
- "!**/__mocks__"
24
+ "lib"
29
25
  ],
30
26
  "types": "lib/typescript/react-navigation/src/index.d.ts",
31
27
  "react-native": "src/index",
@@ -40,7 +36,7 @@
40
36
  "prepare": "bob build"
41
37
  },
42
38
  "devDependencies": {
43
- "@datadog/mobile-react-native": "^1.0.0-beta4",
39
+ "@datadog/mobile-react-native": "^1.0.0-rc4",
44
40
  "@react-navigation/native": "5.9.2",
45
41
  "@react-navigation/stack": "5.14.2",
46
42
  "react-native-gesture-handler": "1.10.3",
@@ -49,8 +45,8 @@
49
45
  "peerDependencies": {
50
46
  "@datadog/mobile-react-native": "^1.0.0-beta4",
51
47
  "@react-navigation/native": "^5.6.0",
52
- "react": "^16.13.1",
53
- "react-native": "^0.63.4"
48
+ "react": ">=16.13.1",
49
+ "react-native": ">=0.63.4 <1.0"
54
50
  },
55
51
  "jest": {
56
52
  "preset": "react-native",
@@ -76,5 +72,5 @@
76
72
  ]
77
73
  ]
78
74
  },
79
- "gitHead": "7087bb49f594e4125a4884dfbbe96265abd8a86a"
75
+ "gitHead": "c1bd4e6ded0b84298ca011eba679027ae7fe7841"
80
76
  }
@@ -0,0 +1,397 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import 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';
12
+ import { render, fireEvent } from '@testing-library/react-native';
13
+ import { createStackNavigator } from '@react-navigation/stack';
14
+
15
+ jest.mock('@datadog/mobile-react-native', () => {
16
+ return {
17
+ DdRum: {
18
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
19
+ startView: jest.fn().mockImplementation(() => { }),
20
+ stopView: jest.fn().mockImplementation(() => { })
21
+ },
22
+ };
23
+ });
24
+
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
+ // Silence the warning https://github.com/facebook/react-native/issues/11094#issuecomment-263240420
42
+ jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');
43
+ jest.useFakeTimers();
44
+
45
+
46
+ beforeEach(() => {
47
+
48
+ jest.setTimeout(20000);
49
+ DdRum.startView.mockClear();
50
+ DdRum.stopView.mockClear();
51
+ AppState.addEventListener.mockClear();
52
+ AppState.removeEventListener.mockClear();
53
+
54
+ DdRumReactNavigationTracking.registeredContainer = null;
55
+ DdRumReactNavigationTracking.navigationStateChangeListener = null;
56
+ DdRumReactNavigationTracking.appStateListener = null;
57
+ })
58
+
59
+ // 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
+ }
package/src/index.tsx CHANGED
@@ -4,6 +4,6 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import DdRumReactNavigationTracking from './rum/instrumentation/DdRumReactNavigationTracking';
7
+ import { DdRumReactNavigationTracking, ViewNamePredicate } from './rum/instrumentation/DdRumReactNavigationTracking';
8
8
 
9
- export { DdRumReactNavigationTracking };
9
+ export { DdRumReactNavigationTracking, ViewNamePredicate };