-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
118 lines (95 loc) · 3.04 KB
/
index.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
/**
* External dependencies
*/
import { createStore, applyMiddleware } from 'redux';
/**
* Internal dependencies
*/
import createMiddleware from '../';
jest.useFakeTimers();
describe( 'createMiddleware', () => {
function createStoreWithMiddleware( middleware ) {
const reducer = ( state = null, action ) => action.nextState || state;
return createStore( reducer, applyMiddleware( middleware ) );
}
it( 'should not alter dispatch flow of uncontrolled action', () => {
const middleware = createMiddleware();
const store = createStoreWithMiddleware( middleware );
store.dispatch( { type: 'CHANGE', nextState: 1 } );
expect( store.getState() ).toBe( 1 );
} );
it( 'should dispatch yielded actions', () => {
const middleware = createMiddleware();
const store = createStoreWithMiddleware( middleware );
function* createAction() {
yield { type: 'CHANGE', nextState: 1 };
}
store.dispatch( createAction() );
expect( store.getState() ).toBe( 1 );
} );
it( 'should continue only once control condition resolves', ( done ) => {
const middleware = createMiddleware( {
WAIT: () => new Promise( ( resolve ) => setTimeout( resolve, 0 ) ),
} );
const store = createStoreWithMiddleware( middleware );
function* createAction() {
yield { type: 'WAIT' };
yield { type: 'CHANGE', nextState: 1 };
}
store.dispatch( createAction() );
expect( store.getState() ).toBe( null );
jest.runAllTimers();
expect( store.getState() ).toBe( null );
// Promise resolution occurs on next tick.
process.nextTick( () => {
expect( store.getState() ).toBe( 1 );
done();
} );
} );
it( 'assigns sync controlled return value into yield assignment', () => {
const middleware = createMiddleware( {
RETURN_TWO: () => 2,
} );
const store = createStoreWithMiddleware( middleware );
function* createAction() {
const nextState = yield { type: 'RETURN_TWO' };
yield { type: 'CHANGE', nextState };
}
store.dispatch( createAction() );
expect( store.getState() ).toBe( 2 );
} );
it( 'assigns async controlled return value into yield assignment', ( done ) => {
const middleware = createMiddleware( {
WAIT: ( action ) => new Promise( ( resolve ) => {
setTimeout( () => {
resolve( action.value );
}, 0 );
} ),
} );
const store = createStoreWithMiddleware( middleware );
function* createAction() {
const nextState = yield { type: 'WAIT', value: 2 };
return { type: 'CHANGE', nextState };
}
store.dispatch( createAction() );
expect( store.getState() ).toBe( null );
jest.runAllTimers();
expect( store.getState() ).toBe( null );
process.nextTick( () => {
expect( store.getState() ).toBe( 2 );
done();
} );
} );
it( 'kills continuation if control returns undefined', () => {
const middleware = createMiddleware( {
KILL: () => {},
} );
const store = createStoreWithMiddleware( middleware );
function* createAction() {
yield { type: 'KILL' };
return { type: 'CHANGE', nextState: 1 };
}
store.dispatch( createAction() );
expect( store.getState() ).toBe( null );
} );
} );