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