Skip to content

Commit fa32363

Browse files
committed
Async test for cached events
1 parent a7cdf26 commit fa32363

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

test/BranchSubscriber.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,56 @@ test('subscribes to init session success & error events', () => {
4747
expect(subscriber._nativeEventEmitter.addListener.mock.calls[0][1]).toBe(subscriber.options.onOpenComplete)
4848
expect(subscriber._nativeEventEmitter.addListener.mock.calls[1][1]).toBe(subscriber.options.onOpenComplete)
4949
})
50+
51+
// async test
52+
test('will return a cached event when appropriate', done => {
53+
const mockResult = {
54+
params: {
55+
'+clicked_branch_link': false,
56+
'+is_first_session': false,
57+
},
58+
error: null,
59+
uri: null,
60+
}
61+
62+
//* Mock promise from redeemInitSessionResult
63+
RNBranch.redeemInitSessionResult.mockReturnValueOnce(Promise.resolve(mockResult))
64+
// */
65+
66+
// Set up subscriber, mocking the callbacks
67+
const subscriber = new BranchSubscriber({
68+
checkCachedEvents: true,
69+
onOpenStart: jest.fn(({uri}) => {}),
70+
onOpenComplete: jest.fn(({params, error, uri}) => {}),
71+
})
72+
73+
// mock subscriber._nativeEventEmitter.addListener.
74+
75+
// TODO: Brittle test
76+
// Expect first onOpenStart, then onOpenComplete, then _nativeEventEmitter.addListener three times,
77+
// with INIT_SESSION_ERROR last.
78+
subscriber._nativeEventEmitter.addListener = (eventType, listener) => {
79+
if (eventType !== RNBranch.INIT_SESSION_ERROR) return
80+
81+
// --- Check results ---
82+
83+
// Expect onOpenStart and onOpenComplete both to be called
84+
85+
// uri passed to onOpenStart
86+
expect(subscriber.options.onOpenStart.mock.calls.length).toBe(1)
87+
expect(subscriber.options.onOpenStart.mock.calls[0][0]).toEqual({uri: null})
88+
89+
// full result passed to onOpenComplete
90+
expect(subscriber.options.onOpenComplete.mock.calls.length).toBe(1)
91+
expect(subscriber.options.onOpenComplete.mock.calls[0][0]).toEqual(mockResult)
92+
93+
// state cleared
94+
expect(subscriber._checkCachedEvents).toBe(false)
95+
96+
done()
97+
}
98+
expect(subscriber._checkCachedEvents).toBe(true)
99+
100+
// --- Code under test ---
101+
subscriber.subscribe()
102+
})

0 commit comments

Comments
 (0)