source: node_modules/@reduxjs/toolkit/src/dynamicMiddleware/tests/index.test-d.ts

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

Added visualizations

  • Property mode set to 100644
File size: 2.5 KB
RevLine 
[a762898]1import type { Action, Middleware, UnknownAction } from 'redux'
2import type { ThunkDispatch } from 'redux-thunk'
3import { configureStore } from '../../configureStore'
4import { createDynamicMiddleware } from '../index'
5
6const untypedInstance = createDynamicMiddleware()
7
8interface AppDispatch extends ThunkDispatch<number, undefined, UnknownAction> {
9 (n: 1): 1
10}
11
12const typedInstance = createDynamicMiddleware<number, AppDispatch>()
13
14declare const staticMiddleware: Middleware<(n: 1) => 1>
15
16const store = configureStore({
17 reducer: () => 0,
18 middleware: (gDM) =>
19 gDM().prepend(typedInstance.middleware).concat(staticMiddleware),
20})
21
22declare const compatibleMiddleware: Middleware<{}, number, AppDispatch>
23declare const incompatibleMiddleware: Middleware<{}, string, AppDispatch>
24
25declare const addedMiddleware: Middleware<(n: 2) => 2>
26
27describe('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})
Note: See TracBrowser for help on using the repository browser.