| 1 |
import type { Context } from 'react' |
| 2 |
import type { ReactReduxContextValue } from 'react-redux' |
| 3 |
import type { Action, Middleware, UnknownAction } from 'redux' |
| 4 |
import type { ThunkDispatch } from 'redux-thunk' |
| 5 |
import { createDynamicMiddleware } from '../react' |
| 6 |
|
| 7 |
interface AppDispatch extends ThunkDispatch<number, undefined, UnknownAction> { |
| 8 |
(n: 1): 1 |
| 9 |
} |
| 10 |
|
| 11 |
const untypedInstance = createDynamicMiddleware() |
| 12 |
|
| 13 |
const typedInstance = createDynamicMiddleware<number, AppDispatch>() |
| 14 |
|
| 15 |
declare const compatibleMiddleware: Middleware<{}, number, AppDispatch> |
| 16 |
declare const incompatibleMiddleware: Middleware<{}, string, AppDispatch> |
| 17 |
|
| 18 |
declare const customContext: Context<ReactReduxContextValue | null> |
| 19 |
|
| 20 |
declare const addedMiddleware: Middleware<(n: 2) => 2> |
| 21 |
|
| 22 |
describe('type tests', () => { |
| 23 |
test('instance typed at creation enforces correct middleware type', () => { |
| 24 |
const useDispatch = typedInstance.createDispatchWithMiddlewareHook( |
| 25 |
compatibleMiddleware, |
| 26 |
// @ts-expect-error |
| 27 |
incompatibleMiddleware, |
| 28 |
) |
| 29 |
|
| 30 |
const createDispatchWithMiddlewareHook = |
| 31 |
typedInstance.createDispatchWithMiddlewareHookFactory(customContext) |
| 32 |
const useDispatchWithContext = createDispatchWithMiddlewareHook( |
| 33 |
compatibleMiddleware, |
| 34 |
// @ts-expect-error |
| 35 |
incompatibleMiddleware, |
| 36 |
) |
| 37 |
}) |
| 38 |
|
| 39 |
test('withTypes() enforces correct middleware type', () => { |
| 40 |
const createDispatchWithMiddlewareHook = |
| 41 |
untypedInstance.createDispatchWithMiddlewareHook.withTypes<{ |
| 42 |
state: number |
| 43 |
dispatch: AppDispatch |
| 44 |
}>() |
| 45 |
const useDispatch = createDispatchWithMiddlewareHook( |
| 46 |
compatibleMiddleware, |
| 47 |
// @ts-expect-error |
| 48 |
incompatibleMiddleware, |
| 49 |
) |
| 50 |
|
| 51 |
const createCustomDispatchWithMiddlewareHook = untypedInstance |
| 52 |
.createDispatchWithMiddlewareHookFactory(customContext) |
| 53 |
.withTypes<{ |
| 54 |
state: number |
| 55 |
dispatch: AppDispatch |
| 56 |
}>() |
| 57 |
const useCustomDispatch = createCustomDispatchWithMiddlewareHook( |
| 58 |
compatibleMiddleware, |
| 59 |
// @ts-expect-error |
| 60 |
incompatibleMiddleware, |
| 61 |
) |
| 62 |
}) |
| 63 |
|
| 64 |
test('useDispatchWithMW returns typed dispatch, with any applicable extensions', () => { |
| 65 |
const useDispatchWithMW = |
| 66 |
typedInstance.createDispatchWithMiddlewareHook(addedMiddleware) |
| 67 |
const dispatch = useDispatchWithMW() |
| 68 |
|
| 69 |
// standard |
| 70 |
expectTypeOf(dispatch({ type: 'foo' })).toEqualTypeOf<Action<string>>() |
| 71 |
|
| 72 |
// thunk |
| 73 |
expectTypeOf(dispatch(() => 'foo')).toBeString() |
| 74 |
|
| 75 |
// static |
| 76 |
expectTypeOf(dispatch(1)).toEqualTypeOf<1>() |
| 77 |
|
| 78 |
// added |
| 79 |
expectTypeOf(dispatch(2)).toEqualTypeOf<2>() |
| 80 |
}) |
| 81 |
}) |