Skip to content

Commit b4dec4d

Browse files
committed
Merge pull request #1291 from slorber/master
Add tests for the behavior of unsubscribing affecting next dispatch a…
2 parents ce533b4 + a9e615a commit b4dec4d

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

test/createStore.spec.js

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('createStore', () => {
9090
])
9191

9292
store.dispatch(unknownAction())
93-
expect(store.getState()).toEqual([
93+
expect(store.getState()).toEqual([
9494
{
9595
id: 1,
9696
text: 'Hello'
@@ -140,11 +140,11 @@ describe('createStore', () => {
140140
{
141141
id: 3,
142142
text: 'Perhaps'
143-
},
143+
},
144144
{
145145
id: 1,
146146
text: 'Hello'
147-
},
147+
},
148148
{
149149
id: 2,
150150
text: 'World'
@@ -156,11 +156,11 @@ describe('createStore', () => {
156156
{
157157
id: 3,
158158
text: 'Perhaps'
159-
},
159+
},
160160
{
161161
id: 1,
162162
text: 'Hello'
163-
},
163+
},
164164
{
165165
id: 2,
166166
text: 'World'
@@ -172,15 +172,15 @@ describe('createStore', () => {
172172
{
173173
id: 3,
174174
text: 'Perhaps'
175-
},
175+
},
176176
{
177177
id: 1,
178178
text: 'Hello'
179-
},
179+
},
180180
{
181181
id: 2,
182182
text: 'World'
183-
},
183+
},
184184
{
185185
id: 4,
186186
text: 'Surely'
@@ -286,6 +286,59 @@ describe('createStore', () => {
286286
expect(listenerC.calls.length).toBe(2)
287287
})
288288

289+
it('removes listeners NOT immediately when unsubscribe is called', () => {
290+
const store = createStore(reducers.todos)
291+
292+
const unsubscribeHandles = []
293+
const doUnsubscribeAll = () => unsubscribeHandles.forEach(unsubscribe => unsubscribe() )
294+
295+
const listener1 = expect.createSpy(() => {})
296+
const listener2 = expect.createSpy(() => {})
297+
const listener3 = expect.createSpy(() => {})
298+
299+
unsubscribeHandles.push(store.subscribe(() => listener1()))
300+
unsubscribeHandles.push(store.subscribe(() => {
301+
listener2()
302+
doUnsubscribeAll()
303+
}))
304+
unsubscribeHandles.push(store.subscribe(() => listener3()))
305+
306+
store.dispatch(unknownAction())
307+
store.dispatch(unknownAction())
308+
expect(listener1.calls.length).toBe(1)
309+
expect(listener2.calls.length).toBe(1)
310+
// listener3 is called! decided in #1180
311+
expect(listener3.calls.length).toBe(1)
312+
})
313+
314+
it('does not fire immediately if a listener is added inside another listener', () => {
315+
const store = createStore(reducers.todos)
316+
317+
const listener1 = expect.createSpy(() => {})
318+
const listener2 = expect.createSpy(() => {})
319+
const listener3 = expect.createSpy(() => {})
320+
321+
let listener3Added = false
322+
const maybeAddThirdListener = () => {
323+
if (!listener3Added) {
324+
listener3Added = true
325+
store.subscribe(() => listener3())
326+
}
327+
}
328+
329+
store.subscribe(() => listener1())
330+
store.subscribe(() => {
331+
listener2()
332+
maybeAddThirdListener()
333+
})
334+
335+
store.dispatch(unknownAction())
336+
store.dispatch(unknownAction())
337+
expect(listener1.calls.length).toBe(2)
338+
expect(listener2.calls.length).toBe(2)
339+
expect(listener3.calls.length).toBe(1)
340+
})
341+
289342
it('provides an up-to-date state when a subscriber is notified', done => {
290343
const store = createStore(reducers.todos)
291344
store.subscribe(() => {

0 commit comments

Comments
 (0)