Skip to content

Commit

Permalink
fix(data): remove state mutation in trackDeleteMany
Browse files Browse the repository at this point in the history
  • Loading branch information
donohoea committed Jul 5, 2021
1 parent 97643eb commit abb0589
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions modules/data/spec/reducers/entity-change-tracker-base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,22 @@ describe('EntityChangeTrackerBase', () => {
const collection = tracker.trackDeleteMany(['1234', 456], origCollection);
expect(collection).toBe(origCollection);
});

it('should not mutate changeState when called on a tracked "updated" entity', () => {
const existingEntity = getFirstExistingEntity();
const updatedEntity = toUpdate({
...existingEntity,
name: 'test update',
});
const collection = tracker.trackUpdateOne(updatedEntity, origCollection);
const change = collection.changeState[existingEntity!.id];
expect(change).toBeDefined();
expectChangeType(change, ChangeType.Updated);
Object.freeze(change);
expect(() => {
tracker.trackDeleteMany([existingEntity!.id], collection);
}).not.toThrowError();
});
});

describe('#trackUpdateOne', () => {
Expand Down
2 changes: 1 addition & 1 deletion modules/data/src/reducers/entity-change-tracker-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ export class EntityChangeTrackerBase<T> implements EntityChangeTracker<T> {
} else if (trackedChange.changeType === ChangeType.Updated) {
// Special case: switch change type from Updated to Deleted.
cloneChgStateOnce();
trackedChange.changeType = ChangeType.Deleted;
chgState[id] = { ...chgState[id], changeType: ChangeType.Deleted };
}
} else {
// Start tracking this entity
Expand Down

0 comments on commit abb0589

Please sign in to comment.