Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/toolkit/src/entities/sorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
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[]) {
Expand Down
22 changes: 22 additions & 0 deletions packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions packages/toolkit/src/entities/tests/unsorted_state_adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/entities/unsorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ export function createUnsortedStateAdapter<T, Id extends EntityId>(
state,
)

updateManyMutably(updated, state)
addManyMutably(added, state)
updateManyMutably(updated, state)
}

return {
Expand Down
4 changes: 3 additions & 1 deletion packages/toolkit/src/entities/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ export function splitAddedUpdatedEntities<T, Id extends EntityId>(
const existingIds = new Set<Id>(existingIdsArray)

const added: T[] = []
const addedIds = new Set<Id>([])
const updated: Update<T, Id>[] = []

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)
}
}
Expand Down