diff --git a/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts b/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts index cc1bf568de..fb09089ece 100644 --- a/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts +++ b/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts @@ -5,6 +5,7 @@ import { createSlice, configureStore, nanoid, + PayloadAction, } from '@reduxjs/toolkit' import type { BookModel } from './fixtures/book' import { @@ -783,6 +784,30 @@ describe('Sorted State Adapter', () => { //expect(numSorts).toBeLessThan(25_000) }) + it('should not throw an Immer `current` error when `state.ids` is a plain array', () => { + const book1: BookModel = { id: 'a', title: 'First' } + const initialState = adapter.getInitialState() + const withItems = adapter.addMany(initialState, [book1]) + const booksSlice = createSlice({ + name: 'books', + initialState, + reducers: { + testCurrentBehavior(state, action: PayloadAction) { + // Will overwrite `state.ids` with a plain array + adapter.removeAll(state) + + // will call `splitAddedUpdatedEntities` and call `current(state.ids)` + adapter.upsertMany(state, [book1]) + }, + }, + }) + + booksSlice.reducer( + initialState, + booksSlice.actions.testCurrentBehavior(book1), + ) + }) + describe('can be used mutably when wrapped in createNextState', () => { test('removeAll', () => { const withTwo = adapter.addMany(state, [TheGreatGatsby, AnimalFarm]) diff --git a/packages/toolkit/src/entities/utils.ts b/packages/toolkit/src/entities/utils.ts index 895ef2b35f..c073a17daa 100644 --- a/packages/toolkit/src/entities/utils.ts +++ b/packages/toolkit/src/entities/utils.ts @@ -47,7 +47,7 @@ export function splitAddedUpdatedEntities( ): [T[], Update[], Id[]] { newEntities = ensureEntitiesArray(newEntities) - const existingIdsArray = current(state.ids) as Id[] + const existingIdsArray = getCurrent(state.ids) as Id[] const existingIds = new Set(existingIdsArray) const added: T[] = []