/
index.js
171 lines (147 loc) · 4.66 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* External dependencies
*/
import { createStore, applyMiddleware } from 'redux';
/**
* Internal dependencies
*/
import createMiddleware from '../';
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', async () => {
const middleware = createMiddleware( {
WAIT: () => new Promise( ( resolve ) => resolve() ),
} );
const store = createStoreWithMiddleware( middleware );
function* createAction() {
yield { type: 'WAIT' };
yield { type: 'CHANGE', nextState: 1 };
}
await store.dispatch( createAction() );
expect( store.getState() ).toBe( 1 );
} );
it( 'should throw if promise rejects', async () => {
expect.hasAssertions();
const middleware = createMiddleware( {
WAIT_FAIL: () =>
new Promise( ( resolve, reject ) => reject( 'Message' ) ),
} );
const store = createStoreWithMiddleware( middleware );
function* createAction() {
try {
yield { type: 'WAIT_FAIL' };
} catch ( error ) {
// eslint-disable-next-line jest/no-conditional-expect
expect( error ).toBe( 'Message' );
}
}
await store.dispatch( createAction() );
} );
it( 'should throw if promise throws', () => {
expect.hasAssertions();
const middleware = createMiddleware( {
WAIT_FAIL: () =>
new Promise( () => {
throw new Error( 'Message' );
} ),
} );
const store = createStoreWithMiddleware( middleware );
function* createAction() {
try {
yield { type: 'WAIT_FAIL' };
} catch ( error ) {
// eslint-disable-next-line jest/no-conditional-expect
expect( error.message ).toBe( 'Message' );
}
}
return store.dispatch( createAction() );
} );
// Currently this test will not error even under conditions producing it but
// instead will have an uncaught error/warning printed in the cli console:
// - (node:37109) UnhandledPromiseRejectionWarning: TypeError: Cannot read
// property 'type' of null (and others)
// See this github thread for context:
// https://github.com/facebook/jest/issues/3251
it( 'should handle a null returned from a caught promise error', () => {
expect.hasAssertions();
const middleware = createMiddleware( {
WAIT_FAIL: () =>
new Promise( () => {
throw new Error( 'Message' );
} ),
} );
const store = createStoreWithMiddleware( middleware );
function* createAction() {
try {
yield { type: 'WAIT_FAIL' };
} catch ( error ) {
// eslint-disable-next-line jest/no-conditional-expect
expect( error.message ).toBe( 'Message' );
return null;
}
}
store.dispatch( createAction() );
} );
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', async () => {
const middleware = createMiddleware( {
WAIT: ( action ) =>
new Promise( ( resolve ) => {
resolve( action.value );
} ),
} );
const store = createStoreWithMiddleware( middleware );
function* createAction() {
const nextState = yield { type: 'WAIT', value: 2 };
return { type: 'CHANGE', nextState };
}
await store.dispatch( createAction() );
expect( store.getState() ).toBe( 2 );
} );
it(
'does not recurse when action like object returns from a sync ' +
'control',
() => {
const post = { type: 'post' };
const middleware = createMiddleware( {
UPDATE: () => post,
} );
const store = createStoreWithMiddleware( middleware );
function* getPostAction() {
const nextState = yield { type: 'UPDATE' };
return { type: 'CHANGE', nextState };
}
store.dispatch( getPostAction() );
expect( store.getState() ).toEqual( post );
}
);
} );