Skip to content

Commit

Permalink
migrate applyMiddleware test to typescript (reduxjs#3505)
Browse files Browse the repository at this point in the history
Former-commit-id: c2bca0c
  • Loading branch information
cellog authored and timdorr committed Aug 16, 2019
1 parent c81ec47 commit 3b91f0c
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions test/applyMiddleware.spec.js → test/applyMiddleware.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { createStore, applyMiddleware } from '../'
import {
createStore,
applyMiddleware,
Middleware,
Dispatch,
AnyAction,
Action
} from '..'
import * as reducers from './helpers/reducers'
import { addTodo, addTodoAsync, addTodoIfEmpty } from './helpers/actionCreators'
import { thunk } from './helpers/middleware'
Expand Down Expand Up @@ -51,7 +58,11 @@ describe('applyMiddleware', () => {
const spy = jest.fn()
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos)

return store.dispatch(addTodoAsync('Use Redux')).then(() => {
// the typing for redux-thunk is super complex, so we will use an as unknown hack
const dispatchedValue = (store.dispatch(
addTodoAsync('Use Redux')
) as unknown) as Promise<void>
return dispatchedValue.then(() => {
expect(spy.mock.calls.length).toEqual(2)
})
})
Expand Down Expand Up @@ -87,7 +98,11 @@ describe('applyMiddleware', () => {
}
])

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

function multiArgMiddleware() {
return next => (action, callArgs) => {
interface MultiDispatch<A extends Action = AnyAction> {
<T extends A>(action: T, extraArg?: string[]): T
}

const multiArgMiddleware: Middleware<
MultiDispatch,
any,
MultiDispatch
> = _store => {
return next => (action, callArgs?: any) => {
if (Array.isArray(callArgs)) {
return action(...callArgs)
}
Expand Down

0 comments on commit 3b91f0c

Please sign in to comment.