Skip to content

Commit

Permalink
feat(forms): add tests for markAllAsTouched()
Browse files Browse the repository at this point in the history
Add tests to validate the behavior
  • Loading branch information
alsami committed Oct 29, 2018
1 parent 2953b99 commit 59a17db
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions packages/forms/test/form_group_spec.ts
Expand Up @@ -86,6 +86,56 @@ import {of } from 'rxjs';

});

describe('markAllAsTouched', () => {
it('should mark all descendants as touched', () => {
const formGroup: FormGroup = new FormGroup({
'c1': new FormControl('v1'),
'group': new FormGroup({'c2': new FormControl('v2'), 'c3': new FormControl('v3')}),
'array': new FormArray([new FormControl('v4'), new FormControl('v5'), new FormGroup({'c4': new FormControl('v4')})])
});

formGroup.markAllAsTouched();

expect(formGroup.touched).toBe(true);

const control1 = formGroup.get('c1') as FormControl;

expect(control1.touched).toBe(true);

const innerGroup = formGroup.get('group') as FormGroup;

expect(innerGroup.touched).toBe(true);

const innerGroupFirstChildCtrl = innerGroup.get('c2') as FormControl;

expect(innerGroupFirstChildCtrl.touched).toBe(true);

const innerGroupSecondChildCtrl = innerGroup.get('c3') as FormControl;

expect(innerGroupSecondChildCtrl.touched).toBe(true);

const array = formGroup.get('array') as FormArray;

expect(array.touched).toBe(true);

const arrayFirstChildCtrl = array.controls[0] as FormControl;

expect(arrayFirstChildCtrl.touched).toBe(true);

const arraySecondChildCtrl = array.controls[1] as FormControl;

expect(arraySecondChildCtrl.touched).toBe(true);

const arrayFirstChildGroup = array.controls[2] as FormGroup;

expect(arrayFirstChildGroup.touched).toBe(true);

const arrayFirstChildGroupFirstChildCtrl = arrayFirstChildGroup.get('c4') as FormControl;

expect(arrayFirstChildGroupFirstChildCtrl.touched).toBe(true);
})
});

describe('adding and removing controls', () => {
it('should update value and validity when control is added', () => {
const g = new FormGroup({'one': new FormControl('1')});
Expand Down

0 comments on commit 59a17db

Please sign in to comment.