Skip to content

Commit

Permalink
fix(entities): correct behavior of upsertEntities with duplicate ids (
Browse files Browse the repository at this point in the history
  • Loading branch information
Char2sGu authored Apr 16, 2024
1 parent 9dbc51d commit 9411e87
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/entities/src/lib/update.mutation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,19 @@ describe('update', () => {

expect(ids).toBe(sameIds);
});

it('should be able to handle duplicate ids', () => {
const store = createEntitiesStore();
store.update(
upsertEntities([
{ id: 1, completed: true },
{ id: 1, completed: false },
]),
);
const { ids, entities } = store.getValue();
expect(ids).toEqual([1]);
expect(entities).toEqual({ 1: { id: 1, completed: false } });
});
});

describe('UpdateEntitiesIds', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/entities/src/lib/update.mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ export function upsertEntities<

for (const entity of entitiesArray) {
const id: getIdType<S, Ref> = entity[idKey];
// if entity exists, merge update, else add
if (hasEntity(id, options)(state)) {
if (asObject[id]) {
asObject[id] = { ...asObject[id], ...entity };
} else if (hasEntity(id, options)(state)) {
asObject[id] = { ...state[entitiesKey][id], ...entity };
updatedEntitiesId.push(id);
} else {
Expand Down

0 comments on commit 9411e87

Please sign in to comment.