Skip to content

Commit

Permalink
fix rollbackInvalid removing valid changes (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
vstefanovic97 authored Aug 5, 2022
1 parent 6dd774f commit f3f256f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ export class BufferedChangeset implements IChangeset {
* @return {Changeset}
*/
rollbackInvalid(key: string | void): this {
let errorKeys = keys(this[ERRORS]);
let errorKeys = this.errors.map(({ key }) => key);

if (key) {
this._notifyVirtualProperties([key]);
Expand Down
22 changes: 22 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2617,6 +2617,28 @@ describe('Unit | Utility | changeset', () => {
expect(dummyChangeset.isValid).toBeTruthy();
});

it('#rollbackInvalid will not remove deep changes that are valid after being invalid', () => {
let dummyChangeset = Changeset(dummyModel, lookupValidator(dummyValidations));
dummyChangeset.set('name', 'abcd');
dummyChangeset.set('org.usa.ny', '');

dummyChangeset.validate('name');
dummyChangeset.validate('org.usa.ny');

dummyChangeset.set('org.usa.ny', 'ny');
dummyChangeset.validate('org.usa.ny');

const expectedChanges = [
{ key: 'name', value: 'abcd' },
{ key: 'org.usa.ny', value: 'ny' }
];

dummyChangeset.rollbackInvalid();

expect(dummyChangeset.changes).toEqual(expectedChanges);
expect(dummyChangeset.isValid).toBeTruthy();
});

it('#rollbackInvalid works for keys not on changeset', () => {
let dummyChangeset = Changeset(dummyModel, lookupValidator(dummyValidations));
let expectedChanges = [
Expand Down

0 comments on commit f3f256f

Please sign in to comment.