Skip to content

Commit c2bca0c

Browse files
cellogtimdorr
authored andcommitted
migrate applyMiddleware test to typescript (reduxjs#3505)
1 parent 80cb6b8 commit c2bca0c

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

test/applyMiddleware.spec.js renamed to test/applyMiddleware.spec.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { createStore, applyMiddleware } from '../'
1+
import {
2+
createStore,
3+
applyMiddleware,
4+
Middleware,
5+
Dispatch,
6+
AnyAction,
7+
Action
8+
} from '..'
29
import * as reducers from './helpers/reducers'
310
import { addTodo, addTodoAsync, addTodoIfEmpty } from './helpers/actionCreators'
411
import { thunk } from './helpers/middleware'
@@ -51,7 +58,11 @@ describe('applyMiddleware', () => {
5158
const spy = jest.fn()
5259
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos)
5360

54-
return store.dispatch(addTodoAsync('Use Redux')).then(() => {
61+
// the typing for redux-thunk is super complex, so we will use an as unknown hack
62+
const dispatchedValue = (store.dispatch(
63+
addTodoAsync('Use Redux')
64+
) as unknown) as Promise<void>
65+
return dispatchedValue.then(() => {
5566
expect(spy.mock.calls.length).toEqual(2)
5667
})
5768
})
@@ -87,7 +98,11 @@ describe('applyMiddleware', () => {
8798
}
8899
])
89100

90-
store.dispatch(addTodoAsync('Maybe')).then(() => {
101+
// the typing for redux-thunk is super complex, so we will use an "as unknown" hack
102+
const dispatchedValue = (store.dispatch(
103+
addTodoAsync('Maybe')
104+
) as unknown) as Promise<void>
105+
dispatchedValue.then(() => {
91106
expect(store.getState()).toEqual([
92107
{
93108
id: 1,
@@ -110,8 +125,16 @@ describe('applyMiddleware', () => {
110125
const spy = jest.fn()
111126
const testCallArgs = ['test']
112127

113-
function multiArgMiddleware() {
114-
return next => (action, callArgs) => {
128+
interface MultiDispatch<A extends Action = AnyAction> {
129+
<T extends A>(action: T, extraArg?: string[]): T
130+
}
131+
132+
const multiArgMiddleware: Middleware<
133+
MultiDispatch,
134+
any,
135+
MultiDispatch
136+
> = _store => {
137+
return next => (action, callArgs?: any) => {
115138
if (Array.isArray(callArgs)) {
116139
return action(...callArgs)
117140
}

0 commit comments

Comments
 (0)