| 1 |
import { noop } from '@internal/listenerMiddleware/utils' |
| 2 |
import type { PayloadAction, WithSlice } from '@reduxjs/toolkit' |
| 3 |
import { |
| 4 |
asyncThunkCreator, |
| 5 |
buildCreateSlice, |
| 6 |
combineSlices, |
| 7 |
configureStore, |
| 8 |
createAction, |
| 9 |
createSlice, |
| 10 |
} from '@reduxjs/toolkit' |
| 11 |
|
| 12 |
type CreateSlice = typeof createSlice |
| 13 |
|
| 14 |
describe('createSlice', () => { |
| 15 |
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(noop) |
| 16 |
|
| 17 |
beforeEach(() => { |
| 18 |
vi.clearAllMocks() |
| 19 |
}) |
| 20 |
|
| 21 |
afterAll(() => { |
| 22 |
vi.restoreAllMocks() |
| 23 |
}) |
| 24 |
|
| 25 |
describe('when slice is undefined', () => { |
| 26 |
it('should throw an error', () => { |
| 27 |
expect(() => |
| 28 |
// @ts-ignore |
| 29 |
createSlice({ |
| 30 |
reducers: { |
| 31 |
increment: (state) => state + 1, |
| 32 |
multiply: (state, action: PayloadAction<number>) => |
| 33 |
state * action.payload, |
| 34 |
}, |
| 35 |
initialState: 0, |
| 36 |
}), |
| 37 |
).toThrowError() |
| 38 |
}) |
| 39 |
}) |
| 40 |
|
| 41 |
describe('when slice is an empty string', () => { |
| 42 |
it('should throw an error', () => { |
| 43 |
expect(() => |
| 44 |
createSlice({ |
| 45 |
name: '', |
| 46 |
reducers: { |
| 47 |
increment: (state) => state + 1, |
| 48 |
multiply: (state, action: PayloadAction<number>) => |
| 49 |
state * action.payload, |
| 50 |
}, |
| 51 |
initialState: 0, |
| 52 |
}), |
| 53 |
).toThrowError() |
| 54 |
}) |
| 55 |
}) |
| 56 |
|
| 57 |
describe('when initial state is undefined', () => { |
| 58 |
beforeEach(() => { |
| 59 |
vi.stubEnv('NODE_ENV', 'development') |
| 60 |
}) |
| 61 |
|
| 62 |
afterEach(() => { |
| 63 |
vi.unstubAllEnvs() |
| 64 |
}) |
| 65 |
|
| 66 |
it('should throw an error', () => { |
| 67 |
createSlice({ |
| 68 |
name: 'test', |
| 69 |
reducers: {}, |
| 70 |
initialState: undefined, |
| 71 |
}) |
| 72 |
|
| 73 |
expect(consoleErrorSpy).toHaveBeenCalledOnce() |
| 74 |
|
| 75 |
expect(consoleErrorSpy).toHaveBeenLastCalledWith( |
| 76 |
'You must provide an `initialState` value that is not `undefined`. You may have misspelled `initialState`', |
| 77 |
) |
| 78 |
}) |
| 79 |
}) |
| 80 |
|
| 81 |
describe('when passing slice', () => { |
| 82 |
const { actions, reducer, caseReducers } = createSlice({ |
| 83 |
reducers: { |
| 84 |
increment: (state) => state + 1, |
| 85 |
}, |
| 86 |
initialState: 0, |
| 87 |
name: 'cool', |
| 88 |
}) |
| 89 |
|
| 90 |
it('should create increment action', () => { |
| 91 |
expect(actions.hasOwnProperty('increment')).toBe(true) |
| 92 |
}) |
| 93 |
|
| 94 |
it('should have the correct action for increment', () => { |
| 95 |
expect(actions.increment()).toEqual({ |
| 96 |
type: 'cool/increment', |
| 97 |
payload: undefined, |
| 98 |
}) |
| 99 |
}) |
| 100 |
|
| 101 |
it('should return the correct value from reducer', () => { |
| 102 |
expect(reducer(undefined, actions.increment())).toEqual(1) |
| 103 |
}) |
| 104 |
|
| 105 |
it('should include the generated case reducers', () => { |
| 106 |
expect(caseReducers).toBeTruthy() |
| 107 |
expect(caseReducers.increment).toBeTruthy() |
| 108 |
expect(typeof caseReducers.increment).toBe('function') |
| 109 |
}) |
| 110 |
|
| 111 |
it('getInitialState should return the state', () => { |
| 112 |
const initialState = 42 |
| 113 |
const slice = createSlice({ |
| 114 |
name: 'counter', |
| 115 |
initialState, |
| 116 |
reducers: {}, |
| 117 |
}) |
| 118 |
|
| 119 |
expect(slice.getInitialState()).toBe(initialState) |
| 120 |
}) |
| 121 |
|
| 122 |
it('should allow non-draftable initial state', () => { |
| 123 |
expect(() => |
| 124 |
createSlice({ |
| 125 |
name: 'params', |
| 126 |
initialState: new URLSearchParams(), |
| 127 |
reducers: {}, |
| 128 |
}), |
| 129 |
).not.toThrowError() |
| 130 |
}) |
| 131 |
}) |
| 132 |
|
| 133 |
describe('when initialState is a function', () => { |
| 134 |
const initialState = () => ({ user: '' }) |
| 135 |
|
| 136 |
const { actions, reducer } = createSlice({ |
| 137 |
reducers: { |
| 138 |
setUserName: (state, action) => { |
| 139 |
state.user = action.payload |
| 140 |
}, |
| 141 |
}, |
| 142 |
initialState, |
| 143 |
name: 'user', |
| 144 |
}) |
| 145 |
|
| 146 |
it('should set the username', () => { |
| 147 |
expect(reducer(undefined, actions.setUserName('eric'))).toEqual({ |
| 148 |
user: 'eric', |
| 149 |
}) |
| 150 |
}) |
| 151 |
|
| 152 |
it('getInitialState should return the state', () => { |
| 153 |
const initialState = () => 42 |
| 154 |
const slice = createSlice({ |
| 155 |
name: 'counter', |
| 156 |
initialState, |
| 157 |
reducers: {}, |
| 158 |
}) |
| 159 |
|
| 160 |
expect(slice.getInitialState()).toBe(42) |
| 161 |
}) |
| 162 |
|
| 163 |
it('should allow non-draftable initial state', () => { |
| 164 |
expect(() => |
| 165 |
createSlice({ |
| 166 |
name: 'params', |
| 167 |
initialState: () => new URLSearchParams(), |
| 168 |
reducers: {}, |
| 169 |
}), |
| 170 |
).not.toThrowError() |
| 171 |
}) |
| 172 |
}) |
| 173 |
|
| 174 |
describe('when mutating state object', () => { |
| 175 |
const initialState = { user: '' } |
| 176 |
|
| 177 |
const { actions, reducer } = createSlice({ |
| 178 |
reducers: { |
| 179 |
setUserName: (state, action) => { |
| 180 |
state.user = action.payload |
| 181 |
}, |
| 182 |
}, |
| 183 |
initialState, |
| 184 |
name: 'user', |
| 185 |
}) |
| 186 |
|
| 187 |
it('should set the username', () => { |
| 188 |
expect(reducer(initialState, actions.setUserName('eric'))).toEqual({ |
| 189 |
user: 'eric', |
| 190 |
}) |
| 191 |
}) |
| 192 |
}) |
| 193 |
|
| 194 |
describe('when passing extra reducers', () => { |
| 195 |
const addMore = createAction<{ amount: number }>('ADD_MORE') |
| 196 |
|
| 197 |
const { reducer } = createSlice({ |
| 198 |
name: 'test', |
| 199 |
reducers: { |
| 200 |
increment: (state) => state + 1, |
| 201 |
multiply: (state, action) => state * action.payload, |
| 202 |
}, |
| 203 |
extraReducers: (builder) => { |
| 204 |
builder.addCase( |
| 205 |
addMore, |
| 206 |
(state, action) => state + action.payload.amount, |
| 207 |
) |
| 208 |
}, |
| 209 |
|
| 210 |
initialState: 0, |
| 211 |
}) |
| 212 |
|
| 213 |
it('should call extra reducers when their actions are dispatched', () => { |
| 214 |
const result = reducer(10, addMore({ amount: 5 })) |
| 215 |
|
| 216 |
expect(result).toBe(15) |
| 217 |
}) |
| 218 |
|
| 219 |
describe('builder callback for extraReducers', () => { |
| 220 |
const increment = createAction<number, 'increment'>('increment') |
| 221 |
|
| 222 |
test('can be used with actionCreators', () => { |
| 223 |
const slice = createSlice({ |
| 224 |
name: 'counter', |
| 225 |
initialState: 0, |
| 226 |
reducers: {}, |
| 227 |
extraReducers: (builder) => |
| 228 |
builder.addCase( |
| 229 |
increment, |
| 230 |
(state, action) => state + action.payload, |
| 231 |
), |
| 232 |
}) |
| 233 |
expect(slice.reducer(0, increment(5))).toBe(5) |
| 234 |
}) |
| 235 |
|
| 236 |
test('can be used with string action types', () => { |
| 237 |
const slice = createSlice({ |
| 238 |
name: 'counter', |
| 239 |
initialState: 0, |
| 240 |
reducers: {}, |
| 241 |
extraReducers: (builder) => |
| 242 |
builder.addCase( |
| 243 |
'increment', |
| 244 |
(state, action: { type: 'increment'; payload: number }) => |
| 245 |
state + action.payload, |
| 246 |
), |
| 247 |
}) |
| 248 |
expect(slice.reducer(0, increment(5))).toBe(5) |
| 249 |
}) |
| 250 |
|
| 251 |
test('prevents the same action type from being specified twice', () => { |
| 252 |
expect(() => { |
| 253 |
const slice = createSlice({ |
| 254 |
name: 'counter', |
| 255 |
initialState: 0, |
| 256 |
reducers: {}, |
| 257 |
extraReducers: (builder) => |
| 258 |
builder |
| 259 |
.addCase('increment', (state) => state + 1) |
| 260 |
.addCase('increment', (state) => state + 1), |
| 261 |
}) |
| 262 |
slice.reducer(undefined, { type: 'unrelated' }) |
| 263 |
}).toThrowErrorMatchingInlineSnapshot( |
| 264 |
`[Error: \`builder.addCase\` cannot be called with two reducers for the same action type 'increment']`, |
| 265 |
) |
| 266 |
}) |
| 267 |
|
| 268 |
test('can be used with addMatcher and type guard functions', () => { |
| 269 |
const slice = createSlice({ |
| 270 |
name: 'counter', |
| 271 |
initialState: 0, |
| 272 |
reducers: {}, |
| 273 |
extraReducers: (builder) => |
| 274 |
builder.addMatcher( |
| 275 |
increment.match, |
| 276 |
(state, action: { type: 'increment'; payload: number }) => |
| 277 |
state + action.payload, |
| 278 |
), |
| 279 |
}) |
| 280 |
expect(slice.reducer(0, increment(5))).toBe(5) |
| 281 |
}) |
| 282 |
|
| 283 |
test('can be used with addDefaultCase', () => { |
| 284 |
const slice = createSlice({ |
| 285 |
name: 'counter', |
| 286 |
initialState: 0, |
| 287 |
reducers: {}, |
| 288 |
extraReducers: (builder) => |
| 289 |
builder.addDefaultCase( |
| 290 |
(state, action) => |
| 291 |
state + (action as PayloadAction<number>).payload, |
| 292 |
), |
| 293 |
}) |
| 294 |
expect(slice.reducer(0, increment(5))).toBe(5) |
| 295 |
}) |
| 296 |
|
| 297 |
// for further tests, see the test of createReducer that goes way more into depth on this |
| 298 |
}) |
| 299 |
}) |
| 300 |
|
| 301 |
describe('behavior with enhanced case reducers', () => { |
| 302 |
it('should pass all arguments to the prepare function', () => { |
| 303 |
const prepare = vi.fn((payload, somethingElse) => ({ payload })) |
| 304 |
|
| 305 |
const testSlice = createSlice({ |
| 306 |
name: 'test', |
| 307 |
initialState: 0, |
| 308 |
reducers: { |
| 309 |
testReducer: { |
| 310 |
reducer: (s) => s, |
| 311 |
prepare, |
| 312 |
}, |
| 313 |
}, |
| 314 |
}) |
| 315 |
|
| 316 |
expect(testSlice.actions.testReducer('a', 1)).toEqual({ |
| 317 |
type: 'test/testReducer', |
| 318 |
payload: 'a', |
| 319 |
}) |
| 320 |
expect(prepare).toHaveBeenCalledWith('a', 1) |
| 321 |
}) |
| 322 |
|
| 323 |
it('should call the reducer function', () => { |
| 324 |
const reducer = vi.fn(() => 5) |
| 325 |
|
| 326 |
const testSlice = createSlice({ |
| 327 |
name: 'test', |
| 328 |
initialState: 0, |
| 329 |
reducers: { |
| 330 |
testReducer: { |
| 331 |
reducer, |
| 332 |
prepare: (payload: any) => ({ payload }), |
| 333 |
}, |
| 334 |
}, |
| 335 |
}) |
| 336 |
|
| 337 |
testSlice.reducer(0, testSlice.actions.testReducer('testPayload')) |
| 338 |
expect(reducer).toHaveBeenCalledWith( |
| 339 |
0, |
| 340 |
expect.objectContaining({ payload: 'testPayload' }), |
| 341 |
) |
| 342 |
}) |
| 343 |
}) |
| 344 |
|
| 345 |
describe('circularity', () => { |
| 346 |
test('extraReducers can reference each other circularly', () => { |
| 347 |
const first = createSlice({ |
| 348 |
name: 'first', |
| 349 |
initialState: 'firstInitial', |
| 350 |
reducers: { |
| 351 |
something() { |
| 352 |
return 'firstSomething' |
| 353 |
}, |
| 354 |
}, |
| 355 |
extraReducers(builder) { |
| 356 |
// eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 357 |
builder.addCase(second.actions.other, () => { |
| 358 |
return 'firstOther' |
| 359 |
}) |
| 360 |
}, |
| 361 |
}) |
| 362 |
const second = createSlice({ |
| 363 |
name: 'second', |
| 364 |
initialState: 'secondInitial', |
| 365 |
reducers: { |
| 366 |
other() { |
| 367 |
return 'secondOther' |
| 368 |
}, |
| 369 |
}, |
| 370 |
extraReducers(builder) { |
| 371 |
builder.addCase(first.actions.something, () => { |
| 372 |
return 'secondSomething' |
| 373 |
}) |
| 374 |
}, |
| 375 |
}) |
| 376 |
|
| 377 |
expect(first.reducer(undefined, { type: 'unrelated' })).toBe( |
| 378 |
'firstInitial', |
| 379 |
) |
| 380 |
expect(first.reducer(undefined, first.actions.something())).toBe( |
| 381 |
'firstSomething', |
| 382 |
) |
| 383 |
expect(first.reducer(undefined, second.actions.other())).toBe( |
| 384 |
'firstOther', |
| 385 |
) |
| 386 |
|
| 387 |
expect(second.reducer(undefined, { type: 'unrelated' })).toBe( |
| 388 |
'secondInitial', |
| 389 |
) |
| 390 |
expect(second.reducer(undefined, first.actions.something())).toBe( |
| 391 |
'secondSomething', |
| 392 |
) |
| 393 |
expect(second.reducer(undefined, second.actions.other())).toBe( |
| 394 |
'secondOther', |
| 395 |
) |
| 396 |
}) |
| 397 |
}) |
| 398 |
|
| 399 |
describe('Deprecation warnings', () => { |
| 400 |
beforeEach(() => { |
| 401 |
vi.resetModules() |
| 402 |
}) |
| 403 |
|
| 404 |
afterEach(() => { |
| 405 |
vi.unstubAllEnvs() |
| 406 |
}) |
| 407 |
|
| 408 |
// NOTE: This needs to be in front of the later `createReducer` call to check the one-time warning |
| 409 |
it('Throws an error if the legacy object notation is used', async () => { |
| 410 |
const { createSlice } = await import('../createSlice') |
| 411 |
|
| 412 |
let dummySlice = (createSlice as CreateSlice)({ |
| 413 |
name: 'dummy', |
| 414 |
initialState: [], |
| 415 |
reducers: {}, |
| 416 |
extraReducers: { |
| 417 |
// @ts-ignore |
| 418 |
a: () => [], |
| 419 |
}, |
| 420 |
}) |
| 421 |
let reducer: any |
| 422 |
// Have to trigger the lazy creation |
| 423 |
const wrapper = () => { |
| 424 |
reducer = dummySlice.reducer |
| 425 |
reducer(undefined, { type: 'dummy' }) |
| 426 |
} |
| 427 |
|
| 428 |
expect(wrapper).toThrowError( |
| 429 |
/The object notation for `createSlice.extraReducers` has been removed/, |
| 430 |
) |
| 431 |
|
| 432 |
dummySlice = (createSlice as CreateSlice)({ |
| 433 |
name: 'dummy', |
| 434 |
initialState: [], |
| 435 |
reducers: {}, |
| 436 |
extraReducers: { |
| 437 |
// @ts-ignore |
| 438 |
a: () => [], |
| 439 |
}, |
| 440 |
}) |
| 441 |
expect(wrapper).toThrowError( |
| 442 |
/The object notation for `createSlice.extraReducers` has been removed/, |
| 443 |
) |
| 444 |
}) |
| 445 |
|
| 446 |
// TODO Determine final production behavior here |
| 447 |
it.todo('Crashes in production', () => { |
| 448 |
vi.stubEnv('NODE_ENV', 'production') |
| 449 |
|
| 450 |
const { createSlice } = require('../createSlice') |
| 451 |
|
| 452 |
const dummySlice = (createSlice as CreateSlice)({ |
| 453 |
name: 'dummy', |
| 454 |
initialState: [], |
| 455 |
reducers: {}, |
| 456 |
// @ts-ignore |
| 457 |
extraReducers: {}, |
| 458 |
}) |
| 459 |
const wrapper = () => { |
| 460 |
const { reducer } = dummySlice |
| 461 |
reducer(undefined, { type: 'dummy' }) |
| 462 |
} |
| 463 |
|
| 464 |
expect(wrapper).toThrowError( |
| 465 |
/The object notation for `createSlice.extraReducers` has been removed/, |
| 466 |
) |
| 467 |
|
| 468 |
vi.unstubAllEnvs() |
| 469 |
}) |
| 470 |
}) |
| 471 |
describe('slice selectors', () => { |
| 472 |
const slice = createSlice({ |
| 473 |
name: 'counter', |
| 474 |
initialState: 42, |
| 475 |
reducers: {}, |
| 476 |
selectors: { |
| 477 |
selectSlice: (state) => state, |
| 478 |
selectMultiple: Object.assign( |
| 479 |
(state: number, multiplier: number) => state * multiplier, |
| 480 |
{ test: 0 }, |
| 481 |
), |
| 482 |
}, |
| 483 |
}) |
| 484 |
it('expects reducer under slice.reducerPath if no selectState callback passed', () => { |
| 485 |
const testState = { |
| 486 |
[slice.reducerPath]: slice.getInitialState(), |
| 487 |
} |
| 488 |
const { selectSlice, selectMultiple } = slice.selectors |
| 489 |
expect(selectSlice(testState)).toBe(slice.getInitialState()) |
| 490 |
expect(selectMultiple(testState, 2)).toBe(slice.getInitialState() * 2) |
| 491 |
}) |
| 492 |
it('allows passing a selector for a custom location', () => { |
| 493 |
const customState = { |
| 494 |
number: slice.getInitialState(), |
| 495 |
} |
| 496 |
const { selectSlice, selectMultiple } = slice.getSelectors( |
| 497 |
(state: typeof customState) => state.number, |
| 498 |
) |
| 499 |
expect(selectSlice(customState)).toBe(slice.getInitialState()) |
| 500 |
expect(selectMultiple(customState, 2)).toBe(slice.getInitialState() * 2) |
| 501 |
}) |
| 502 |
it('allows accessing properties on the selector', () => { |
| 503 |
expect(slice.selectors.selectMultiple.unwrapped.test).toBe(0) |
| 504 |
}) |
| 505 |
it('has selectSlice attached to slice, which can go without this', () => { |
| 506 |
const slice = createSlice({ |
| 507 |
name: 'counter', |
| 508 |
initialState: 42, |
| 509 |
reducers: {}, |
| 510 |
}) |
| 511 |
const { selectSlice } = slice |
| 512 |
expect(() => selectSlice({ counter: 42 })).not.toThrow() |
| 513 |
expect(selectSlice({ counter: 42 })).toBe(42) |
| 514 |
}) |
| 515 |
}) |
| 516 |
describe('slice injections', () => { |
| 517 |
it('uses injectInto to inject slice into combined reducer', () => { |
| 518 |
const slice = createSlice({ |
| 519 |
name: 'counter', |
| 520 |
initialState: 42, |
| 521 |
reducers: { |
| 522 |
increment: (state) => ++state, |
| 523 |
}, |
| 524 |
selectors: { |
| 525 |
selectMultiple: (state, multiplier: number) => state * multiplier, |
| 526 |
}, |
| 527 |
}) |
| 528 |
|
| 529 |
const { increment } = slice.actions |
| 530 |
|
| 531 |
const combinedReducer = combineSlices({ |
| 532 |
static: slice.reducer, |
| 533 |
}).withLazyLoadedSlices<WithSlice<typeof slice>>() |
| 534 |
|
| 535 |
const uninjectedState = combinedReducer(undefined, increment()) |
| 536 |
|
| 537 |
expect(uninjectedState.counter).toBe(undefined) |
| 538 |
|
| 539 |
const injectedSlice = slice.injectInto(combinedReducer) |
| 540 |
|
| 541 |
// selector returns initial state if undefined in real state |
| 542 |
expect(injectedSlice.selectSlice(uninjectedState)).toBe( |
| 543 |
slice.getInitialState(), |
| 544 |
) |
| 545 |
expect(injectedSlice.selectors.selectMultiple({}, 1)).toBe( |
| 546 |
slice.getInitialState(), |
| 547 |
) |
| 548 |
expect(injectedSlice.getSelectors().selectMultiple(undefined, 1)).toBe( |
| 549 |
slice.getInitialState(), |
| 550 |
) |
| 551 |
|
| 552 |
const injectedState = combinedReducer(undefined, increment()) |
| 553 |
|
| 554 |
expect(injectedSlice.selectSlice(injectedState)).toBe( |
| 555 |
slice.getInitialState() + 1, |
| 556 |
) |
| 557 |
expect(injectedSlice.selectors.selectMultiple(injectedState, 1)).toBe( |
| 558 |
slice.getInitialState() + 1, |
| 559 |
) |
| 560 |
}) |
| 561 |
it('allows providing a custom name to inject under', () => { |
| 562 |
const slice = createSlice({ |
| 563 |
name: 'counter', |
| 564 |
reducerPath: 'injected', |
| 565 |
initialState: 42, |
| 566 |
reducers: { |
| 567 |
increment: (state) => ++state, |
| 568 |
}, |
| 569 |
selectors: { |
| 570 |
selectMultiple: (state, multiplier: number) => state * multiplier, |
| 571 |
}, |
| 572 |
}) |
| 573 |
|
| 574 |
const { increment } = slice.actions |
| 575 |
|
| 576 |
const combinedReducer = combineSlices({ |
| 577 |
static: slice.reducer, |
| 578 |
}).withLazyLoadedSlices<WithSlice<typeof slice> & { injected2: number }>() |
| 579 |
|
| 580 |
const uninjectedState = combinedReducer(undefined, increment()) |
| 581 |
|
| 582 |
expect(uninjectedState.injected).toBe(undefined) |
| 583 |
|
| 584 |
const injected = slice.injectInto(combinedReducer) |
| 585 |
|
| 586 |
const injectedState = combinedReducer(undefined, increment()) |
| 587 |
|
| 588 |
expect(injected.selectSlice(injectedState)).toBe( |
| 589 |
slice.getInitialState() + 1, |
| 590 |
) |
| 591 |
expect(injected.selectors.selectMultiple(injectedState, 2)).toBe( |
| 592 |
(slice.getInitialState() + 1) * 2, |
| 593 |
) |
| 594 |
|
| 595 |
const injected2 = slice.injectInto(combinedReducer, { |
| 596 |
reducerPath: 'injected2', |
| 597 |
}) |
| 598 |
|
| 599 |
const injected2State = combinedReducer(undefined, increment()) |
| 600 |
|
| 601 |
expect(injected2.selectSlice(injected2State)).toBe( |
| 602 |
slice.getInitialState() + 1, |
| 603 |
) |
| 604 |
expect(injected2.selectors.selectMultiple(injected2State, 2)).toBe( |
| 605 |
(slice.getInitialState() + 1) * 2, |
| 606 |
) |
| 607 |
}) |
| 608 |
it('avoids incorrectly caching selectors', () => { |
| 609 |
const slice = createSlice({ |
| 610 |
name: 'counter', |
| 611 |
reducerPath: 'injected', |
| 612 |
initialState: 42, |
| 613 |
reducers: { |
| 614 |
increment: (state) => ++state, |
| 615 |
}, |
| 616 |
selectors: { |
| 617 |
selectMultiple: (state, multiplier: number) => state * multiplier, |
| 618 |
}, |
| 619 |
}) |
| 620 |
expect(slice.getSelectors()).toBe(slice.getSelectors()) |
| 621 |
const combinedReducer = combineSlices({ |
| 622 |
static: slice.reducer, |
| 623 |
}).withLazyLoadedSlices<WithSlice<typeof slice>>() |
| 624 |
|
| 625 |
const injected = slice.injectInto(combinedReducer) |
| 626 |
|
| 627 |
expect(injected.getSelectors()).not.toBe(slice.getSelectors()) |
| 628 |
|
| 629 |
expect(injected.getSelectors().selectMultiple(undefined, 1)).toBe(42) |
| 630 |
|
| 631 |
expect(() => |
| 632 |
// @ts-expect-error |
| 633 |
slice.getSelectors().selectMultiple(undefined, 1), |
| 634 |
).toThrowErrorMatchingInlineSnapshot( |
| 635 |
`[Error: selectState returned undefined for an uninjected slice reducer]`, |
| 636 |
) |
| 637 |
|
| 638 |
const injected2 = slice.injectInto(combinedReducer, { |
| 639 |
reducerPath: 'other', |
| 640 |
}) |
| 641 |
|
| 642 |
// can use same cache for localised selectors |
| 643 |
expect(injected.getSelectors()).toBe(injected2.getSelectors()) |
| 644 |
// these should be different |
| 645 |
expect(injected.selectors).not.toBe(injected2.selectors) |
| 646 |
}) |
| 647 |
}) |
| 648 |
describe('reducers definition with asyncThunks', () => { |
| 649 |
it('is disabled by default', () => { |
| 650 |
expect(() => |
| 651 |
createSlice({ |
| 652 |
name: 'test', |
| 653 |
initialState: [] as any[], |
| 654 |
reducers: (create) => ({ thunk: create.asyncThunk(() => {}) }), |
| 655 |
}), |
| 656 |
).toThrowErrorMatchingInlineSnapshot( |
| 657 |
`[Error: Cannot use \`create.asyncThunk\` in the built-in \`createSlice\`. Use \`buildCreateSlice({ creators: { asyncThunk: asyncThunkCreator } })\` to create a customised version of \`createSlice\`.]`, |
| 658 |
) |
| 659 |
}) |
| 660 |
const createAppSlice = buildCreateSlice({ |
| 661 |
creators: { asyncThunk: asyncThunkCreator }, |
| 662 |
}) |
| 663 |
function pending(state: any[], action: any) { |
| 664 |
state.push(['pendingReducer', action]) |
| 665 |
} |
| 666 |
function fulfilled(state: any[], action: any) { |
| 667 |
state.push(['fulfilledReducer', action]) |
| 668 |
} |
| 669 |
function rejected(state: any[], action: any) { |
| 670 |
state.push(['rejectedReducer', action]) |
| 671 |
} |
| 672 |
function settled(state: any[], action: any) { |
| 673 |
state.push(['settledReducer', action]) |
| 674 |
} |
| 675 |
|
| 676 |
test('successful thunk', async () => { |
| 677 |
const slice = createAppSlice({ |
| 678 |
name: 'test', |
| 679 |
initialState: [] as any[], |
| 680 |
reducers: (create) => ({ |
| 681 |
thunkReducers: create.asyncThunk( |
| 682 |
function payloadCreator(arg: string, api) { |
| 683 |
return Promise.resolve('resolved payload') |
| 684 |
}, |
| 685 |
{ pending, fulfilled, rejected, settled }, |
| 686 |
), |
| 687 |
}), |
| 688 |
}) |
| 689 |
|
| 690 |
const store = configureStore({ |
| 691 |
reducer: slice.reducer, |
| 692 |
}) |
| 693 |
await store.dispatch(slice.actions.thunkReducers('test')) |
| 694 |
expect(store.getState()).toMatchObject([ |
| 695 |
[ |
| 696 |
'pendingReducer', |
| 697 |
{ |
| 698 |
type: 'test/thunkReducers/pending', |
| 699 |
payload: undefined, |
| 700 |
}, |
| 701 |
], |
| 702 |
[ |
| 703 |
'fulfilledReducer', |
| 704 |
{ |
| 705 |
type: 'test/thunkReducers/fulfilled', |
| 706 |
payload: 'resolved payload', |
| 707 |
}, |
| 708 |
], |
| 709 |
[ |
| 710 |
'settledReducer', |
| 711 |
{ |
| 712 |
type: 'test/thunkReducers/fulfilled', |
| 713 |
payload: 'resolved payload', |
| 714 |
}, |
| 715 |
], |
| 716 |
]) |
| 717 |
}) |
| 718 |
|
| 719 |
test('rejected thunk', async () => { |
| 720 |
const slice = createAppSlice({ |
| 721 |
name: 'test', |
| 722 |
initialState: [] as any[], |
| 723 |
reducers: (create) => ({ |
| 724 |
thunkReducers: create.asyncThunk( |
| 725 |
// payloadCreator isn't allowed to return never |
| 726 |
function payloadCreator(arg: string, api): any { |
| 727 |
throw new Error('') |
| 728 |
}, |
| 729 |
{ pending, fulfilled, rejected, settled }, |
| 730 |
), |
| 731 |
}), |
| 732 |
}) |
| 733 |
|
| 734 |
const store = configureStore({ |
| 735 |
reducer: slice.reducer, |
| 736 |
}) |
| 737 |
await store.dispatch(slice.actions.thunkReducers('test')) |
| 738 |
expect(store.getState()).toMatchObject([ |
| 739 |
[ |
| 740 |
'pendingReducer', |
| 741 |
{ |
| 742 |
type: 'test/thunkReducers/pending', |
| 743 |
payload: undefined, |
| 744 |
}, |
| 745 |
], |
| 746 |
[ |
| 747 |
'rejectedReducer', |
| 748 |
{ |
| 749 |
type: 'test/thunkReducers/rejected', |
| 750 |
payload: undefined, |
| 751 |
}, |
| 752 |
], |
| 753 |
[ |
| 754 |
'settledReducer', |
| 755 |
{ |
| 756 |
type: 'test/thunkReducers/rejected', |
| 757 |
payload: undefined, |
| 758 |
}, |
| 759 |
], |
| 760 |
]) |
| 761 |
}) |
| 762 |
|
| 763 |
test('with options', async () => { |
| 764 |
const slice = createAppSlice({ |
| 765 |
name: 'test', |
| 766 |
initialState: [] as any[], |
| 767 |
reducers: (create) => ({ |
| 768 |
thunkReducers: create.asyncThunk( |
| 769 |
function payloadCreator(arg: string, api) { |
| 770 |
return 'should not call this' |
| 771 |
}, |
| 772 |
{ |
| 773 |
options: { |
| 774 |
condition() { |
| 775 |
return false |
| 776 |
}, |
| 777 |
dispatchConditionRejection: true, |
| 778 |
}, |
| 779 |
pending, |
| 780 |
fulfilled, |
| 781 |
rejected, |
| 782 |
settled, |
| 783 |
}, |
| 784 |
), |
| 785 |
}), |
| 786 |
}) |
| 787 |
|
| 788 |
const store = configureStore({ |
| 789 |
reducer: slice.reducer, |
| 790 |
}) |
| 791 |
await store.dispatch(slice.actions.thunkReducers('test')) |
| 792 |
expect(store.getState()).toMatchObject([ |
| 793 |
[ |
| 794 |
'rejectedReducer', |
| 795 |
{ |
| 796 |
type: 'test/thunkReducers/rejected', |
| 797 |
payload: undefined, |
| 798 |
meta: { condition: true }, |
| 799 |
}, |
| 800 |
], |
| 801 |
[ |
| 802 |
'settledReducer', |
| 803 |
{ |
| 804 |
type: 'test/thunkReducers/rejected', |
| 805 |
payload: undefined, |
| 806 |
meta: { condition: true }, |
| 807 |
}, |
| 808 |
], |
| 809 |
]) |
| 810 |
}) |
| 811 |
|
| 812 |
test('has caseReducers for the asyncThunk', async () => { |
| 813 |
const slice = createAppSlice({ |
| 814 |
name: 'test', |
| 815 |
initialState: [], |
| 816 |
reducers: (create) => ({ |
| 817 |
thunkReducers: create.asyncThunk( |
| 818 |
function payloadCreator(arg, api) { |
| 819 |
return Promise.resolve('resolved payload') |
| 820 |
}, |
| 821 |
{ pending, fulfilled, settled }, |
| 822 |
), |
| 823 |
}), |
| 824 |
}) |
| 825 |
|
| 826 |
expect(slice.caseReducers.thunkReducers.pending).toBe(pending) |
| 827 |
expect(slice.caseReducers.thunkReducers.fulfilled).toBe(fulfilled) |
| 828 |
expect(slice.caseReducers.thunkReducers.settled).toBe(settled) |
| 829 |
// even though it is not defined above, this should at least be a no-op function to match the TypeScript typings |
| 830 |
// and should be callable as a reducer even if it does nothing |
| 831 |
expect(() => |
| 832 |
slice.caseReducers.thunkReducers.rejected( |
| 833 |
[], |
| 834 |
slice.actions.thunkReducers.rejected( |
| 835 |
new Error('test'), |
| 836 |
'fakeRequestId', |
| 837 |
), |
| 838 |
), |
| 839 |
).not.toThrow() |
| 840 |
}) |
| 841 |
|
| 842 |
test('can define reducer with prepare statement using create.preparedReducer', async () => { |
| 843 |
const slice = createSlice({ |
| 844 |
name: 'test', |
| 845 |
initialState: [] as any[], |
| 846 |
reducers: (create) => ({ |
| 847 |
prepared: create.preparedReducer( |
| 848 |
(p: string, m: number, e: { message: string }) => ({ |
| 849 |
payload: p, |
| 850 |
meta: m, |
| 851 |
error: e, |
| 852 |
}), |
| 853 |
(state, action) => { |
| 854 |
state.push(action) |
| 855 |
}, |
| 856 |
), |
| 857 |
}), |
| 858 |
}) |
| 859 |
|
| 860 |
expect( |
| 861 |
slice.reducer( |
| 862 |
[], |
| 863 |
slice.actions.prepared('test', 1, { message: 'err' }), |
| 864 |
), |
| 865 |
).toMatchInlineSnapshot(` |
| 866 |
[ |
| 867 |
{ |
| 868 |
"error": { |
| 869 |
"message": "err", |
| 870 |
}, |
| 871 |
"meta": 1, |
| 872 |
"payload": "test", |
| 873 |
"type": "test/prepared", |
| 874 |
}, |
| 875 |
] |
| 876 |
`) |
| 877 |
}) |
| 878 |
|
| 879 |
test('throws an error when invoked with a normal `prepare` object that has not gone through a `create.preparedReducer` call', async () => { |
| 880 |
expect(() => |
| 881 |
createSlice({ |
| 882 |
name: 'test', |
| 883 |
initialState: [] as any[], |
| 884 |
reducers: (create) => ({ |
| 885 |
prepared: { |
| 886 |
prepare: (p: string, m: number, e: { message: string }) => ({ |
| 887 |
payload: p, |
| 888 |
meta: m, |
| 889 |
error: e, |
| 890 |
}), |
| 891 |
reducer: (state, action) => { |
| 892 |
state.push(action) |
| 893 |
}, |
| 894 |
}, |
| 895 |
}), |
| 896 |
}), |
| 897 |
).toThrowErrorMatchingInlineSnapshot( |
| 898 |
`[Error: Please use the \`create.preparedReducer\` notation for prepared action creators with the \`create\` notation.]`, |
| 899 |
) |
| 900 |
}) |
| 901 |
}) |
| 902 |
}) |