Skip to content

Commit c4c3d56

Browse files
committed
Be sure to use try/catch in async tests
1 parent 32bc723 commit c4c3d56

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

test/BranchSubscriber.test.js

+40-25
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ test('will return a cached event when appropriate', done => {
5959
uri: null,
6060
}
6161

62+
// expectedResult is mockResult with +rn_cached_initial_event added to
63+
// params.
64+
const expectedParams = {
65+
...mockResult.params,
66+
'+rn_cached_initial_event': true,
67+
}
68+
expectedParams['+rn_cached_initial_event'] = true
69+
const expectedResult = {
70+
params: expectedParams,
71+
error: mockResult.error,
72+
uri: mockResult.uri,
73+
}
74+
6275
// Mock promise from redeemInitSessionResult
6376
RNBranch.redeemInitSessionResult.mockReturnValueOnce(Promise.resolve(mockResult))
6477

@@ -70,35 +83,33 @@ test('will return a cached event when appropriate', done => {
7083
})
7184

7285
// mock subscriber._nativeEventEmitter.addListener.
73-
74-
// TODO: Brittle test
75-
// Expect first onOpenStart, then onOpenComplete, then _nativeEventEmitter.addListener three times,
76-
// with INIT_SESSION_ERROR last.
7786
subscriber._nativeEventEmitter.addListener = (eventType, listener) => {
87+
// TODO: Brittle test
88+
// Expect first onOpenStart, then onOpenComplete, then _nativeEventEmitter.addListener three times,
89+
// with INIT_SESSION_ERROR last.
7890
if (eventType !== RNBranch.INIT_SESSION_ERROR) return
7991

8092
// --- Check results ---
8193

82-
// Expect onOpenStart and onOpenComplete both to be called
83-
84-
// uri passed to onOpenStart
85-
expect(subscriber.options.onOpenStart.mock.calls.length).toBe(1)
86-
expect(subscriber.options.onOpenStart.mock.calls[0][0]).toEqual({uri: null})
94+
try {
95+
// Expect onOpenStart and onOpenComplete both to be called
8796

88-
// full result passed to onOpenComplete with +rn_cached_initial_event: true
89-
expect(subscriber.options.onOpenComplete.mock.calls.length).toBe(1)
90-
const actualResult = subscriber.options.onOpenComplete.mock.calls[0][0]
97+
// uri passed to onOpenStart
98+
expect(subscriber.options.onOpenStart.mock.calls.length).toBe(1)
99+
expect(subscriber.options.onOpenStart.mock.calls[0][0]).toEqual({uri: null})
91100

92-
let expectedParams = mockResult.params
93-
expectedParams['+rn_cached_initial_event'] = true
94-
expect(actualResult.params).toEqual(expectedParams)
95-
expect(actualResult.error).toEqual(mockResult.error)
96-
expect(actualResult.uri).toEqual(mockResult.uri)
101+
// full result passed to onOpenComplete with +rn_cached_initial_event: true
102+
expect(subscriber.options.onOpenComplete.mock.calls.length).toBe(1)
103+
const actualResult = subscriber.options.onOpenComplete.mock.calls[0][0]
104+
expect(actualResult).toEqual(expectedResult)
97105

98-
// state cleared
99-
expect(subscriber._checkCachedEvents).toBe(false)
106+
// state cleared
107+
expect(subscriber._checkCachedEvents).toBe(false)
100108

101-
done()
109+
done()
110+
} catch(error) {
111+
done(error)
112+
}
102113
}
103114
expect(subscriber._checkCachedEvents).toBe(true)
104115

@@ -129,12 +140,16 @@ test('passes a non-null uri to onOpenStart when available', done => {
129140
subscriber._nativeEventEmitter.addListener = (eventType, listener) => {
130141
// --- Check results ---
131142

132-
// Expect onOpenStart to be called
133-
// uri passed to onOpenStart
134-
expect(subscriber.options.onOpenStart.mock.calls.length).toBe(1)
135-
expect(subscriber.options.onOpenStart.mock.calls[0][0]).toEqual({uri: mockResult.uri})
143+
try {
144+
// Expect onOpenStart to be called
145+
// uri passed to onOpenStart
146+
expect(subscriber.options.onOpenStart.mock.calls.length).toBe(1)
147+
expect(subscriber.options.onOpenStart.mock.calls[0][0]).toEqual({uri: mockResult.uri})
136148

137-
done()
149+
done()
150+
} catch(error) {
151+
done(error)
152+
}
138153
}
139154
expect(subscriber._checkCachedEvents).toBe(true)
140155

0 commit comments

Comments
 (0)