1
- import { createStore , applyMiddleware , Middleware , AnyAction , Action } from '..'
1
+ import {
2
+ createStore ,
3
+ applyMiddleware ,
4
+ Middleware ,
5
+ MiddlewareAPI ,
6
+ AnyAction ,
7
+ Action ,
8
+ Store ,
9
+ Dispatch
10
+ } from '..'
2
11
import * as reducers from './helpers/reducers'
3
12
import { addTodo , addTodoAsync , addTodoIfEmpty } from './helpers/actionCreators'
4
13
import { thunk } from './helpers/middleware'
5
14
6
15
describe ( 'applyMiddleware' , ( ) => {
7
16
it ( 'warns when dispatching during middleware setup' , ( ) => {
8
- function dispatchingMiddleware ( store ) {
9
- store . dispatch ( addTodo ( 'Dont dispatch in middleware setup' ) )
10
- return next => action => next ( action )
17
+ function dispatchingMiddleware ( store : Store ) {
18
+ store . dispatch ( addTodo ( "Don't dispatch in middleware setup" ) )
19
+ return ( next : Dispatch ) => ( action : Action ) => next ( action )
11
20
}
12
21
13
22
expect ( ( ) =>
14
- applyMiddleware ( dispatchingMiddleware ) ( createStore ) ( reducers . todos )
23
+ applyMiddleware ( dispatchingMiddleware as Middleware ) ( createStore ) (
24
+ reducers . todos
25
+ )
15
26
) . toThrow ( )
16
27
} )
17
28
18
29
it ( 'wraps dispatch method with middleware once' , ( ) => {
19
- function test ( spyOnMethods ) {
20
- return methods => {
30
+ function test ( spyOnMethods : any ) {
31
+ return ( methods : any ) => {
21
32
spyOnMethods ( methods )
22
- return next => action => next ( action )
33
+ return ( next : Dispatch ) => ( action : Action ) => next ( action )
23
34
}
24
35
}
25
36
@@ -41,8 +52,8 @@ describe('applyMiddleware', () => {
41
52
} )
42
53
43
54
it ( 'passes recursive dispatches through the middleware chain' , ( ) => {
44
- function test ( spyOnMethods ) {
45
- return ( ) => next => action => {
55
+ function test ( spyOnMethods : any ) {
56
+ return ( ) => ( next : Dispatch ) => ( action : Action ) => {
46
57
spyOnMethods ( action )
47
58
return next ( action )
48
59
}
@@ -53,7 +64,7 @@ describe('applyMiddleware', () => {
53
64
54
65
// the typing for redux-thunk is super complex, so we will use an as unknown hack
55
66
const dispatchedValue = store . dispatch (
56
- addTodoAsync ( 'Use Redux' )
67
+ addTodoAsync ( 'Use Redux' ) as any
57
68
) as unknown as Promise < void >
58
69
return dispatchedValue . then ( ( ) => {
59
70
expect ( spy . mock . calls . length ) . toEqual ( 2 )
@@ -63,15 +74,15 @@ describe('applyMiddleware', () => {
63
74
it ( 'works with thunk middleware' , done => {
64
75
const store = applyMiddleware ( thunk ) ( createStore ) ( reducers . todos )
65
76
66
- store . dispatch ( addTodoIfEmpty ( 'Hello' ) )
77
+ store . dispatch ( addTodoIfEmpty ( 'Hello' ) as any )
67
78
expect ( store . getState ( ) ) . toEqual ( [
68
79
{
69
80
id : 1 ,
70
81
text : 'Hello'
71
82
}
72
83
] )
73
84
74
- store . dispatch ( addTodoIfEmpty ( 'Hello' ) )
85
+ store . dispatch ( addTodoIfEmpty ( 'Hello' ) as any )
75
86
expect ( store . getState ( ) ) . toEqual ( [
76
87
{
77
88
id : 1 ,
@@ -93,7 +104,7 @@ describe('applyMiddleware', () => {
93
104
94
105
// the typing for redux-thunk is super complex, so we will use an "as unknown" hack
95
106
const dispatchedValue = store . dispatch (
96
- addTodoAsync ( 'Maybe' )
107
+ addTodoAsync ( 'Maybe' ) as any
97
108
) as unknown as Promise < void >
98
109
dispatchedValue . then ( ( ) => {
99
110
expect ( store . getState ( ) ) . toEqual ( [
@@ -124,24 +135,25 @@ describe('applyMiddleware', () => {
124
135
125
136
const multiArgMiddleware : Middleware < MultiDispatch , any , MultiDispatch > =
126
137
_store => {
127
- return next => ( action , callArgs ?: any ) => {
138
+ return next => ( action : any , callArgs ?: any ) => {
128
139
if ( Array . isArray ( callArgs ) ) {
129
140
return action ( ...callArgs )
130
141
}
131
142
return next ( action )
132
143
}
133
144
}
134
145
135
- function dummyMiddleware ( { dispatch } ) {
136
- return _next => action => dispatch ( action , testCallArgs )
146
+ function dummyMiddleware ( { dispatch } : MiddlewareAPI ) {
147
+ return ( _next : Dispatch ) => ( action : Action ) =>
148
+ dispatch ( action , testCallArgs )
137
149
}
138
150
139
151
const store = createStore (
140
152
reducers . todos ,
141
153
applyMiddleware ( multiArgMiddleware , dummyMiddleware )
142
154
)
143
155
144
- store . dispatch ( spy )
156
+ store . dispatch ( spy as any )
145
157
expect ( spy . mock . calls [ 0 ] ) . toEqual ( testCallArgs )
146
158
} )
147
159
} )
0 commit comments