-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
Copy pathmiddleware.test-d.ts
241 lines (189 loc) · 7.13 KB
/
middleware.test-d.ts
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import type {
Action,
Dispatch,
Middleware,
MiddlewareAPI,
Reducer
} from 'redux'
import { applyMiddleware, createStore } from 'redux'
type PromiseDispatch = <T extends Action>(promise: Promise<T>) => Promise<T>
interface Thunk<R, S, DispatchExt = {}> {
(dispatch: Dispatch & ThunkDispatch<S> & DispatchExt, getState: () => S): R
}
interface ThunkDispatch<S, DispatchExt = {}> {
<R>(thunk: Thunk<R, S, DispatchExt>): R
}
declare const logger: () => Middleware
declare const promise: () => Middleware<PromiseDispatch>
declare const thunk: <S, DispatchExt>() => Middleware<
ThunkDispatch<S, DispatchExt>,
S,
Dispatch & ThunkDispatch<S>
>
describe('type tests', () => {
test("Logger middleware doesn't add any extra types to dispatch, just logs actions and state.", () => {
const loggerMiddleware: Middleware =
({ getState }) =>
next =>
action => {
console.log('will dispatch', action)
// Call the next dispatch method in the middleware chain.
const returnValue = next(action)
console.log('state after dispatch', getState())
// This will likely be the action itself, unless
// a middleware further in chain changed it.
return returnValue
}
})
test('Promise middleware adds support for dispatching promises.', () => {
const promiseMiddleware: Middleware<PromiseDispatch> =
({ dispatch }) =>
next =>
action => {
if (action instanceof Promise) {
action.then(dispatch)
return action
}
return next(action)
}
return promiseMiddleware
})
test('Thunk middleware adds support for dispatching thunks.', () => {
function thunk<S, DispatchExt>() {
const thunkMiddleware: Middleware<
ThunkDispatch<S, DispatchExt>,
S,
Dispatch & ThunkDispatch<S>
> = api => next => action =>
typeof action === 'function'
? action(api.dispatch, api.getState)
: next(action)
return thunkMiddleware
}
})
test('middleware that expects exact state type.', () => {
type State = { field: 'string' }
const customMiddleware: Middleware<{}, State> = api => next => action => {
expectTypeOf(api.getState()).toHaveProperty('field')
expectTypeOf(api.getState()).not.toHaveProperty('wrongField')
return next(action)
}
})
test('middleware that expects custom dispatch.', () => {
type MyAction = { type: 'INCREMENT' } | { type: 'DECREMENT' }
// dispatch that expects action union
type MyDispatch = Dispatch<MyAction>
const customDispatch: Middleware =
(api: MiddlewareAPI<MyDispatch>) => next => action => {
expectTypeOf(api.dispatch).toBeCallableWith({ type: 'INCREMENT' })
expectTypeOf(api.dispatch).toBeCallableWith({ type: 'DECREMENT' })
// `.not.toMatchTypeOf` does not work in this scenario.
expectTypeOf(api.dispatch)
.parameter(0)
.not.toEqualTypeOf({ type: 'UNKNOWN' })
}
})
test('test the type of store.dispatch after applying different middleware.', () => {
interface State {
someField: 'string'
}
const reducer: Reducer<State> = null as any
test('logger', () => {
const storeWithLogger = createStore(reducer, applyMiddleware(logger()))
// can only dispatch actions
expectTypeOf(storeWithLogger.dispatch).toBeCallableWith({
type: 'INCREMENT'
})
expectTypeOf(storeWithLogger.dispatch)
.parameter(0)
.not.toMatchTypeOf(Promise.resolve({ type: 'INCREMENT' }))
expectTypeOf(storeWithLogger.dispatch)
.parameter(0)
.not.toMatchTypeOf('not-an-action')
})
test('promise', () => {
const storeWithPromise = createStore(reducer, applyMiddleware(promise()))
// can dispatch actions and promises
// `.toBeCallableWith or .parameter(0).toMatchTypeOf`
// do not work in this scenario.
storeWithPromise.dispatch({ type: 'INCREMENT' })
storeWithPromise.dispatch(Promise.resolve({ type: 'INCREMENT' }))
expectTypeOf(storeWithPromise.dispatch)
.parameter(0)
.not.toMatchTypeOf('not-an-action')
expectTypeOf(storeWithPromise.dispatch)
.parameter(0)
.not.toMatchTypeOf(Promise.resolve('not-an-action'))
})
test('promise + logger', () => {
const storeWithPromiseAndLogger = createStore(
reducer,
applyMiddleware(promise(), logger())
)
// can dispatch actions and promises
// `.toBeCallableWith or .parameter(0).toMatchTypeOf`
// do not work in this scenario.
storeWithPromiseAndLogger.dispatch({ type: 'INCREMENT' })
storeWithPromiseAndLogger.dispatch(Promise.resolve({ type: 'INCREMENT' }))
expectTypeOf(storeWithPromiseAndLogger.dispatch)
.parameter(0)
.not.toMatchTypeOf('not-an-action')
expectTypeOf(storeWithPromiseAndLogger.dispatch)
.parameter(0)
.not.toMatchTypeOf(Promise.resolve('not-an-action'))
})
test('promise + thunk', () => {
const storeWithPromiseAndThunk = createStore(
reducer,
applyMiddleware(promise(), thunk<State, PromiseDispatch>(), logger())
)
// can dispatch actions, promises and thunks
// `.toBeCallableWith or .parameter(0).toMatchTypeOf`
// do not work in this scenario.
storeWithPromiseAndThunk.dispatch({ type: 'INCREMENT' })
// `.toBeCallableWith or .parameter(0).toMatchTypeOf`
// do not work in this scenario.
storeWithPromiseAndThunk.dispatch(Promise.resolve({ type: 'INCREMENT' }))
storeWithPromiseAndThunk.dispatch((dispatch, getState) => {
expectTypeOf(getState()).toHaveProperty('someField')
expectTypeOf(getState()).not.toHaveProperty('wrongField')
// injected dispatch accepts actions, thunks and promises
// `.toBeCallableWith or .parameter(0).toMatchTypeOf`
// do not work in this scenario.
dispatch({ type: 'INCREMENT' })
// `.toBeCallableWith or .parameter(0).toMatchTypeOf`
// do not work in this scenario.
dispatch(dispatch => dispatch({ type: 'INCREMENT' }))
expectTypeOf(dispatch).toBeCallableWith(
Promise.resolve({ type: 'INCREMENT' })
)
expectTypeOf(dispatch).parameter(0).not.toMatchTypeOf('not-an-action')
})
expectTypeOf(storeWithPromiseAndThunk.dispatch)
.parameter(0)
.not.toMatchTypeOf('not-an-action')
expectTypeOf(storeWithPromiseAndThunk.dispatch)
.parameter(0)
.not.toMatchTypeOf(Promise.resolve('not-an-action'))
})
test('test variadic signature.', () => {
const storeWithLotsOfMiddleware = createStore(
reducer,
applyMiddleware<PromiseDispatch>(
promise(),
logger(),
logger(),
logger(),
logger(),
logger()
)
)
// `.toBeCallableWith or .parameter(0).toMatchTypeOf`
// do not work in this scenario.
storeWithLotsOfMiddleware.dispatch({ type: 'INCREMENT' })
expectTypeOf(storeWithLotsOfMiddleware.dispatch).toBeCallableWith(
Promise.resolve({ type: 'INCREMENT' })
)
})
})
})