Skip to content

Commit 2e400d5

Browse files
authored
Merge pull request #4412 from reduxjs/bugfix/4411-immer-current
2 parents f2cc82c + 4cc7b1f commit 2e400d5

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

packages/toolkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
"format": "prettier --write \"(src|examples)/**/*.{ts,tsx}\" \"**/*.md\"",
104104
"format:check": "prettier --list-different \"(src|examples)/**/*.{ts,tsx}\" \"docs/*/**.md\"",
105105
"lint": "eslint src examples",
106-
"test": "vitest --run --typecheck",
106+
"test": "vitest --typecheck --run ",
107107
"test:watch": "vitest --watch",
108108
"type-tests": "yarn tsc -p tsconfig.test.json --noEmit",
109109
"prepack": "yarn build",

packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
createSlice,
66
configureStore,
77
nanoid,
8+
PayloadAction,
89
} from '@reduxjs/toolkit'
910
import type { BookModel } from './fixtures/book'
1011
import {
@@ -783,6 +784,30 @@ describe('Sorted State Adapter', () => {
783784
//expect(numSorts).toBeLessThan(25_000)
784785
})
785786

787+
it('should not throw an Immer `current` error when `state.ids` is a plain array', () => {
788+
const book1: BookModel = { id: 'a', title: 'First' }
789+
const initialState = adapter.getInitialState()
790+
const withItems = adapter.addMany(initialState, [book1])
791+
const booksSlice = createSlice({
792+
name: 'books',
793+
initialState,
794+
reducers: {
795+
testCurrentBehavior(state, action: PayloadAction<BookModel>) {
796+
// Will overwrite `state.ids` with a plain array
797+
adapter.removeAll(state)
798+
799+
// will call `splitAddedUpdatedEntities` and call `current(state.ids)`
800+
adapter.upsertMany(state, [book1])
801+
},
802+
},
803+
})
804+
805+
booksSlice.reducer(
806+
initialState,
807+
booksSlice.actions.testCurrentBehavior(book1),
808+
)
809+
})
810+
786811
describe('can be used mutably when wrapped in createNextState', () => {
787812
test('removeAll', () => {
788813
const withTwo = adapter.addMany(state, [TheGreatGatsby, AnimalFarm])

packages/toolkit/src/entities/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function splitAddedUpdatedEntities<T, Id extends EntityId>(
4747
): [T[], Update<T, Id>[], Id[]] {
4848
newEntities = ensureEntitiesArray(newEntities)
4949

50-
const existingIdsArray = current(state.ids) as Id[]
50+
const existingIdsArray = getCurrent(state.ids) as Id[]
5151
const existingIds = new Set<Id>(existingIdsArray)
5252

5353
const added: T[] = []

0 commit comments

Comments
 (0)