Debian Codegraph @

install/freeipa-webui/node_modules/@reduxjs/toolkit/src/tests/actionCreatorInvariantMiddleware.test.ts

raw
1 import type { ActionCreatorInvariantMiddlewareOptions } from '@internal/actionCreatorInvariantMiddleware'
2 import { getMessage } from '@internal/actionCreatorInvariantMiddleware'
3 import { createActionCreatorInvariantMiddleware } from '@internal/actionCreatorInvariantMiddleware'
4 import type { MiddlewareAPI } from '@reduxjs/toolkit'
5 import { createAction } from '@reduxjs/toolkit'
6
7 describe('createActionCreatorInvariantMiddleware', () => {
8 const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
9
10 afterEach(() => {
11 consoleSpy.mockClear()
12 })
13 afterAll(() => {
14 consoleSpy.mockRestore()
15 })
16
17 const dummyAction = createAction('aSlice/anAction')
18
19 it('sends the action through the middleware chain', () => {
20 const next = vi.fn()
21 const dispatch = createActionCreatorInvariantMiddleware()(
22 {} as MiddlewareAPI,
23 )(next)
24 dispatch({ type: 'SOME_ACTION' })
25
26 expect(next).toHaveBeenCalledWith({
27 type: 'SOME_ACTION',
28 })
29 })
30
31 const makeActionTester = (
32 options?: ActionCreatorInvariantMiddlewareOptions,
33 ) =>
34 createActionCreatorInvariantMiddleware(options)({} as MiddlewareAPI)(
35 (action) => action,
36 )
37
38 it('logs a warning to console if an action creator is mistakenly dispatched', () => {
39 const testAction = makeActionTester()
40
41 testAction(dummyAction())
42
43 expect(consoleSpy).not.toHaveBeenCalled()
44
45 testAction(dummyAction)
46
47 expect(consoleSpy).toHaveBeenLastCalledWith(getMessage(dummyAction.type))
48 })
49
50 it('allows passing a custom predicate', () => {
51 let predicateCalled = false
52 const testAction = makeActionTester({
53 isActionCreator(action): action is Function {
54 predicateCalled = true
55 return false
56 },
57 })
58 testAction(dummyAction())
59 expect(predicateCalled).toBe(true)
60 })
61 })