Skip to content

Commit

Permalink
fix(data): immutably delete an entity (#3040)
Browse files Browse the repository at this point in the history
Closes #2553
  • Loading branch information
donohoea authored Jul 6, 2021
1 parent bf2bd1a commit a6c199f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
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
32 changes: 22 additions & 10 deletions modules/store/spec/runtime_checks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,6 @@ describe('Runtime checks:', () => {
flush();
}).not.toThrow();
}));

it('should not throw for NgRx actions', fakeAsync(() => {
const store = setupStore({ strictStateSerializability: true });

expect(() => {
store.dispatch(makeNgrxAction(invalidAction()));
flush();
}).not.toThrow();
}));
});

describe('Action Serialization:', () => {
Expand All @@ -172,6 +163,15 @@ describe('Runtime checks:', () => {
flush();
}).not.toThrow();
}));

it('should not throw for NgRx actions', fakeAsync(() => {
const store = setupStore({ strictActionSerializability: true });

expect(() => {
store.dispatch(makeNgrxAction(invalidAction()));
flush();
}).not.toThrow();
}));
});

describe('State Mutations', () => {
Expand Down Expand Up @@ -338,19 +338,31 @@ function reducerWithBugs(state: any = {}, action: any) {
invalidSerializationState: true,
invalid: new Date(),
};

case ErrorTypes.UnserializableAction: {
return {
invalidSerializationAction: true,
};
}

case '@ngrx ' + ErrorTypes.UnserializableAction: {
return {
invalidSerializationAction: true,
};
}

case ErrorTypes.MutateAction: {
action.foo = 'foo';
return {
invalidMutationAction: true,
};
}
case '@ngrx ' + ErrorTypes.MutateAction: {
action.foo = 'foo';
return {
invalidMutationAction: true,
};
}

case ErrorTypes.MutateState: {
state.invalidMutationState = true;
Expand Down

0 comments on commit a6c199f

Please sign in to comment.