diff --git a/packages/toolkit/src/entities/sorted_state_adapter.ts b/packages/toolkit/src/entities/sorted_state_adapter.ts index ec2ae5dd50..7731bd5ba7 100644 --- a/packages/toolkit/src/entities/sorted_state_adapter.ts +++ b/packages/toolkit/src/entities/sorted_state_adapter.ts @@ -160,12 +160,12 @@ export function createSortedStateAdapter( state, ) - if (updated.length) { - updateManyMutably(updated, state) - } if (added.length) { addManyMutably(added, state, existingIdsArray) } + if (updated.length) { + updateManyMutably(updated, state) + } } function areArraysEqual(a: readonly unknown[], b: readonly unknown[]) { 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 042ce37218..d378449886 100644 --- a/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts +++ b/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts @@ -509,6 +509,28 @@ describe('Sorted State Adapter', () => { }) }) + it('should let you add a new entity then apply changes to it', () => { + const firstChange = { author: TheHobbit.author } + const secondChange = { title: 'Zack' } + const withMany = adapter.setAll(state, [AClockworkOrange]) + + const withUpserts = adapter.upsertMany(withMany, [ + {...TheGreatGatsby}, { ...TheGreatGatsby, ...firstChange }, {...TheGreatGatsby, ...secondChange} + ]) + + expect(withUpserts).toEqual({ + ids: [AClockworkOrange.id, TheGreatGatsby.id], + entities: { + [TheGreatGatsby.id]: { + ...TheGreatGatsby, + ...firstChange, + ...secondChange, + }, + [AClockworkOrange.id]: AClockworkOrange, + }, + }) + }) + it('should let you add a new entity in the state with setOne() and keep the sorting', () => { const withMany = adapter.setAll(state, [AnimalFarm, TheHobbit]) const withOneMore = adapter.setOne(withMany, TheGreatGatsby) diff --git a/packages/toolkit/src/entities/tests/unsorted_state_adapter.test.ts b/packages/toolkit/src/entities/tests/unsorted_state_adapter.test.ts index 6d2fc8eaf3..d7b551e63b 100644 --- a/packages/toolkit/src/entities/tests/unsorted_state_adapter.test.ts +++ b/packages/toolkit/src/entities/tests/unsorted_state_adapter.test.ts @@ -355,6 +355,28 @@ describe('Unsorted State Adapter', () => { }) }) + it('should let you add a new entity then apply changes to it', () => { + const firstChange = { author: TheHobbit.author } + const secondChange = { title: 'Zack' } + const withMany = adapter.setAll(state, [TheGreatGatsby]) + + const withUpserts = adapter.upsertMany(withMany, [ + {...AClockworkOrange}, { ...AClockworkOrange, ...firstChange }, {...AClockworkOrange, ...secondChange} + ]) + + expect(withUpserts).toEqual({ + ids: [TheGreatGatsby.id, AClockworkOrange.id], + entities: { + [TheGreatGatsby.id]: TheGreatGatsby, + [AClockworkOrange.id]: { + ...AClockworkOrange, + ...firstChange, + ...secondChange, + }, + }, + }) + }) + it('should let you add a new entity in the state with setOne()', () => { const withOne = adapter.setOne(state, TheGreatGatsby) expect(withOne).toEqual({ diff --git a/packages/toolkit/src/entities/unsorted_state_adapter.ts b/packages/toolkit/src/entities/unsorted_state_adapter.ts index 54739ec9d7..c7ec7f123b 100644 --- a/packages/toolkit/src/entities/unsorted_state_adapter.ts +++ b/packages/toolkit/src/entities/unsorted_state_adapter.ts @@ -183,8 +183,8 @@ export function createUnsortedStateAdapter( state, ) - updateManyMutably(updated, state) addManyMutably(added, state) + updateManyMutably(updated, state) } return { diff --git a/packages/toolkit/src/entities/utils.ts b/packages/toolkit/src/entities/utils.ts index f9f58a6a0c..1d8c087235 100644 --- a/packages/toolkit/src/entities/utils.ts +++ b/packages/toolkit/src/entities/utils.ts @@ -52,13 +52,15 @@ export function splitAddedUpdatedEntities( const existingIds = new Set(existingIdsArray) const added: T[] = [] + const addedIds = new Set([]) const updated: Update[] = [] for (const entity of newEntities) { const id = selectIdValue(entity, selectId) - if (existingIds.has(id)) { + if (existingIds.has(id) || addedIds.has(id)) { updated.push({ id, changes: entity }) } else { + addedIds.add(id) added.push(entity) } }