-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBranchSubscriber.test.js
102 lines (82 loc) · 3.45 KB
/
BranchSubscriber.test.js
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
import { NativeModules } from 'react-native'
const { RNBranch } = NativeModules
import { BranchSubscriber } from 'react-native-branch'
test('default initializes with no options', () => {
const subscriber = new BranchSubscriber(null)
expect(subscriber.options).toEqual({})
})
test('stores options passed to the constructor', () => {
const subscriber = new BranchSubscriber({
checkCachedEvents: false
})
expect(subscriber.options).toEqual({ checkCachedEvents: false })
})
test('subscribes to init session start events', () => {
const subscriber = new BranchSubscriber({
checkCachedEvents: false,
onOpenStart: ({uri}) => {},
})
subscriber._nativeEventEmitter.addListener = jest.fn((eventType, listener) => {})
subscriber.subscribe()
expect(subscriber._nativeEventEmitter.addListener.mock.calls.length).toBe(1)
expect(subscriber._nativeEventEmitter.addListener.mock.calls[0][0]).toBe(RNBranch.INIT_SESSION_START)
expect(subscriber._nativeEventEmitter.addListener.mock.calls[0][1]).toBe(subscriber.options.onOpenStart)
})
test('subscribes to init session success & error events', () => {
const subscriber = new BranchSubscriber({
checkCachedEvents: false,
onOpenComplete: ({params, error, uri}) => {},
})
subscriber._nativeEventEmitter.addListener = jest.fn((eventType, listener) => {})
subscriber.subscribe()
expect(subscriber._nativeEventEmitter.addListener.mock.calls.length).toBe(2)
// This comparison ignores the call order.
const mockArgs = subscriber._nativeEventEmitter.addListener.mock.calls.map(call => call[0]).sort()
expect(mockArgs).toEqual([
RNBranch.INIT_SESSION_ERROR,
RNBranch.INIT_SESSION_SUCCESS,
])
expect(subscriber._nativeEventEmitter.addListener.mock.calls[0][1]).toBe(subscriber.options.onOpenComplete)
expect(subscriber._nativeEventEmitter.addListener.mock.calls[1][1]).toBe(subscriber.options.onOpenComplete)
})
// async test
test('will return a cached event when appropriate', done => {
const mockResult = {
params: {
'+clicked_branch_link': false,
'+is_first_session': false,
},
error: null,
uri: null,
}
//* Mock promise from redeemInitSessionResult
RNBranch.redeemInitSessionResult.mockReturnValueOnce(Promise.resolve(mockResult))
// */
// Set up subscriber, mocking the callbacks
const subscriber = new BranchSubscriber({
checkCachedEvents: true,
onOpenStart: jest.fn(({uri}) => {}),
onOpenComplete: jest.fn(({params, error, uri}) => {}),
})
// mock subscriber._nativeEventEmitter.addListener.
// TODO: Brittle test
// Expect first onOpenStart, then onOpenComplete, then _nativeEventEmitter.addListener three times,
// with INIT_SESSION_ERROR last.
subscriber._nativeEventEmitter.addListener = (eventType, listener) => {
if (eventType !== RNBranch.INIT_SESSION_ERROR) return
// --- Check results ---
// Expect onOpenStart and onOpenComplete both to be called
// uri passed to onOpenStart
expect(subscriber.options.onOpenStart.mock.calls.length).toBe(1)
expect(subscriber.options.onOpenStart.mock.calls[0][0]).toEqual({uri: null})
// full result passed to onOpenComplete
expect(subscriber.options.onOpenComplete.mock.calls.length).toBe(1)
expect(subscriber.options.onOpenComplete.mock.calls[0][0]).toEqual(mockResult)
// state cleared
expect(subscriber._checkCachedEvents).toBe(false)
done()
}
expect(subscriber._checkCachedEvents).toBe(true)
// --- Code under test ---
subscriber.subscribe()
})