Skip to content

Commit

Permalink
fix(data): make undoMany remove tracking changes in changeState (#2346)…
Browse files Browse the repository at this point in the history
… (#2352)

Closes #2346
  • Loading branch information
AdditionAddict authored Feb 8, 2020
1 parent f09df95 commit 637b2c7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
42 changes: 42 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 @@ -649,6 +649,22 @@ describe('EntityChangeTrackerBase', () => {
});

describe('#undoOne', () => {
it('should clear one tracked change', () => {
let { collection, deletedEntity } = createTestTrackedEntities();

expect(Object.keys(collection.changeState).length).toBe(
3,
'tracking 3 entities'
);

collection = tracker.undoOne(deletedEntity as Hero, collection);

expect(Object.keys(collection.changeState).length).toBe(
2,
'tracking 2 entities'
);
});

it('should restore the collection to the pre-change state for the given entity', () => {
// tslint:disable-next-line:prefer-const
let {
Expand Down Expand Up @@ -698,6 +714,32 @@ describe('EntityChangeTrackerBase', () => {
});

describe('#undoMany', () => {
it('should clear many tracked changes', () => {
// tslint:disable-next-line:prefer-const
let {
collection,
addedEntity,
deletedEntity,
preUpdatedEntity,
updatedEntity,
} = createTestTrackedEntities();

expect(Object.keys(collection.changeState).length).toBe(
3,
'tracking 3 entities'
);

collection = tracker.undoMany(
[addedEntity, deletedEntity, updatedEntity],
collection
);

expect(Object.keys(collection.changeState).length).toBe(
0,
'tracking 2 entities'
);
});

it('should restore the collection to the pre-change state for the given entities', () => {
// tslint:disable-next-line:prefer-const
let {
Expand Down
3 changes: 2 additions & 1 deletion modules/data/src/reducers/entity-change-tracker-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ export class EntityChangeTrackerBase<T> implements EntityChangeTracker<T> {
didMutate = true;
}
delete chgState[id]; // clear tracking of this entity
acc.changeState = chgState;
switch (change.changeType) {
case ChangeType.Added:
acc.remove.push(id);
Expand Down Expand Up @@ -714,7 +715,7 @@ export class EntityChangeTrackerBase<T> implements EntityChangeTracker<T> {

collection = this.adapter.removeMany(remove as string[], collection);
collection = this.adapter.upsertMany(upsert, collection);
return didMutate ? collection : { ...collection, changeState };
return didMutate ? { ...collection, changeState } : collection;
}

/**
Expand Down

0 comments on commit 637b2c7

Please sign in to comment.