source: node_modules/@reduxjs/toolkit/src/tests/actionCreatorInvariantMiddleware.test.ts@ a762898

Last change on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.8 KB
Line 
1import type { ActionCreatorInvariantMiddlewareOptions } from '@internal/actionCreatorInvariantMiddleware'
2import { getMessage } from '@internal/actionCreatorInvariantMiddleware'
3import { createActionCreatorInvariantMiddleware } from '@internal/actionCreatorInvariantMiddleware'
4import type { MiddlewareAPI } from '@reduxjs/toolkit'
5import { createAction } from '@reduxjs/toolkit'
6
7describe('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})
Note: See TracBrowser for help on using the repository browser.