source: node_modules/@reduxjs/toolkit/src/tests/getDefaultMiddleware.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: 5.1 KB
Line 
1import { buildGetDefaultMiddleware } from '@internal/getDefaultMiddleware'
2import type {
3 Action,
4 Dispatch,
5 Middleware,
6 ThunkAction,
7 ThunkDispatch,
8 ThunkMiddleware,
9 Tuple,
10 UnknownAction,
11} from '@reduxjs/toolkit'
12import { configureStore } from '@reduxjs/toolkit'
13
14declare const middleware1: Middleware<{
15 (_: string): number
16}>
17
18declare const middleware2: Middleware<{
19 (_: number): string
20}>
21
22type ThunkReturn = Promise<'thunk'>
23declare const thunkCreator: () => () => ThunkReturn
24
25const getDefaultMiddleware = buildGetDefaultMiddleware()
26
27describe('type tests', () => {
28 test('prepend single element', () => {
29 const store = configureStore({
30 reducer: () => 0,
31 middleware: (gDM) => gDM().prepend(middleware1),
32 })
33
34 expectTypeOf(store.dispatch('foo')).toBeNumber()
35
36 expectTypeOf(store.dispatch(thunkCreator())).toEqualTypeOf<ThunkReturn>()
37
38 expectTypeOf(store.dispatch('foo')).not.toBeString()
39 })
40
41 test('prepend multiple (rest)', () => {
42 const store = configureStore({
43 reducer: () => 0,
44 middleware: (gDM) => gDM().prepend(middleware1, middleware2),
45 })
46
47 expectTypeOf(store.dispatch('foo')).toBeNumber()
48
49 expectTypeOf(store.dispatch(5)).toBeString()
50
51 expectTypeOf(store.dispatch(thunkCreator())).toEqualTypeOf<ThunkReturn>()
52
53 expectTypeOf(store.dispatch('foo')).not.toBeString()
54 })
55
56 test('prepend multiple (array notation)', () => {
57 const store = configureStore({
58 reducer: () => 0,
59 middleware: (gDM) => gDM().prepend([middleware1, middleware2] as const),
60 })
61
62 expectTypeOf(store.dispatch('foo')).toBeNumber()
63
64 expectTypeOf(store.dispatch(5)).toBeString()
65
66 expectTypeOf(store.dispatch(thunkCreator())).toEqualTypeOf<ThunkReturn>()
67
68 expectTypeOf(store.dispatch('foo')).not.toBeString()
69 })
70
71 test('concat single element', () => {
72 const store = configureStore({
73 reducer: () => 0,
74 middleware: (gDM) => gDM().concat(middleware1),
75 })
76
77 expectTypeOf(store.dispatch('foo')).toBeNumber()
78
79 expectTypeOf(store.dispatch(thunkCreator())).toEqualTypeOf<ThunkReturn>()
80
81 expectTypeOf(store.dispatch('foo')).not.toBeString()
82 })
83
84 test('prepend multiple (rest)', () => {
85 const store = configureStore({
86 reducer: () => 0,
87 middleware: (gDM) => gDM().concat(middleware1, middleware2),
88 })
89
90 expectTypeOf(store.dispatch('foo')).toBeNumber()
91
92 expectTypeOf(store.dispatch(5)).toBeString()
93
94 expectTypeOf(store.dispatch(thunkCreator())).toEqualTypeOf<ThunkReturn>()
95
96 expectTypeOf(store.dispatch('foo')).not.toBeString()
97 })
98
99 test('concat multiple (array notation)', () => {
100 const store = configureStore({
101 reducer: () => 0,
102 middleware: (gDM) => gDM().concat([middleware1, middleware2] as const),
103 })
104
105 expectTypeOf(store.dispatch('foo')).toBeNumber()
106
107 expectTypeOf(store.dispatch(5)).toBeString()
108
109 expectTypeOf(store.dispatch(thunkCreator())).toEqualTypeOf<ThunkReturn>()
110
111 expectTypeOf(store.dispatch('foo')).not.toBeString()
112 })
113
114 test('concat and prepend', () => {
115 const store = configureStore({
116 reducer: () => 0,
117 middleware: (gDM) => gDM().concat(middleware1).prepend(middleware2),
118 })
119
120 expectTypeOf(store.dispatch('foo')).toBeNumber()
121
122 expectTypeOf(store.dispatch(5)).toBeString()
123
124 expectTypeOf(store.dispatch(thunkCreator())).toEqualTypeOf<ThunkReturn>()
125
126 expectTypeOf(store.dispatch('foo')).not.toBeString()
127 })
128
129 test('allows passing options to thunk', () => {
130 const extraArgument = 42 as const
131
132 const m2 = getDefaultMiddleware({
133 thunk: false,
134 })
135
136 expectTypeOf(m2).toMatchTypeOf<Tuple<[]>>()
137
138 const dummyMiddleware: Middleware<
139 {
140 (action: Action<'actionListenerMiddleware/add'>): () => void
141 },
142 { counter: number }
143 > = (storeApi) => (next) => (action) => {
144 return next(action)
145 }
146
147 const dummyMiddleware2: Middleware<{}, { counter: number }> =
148 (storeApi) => (next) => (action) => {}
149
150 const testThunk: ThunkAction<
151 void,
152 { counter: number },
153 number,
154 UnknownAction
155 > = (dispatch, getState, extraArg) => {
156 expect(extraArg).toBe(extraArgument)
157 }
158
159 const reducer = () => ({ counter: 123 })
160
161 const store = configureStore({
162 reducer,
163 middleware: (gDM) => {
164 const middleware = gDM({
165 thunk: { extraArgument },
166 immutableCheck: false,
167 serializableCheck: false,
168 actionCreatorCheck: false,
169 })
170
171 const m3 = middleware.concat(dummyMiddleware, dummyMiddleware2)
172
173 expectTypeOf(m3).toMatchTypeOf<
174 Tuple<
175 [
176 ThunkMiddleware<any, UnknownAction, 42>,
177 Middleware<
178 (action: Action<'actionListenerMiddleware/add'>) => () => void,
179 {
180 counter: number
181 },
182 Dispatch<UnknownAction>
183 >,
184 Middleware<{}, any, Dispatch<UnknownAction>>,
185 ]
186 >
187 >()
188
189 return m3
190 },
191 })
192
193 expectTypeOf(store.dispatch).toMatchTypeOf<
194 ThunkDispatch<any, 42, UnknownAction> & Dispatch<UnknownAction>
195 >()
196
197 store.dispatch(testThunk)
198 })
199})
Note: See TracBrowser for help on using the repository browser.