Files
NapCatQQ/.pnpm-store/v3/files/65/725b4def11608346d4947bf4c64d56757d65d409192d4523b098fa87e2df963c5b24f312cfbb4cc13c5d80661e46b4ce93a799299a2bb3ef9bd030b6ae583f
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

62 lines
1.8 KiB
Plaintext

import type { ActionCreatorInvariantMiddlewareOptions } from '@internal/actionCreatorInvariantMiddleware'
import { getMessage } from '@internal/actionCreatorInvariantMiddleware'
import { createActionCreatorInvariantMiddleware } from '@internal/actionCreatorInvariantMiddleware'
import type { MiddlewareAPI } from '@reduxjs/toolkit'
import { createAction } from '@reduxjs/toolkit'
describe('createActionCreatorInvariantMiddleware', () => {
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
afterEach(() => {
consoleSpy.mockClear()
})
afterAll(() => {
consoleSpy.mockRestore()
})
const dummyAction = createAction('aSlice/anAction')
it('sends the action through the middleware chain', () => {
const next = vi.fn()
const dispatch = createActionCreatorInvariantMiddleware()(
{} as MiddlewareAPI,
)(next)
dispatch({ type: 'SOME_ACTION' })
expect(next).toHaveBeenCalledWith({
type: 'SOME_ACTION',
})
})
const makeActionTester = (
options?: ActionCreatorInvariantMiddlewareOptions,
) =>
createActionCreatorInvariantMiddleware(options)({} as MiddlewareAPI)(
(action) => action,
)
it('logs a warning to console if an action creator is mistakenly dispatched', () => {
const testAction = makeActionTester()
testAction(dummyAction())
expect(consoleSpy).not.toHaveBeenCalled()
testAction(dummyAction)
expect(consoleSpy).toHaveBeenLastCalledWith(getMessage(dummyAction.type))
})
it('allows passing a custom predicate', () => {
let predicateCalled = false
const testAction = makeActionTester({
isActionCreator(action): action is Function {
predicateCalled = true
return false
},
})
testAction(dummyAction())
expect(predicateCalled).toBe(true)
})
})