Skip to content

Commit fb642a5

Browse files
author
Adrian Riepl
committed
fix: 🐛 clear all initial values when clearing complete store
Clears complete Map of initial values when the store is getting cleared. Closes: #29
1 parent 0ee5eff commit fb642a5

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

projects/ngneat/forms-manager/src/lib/forms-manager.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,11 @@ describe('FormsManager', () => {
12731273
}),
12741274
});
12751275

1276+
const addressForm = new FormGroup({
1277+
zip: new FormControl('12345'),
1278+
place: new FormControl('Schenectady'),
1279+
});
1280+
12761281
it('should get and set initial Value for a control', () => {
12771282
formsManager.setInitialValue('config', 'initial');
12781283

@@ -1306,6 +1311,26 @@ describe('FormsManager', () => {
13061311
it('should get undefined if no initial Value set', () => {
13071312
expect(formsManager.getInitialValue('other')).toBeUndefined();
13081313
});
1314+
1315+
it('should clear initial Value for a group of control', () => {
1316+
formsManager.upsert('user', userForm, { withInitialValue: true });
1317+
formsManager.upsert('address', addressForm, { withInitialValue: true });
1318+
1319+
formsManager.clear('user');
1320+
1321+
expect(formsManager.getInitialValue('user')).toBeUndefined();
1322+
expect(formsManager.getInitialValue('address')).toBeDefined();
1323+
});
1324+
1325+
it('should clear all initial Values when clearing full store', () => {
1326+
formsManager.upsert('user', userForm, { withInitialValue: true });
1327+
formsManager.upsert('address', addressForm, { withInitialValue: true });
1328+
1329+
formsManager.clear();
1330+
1331+
expect(formsManager.getInitialValue('user')).toBeUndefined();
1332+
expect(formsManager.getInitialValue('address')).toBeUndefined();
1333+
});
13091334
});
13101335

13111336
describe('Debounce', () => {

projects/ngneat/forms-manager/src/lib/forms-manager.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,10 @@ export class NgFormsManager<FormsState = any> {
638638
return value;
639639
}
640640

641-
private removeInitialValue(name: FormKeys<FormsState>) {
642-
coerceArray(name).forEach(name => this.initialValues$$.delete(name));
641+
private removeInitialValue(name?: FormKeys<FormsState>) {
642+
name
643+
? coerceArray(name).forEach(name => this.initialValues$$.delete(name))
644+
: this.initialValues$$.clear();
643645
}
644646

645647
private markDescendantsAsDirty(

0 commit comments

Comments
 (0)