Files
NapCatQQ/.pnpm-store/v3/files/de/2da64b4ad373e9f56e9a8f240a257a66504ee6908d51258bab03512c82dab8657c7391b3e23a597279c57089437f0579af555d928d5baea799c578ecb96c4d
T
TanTan e311ca3275
Build NapCat Artifacts / Windows Loader Test (push) Canceled after 0s
Build NapCat Artifacts / Type Check & Test (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Framework (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Shell (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

96 lines
2.5 KiB
Plaintext

import type { Action, Middleware, UnknownAction } from 'redux'
import type { ThunkDispatch } from 'redux-thunk'
import { configureStore } from '../../configureStore'
import { createDynamicMiddleware } from '../index'
const untypedInstance = createDynamicMiddleware()
interface AppDispatch extends ThunkDispatch<number, undefined, UnknownAction> {
(n: 1): 1
}
const typedInstance = createDynamicMiddleware<number, AppDispatch>()
declare const staticMiddleware: Middleware<(n: 1) => 1>
const store = configureStore({
reducer: () => 0,
middleware: (gDM) =>
gDM().prepend(typedInstance.middleware).concat(staticMiddleware),
})
declare const compatibleMiddleware: Middleware<{}, number, AppDispatch>
declare const incompatibleMiddleware: Middleware<{}, string, AppDispatch>
declare const addedMiddleware: Middleware<(n: 2) => 2>
describe('type tests', () => {
test('instance typed at creation ensures middleware compatibility with store', () => {
const store = configureStore({
reducer: () => '',
// @ts-expect-error
middleware: (gDM) => gDM().prepend(typedInstance.middleware),
})
})
test('instance typed at creation enforces correct middleware type', () => {
typedInstance.addMiddleware(
compatibleMiddleware,
// @ts-expect-error
incompatibleMiddleware,
)
const dispatch = store.dispatch(
typedInstance.withMiddleware(
compatibleMiddleware,
// @ts-expect-error
incompatibleMiddleware,
),
)
})
test('withTypes() enforces correct middleware type', () => {
const addMiddleware = untypedInstance.addMiddleware.withTypes<{
state: number
dispatch: AppDispatch
}>()
addMiddleware(
compatibleMiddleware,
// @ts-expect-error
incompatibleMiddleware,
)
const withMiddleware = untypedInstance.withMiddleware.withTypes<{
state: number
dispatch: AppDispatch
}>()
const dispatch = store.dispatch(
withMiddleware(
compatibleMiddleware,
// @ts-expect-error
incompatibleMiddleware,
),
)
})
test('withMiddleware returns typed dispatch, with any applicable extensions', () => {
const dispatch = store.dispatch(
typedInstance.withMiddleware(addedMiddleware),
)
// standard
expectTypeOf(dispatch({ type: 'foo' })).toEqualTypeOf<Action<string>>()
// thunk
expectTypeOf(dispatch(() => 'foo')).toBeString()
// static
expectTypeOf(dispatch(1)).toEqualTypeOf<1>()
// added
expectTypeOf(dispatch(2)).toEqualTypeOf<2>()
})
})