Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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