|
1 | 1 | import expect from 'expect'
|
2 | 2 | import { createStore, combineReducers } from '../src/index'
|
3 |
| -import { |
4 |
| - addTodo, |
5 |
| - dispatchInMiddle, |
| 3 | +import { |
| 4 | + addTodo, |
| 5 | + dispatchInMiddle, |
6 | 6 | getStateInMiddle,
|
7 | 7 | subscribeInMiddle,
|
8 | 8 | unsubscribeInMiddle,
|
9 |
| - throwError, |
10 |
| - unknownAction |
| 9 | + throwError, |
| 10 | + unknownAction |
11 | 11 | } from './helpers/actionCreators'
|
12 | 12 | import * as reducers from './helpers/reducers'
|
13 | 13 | import * as Rx from 'rxjs'
|
@@ -388,18 +388,40 @@ describe('createStore', () => {
|
388 | 388 |
|
389 | 389 | store.dispatch(unknownAction())
|
390 | 390 | expect(listener1.calls.length).toBe(1)
|
391 |
| - expect(listener2.calls.length).toBe(2) |
392 |
| - expect(listener3.calls.length).toBe(2) |
| 391 | + expect(listener2.calls.length).toBe(1) |
| 392 | + expect(listener3.calls.length).toBe(1) |
393 | 393 | expect(listener4.calls.length).toBe(1)
|
394 | 394 |
|
395 | 395 | unsubscribe4()
|
396 | 396 | store.dispatch(unknownAction())
|
397 | 397 | expect(listener1.calls.length).toBe(1)
|
398 |
| - expect(listener2.calls.length).toBe(3) |
399 |
| - expect(listener3.calls.length).toBe(3) |
| 398 | + expect(listener2.calls.length).toBe(2) |
| 399 | + expect(listener3.calls.length).toBe(2) |
400 | 400 | expect(listener4.calls.length).toBe(1)
|
401 | 401 | })
|
402 | 402 |
|
| 403 | + it('notifies a listener unsubscribed with nested dispatch with the new state', () => { |
| 404 | + const store = createStore(reducers.todos) |
| 405 | + |
| 406 | + var firstCall = true |
| 407 | + const listener1 = expect.createSpy().andCall(() => { |
| 408 | + unsubscribe2() |
| 409 | + if (firstCall) { |
| 410 | + firstCall = false |
| 411 | + store.dispatch(unknownAction()) |
| 412 | + } |
| 413 | + }) |
| 414 | + const listener2 = expect.createSpy() |
| 415 | + |
| 416 | + store.subscribe(listener1) |
| 417 | + let unsubscribe2 = store.subscribe(listener2) |
| 418 | + |
| 419 | + store.dispatch(unknownAction()) |
| 420 | + expect(listener1.calls.length).toBe(2) |
| 421 | + expect(listener2.calls.length).toBe(1) |
| 422 | + }) |
| 423 | + |
| 424 | + |
403 | 425 | it('provides an up-to-date state when a subscriber is notified', done => {
|
404 | 426 | const store = createStore(reducers.todos)
|
405 | 427 | store.subscribe(() => {
|
|
0 commit comments