From f3f256fa1055ce06f77675b6e5dec962bdc561c7 Mon Sep 17 00:00:00 2001 From: Vuk <60734210+vstefanovic97@users.noreply.github.com> Date: Fri, 5 Aug 2022 14:15:49 +0200 Subject: [PATCH] fix rollbackInvalid removing valid changes (#176) --- src/index.ts | 2 +- test/index.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 218ad4f..5352e2c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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]); diff --git a/test/index.test.ts b/test/index.test.ts index 1d9044b..158aa97 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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 = [