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
9 changes: 7 additions & 2 deletions packages/toolkit/src/entities/sorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
newEntities = ensureEntitiesArray(newEntities)

const existingKeys = new Set<Id>(existingIds ?? getCurrent(state.ids))

const addedKeys = new Set<Id>();
const models = newEntities.filter(
(model) => !existingKeys.has(selectIdValue(model, selectId)),
(model) => {
const modelId = selectIdValue(model, selectId);
const notAdded = !addedKeys.has(modelId);
if (notAdded) addedKeys.add(modelId);
return !existingKeys.has(modelId) && notAdded;
}
)

if (models.length !== 0) {
Expand Down
19 changes: 19 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 @@ -588,6 +588,25 @@ describe('Sorted State Adapter', () => {
})
})

it('should work consistent with Unsorted State Adapter adding duplicate ids', () => {
const unsortedAdaptor = createEntityAdapter({
selectId: (book: BookModel) => book.id
});

const firstEntry = {id: AClockworkOrange.id, author: TheHobbit.author }
const secondEntry = {id: AClockworkOrange.id, title: 'Zack' }
const withNothingSorted = adapter.setAll(state, []);
const withNothingUnsorted = unsortedAdaptor.setAll(state, []);
const withOneSorted = adapter.addMany(withNothingSorted, [
{ ...AClockworkOrange, ...firstEntry }, {...AClockworkOrange, ...secondEntry}
])
const withOneUnsorted = adapter.addMany(withNothingUnsorted, [
{ ...AClockworkOrange, ...firstEntry }, {...AClockworkOrange, ...secondEntry}
])

expect(withOneSorted).toEqual(withOneUnsorted);
})

it('should let you set many entities in the state when passing in a dictionary', () => {
const changeWithoutAuthor = { id: TheHobbit.id, title: 'Silmarillion' }
const withMany = adapter.setAll(state, [TheHobbit])
Expand Down
20 changes: 20 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 @@ -89,6 +89,26 @@ describe('Unsorted State Adapter', () => {
})
})

it('should let you add the only first occurrence for duplicate ids', () => {
const firstEntry = {id: AClockworkOrange.id, author: TheHobbit.author }
const secondEntry = {id: AClockworkOrange.id, title: 'Zack' }
const withOne = adapter.setAll(state, [TheGreatGatsby])
const withMany = adapter.addMany(withOne, [
{ ...AClockworkOrange, ...firstEntry }, {...AClockworkOrange, ...secondEntry}
])

expect(withMany).toEqual({
ids: [TheGreatGatsby.id, AClockworkOrange.id],
entities: {
[TheGreatGatsby.id]: TheGreatGatsby,
[AClockworkOrange.id]: {
...AClockworkOrange,
...firstEntry,
},
},
})
})

it('should remove existing and add new ones on setAll', () => {
const withOneEntity = adapter.addOne(state, TheGreatGatsby)

Expand Down
Loading