-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBranchSubscriber.test.js
137 lines (110 loc) · 4.48 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
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
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()
})
test('passes a non-null uri to onOpenStart when available', done => {
const mockResult = {
params: {
'+clicked_branch_link': true,
'+is_first_session': false,
},
error: null,
uri: 'https://abcd.app.link/xyz',
}
// 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}) => {}),
})
// mock subscriber._nativeEventEmitter.addListener.
subscriber._nativeEventEmitter.addListener = (eventType, listener) => {
// --- Check results ---
// Expect onOpenStart 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: mockResult.uri})
done()
}
expect(subscriber._checkCachedEvents).toBe(true)
// --- Code under test ---
subscriber.subscribe()
})