-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathreact-native-navigation.test.ts
More file actions
363 lines (277 loc) · 11.5 KB
/
react-native-navigation.test.ts
File metadata and controls
363 lines (277 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import Plugin from '../react-native-navigation'
import { Breadcrumb } from '@bugsnag/core'
import Client from '@bugsnag/core/client'
interface Event {
componentId: number
componentName: string
passProps: object
}
describe('plugin-react-native-navigation', () => {
it('should throw when Navigation is not passed', () => {
const message = '@bugsnag/plugin-react-native-navigation reference to `Navigation` was undefined'
expect(() => new Plugin()).toThrow(new Error(message))
})
it('adds an event listener on load', () => {
const spy = jest.fn()
const Navigation = {
events () {
return { registerComponentDidAppearListener: spy }
}
}
const plugin = new Plugin(Navigation)
expect(spy).not.toHaveBeenCalled()
const client = new Client({ apiKey: 'API_KEY_YEAH', plugins: [plugin] })
expect(spy).toHaveBeenCalledTimes(1)
expect(client.getContext()).toBe(undefined)
})
it('updates the client context when the listener is triggered', () => {
// Setup a fake listener that should never be called - the plugin should
// replace this on load by calling 'registerComponentDidAppearListener'
let listener = (event: Event) => {
throw new Error(`This function was not supposed to be called! ${event.componentName}`)
}
const Navigation = {
events () {
return {
registerComponentDidAppearListener (callback: (event: Event) => never) {
listener = callback
}
}
}
}
const plugin = new Plugin(Navigation)
const client = new Client({ apiKey: 'API_KEY_YEAH', plugins: [plugin] })
expect(client.getContext()).toBe(undefined)
listener({ componentId: 1, componentName: 'abc xyz', passProps: {} })
expect(client.getContext()).toBe('abc xyz')
listener({ componentId: 2, componentName: 'xyz abc', passProps: {} })
expect(client.getContext()).toBe('xyz abc')
})
it('leaves a breadcrumb when the listener is triggered', () => {
let listener = (event: Event) => {
throw new Error(`This function was not supposed to be called! ${event.componentName}`)
}
const Navigation = {
events () {
return {
registerComponentDidAppearListener (callback: (event: Event) => never) {
listener = callback
}
}
}
}
const breadcrumbs: Breadcrumb[] = []
const plugin = new Plugin(Navigation)
const client = new Client({ apiKey: 'API_KEY_YEAH', plugins: [plugin] })
client.addOnBreadcrumb(breadcrumb => { breadcrumbs.push(breadcrumb) })
expect(breadcrumbs.length).toBe(0)
listener({ componentId: 1, componentName: 'abc xyz', passProps: {} })
expect(breadcrumbs.length).toBe(1)
expect(breadcrumbs[0].message).toBe('React Native Navigation componentDidAppear')
expect(breadcrumbs[0].metadata).toStrictEqual({ to: 'abc xyz', from: undefined })
expect(breadcrumbs[0].type).toBe('navigation')
listener({ componentId: 2, componentName: 'xyz abc', passProps: {} })
expect(breadcrumbs.length).toBe(2)
expect(breadcrumbs[1].message).toBe('React Native Navigation componentDidAppear')
expect(breadcrumbs[1].metadata).toStrictEqual({ to: 'xyz abc', from: 'abc xyz' })
expect(breadcrumbs[1].type).toBe('navigation')
})
it('does not leave a breadcrumb when the component has not changed', () => {
let listener = (event: Event) => {
throw new Error(`This function was not supposed to be called! ${event.componentName}`)
}
const Navigation = {
events () {
return {
registerComponentDidAppearListener (callback: (event: Event) => never) {
listener = callback
}
}
}
}
const breadcrumbs: Breadcrumb[] = []
const plugin = new Plugin(Navigation)
const client = new Client({ apiKey: 'API_KEY_YEAH', plugins: [plugin] })
client.addOnBreadcrumb(breadcrumb => { breadcrumbs.push(breadcrumb) })
expect(breadcrumbs.length).toBe(0)
listener({ componentId: 1, componentName: 'abc xyz', passProps: {} })
expect(breadcrumbs.length).toBe(1)
expect(breadcrumbs[0].message).toBe('React Native Navigation componentDidAppear')
expect(breadcrumbs[0].metadata).toStrictEqual({ to: 'abc xyz', from: undefined })
expect(breadcrumbs[0].type).toBe('navigation')
listener({ componentId: 1, componentName: 'abc xyz', passProps: {} })
expect(breadcrumbs.length).toBe(1)
listener({ componentId: 1, componentName: 'xyz abc', passProps: {} })
expect(breadcrumbs.length).toBe(2)
expect(breadcrumbs[1].message).toBe('React Native Navigation componentDidAppear')
expect(breadcrumbs[1].metadata).toStrictEqual({ to: 'xyz abc', from: 'abc xyz' })
expect(breadcrumbs[1].type).toBe('navigation')
listener({ componentId: 1, componentName: 'xyz abc', passProps: {} })
expect(breadcrumbs.length).toBe(2)
})
it('does not leave a breadcrumb when the "navigation" breadcrumb type is disabled', () => {
let listener = (event: Event) => {
throw new Error(`This function was not supposed to be called! ${event.componentName}`)
}
const Navigation = {
events () {
return {
registerComponentDidAppearListener (callback: (event: Event) => never) {
listener = callback
}
}
}
}
const breadcrumbs: Breadcrumb[] = []
const plugin = new Plugin(Navigation)
const client = new Client({
apiKey: 'API_KEY_YEAH',
plugins: [plugin],
enabledBreadcrumbTypes: ['request', 'process', 'log', 'user', 'state', 'error', 'manual']
})
client.addOnBreadcrumb(breadcrumb => { breadcrumbs.push(breadcrumb) })
expect(breadcrumbs.length).toBe(0)
listener({ componentId: 1, componentName: 'abc xyz', passProps: {} })
expect(breadcrumbs.length).toBe(0)
listener({ componentId: 1, componentName: 'abc xyz', passProps: {} })
expect(breadcrumbs.length).toBe(0)
listener({ componentId: 1, componentName: 'abc xyz', passProps: {} })
expect(breadcrumbs.length).toBe(0)
})
describe('navigation context tracking', () => {
it('tracks navigation between multiple screens', () => {
let listener = (event: Event) => {
throw new Error(`This function was not supposed to be called! ${event.componentName}`)
}
const Navigation = {
events () {
return {
registerComponentDidAppearListener (callback: (event: Event) => never) {
listener = callback
}
}
}
}
const plugin = new Plugin(Navigation)
const client = new Client({ apiKey: 'API_KEY_YEAH', plugins: [plugin] })
expect(client.getContext()).toBe(undefined)
// Navigate to Home
listener({ componentId: 1, componentName: 'Home', passProps: {} })
expect(client.getContext()).toBe('Home')
// Navigate to Profile
listener({ componentId: 2, componentName: 'Profile', passProps: {} })
expect(client.getContext()).toBe('Profile')
// Navigate to Settings
listener({ componentId: 3, componentName: 'Settings', passProps: {} })
expect(client.getContext()).toBe('Settings')
// Back to Home
listener({ componentId: 1, componentName: 'Home', passProps: {} })
expect(client.getContext()).toBe('Home')
})
it('handles navigation with complex component names', () => {
let listener = (event: Event) => {
throw new Error(`This function was not supposed to be called! ${event.componentName}`)
}
const Navigation = {
events () {
return {
registerComponentDidAppearListener (callback: (event: Event) => never) {
listener = callback
}
}
}
}
const plugin = new Plugin(Navigation)
const client = new Client({ apiKey: 'API_KEY_YEAH', plugins: [plugin] })
// Test with namespaced component names
listener({ componentId: 1, componentName: 'com.example.screens.HomeScreen', passProps: {} })
expect(client.getContext()).toBe('com.example.screens.HomeScreen')
// Test with screen names containing special characters
listener({ componentId: 2, componentName: 'User-Details-Screen', passProps: {} })
expect(client.getContext()).toBe('User-Details-Screen')
})
})
describe('breadcrumb metadata', () => {
it('includes previous screen context in breadcrumb', () => {
let listener = (event: Event) => {
throw new Error(`This function was not supposed to be called! ${event.componentName}`)
}
const Navigation = {
events () {
return {
registerComponentDidAppearListener (callback: (event: Event) => never) {
listener = callback
}
}
}
}
const breadcrumbs: Breadcrumb[] = []
const plugin = new Plugin(Navigation)
const client = new Client({ apiKey: 'API_KEY_YEAH', plugins: [plugin] })
client.addOnBreadcrumb(breadcrumb => { breadcrumbs.push(breadcrumb) })
listener({ componentId: 1, componentName: 'Home', passProps: {} })
listener({ componentId: 2, componentName: 'Details', passProps: {} })
expect(breadcrumbs.length).toBe(2)
expect(breadcrumbs[1].metadata).toStrictEqual({ to: 'Details', from: 'Home' })
})
it('sets from as undefined for initial navigation', () => {
let listener = (event: Event) => {
throw new Error(`This function was not supposed to be called! ${event.componentName}`)
}
const Navigation = {
events () {
return {
registerComponentDidAppearListener (callback: (event: Event) => never) {
listener = callback
}
}
}
}
const breadcrumbs: Breadcrumb[] = []
const plugin = new Plugin(Navigation)
const client = new Client({ apiKey: 'API_KEY_YEAH', plugins: [plugin] })
client.addOnBreadcrumb(breadcrumb => { breadcrumbs.push(breadcrumb) })
listener({ componentId: 1, componentName: 'Splash', passProps: {} })
expect(breadcrumbs.length).toBe(1)
expect(breadcrumbs[0].metadata).toStrictEqual({ to: 'Splash', from: undefined })
})
})
describe('plugin error handling', () => {
it('handles events without Navigation API gracefully', () => {
const message = '@bugsnag/plugin-react-native-navigation reference to `Navigation` was undefined'
expect(() => {
// eslint-disable-next-line no-new
new Plugin(undefined)
}).toThrow(new Error(message))
})
it('does not crash when Navigation.events is not available', () => {
// Create a Navigation object without the events method
const Navigation = {
events: () => ({
registerComponentDidAppearListener: jest.fn()
})
}
const plugin = new Plugin(Navigation)
const client = new Client({ apiKey: 'API_KEY_YEAH', plugins: [plugin] })
// Should not throw and client should be properly initialized
expect(client).toBeDefined()
})
it('handles missing Navigation object in plugin constructor', () => {
const message = '@bugsnag/plugin-react-native-navigation reference to `Navigation` was undefined'
expect(() => {
// eslint-disable-next-line no-new
new Plugin(null)
}).toThrow(new Error(message))
})
it('initializes plugin with valid Navigation API', () => {
const Navigation = {
events: () => ({
registerComponentDidAppearListener: jest.fn()
})
}
const plugin = new Plugin(Navigation)
// Plugin should be created successfully
expect(plugin).toBeDefined()
})
})
})