Skip to content

Commit

Permalink
test(form-array): write spec for remove helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioArnt committed Aug 14, 2020
1 parent af39138 commit 419c8c2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
43 changes: 43 additions & 0 deletions projects/ngneat/reactive-forms/src/lib/formArray.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { of, Subject } from 'rxjs';
import { FormArray } from './formArray';
import { FormControl } from './formControl';
import { FormGroup } from '@angular/forms';

const errorFn = group => {
return { isInvalid: true };
Expand Down Expand Up @@ -212,4 +213,46 @@ describe('FormArray', () => {
control.push(new FormControl('Phone'));
expect(spy).toHaveBeenCalledWith(null);
});
it('should remove', () => {
const control = createArray();
control.clear();
control.push(new FormControl('Name'));
control.push(new FormControl('Name'));
control.push(new FormControl('Phone'));
control.push(new FormControl('Name'));
control.push(new FormControl('Address'));
control.remove('Name');
expect(control.getRawValue()).toEqual(['Phone', 'Address']);
});
it('should removeWhen', () => {
const control = createArray();
control.clear();
control.push(new FormControl('FirstName'));
control.push(new FormControl('LastName'));
control.push(new FormControl('Phone'));
control.push(new FormControl('MiddleName'));
control.push(new FormControl('StreetNumber'));
control.push(new FormControl('StreetName'));
control.push(new FormControl('ZipCode'));
control.removeWhen(elt => elt.value.match(/Name$/) != null);
expect(control.getRawValue()).toEqual(['Phone', 'StreetNumber', 'ZipCode']);
});
it('should removeWhen', () => {
const control = createArray();
control.clear();
control.push(new FormGroup({ type: new FormControl('Jedi'), name: new FormControl('Luke') }));
control.push(new FormGroup({ type: new FormControl('Sith'), name: new FormControl('Vader') }));
control.push(new FormGroup({ type: new FormControl('Jedi'), name: new FormControl('Yoda') }));
control.push(new FormGroup({ type: new FormControl('Jedi'), name: new FormControl('Obi-Wan') }));
control.push(new FormGroup({ type: new FormControl('Sith'), name: new FormControl('Doku') }));
control.push(new FormGroup({ type: new FormControl('Jedi'), name: new FormControl('Windu') }));
control.push(new FormGroup({ type: new FormControl('Sith'), name: new FormControl('Palpatine') }));
control.removeWhen(elt => elt.get('type').value === 'Sith');
expect(control.getRawValue()).toEqual([
{ type: 'Jedi', name: 'Luke' },
{ type: 'Jedi', name: 'Yoda' },
{ type: 'Jedi', name: 'Obi-Wan' },
{ type: 'Jedi', name: 'Windu' }
]);
});
});
8 changes: 8 additions & 0 deletions projects/ngneat/reactive-forms/src/lib/formArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,12 @@ export class FormArray<T = any, E extends object = any> extends NgFormArray {
setDisable(disable = true, opts?: ControlEventOptions) {
disableControl(this, disable, opts);
}

remove(value: T) {
throw Error('NotImplemented');
}

removeWhen(predicate: (element: AbstractControl<T>) => boolean) {
throw Error('NotImplemented');
}
}

0 comments on commit 419c8c2

Please sign in to comment.