Debian Codegraph @

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

raw
1 import * as DevTools from '@internal/devtoolsExtension'
2 import type { StoreEnhancer } from '@reduxjs/toolkit'
3 import { Tuple } from '@reduxjs/toolkit'
4 import type * as Redux from 'redux'
5 import { vi } from 'vitest'
6
7 vi.doMock('redux', async (importOriginal) => {
8 const redux = await importOriginal<typeof import('redux')>()
9
10 vi.spyOn(redux, 'applyMiddleware')
11 vi.spyOn(redux, 'combineReducers')
12 vi.spyOn(redux, 'compose')
13 vi.spyOn(redux, 'createStore')
14
15 return redux
16 })
17
18 describe('configureStore', async () => {
19 const composeWithDevToolsSpy = vi.spyOn(DevTools, 'composeWithDevTools')
20
21 const redux = await import('redux')
22
23 const { configureStore } = await import('@reduxjs/toolkit')
24
25 const reducer: Redux.Reducer = (state = {}, _action) => state
26
27 beforeEach(() => {
28 vi.clearAllMocks()
29 })
30
31 describe('given a function reducer', () => {
32 it('calls createStore with the reducer', () => {
33 configureStore({ reducer })
34 expect(configureStore({ reducer })).toBeInstanceOf(Object)
35
36 expect(redux.createStore).toHaveBeenCalledWith(
37 reducer,
38 undefined,
39 expect.any(Function),
40 )
41 expect(redux.applyMiddleware).toHaveBeenCalled()
42 if (process.env.TEST_DIST) {
43 expect(composeWithDevToolsSpy).not.toHaveBeenCalled()
44 } else {
45 expect(composeWithDevToolsSpy).toHaveBeenCalledTimes(2)
46 }
47 })
48 })
49
50 describe('given an object of reducers', () => {
51 it('calls createStore with the combined reducers', () => {
52 const reducer = {
53 reducer() {
54 return true
55 },
56 }
57 expect(configureStore({ reducer })).toBeInstanceOf(Object)
58 expect(redux.combineReducers).toHaveBeenCalledWith(reducer)
59 expect(redux.applyMiddleware).toHaveBeenCalled()
60 if (process.env.TEST_DIST) {
61 expect(composeWithDevToolsSpy).not.toHaveBeenCalled()
62 } else {
63 expect(composeWithDevToolsSpy).toHaveBeenCalledOnce()
64 }
65 expect(redux.createStore).toHaveBeenCalledWith(
66 expect.any(Function),
67 undefined,
68 expect.any(Function),
69 )
70 })
71 })
72
73 describe('given no reducer', () => {
74 it('throws', () => {
75 expect(configureStore).toThrow(
76 '`reducer` is a required argument, and must be a function or an object of functions that can be passed to combineReducers',
77 )
78 })
79 })
80
81 describe('given no middleware', () => {
82 it('calls createStore without any middleware', () => {
83 expect(
84 configureStore({ middleware: () => new Tuple(), reducer }),
85 ).toBeInstanceOf(Object)
86 expect(redux.applyMiddleware).toHaveBeenCalledWith()
87 if (process.env.TEST_DIST) {
88 expect(composeWithDevToolsSpy).not.toHaveBeenCalled()
89 } else {
90 expect(composeWithDevToolsSpy).toHaveBeenCalledOnce()
91 }
92 expect(redux.createStore).toHaveBeenCalledWith(
93 reducer,
94 undefined,
95 expect.any(Function),
96 )
97 })
98 })
99
100 describe('given an array of middleware', () => {
101 it('throws an error requiring a callback', () => {
102 // @ts-expect-error
103 expect(() => configureStore({ middleware: [], reducer })).toThrow(
104 '`middleware` field must be a callback',
105 )
106 })
107 })
108
109 describe('given undefined middleware', () => {
110 it('calls createStore with default middleware', () => {
111 expect(configureStore({ middleware: undefined, reducer })).toBeInstanceOf(
112 Object,
113 )
114 expect(redux.applyMiddleware).toHaveBeenCalledWith(
115 expect.any(Function), // immutableCheck
116 expect.any(Function), // thunk
117 expect.any(Function), // serializableCheck
118 expect.any(Function), // actionCreatorCheck
119 )
120 if (process.env.TEST_DIST) {
121 expect(composeWithDevToolsSpy).not.toHaveBeenCalled()
122 } else {
123 expect(composeWithDevToolsSpy).toHaveBeenCalledOnce()
124 }
125 expect(redux.createStore).toHaveBeenCalledWith(
126 reducer,
127 undefined,
128 expect.any(Function),
129 )
130 })
131 })
132
133 describe('given a middleware creation function that returns undefined', () => {
134 it('throws an error', () => {
135 const invalidBuilder = vi.fn((getDefaultMiddleware) => undefined as any)
136 expect(() =>
137 configureStore({ middleware: invalidBuilder, reducer }),
138 ).toThrow(
139 'when using a middleware builder function, an array of middleware must be returned',
140 )
141 })
142 })
143
144 describe('given a middleware creation function that returns an array with non-functions', () => {
145 it('throws an error', () => {
146 const invalidBuilder = vi.fn((getDefaultMiddleware) => [true] as any)
147 expect(() =>
148 configureStore({ middleware: invalidBuilder, reducer }),
149 ).toThrow('each middleware provided to configureStore must be a function')
150 })
151 })
152
153 describe('given custom middleware', () => {
154 it('calls createStore with custom middleware and without default middleware', () => {
155 const thank: Redux.Middleware = (_store) => (next) => (action) =>
156 next(action)
157 expect(
158 configureStore({ middleware: () => new Tuple(thank), reducer }),
159 ).toBeInstanceOf(Object)
160 expect(redux.applyMiddleware).toHaveBeenCalledWith(thank)
161 if (process.env.TEST_DIST) {
162 expect(composeWithDevToolsSpy).not.toHaveBeenCalled()
163 } else {
164 expect(composeWithDevToolsSpy).toHaveBeenCalledOnce()
165 }
166 expect(redux.createStore).toHaveBeenCalledWith(
167 reducer,
168 undefined,
169 expect.any(Function),
170 )
171 })
172 })
173
174 describe('middleware builder notation', () => {
175 it('calls builder, passes getDefaultMiddleware and uses returned middlewares', () => {
176 const thank = vi.fn(
177 ((_store) => (next) => (action) => 'foobar') as Redux.Middleware,
178 )
179
180 const builder = vi.fn((getDefaultMiddleware) => {
181 expect(getDefaultMiddleware).toEqual(expect.any(Function))
182 expect(getDefaultMiddleware()).toEqual(expect.any(Array))
183
184 return new Tuple(thank)
185 })
186
187 const store = configureStore({ middleware: builder, reducer })
188
189 expect(builder).toHaveBeenCalled()
190
191 expect(store.dispatch({ type: 'test' })).toBe('foobar')
192 })
193 })
194
195 describe('with devTools disabled', () => {
196 it('calls createStore without devTools enhancer', () => {
197 expect(configureStore({ devTools: false, reducer })).toBeInstanceOf(
198 Object,
199 )
200 expect(redux.applyMiddleware).toHaveBeenCalled()
201 expect(redux.compose).toHaveBeenCalled()
202 expect(redux.createStore).toHaveBeenCalledWith(
203 reducer,
204 undefined,
205 expect.any(Function),
206 )
207 })
208 })
209
210 describe('with devTools options', () => {
211 it('calls createStore with devTools enhancer and option', () => {
212 const options = {
213 name: 'myApp',
214 trace: true,
215 }
216 expect(configureStore({ devTools: options, reducer })).toBeInstanceOf(
217 Object,
218 )
219 expect(redux.applyMiddleware).toHaveBeenCalled()
220 if (process.env.TEST_DIST) {
221 expect(composeWithDevToolsSpy).not.toHaveBeenCalled()
222 } else {
223 expect(composeWithDevToolsSpy).toHaveBeenCalledOnce()
224
225 expect(composeWithDevToolsSpy).toHaveBeenLastCalledWith(options)
226 }
227 expect(redux.createStore).toHaveBeenCalledWith(
228 reducer,
229 undefined,
230 expect.any(Function),
231 )
232 })
233 })
234
235 describe('given preloadedState', () => {
236 it('calls createStore with preloadedState', () => {
237 expect(configureStore({ reducer })).toBeInstanceOf(Object)
238 expect(redux.applyMiddleware).toHaveBeenCalled()
239 if (process.env.TEST_DIST) {
240 expect(composeWithDevToolsSpy).not.toHaveBeenCalled()
241 } else {
242 expect(composeWithDevToolsSpy).toHaveBeenCalledOnce()
243 }
244 expect(redux.createStore).toHaveBeenCalledWith(
245 reducer,
246 undefined,
247 expect.any(Function),
248 )
249 })
250 })
251
252 describe('given enhancers', () => {
253 let dummyEnhancerCalled = false
254
255 const dummyEnhancer: StoreEnhancer =
256 (createStore) => (reducer, preloadedState) => {
257 dummyEnhancerCalled = true
258
259 return createStore(reducer, preloadedState)
260 }
261
262 beforeEach(() => {
263 dummyEnhancerCalled = false
264 })
265
266 it('calls createStore with enhancers', () => {
267 expect(
268 configureStore({
269 enhancers: (gDE) => gDE().concat(dummyEnhancer),
270 reducer,
271 }),
272 ).toBeInstanceOf(Object)
273 expect(redux.applyMiddleware).toHaveBeenCalled()
274 if (process.env.TEST_DIST) {
275 expect(composeWithDevToolsSpy).not.toHaveBeenCalled()
276 } else {
277 expect(composeWithDevToolsSpy).toHaveBeenCalledOnce()
278 }
279 expect(redux.createStore).toHaveBeenCalledWith(
280 reducer,
281 undefined,
282 expect.any(Function),
283 )
284
285 expect(dummyEnhancerCalled).toBe(true)
286 })
287
288 describe('invalid arguments', () => {
289 test('enhancers is not a callback', () => {
290 expect(() => configureStore({ reducer, enhancers: [] as any })).toThrow(
291 '`enhancers` field must be a callback',
292 )
293 })
294
295 test('callback fails to return array', () => {
296 expect(() =>
297 configureStore({ reducer, enhancers: (() => {}) as any }),
298 ).toThrow('`enhancers` callback must return an array')
299 })
300
301 test('array contains non-function', () => {
302 expect(() =>
303 configureStore({ reducer, enhancers: (() => ['']) as any }),
304 ).toThrow('each enhancer provided to configureStore must be a function')
305 })
306 })
307
308 const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
309 beforeEach(() => {
310 consoleSpy.mockClear()
311 })
312 afterAll(() => {
313 consoleSpy.mockRestore()
314 })
315
316 it('warns if middleware enhancer is excluded from final array when middlewares are provided', () => {
317 const store = configureStore({
318 reducer,
319 enhancers: () => new Tuple(dummyEnhancer),
320 })
321
322 expect(dummyEnhancerCalled).toBe(true)
323
324 expect(consoleSpy).toHaveBeenCalledWith(
325 'middlewares were provided, but middleware enhancer was not included in final enhancers - make sure to call `getDefaultEnhancers`',
326 )
327 })
328 it("doesn't warn when middleware enhancer is excluded if no middlewares provided", () => {
329 const store = configureStore({
330 reducer,
331 middleware: () => new Tuple(),
332 enhancers: () => new Tuple(dummyEnhancer),
333 })
334
335 expect(dummyEnhancerCalled).toBe(true)
336
337 expect(consoleSpy).not.toHaveBeenCalled()
338 })
339 })
340 })