| 1 | import type { Action, Middleware, UnknownAction } from 'redux'
|
|---|
| 2 | import type { ThunkDispatch } from 'redux-thunk'
|
|---|
| 3 | import { configureStore } from '../../configureStore'
|
|---|
| 4 | import { createDynamicMiddleware } from '../index'
|
|---|
| 5 |
|
|---|
| 6 | const untypedInstance = createDynamicMiddleware()
|
|---|
| 7 |
|
|---|
| 8 | interface AppDispatch extends ThunkDispatch<number, undefined, UnknownAction> {
|
|---|
| 9 | (n: 1): 1
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 | const typedInstance = createDynamicMiddleware<number, AppDispatch>()
|
|---|
| 13 |
|
|---|
| 14 | declare const staticMiddleware: Middleware<(n: 1) => 1>
|
|---|
| 15 |
|
|---|
| 16 | const store = configureStore({
|
|---|
| 17 | reducer: () => 0,
|
|---|
| 18 | middleware: (gDM) =>
|
|---|
| 19 | gDM().prepend(typedInstance.middleware).concat(staticMiddleware),
|
|---|
| 20 | })
|
|---|
| 21 |
|
|---|
| 22 | declare const compatibleMiddleware: Middleware<{}, number, AppDispatch>
|
|---|
| 23 | declare const incompatibleMiddleware: Middleware<{}, string, AppDispatch>
|
|---|
| 24 |
|
|---|
| 25 | declare const addedMiddleware: Middleware<(n: 2) => 2>
|
|---|
| 26 |
|
|---|
| 27 | describe('type tests', () => {
|
|---|
| 28 | test('instance typed at creation ensures middleware compatibility with store', () => {
|
|---|
| 29 | const store = configureStore({
|
|---|
| 30 | reducer: () => '',
|
|---|
| 31 | // @ts-expect-error
|
|---|
| 32 | middleware: (gDM) => gDM().prepend(typedInstance.middleware),
|
|---|
| 33 | })
|
|---|
| 34 | })
|
|---|
| 35 |
|
|---|
| 36 | test('instance typed at creation enforces correct middleware type', () => {
|
|---|
| 37 | typedInstance.addMiddleware(
|
|---|
| 38 | compatibleMiddleware,
|
|---|
| 39 | // @ts-expect-error
|
|---|
| 40 | incompatibleMiddleware,
|
|---|
| 41 | )
|
|---|
| 42 |
|
|---|
| 43 | const dispatch = store.dispatch(
|
|---|
| 44 | typedInstance.withMiddleware(
|
|---|
| 45 | compatibleMiddleware,
|
|---|
| 46 | // @ts-expect-error
|
|---|
| 47 | incompatibleMiddleware,
|
|---|
| 48 | ),
|
|---|
| 49 | )
|
|---|
| 50 | })
|
|---|
| 51 |
|
|---|
| 52 | test('withTypes() enforces correct middleware type', () => {
|
|---|
| 53 | const addMiddleware = untypedInstance.addMiddleware.withTypes<{
|
|---|
| 54 | state: number
|
|---|
| 55 | dispatch: AppDispatch
|
|---|
| 56 | }>()
|
|---|
| 57 |
|
|---|
| 58 | addMiddleware(
|
|---|
| 59 | compatibleMiddleware,
|
|---|
| 60 | // @ts-expect-error
|
|---|
| 61 | incompatibleMiddleware,
|
|---|
| 62 | )
|
|---|
| 63 |
|
|---|
| 64 | const withMiddleware = untypedInstance.withMiddleware.withTypes<{
|
|---|
| 65 | state: number
|
|---|
| 66 | dispatch: AppDispatch
|
|---|
| 67 | }>()
|
|---|
| 68 |
|
|---|
| 69 | const dispatch = store.dispatch(
|
|---|
| 70 | withMiddleware(
|
|---|
| 71 | compatibleMiddleware,
|
|---|
| 72 | // @ts-expect-error
|
|---|
| 73 | incompatibleMiddleware,
|
|---|
| 74 | ),
|
|---|
| 75 | )
|
|---|
| 76 | })
|
|---|
| 77 |
|
|---|
| 78 | test('withMiddleware returns typed dispatch, with any applicable extensions', () => {
|
|---|
| 79 | const dispatch = store.dispatch(
|
|---|
| 80 | typedInstance.withMiddleware(addedMiddleware),
|
|---|
| 81 | )
|
|---|
| 82 |
|
|---|
| 83 | // standard
|
|---|
| 84 | expectTypeOf(dispatch({ type: 'foo' })).toEqualTypeOf<Action<string>>()
|
|---|
| 85 |
|
|---|
| 86 | // thunk
|
|---|
| 87 | expectTypeOf(dispatch(() => 'foo')).toBeString()
|
|---|
| 88 |
|
|---|
| 89 | // static
|
|---|
| 90 | expectTypeOf(dispatch(1)).toEqualTypeOf<1>()
|
|---|
| 91 |
|
|---|
| 92 | // added
|
|---|
| 93 | expectTypeOf(dispatch(2)).toEqualTypeOf<2>()
|
|---|
| 94 | })
|
|---|
| 95 | })
|
|---|