2016-08-13 20:26:08 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-08-13 20:26:08 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-19 15:10:53 -04:00
|
|
|
import {async, fakeAsync, tick} from '@angular/core/testing';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {AsyncTestCompleter, beforeEach, describe, inject, it} from '@angular/core/testing/src/testing_internal';
|
2017-07-25 18:01:04 -04:00
|
|
|
import {AbstractControl, FormArray, FormControl, FormGroup, ValidationErrors, Validators} from '@angular/forms';
|
2020-04-06 18:44:00 -04:00
|
|
|
import {of} from 'rxjs';
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-07-12 19:35:09 -04:00
|
|
|
import {asyncValidator, asyncValidatorReturningObservable, currentStateOf, simpleAsyncValidator} from './util';
|
|
|
|
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2017-12-17 18:10:54 -05:00
|
|
|
(function() {
|
2020-04-06 18:44:00 -04:00
|
|
|
function simpleValidator(c: AbstractControl): ValidationErrors|null {
|
|
|
|
return c.get('one')!.value === 'correct' ? null : {'broken': true};
|
|
|
|
}
|
|
|
|
|
|
|
|
function otherObservableValidator() {
|
|
|
|
return of({'other': true});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('FormGroup', () => {
|
|
|
|
describe('value', () => {
|
|
|
|
it('should be the reduced value of the child controls', () => {
|
|
|
|
const g = new FormGroup({'one': new FormControl('111'), 'two': new FormControl('222')});
|
|
|
|
expect(g.value).toEqual({'one': '111', 'two': '222'});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should be empty when there are no child controls', () => {
|
|
|
|
const g = new FormGroup({});
|
|
|
|
expect(g.value).toEqual({});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should support nested groups', () => {
|
|
|
|
const g = new FormGroup({
|
|
|
|
'one': new FormControl('111'),
|
|
|
|
'nested': new FormGroup({'two': new FormControl('222')})
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.value).toEqual({'one': '111', 'nested': {'two': '222'}});
|
|
|
|
|
|
|
|
(<FormControl>(g.get('nested.two'))).setValue('333');
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.value).toEqual({'one': '111', 'nested': {'two': '333'}});
|
|
|
|
});
|
|
|
|
});
|
2016-11-18 06:44:05 -05:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('getRawValue', () => {
|
|
|
|
let fg: FormGroup;
|
2016-11-18 06:44:05 -05:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should work with nested form groups/arrays', () => {
|
|
|
|
fg = 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')])
|
2016-11-18 06:44:05 -05:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
fg.get('group')!.get('c3')!.disable();
|
|
|
|
(fg.get('array') as FormArray).at(1).disable();
|
2016-11-18 06:44:05 -05:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(fg.getRawValue())
|
|
|
|
.toEqual({'c1': 'v1', 'group': {'c2': 'v2', 'c3': 'v3'}, 'array': ['v4', 'v5']});
|
2016-11-18 06:44:05 -05:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
2016-11-18 06:44:05 -05:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
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')})
|
|
|
|
])
|
|
|
|
});
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(formGroup.touched).toBe(false);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
const control1 = formGroup.get('c1') as FormControl;
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(control1.touched).toBe(false);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
const innerGroup = formGroup.get('group') as FormGroup;
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(innerGroup.touched).toBe(false);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
const innerGroupFirstChildCtrl = innerGroup.get('c2') as FormControl;
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(innerGroupFirstChildCtrl.touched).toBe(false);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
formGroup.markAllAsTouched();
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(formGroup.touched).toBe(true);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(control1.touched).toBe(true);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(innerGroup.touched).toBe(true);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(innerGroupFirstChildCtrl.touched).toBe(true);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
const innerGroupSecondChildCtrl = innerGroup.get('c3') as FormControl;
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(innerGroupSecondChildCtrl.touched).toBe(true);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
const array = formGroup.get('array') as FormArray;
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(array.touched).toBe(true);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
const arrayFirstChildCtrl = array.at(0) as FormControl;
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(arrayFirstChildCtrl.touched).toBe(true);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
const arraySecondChildCtrl = array.at(1) as FormControl;
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(arraySecondChildCtrl.touched).toBe(true);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
const arrayFirstChildGroup = array.at(2) as FormGroup;
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(arrayFirstChildGroup.touched).toBe(true);
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
const arrayFirstChildGroupFirstChildCtrl = arrayFirstChildGroup.get('c4') as FormControl;
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(arrayFirstChildGroupFirstChildCtrl.touched).toBe(true);
|
2018-10-29 04:36:17 -04:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
2018-10-29 04:36:17 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('adding and removing controls', () => {
|
|
|
|
it('should update value and validity when control is added', () => {
|
|
|
|
const g = new FormGroup({'one': new FormControl('1')});
|
|
|
|
expect(g.value).toEqual({'one': '1'});
|
|
|
|
expect(g.valid).toBe(true);
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.addControl('two', new FormControl('2', Validators.minLength(10)));
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.value).toEqual({'one': '1', 'two': '2'});
|
|
|
|
expect(g.valid).toBe(false);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should update value and validity when control is removed', () => {
|
|
|
|
const g = new FormGroup(
|
|
|
|
{'one': new FormControl('1'), 'two': new FormControl('2', Validators.minLength(10))});
|
|
|
|
expect(g.value).toEqual({'one': '1', 'two': '2'});
|
|
|
|
expect(g.valid).toBe(false);
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.removeControl('two');
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.value).toEqual({'one': '1'});
|
|
|
|
expect(g.valid).toBe(true);
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('dirty', () => {
|
|
|
|
let c: FormControl, g: FormGroup;
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
c = new FormControl('value');
|
|
|
|
g = new FormGroup({'one': c});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should be false after creating a control', () => {
|
|
|
|
expect(g.dirty).toEqual(false);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should be true after changing the value of the control', () => {
|
|
|
|
c.markAsDirty();
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.dirty).toEqual(true);
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('touched', () => {
|
|
|
|
let c: FormControl, g: FormGroup;
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
c = new FormControl('value');
|
|
|
|
g = new FormGroup({'one': c});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should be false after creating a control', () => {
|
|
|
|
expect(g.touched).toEqual(false);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should be true after control is marked as touched', () => {
|
|
|
|
c.markAsTouched();
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.touched).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setValue', () => {
|
|
|
|
let c: FormControl, c2: FormControl, g: FormGroup;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
c = new FormControl('');
|
|
|
|
c2 = new FormControl('');
|
|
|
|
g = new FormGroup({'one': c, 'two': c2});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set its own value', () => {
|
|
|
|
g.setValue({'one': 'one', 'two': 'two'});
|
|
|
|
expect(g.value).toEqual({'one': 'one', 'two': 'two'});
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set child values', () => {
|
|
|
|
g.setValue({'one': 'one', 'two': 'two'});
|
|
|
|
expect(c.value).toEqual('one');
|
|
|
|
expect(c2.value).toEqual('two');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set child control values if disabled', () => {
|
|
|
|
c2.disable();
|
|
|
|
g.setValue({'one': 'one', 'two': 'two'});
|
|
|
|
expect(c2.value).toEqual('two');
|
|
|
|
expect(g.value).toEqual({'one': 'one'});
|
|
|
|
expect(g.getRawValue()).toEqual({'one': 'one', 'two': 'two'});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set group value if group is disabled', () => {
|
|
|
|
g.disable();
|
|
|
|
g.setValue({'one': 'one', 'two': 'two'});
|
|
|
|
expect(c.value).toEqual('one');
|
|
|
|
expect(c2.value).toEqual('two');
|
|
|
|
|
|
|
|
expect(g.value).toEqual({'one': 'one', 'two': 'two'});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set parent values', () => {
|
|
|
|
const form = new FormGroup({'parent': g});
|
|
|
|
g.setValue({'one': 'one', 'two': 'two'});
|
|
|
|
expect(form.value).toEqual({'parent': {'one': 'one', 'two': 'two'}});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not update the parent when explicitly specified', () => {
|
|
|
|
const form = new FormGroup({'parent': g});
|
|
|
|
g.setValue({'one': 'one', 'two': 'two'}, {onlySelf: true});
|
|
|
|
|
|
|
|
expect(form.value).toEqual({parent: {'one': '', 'two': ''}});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if fields are missing from supplied value (subset)', () => {
|
|
|
|
expect(() => g.setValue({
|
|
|
|
'one': 'one'
|
|
|
|
})).toThrowError(new RegExp(`Must supply a value for form control with name: 'two'`));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if a value is provided for a missing control (superset)', () => {
|
|
|
|
expect(() => g.setValue({'one': 'one', 'two': 'two', 'three': 'three'}))
|
|
|
|
.toThrowError(new RegExp(`Cannot find form control with name: three`));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if a value is not provided for a disabled control', () => {
|
|
|
|
c2.disable();
|
|
|
|
expect(() => g.setValue({
|
|
|
|
'one': 'one'
|
|
|
|
})).toThrowError(new RegExp(`Must supply a value for form control with name: 'two'`));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if no controls are set yet', () => {
|
|
|
|
const empty = new FormGroup({});
|
|
|
|
expect(() => empty.setValue({
|
|
|
|
'one': 'one'
|
|
|
|
})).toThrowError(new RegExp(`no form controls registered with this group`));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setValue() events', () => {
|
|
|
|
let form: FormGroup;
|
|
|
|
let logger: any[];
|
2016-08-13 20:26:08 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-04-06 18:44:00 -04:00
|
|
|
form = new FormGroup({'parent': g});
|
|
|
|
logger = [];
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should emit one valueChange event per control', () => {
|
|
|
|
form.valueChanges.subscribe(() => logger.push('form'));
|
|
|
|
g.valueChanges.subscribe(() => logger.push('group'));
|
|
|
|
c.valueChanges.subscribe(() => logger.push('control1'));
|
|
|
|
c2.valueChanges.subscribe(() => logger.push('control2'));
|
2016-08-13 20:26:08 -04:00
|
|
|
|
|
|
|
g.setValue({'one': 'one', 'two': 'two'});
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(logger).toEqual(['control1', 'control2', 'group', 'form']);
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not fire an event when explicitly specified', fakeAsync(() => {
|
|
|
|
form.valueChanges.subscribe((value) => {
|
|
|
|
throw 'Should not happen';
|
|
|
|
});
|
|
|
|
g.valueChanges.subscribe((value) => {
|
|
|
|
throw 'Should not happen';
|
|
|
|
});
|
|
|
|
c.valueChanges.subscribe((value) => {
|
|
|
|
throw 'Should not happen';
|
|
|
|
});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.setValue({'one': 'one', 'two': 'two'}, {emitEvent: false});
|
|
|
|
tick();
|
|
|
|
}));
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should emit one statusChange event per control', () => {
|
|
|
|
form.statusChanges.subscribe(() => logger.push('form'));
|
|
|
|
g.statusChanges.subscribe(() => logger.push('group'));
|
|
|
|
c.statusChanges.subscribe(() => logger.push('control1'));
|
|
|
|
c2.statusChanges.subscribe(() => logger.push('control2'));
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2016-08-13 20:26:08 -04:00
|
|
|
g.setValue({'one': 'one', 'two': 'two'});
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(logger).toEqual(['control1', 'control2', 'group', 'form']);
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('patchValue', () => {
|
|
|
|
let c: FormControl, c2: FormControl, g: FormGroup;
|
2016-10-18 01:51:13 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
c = new FormControl('');
|
|
|
|
c2 = new FormControl('');
|
|
|
|
g = new FormGroup({'one': c, 'two': c2});
|
|
|
|
});
|
2016-10-18 01:51:13 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set its own value', () => {
|
|
|
|
g.patchValue({'one': 'one', 'two': 'two'});
|
|
|
|
expect(g.value).toEqual({'one': 'one', 'two': 'two'});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set child values', () => {
|
|
|
|
g.patchValue({'one': 'one', 'two': 'two'});
|
|
|
|
expect(c.value).toEqual('one');
|
|
|
|
expect(c2.value).toEqual('two');
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should patch disabled control values', () => {
|
|
|
|
c2.disable();
|
|
|
|
g.patchValue({'one': 'one', 'two': 'two'});
|
|
|
|
expect(c2.value).toEqual('two');
|
|
|
|
expect(g.value).toEqual({'one': 'one'});
|
|
|
|
expect(g.getRawValue()).toEqual({'one': 'one', 'two': 'two'});
|
|
|
|
});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should patch disabled control groups', () => {
|
|
|
|
g.disable();
|
|
|
|
g.patchValue({'one': 'one', 'two': 'two'});
|
|
|
|
expect(c.value).toEqual('one');
|
|
|
|
expect(c2.value).toEqual('two');
|
|
|
|
expect(g.value).toEqual({'one': 'one', 'two': 'two'});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set parent values', () => {
|
|
|
|
const form = new FormGroup({'parent': g});
|
|
|
|
g.patchValue({'one': 'one', 'two': 'two'});
|
|
|
|
expect(form.value).toEqual({'parent': {'one': 'one', 'two': 'two'}});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not update the parent when explicitly specified', () => {
|
|
|
|
const form = new FormGroup({'parent': g});
|
|
|
|
g.patchValue({'one': 'one', 'two': 'two'}, {onlySelf: true});
|
|
|
|
|
|
|
|
expect(form.value).toEqual({parent: {'one': '', 'two': ''}});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should ignore fields that are missing from supplied value (subset)', () => {
|
|
|
|
g.patchValue({'one': 'one'});
|
|
|
|
expect(g.value).toEqual({'one': 'one', 'two': ''});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not ignore fields that are null', () => {
|
|
|
|
g.patchValue({'one': null});
|
|
|
|
expect(g.value).toEqual({'one': null, 'two': ''});
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should ignore any value provided for a missing control (superset)', () => {
|
|
|
|
g.patchValue({'three': 'three'});
|
|
|
|
expect(g.value).toEqual({'one': '', 'two': ''});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('patchValue() events', () => {
|
|
|
|
let form: FormGroup;
|
|
|
|
let logger: any[];
|
2016-08-13 20:26:08 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-04-06 18:44:00 -04:00
|
|
|
form = new FormGroup({'parent': g});
|
|
|
|
logger = [];
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should emit one valueChange event per control', () => {
|
|
|
|
form.valueChanges.subscribe(() => logger.push('form'));
|
|
|
|
g.valueChanges.subscribe(() => logger.push('group'));
|
|
|
|
c.valueChanges.subscribe(() => logger.push('control1'));
|
|
|
|
c2.valueChanges.subscribe(() => logger.push('control2'));
|
2016-08-13 20:26:08 -04:00
|
|
|
|
|
|
|
g.patchValue({'one': 'one', 'two': 'two'});
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(logger).toEqual(['control1', 'control2', 'group', 'form']);
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not emit valueChange events for skipped controls', () => {
|
|
|
|
form.valueChanges.subscribe(() => logger.push('form'));
|
|
|
|
g.valueChanges.subscribe(() => logger.push('group'));
|
|
|
|
c.valueChanges.subscribe(() => logger.push('control1'));
|
|
|
|
c2.valueChanges.subscribe(() => logger.push('control2'));
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.patchValue({'one': 'one'});
|
|
|
|
expect(logger).toEqual(['control1', 'group', 'form']);
|
2016-08-24 19:58:43 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not fire an event when explicitly specified', fakeAsync(() => {
|
|
|
|
form.valueChanges.subscribe((value) => {
|
|
|
|
throw 'Should not happen';
|
|
|
|
});
|
|
|
|
g.valueChanges.subscribe((value) => {
|
|
|
|
throw 'Should not happen';
|
|
|
|
});
|
|
|
|
c.valueChanges.subscribe((value) => {
|
|
|
|
throw 'Should not happen';
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.patchValue({'one': 'one', 'two': 'two'}, {emitEvent: false});
|
|
|
|
tick();
|
|
|
|
}));
|
2016-10-18 01:51:13 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should emit one statusChange event per control', () => {
|
|
|
|
form.statusChanges.subscribe(() => logger.push('form'));
|
|
|
|
g.statusChanges.subscribe(() => logger.push('group'));
|
|
|
|
c.statusChanges.subscribe(() => logger.push('control1'));
|
|
|
|
c2.statusChanges.subscribe(() => logger.push('control2'));
|
2016-10-18 01:51:13 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.patchValue({'one': 'one', 'two': 'two'});
|
|
|
|
expect(logger).toEqual(['control1', 'control2', 'group', 'form']);
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('reset()', () => {
|
|
|
|
let c: FormControl, c2: FormControl, g: FormGroup;
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
c = new FormControl('initial value');
|
|
|
|
c2 = new FormControl('');
|
|
|
|
g = new FormGroup({'one': c, 'two': c2});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set its own value if value passed', () => {
|
|
|
|
g.setValue({'one': 'new value', 'two': 'new value'});
|
|
|
|
|
|
|
|
g.reset({'one': 'initial value', 'two': ''});
|
|
|
|
expect(g.value).toEqual({'one': 'initial value', 'two': ''});
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set its own value if boxed value passed', () => {
|
|
|
|
g.setValue({'one': 'new value', 'two': 'new value'});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset({'one': {value: 'initial value', disabled: false}, 'two': ''});
|
|
|
|
expect(g.value).toEqual({'one': 'initial value', 'two': ''});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should clear its own value if no value passed', () => {
|
|
|
|
g.setValue({'one': 'new value', 'two': 'new value'});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(g.value).toEqual({'one': null, 'two': null});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set the value of each of its child controls if value passed', () => {
|
|
|
|
g.setValue({'one': 'new value', 'two': 'new value'});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset({'one': 'initial value', 'two': ''});
|
|
|
|
expect(c.value).toBe('initial value');
|
|
|
|
expect(c2.value).toBe('');
|
|
|
|
});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should clear the value of each of its child controls if no value passed', () => {
|
|
|
|
g.setValue({'one': 'new value', 'two': 'new value'});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(c.value).toBe(null);
|
|
|
|
expect(c2.value).toBe(null);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set the value of its parent if value passed', () => {
|
|
|
|
const form = new FormGroup({'g': g});
|
|
|
|
g.setValue({'one': 'new value', 'two': 'new value'});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset({'one': 'initial value', 'two': ''});
|
|
|
|
expect(form.value).toEqual({'g': {'one': 'initial value', 'two': ''}});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should clear the value of its parent if no value passed', () => {
|
|
|
|
const form = new FormGroup({'g': g});
|
|
|
|
g.setValue({'one': 'new value', 'two': 'new value'});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(form.value).toEqual({'g': {'one': null, 'two': null}});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not update the parent when explicitly specified', () => {
|
|
|
|
const form = new FormGroup({'g': g});
|
|
|
|
g.reset({'one': 'new value', 'two': 'new value'}, {onlySelf: true});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(form.value).toEqual({g: {'one': 'initial value', 'two': ''}});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should mark itself as pristine', () => {
|
|
|
|
g.markAsDirty();
|
|
|
|
expect(g.pristine).toBe(false);
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(g.pristine).toBe(true);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should mark all child controls as pristine', () => {
|
|
|
|
c.markAsDirty();
|
|
|
|
c2.markAsDirty();
|
|
|
|
expect(c.pristine).toBe(false);
|
|
|
|
expect(c2.pristine).toBe(false);
|
2016-10-18 01:51:13 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(c.pristine).toBe(true);
|
|
|
|
expect(c2.pristine).toBe(true);
|
|
|
|
});
|
2016-10-18 01:51:13 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should mark the parent as pristine if all siblings pristine', () => {
|
|
|
|
const c3 = new FormControl('');
|
|
|
|
const form = new FormGroup({'g': g, 'c3': c3});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.markAsDirty();
|
|
|
|
expect(form.pristine).toBe(false);
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(form.pristine).toBe(true);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not mark the parent pristine if any dirty siblings', () => {
|
|
|
|
const c3 = new FormControl('');
|
|
|
|
const form = new FormGroup({'g': g, 'c3': c3});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.markAsDirty();
|
|
|
|
c3.markAsDirty();
|
|
|
|
expect(form.pristine).toBe(false);
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(form.pristine).toBe(false);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should mark itself as untouched', () => {
|
|
|
|
g.markAsTouched();
|
|
|
|
expect(g.untouched).toBe(false);
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(g.untouched).toBe(true);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should mark all child controls as untouched', () => {
|
|
|
|
c.markAsTouched();
|
|
|
|
c2.markAsTouched();
|
|
|
|
expect(c.untouched).toBe(false);
|
|
|
|
expect(c2.untouched).toBe(false);
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(c.untouched).toBe(true);
|
|
|
|
expect(c2.untouched).toBe(true);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should mark the parent untouched if all siblings untouched', () => {
|
|
|
|
const c3 = new FormControl('');
|
|
|
|
const form = new FormGroup({'g': g, 'c3': c3});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.markAsTouched();
|
|
|
|
expect(form.untouched).toBe(false);
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(form.untouched).toBe(true);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not mark the parent untouched if any touched siblings', () => {
|
|
|
|
const c3 = new FormControl('');
|
|
|
|
const form = new FormGroup({'g': g, 'c3': c3});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.markAsTouched();
|
|
|
|
c3.markAsTouched();
|
|
|
|
expect(form.untouched).toBe(false);
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(form.untouched).toBe(false);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should retain previous disabled state', () => {
|
|
|
|
g.disable();
|
|
|
|
g.reset();
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.disabled).toBe(true);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set child disabled state if boxed value passed', () => {
|
|
|
|
g.disable();
|
|
|
|
g.reset({'one': {value: '', disabled: false}, 'two': ''});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(c.disabled).toBe(false);
|
|
|
|
expect(g.disabled).toBe(false);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('reset() events', () => {
|
|
|
|
let form: FormGroup, c3: FormControl, logger: any[];
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
c3 = new FormControl('');
|
|
|
|
form = new FormGroup({'g': g, 'c3': c3});
|
|
|
|
logger = [];
|
2016-08-24 19:58:43 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should emit one valueChange event per reset control', () => {
|
|
|
|
form.valueChanges.subscribe(() => logger.push('form'));
|
|
|
|
g.valueChanges.subscribe(() => logger.push('group'));
|
|
|
|
c.valueChanges.subscribe(() => logger.push('control1'));
|
|
|
|
c2.valueChanges.subscribe(() => logger.push('control2'));
|
|
|
|
c3.valueChanges.subscribe(() => logger.push('control3'));
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(logger).toEqual(['control1', 'control2', 'group', 'form']);
|
2016-08-24 19:58:43 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not fire an event when explicitly specified', fakeAsync(() => {
|
|
|
|
form.valueChanges.subscribe((value) => {
|
|
|
|
throw 'Should not happen';
|
|
|
|
});
|
|
|
|
g.valueChanges.subscribe((value) => {
|
|
|
|
throw 'Should not happen';
|
|
|
|
});
|
|
|
|
c.valueChanges.subscribe((value) => {
|
|
|
|
throw 'Should not happen';
|
2019-01-27 16:59:47 -05:00
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset({}, {emitEvent: false});
|
|
|
|
tick();
|
|
|
|
}));
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should emit one statusChange event per reset control', () => {
|
|
|
|
form.statusChanges.subscribe(() => logger.push('form'));
|
|
|
|
g.statusChanges.subscribe(() => logger.push('group'));
|
|
|
|
c.statusChanges.subscribe(() => logger.push('control1'));
|
|
|
|
c2.statusChanges.subscribe(() => logger.push('control2'));
|
|
|
|
c3.statusChanges.subscribe(() => logger.push('control3'));
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset();
|
|
|
|
expect(logger).toEqual(['control1', 'control2', 'group', 'form']);
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should emit one statusChange event per reset control', () => {
|
|
|
|
form.statusChanges.subscribe(() => logger.push('form'));
|
|
|
|
g.statusChanges.subscribe(() => logger.push('group'));
|
|
|
|
c.statusChanges.subscribe(() => logger.push('control1'));
|
|
|
|
c2.statusChanges.subscribe(() => logger.push('control2'));
|
|
|
|
c3.statusChanges.subscribe(() => logger.push('control3'));
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.reset({'one': {value: '', disabled: true}});
|
|
|
|
expect(logger).toEqual(['control1', 'control2', 'group', 'form']);
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should mark as pristine and not dirty before emitting valueChange and statusChange events when resetting',
|
|
|
|
() => {
|
|
|
|
const pristineAndNotDirty = () => {
|
|
|
|
expect(form.pristine).toBe(true);
|
|
|
|
expect(form.dirty).toBe(false);
|
|
|
|
};
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
c3.markAsDirty();
|
|
|
|
expect(form.pristine).toBe(false);
|
|
|
|
expect(form.dirty).toBe(true);
|
2016-09-12 18:15:50 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
form.valueChanges.subscribe(pristineAndNotDirty);
|
|
|
|
form.statusChanges.subscribe(pristineAndNotDirty);
|
2016-09-12 18:15:50 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
form.reset();
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('contains', () => {
|
|
|
|
let group: FormGroup;
|
2018-04-07 03:11:02 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
group = new FormGroup({
|
|
|
|
'required': new FormControl('requiredValue'),
|
|
|
|
'optional': new FormControl({value: 'disabled value', disabled: true})
|
2018-04-07 03:11:02 -04:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
2018-04-07 03:11:02 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should return false when the component is disabled', () => {
|
|
|
|
expect(group.contains('optional')).toEqual(false);
|
2018-04-07 03:11:02 -04:00
|
|
|
});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should return false when there is no component with the given name', () => {
|
|
|
|
expect(group.contains('something else')).toEqual(false);
|
|
|
|
});
|
2016-08-19 15:10:53 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should return true when the component is enabled', () => {
|
|
|
|
expect(group.contains('required')).toEqual(true);
|
2016-08-19 15:10:53 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
group.enable();
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(group.contains('optional')).toEqual(true);
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should support controls with dots in their name', () => {
|
|
|
|
expect(group.contains('some.name')).toBe(false);
|
|
|
|
group.addControl('some.name', new FormControl());
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(group.contains('some.name')).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
2018-12-21 19:39:37 -05:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('retrieve', () => {
|
|
|
|
let group: FormGroup;
|
2018-12-21 19:39:37 -05:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
group = new FormGroup({
|
|
|
|
'required': new FormControl('requiredValue'),
|
2018-12-21 19:39:37 -05:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
2018-12-21 19:39:37 -05:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not get inherited properties', () => {
|
|
|
|
expect(group.get('constructor')).toBe(null);
|
2018-12-21 19:39:37 -05:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
2018-12-21 19:39:37 -05:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('statusChanges', () => {
|
|
|
|
let control: FormControl;
|
|
|
|
let group: FormGroup;
|
|
|
|
|
|
|
|
beforeEach(async(() => {
|
|
|
|
control = new FormControl('', asyncValidatorReturningObservable);
|
|
|
|
group = new FormGroup({'one': control});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
// TODO(kara): update these tests to use fake Async
|
|
|
|
it('should fire a statusChange if child has async validation change',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
|
|
|
const loggedValues: string[] = [];
|
|
|
|
group.statusChanges.subscribe({
|
|
|
|
next: (status: string) => {
|
|
|
|
loggedValues.push(status);
|
|
|
|
if (loggedValues.length === 2) {
|
|
|
|
expect(loggedValues).toEqual(['PENDING', 'INVALID']);
|
|
|
|
}
|
|
|
|
async.done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
control.setValue('');
|
|
|
|
}));
|
|
|
|
});
|
2018-12-21 19:39:37 -05:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('getError', () => {
|
|
|
|
it('should return the error when it is present', () => {
|
|
|
|
const c = new FormControl('', Validators.required);
|
|
|
|
const g = new FormGroup({'one': c});
|
|
|
|
expect(c.getError('required')).toEqual(true);
|
|
|
|
expect(g.getError('required', ['one'])).toEqual(true);
|
|
|
|
});
|
2018-12-21 19:39:37 -05:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should return null otherwise', () => {
|
|
|
|
const c = new FormControl('not empty', Validators.required);
|
|
|
|
const g = new FormGroup({'one': c});
|
|
|
|
expect(c.getError('invalid')).toEqual(null);
|
|
|
|
expect(g.getError('required', ['one'])).toEqual(null);
|
|
|
|
expect(g.getError('required', ['invalid'])).toEqual(null);
|
|
|
|
});
|
2018-12-21 19:39:37 -05:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should be able to traverse group with single string', () => {
|
|
|
|
const c = new FormControl('', Validators.required);
|
|
|
|
const g = new FormGroup({'one': c});
|
|
|
|
expect(c.getError('required')).toEqual(true);
|
|
|
|
expect(g.getError('required', 'one')).toEqual(true);
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should be able to traverse group with string delimited by dots', () => {
|
|
|
|
const c = new FormControl('', Validators.required);
|
|
|
|
const g2 = new FormGroup({'two': c});
|
|
|
|
const g1 = new FormGroup({'one': g2});
|
|
|
|
expect(c.getError('required')).toEqual(true);
|
|
|
|
expect(g1.getError('required', 'one.two')).toEqual(true);
|
|
|
|
});
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should traverse group with form array using string and numbers', () => {
|
|
|
|
const c = new FormControl('', Validators.required);
|
|
|
|
const g2 = new FormGroup({'two': c});
|
|
|
|
const a = new FormArray([g2]);
|
|
|
|
const g1 = new FormGroup({'one': a});
|
|
|
|
expect(c.getError('required')).toEqual(true);
|
|
|
|
expect(g1.getError('required', ['one', 0, 'two'])).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('hasError', () => {
|
|
|
|
it('should return true when it is present', () => {
|
|
|
|
const c = new FormControl('', Validators.required);
|
|
|
|
const g = new FormGroup({'one': c});
|
|
|
|
expect(c.hasError('required')).toEqual(true);
|
|
|
|
expect(g.hasError('required', ['one'])).toEqual(true);
|
|
|
|
});
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should return false otherwise', () => {
|
|
|
|
const c = new FormControl('not empty', Validators.required);
|
|
|
|
const g = new FormGroup({'one': c});
|
|
|
|
expect(c.hasError('invalid')).toEqual(false);
|
|
|
|
expect(g.hasError('required', ['one'])).toEqual(false);
|
|
|
|
expect(g.hasError('required', ['invalid'])).toEqual(false);
|
|
|
|
});
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should be able to traverse group with single string', () => {
|
|
|
|
const c = new FormControl('', Validators.required);
|
|
|
|
const g = new FormGroup({'one': c});
|
|
|
|
expect(c.hasError('required')).toEqual(true);
|
|
|
|
expect(g.hasError('required', 'one')).toEqual(true);
|
|
|
|
});
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should be able to traverse group with string delimited by dots', () => {
|
|
|
|
const c = new FormControl('', Validators.required);
|
|
|
|
const g2 = new FormGroup({'two': c});
|
|
|
|
const g1 = new FormGroup({'one': g2});
|
|
|
|
expect(c.hasError('required')).toEqual(true);
|
|
|
|
expect(g1.hasError('required', 'one.two')).toEqual(true);
|
|
|
|
});
|
|
|
|
it('should traverse group with form array using string and numbers', () => {
|
|
|
|
const c = new FormControl('', Validators.required);
|
|
|
|
const g2 = new FormGroup({'two': c});
|
|
|
|
const a = new FormArray([g2]);
|
|
|
|
const g1 = new FormGroup({'one': a});
|
|
|
|
expect(c.getError('required')).toEqual(true);
|
|
|
|
expect(g1.getError('required', ['one', 0, 'two'])).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('validator', () => {
|
|
|
|
function containsValidator(c: AbstractControl): ValidationErrors|null {
|
|
|
|
return c.get('one')!.value && c.get('one')!.value.indexOf('c') !== -1 ? null :
|
|
|
|
{'missing': true};
|
|
|
|
}
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should run a single validator when the value changes', () => {
|
|
|
|
const c = new FormControl(null);
|
|
|
|
const g = new FormGroup({'one': c}, simpleValidator);
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
c.setValue('correct');
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.valid).toEqual(true);
|
|
|
|
expect(g.errors).toEqual(null);
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
c.setValue('incorrect');
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.valid).toEqual(false);
|
|
|
|
expect(g.errors).toEqual({'broken': true});
|
|
|
|
});
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should support multiple validators from array', () => {
|
|
|
|
const g = new FormGroup({one: new FormControl()}, [simpleValidator, containsValidator]);
|
|
|
|
expect(g.valid).toEqual(false);
|
|
|
|
expect(g.errors).toEqual({missing: true, broken: true});
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.setValue({one: 'c'});
|
|
|
|
expect(g.valid).toEqual(false);
|
|
|
|
expect(g.errors).toEqual({broken: true});
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.setValue({one: 'correct'});
|
|
|
|
expect(g.valid).toEqual(true);
|
|
|
|
});
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set single validator from options obj', () => {
|
|
|
|
const g = new FormGroup({one: new FormControl()}, {validators: simpleValidator});
|
|
|
|
expect(g.valid).toEqual(false);
|
|
|
|
expect(g.errors).toEqual({broken: true});
|
|
|
|
|
|
|
|
g.setValue({one: 'correct'});
|
|
|
|
expect(g.valid).toEqual(true);
|
2017-07-25 18:01:04 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set multiple validators from options obj', () => {
|
|
|
|
const g = new FormGroup(
|
|
|
|
{one: new FormControl()}, {validators: [simpleValidator, containsValidator]});
|
|
|
|
expect(g.valid).toEqual(false);
|
|
|
|
expect(g.errors).toEqual({missing: true, broken: true});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.setValue({one: 'c'});
|
|
|
|
expect(g.valid).toEqual(false);
|
|
|
|
expect(g.errors).toEqual({broken: true});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.setValue({one: 'correct'});
|
|
|
|
expect(g.valid).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('asyncValidator', () => {
|
|
|
|
it('should run the async validator', fakeAsync(() => {
|
|
|
|
const c = new FormControl('value');
|
|
|
|
const g = new FormGroup({'one': c}, null!, asyncValidator('expected'));
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.pending).toEqual(true);
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
tick(1);
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.errors).toEqual({'async': true});
|
|
|
|
expect(g.pending).toEqual(false);
|
|
|
|
}));
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set multiple async validators from array', fakeAsync(() => {
|
|
|
|
const g = new FormGroup(
|
|
|
|
{'one': new FormControl('value')}, null!,
|
|
|
|
[asyncValidator('expected'), otherObservableValidator]);
|
|
|
|
expect(g.pending).toEqual(true);
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
tick();
|
|
|
|
expect(g.errors).toEqual({'async': true, 'other': true});
|
|
|
|
expect(g.pending).toEqual(false);
|
|
|
|
}));
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set single async validator from options obj', fakeAsync(() => {
|
|
|
|
const g = new FormGroup(
|
|
|
|
{'one': new FormControl('value')}, {asyncValidators: asyncValidator('expected')});
|
|
|
|
expect(g.pending).toEqual(true);
|
2017-07-25 18:01:04 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
tick();
|
|
|
|
expect(g.errors).toEqual({'async': true});
|
|
|
|
expect(g.pending).toEqual(false);
|
|
|
|
}));
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set multiple async validators from options obj', fakeAsync(() => {
|
|
|
|
const g = new FormGroup(
|
|
|
|
{'one': new FormControl('value')},
|
|
|
|
{asyncValidators: [asyncValidator('expected'), otherObservableValidator]});
|
|
|
|
expect(g.pending).toEqual(true);
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
tick();
|
|
|
|
expect(g.errors).toEqual({'async': true, 'other': true});
|
|
|
|
expect(g.pending).toEqual(false);
|
|
|
|
}));
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set the parent group\'s status to pending', fakeAsync(() => {
|
|
|
|
const c = new FormControl('value', null!, asyncValidator('expected'));
|
|
|
|
const g = new FormGroup({'one': c});
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.pending).toEqual(true);
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
tick(1);
|
2016-08-13 20:26:08 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.pending).toEqual(false);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should run the parent group\'s async validator when children are pending', fakeAsync(() => {
|
|
|
|
const c = new FormControl('value', null!, asyncValidator('expected'));
|
|
|
|
const g = new FormGroup({'one': c}, null!, asyncValidator('expected'));
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
expect(g.errors).toEqual({'async': true});
|
|
|
|
expect(g.get('one')!.errors).toEqual({'async': true});
|
|
|
|
}));
|
2020-06-13 15:47:30 -04:00
|
|
|
|
|
|
|
it('should handle successful async FormGroup resolving synchronously before a successful async child validator',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 1, shouldFail: false}));
|
|
|
|
const g = new FormGroup(
|
|
|
|
{'one': c}, null!, simpleAsyncValidator({timeout: 0, shouldFail: false}));
|
|
|
|
|
|
|
|
// Initially, the form control validation is pending, and the form group own validation has
|
|
|
|
// synchronously resolved. Still, the form is in pending state due to its child
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, the form control validation has resolved
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle successful async FormGroup resolving after a synchronously and successfully resolving child validator',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 0, shouldFail: false}));
|
|
|
|
const g = new FormGroup(
|
|
|
|
{'one': c}, null!, simpleAsyncValidator({timeout: 1, shouldFail: false}));
|
|
|
|
|
|
|
|
// Initially, form control validator has synchronously resolved. However, g has its own
|
|
|
|
// pending validation
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, the form group validation has resolved
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle successful async FormGroup and child control validators resolving synchronously',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 0, shouldFail: false}));
|
|
|
|
const g = new FormGroup(
|
|
|
|
{'one': c}, null!, simpleAsyncValidator({timeout: 0, shouldFail: false}));
|
|
|
|
|
|
|
|
// Both form control and form group successful async validators have resolved synchronously
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle failing async FormGroup and failing child control validators resolving synchronously',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 0, shouldFail: true}));
|
|
|
|
const g =
|
|
|
|
new FormGroup({'one': c}, null!, simpleAsyncValidator({timeout: 0, shouldFail: true}));
|
|
|
|
|
|
|
|
// FormControl async validator has executed and failed synchronously with the default error
|
|
|
|
// `{async: true}`. Next, the form group status is calculated. Since one of its children is
|
|
|
|
// failing, the form group itself is marked `INVALID`. And its asynchronous validation is
|
|
|
|
// not even triggered. Therefore, we end up with form group that is `INVALID` but whose
|
|
|
|
// errors are null (child errors do not propagate and own async validation not event
|
|
|
|
// triggered).
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: false, status: 'INVALID'}, // Group
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle failing async FormGroup and successful child control validators resolving synchronously',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 0, shouldFail: false}));
|
|
|
|
const g =
|
|
|
|
new FormGroup({'one': c}, null!, simpleAsyncValidator({timeout: 0, shouldFail: true}));
|
|
|
|
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle failing async FormArray and successful children validators resolving synchronously',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 0, shouldFail: false}));
|
|
|
|
const g = new FormGroup(
|
|
|
|
{'one': c}, null!, simpleAsyncValidator({timeout: 0, shouldFail: false}));
|
|
|
|
|
|
|
|
const c2 =
|
|
|
|
new FormControl('fcVal', null!, simpleAsyncValidator({timeout: 0, shouldFail: false}));
|
|
|
|
|
|
|
|
const a =
|
|
|
|
new FormArray([g, c2], null!, simpleAsyncValidator({timeout: 0, shouldFail: true}));
|
|
|
|
|
|
|
|
expect(currentStateOf([a, a.at(0)!, a.at(1)!])).toEqual([
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // Array
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Group p
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control c2
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle failing FormGroup validator resolving after successful child validator',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 1, shouldFail: false}));
|
|
|
|
const g =
|
|
|
|
new FormGroup({'one': c}, null!, simpleAsyncValidator({timeout: 2, shouldFail: true}));
|
|
|
|
|
|
|
|
// Initially, the form group and nested control are in pending state
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, only form control validation has resolved
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, the form group validation fails
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle failing FormArray validator resolving after successful child validator',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 1, shouldFail: false}));
|
|
|
|
const a = new FormArray([c], null!, simpleAsyncValidator({timeout: 2, shouldFail: true}));
|
|
|
|
|
|
|
|
// Initially, the form array and nested control are in pending state
|
|
|
|
expect(currentStateOf([a, a.at(0)!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // FormArray
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, only form control validation has resolved
|
|
|
|
expect(currentStateOf([a, a.at(0)!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // FormArray
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, the form array validation fails
|
|
|
|
expect(currentStateOf([a, a.at(0)!])).toEqual([
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // FormArray
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle successful FormGroup validator resolving after successful child validator',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 1, shouldFail: false}));
|
|
|
|
const g = new FormGroup(
|
|
|
|
{'one': c}, null!, simpleAsyncValidator({timeout: 2, shouldFail: false}));
|
|
|
|
|
|
|
|
// Initially, the form group and nested control are in pending state
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, only form control validation has resolved
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, the form group validation resolves
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle successful FormArray validator resolving after successful child validators',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c1 = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 1, shouldFail: false}));
|
|
|
|
const g = new FormGroup(
|
|
|
|
{'one': c1}, null!, simpleAsyncValidator({timeout: 2, shouldFail: false}));
|
|
|
|
const c2 =
|
|
|
|
new FormControl('fcVal', null!, simpleAsyncValidator({timeout: 3, shouldFail: false}));
|
|
|
|
|
|
|
|
const a =
|
|
|
|
new FormArray([g, c2], null!, simpleAsyncValidator({timeout: 4, shouldFail: false}));
|
|
|
|
|
|
|
|
// Initially, the form array and the tested form group and form control c2 are in pending
|
|
|
|
// state
|
|
|
|
expect(currentStateOf([a, a.at(0)!, a.at(1)!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // FormArray
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // g
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // c2
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(2);
|
|
|
|
|
|
|
|
// After 2ms, g validation has resolved
|
|
|
|
expect(currentStateOf([a, a.at(0)!, a.at(1)!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // FormArray
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // g
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // c2
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, c2 validation has resolved
|
|
|
|
expect(currentStateOf([a, a.at(0)!, a.at(1)!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // FormArray
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // g
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // c2
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, FormArray own validation has resolved
|
|
|
|
expect(currentStateOf([a, a.at(0)!, a.at(1)!])).toEqual([
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // FormArray
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // g
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // c2
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle failing FormArray validator resolving after successful child validators',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c1 = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 1, shouldFail: false}));
|
|
|
|
const g = new FormGroup(
|
|
|
|
{'one': c1}, null!, simpleAsyncValidator({timeout: 2, shouldFail: false}));
|
|
|
|
const c2 =
|
|
|
|
new FormControl('fcVal', null!, simpleAsyncValidator({timeout: 3, shouldFail: false}));
|
|
|
|
|
|
|
|
const a =
|
|
|
|
new FormArray([g, c2], null!, simpleAsyncValidator({timeout: 4, shouldFail: true}));
|
|
|
|
|
|
|
|
// Initially, the form array and the tested form group and form control c2 are in pending
|
|
|
|
// state
|
|
|
|
expect(currentStateOf([a, a.at(0)!, a.at(1)!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // FormArray
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // g
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // c2
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(2);
|
|
|
|
|
|
|
|
// After 2ms, g validation has resolved
|
|
|
|
expect(currentStateOf([a, a.at(0)!, a.at(1)!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // FormArray
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // g
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // c2
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, c2 validation has resolved
|
|
|
|
expect(currentStateOf([a, a.at(0)!, a.at(1)!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // FormArray
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // g
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // c2
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, FormArray own validation has failed
|
|
|
|
expect(currentStateOf([a, a.at(0)!, a.at(1)!])).toEqual([
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // FormArray
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // g
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // c2
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle multiple successful FormGroup validators resolving after successful child validator',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 1, shouldFail: false}));
|
|
|
|
const g = new FormGroup({'one': c}, null!, [
|
|
|
|
simpleAsyncValidator({timeout: 2, shouldFail: false}),
|
|
|
|
simpleAsyncValidator({timeout: 3, shouldFail: false})
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Initially, the form group and nested control are in pending state
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, only form control validation has resolved
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, one form async validator has resolved but not the second
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, the form group validation resolves
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle multiple FormGroup validators (success then failure) resolving after successful child validator',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 1, shouldFail: false}));
|
|
|
|
const g = new FormGroup({'one': c}, null!, [
|
|
|
|
simpleAsyncValidator({timeout: 2, shouldFail: false}),
|
|
|
|
simpleAsyncValidator({timeout: 3, shouldFail: true})
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Initially, the form group and nested control are in pending state
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, only form control validation has resolved
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, one form async validator has resolved but not the second
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, the form group validation fails
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should handle multiple FormGroup validators (failure then success) resolving after successful child validator',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 1, shouldFail: false}));
|
|
|
|
const g = new FormGroup({'one': c}, null!, [
|
|
|
|
simpleAsyncValidator({timeout: 2, shouldFail: true}),
|
|
|
|
simpleAsyncValidator({timeout: 3, shouldFail: false})
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Initially, the form group and nested control are in pending state
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, only form control validation has resolved
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// All async validators are composed into one function. So, after 2ms, the FormGroup g is
|
|
|
|
// still in pending state without errors
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, the form group validation fails
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should handle async validators in nested form groups / arrays', fakeAsync(() => {
|
|
|
|
const c1 = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 1, shouldFail: false}));
|
|
|
|
|
|
|
|
const g1 = new FormGroup(
|
|
|
|
{'one': c1}, null!, simpleAsyncValidator({timeout: 2, shouldFail: true}));
|
|
|
|
|
|
|
|
const c2 =
|
|
|
|
new FormControl('fcVal', null!, simpleAsyncValidator({timeout: 3, shouldFail: false}));
|
|
|
|
|
|
|
|
const g2 =
|
|
|
|
new FormArray([c2], null!, simpleAsyncValidator({timeout: 4, shouldFail: false}));
|
|
|
|
|
|
|
|
const g = new FormGroup(
|
|
|
|
{'g1': g1, 'g2': g2}, null!, simpleAsyncValidator({timeout: 5, shouldFail: false}));
|
|
|
|
|
|
|
|
// Initially, the form group and nested control are in pending state
|
|
|
|
expect(currentStateOf([g, g.get('g1')!, g.get('g2')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group g
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group g1
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group g2
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(2);
|
|
|
|
|
|
|
|
// After 2ms, g1 validation fails
|
|
|
|
expect(currentStateOf([g, g.get('g1')!, g.get('g2')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group g
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // Group g1
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group g2
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(2);
|
|
|
|
|
|
|
|
// After 2ms, g2 validation resolves
|
|
|
|
expect(currentStateOf([g, g.get('g1')!, g.get('g2')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group g
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // Group g1
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Group g2
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, g validation fails because g1 is invalid, but since errors do not cascade, so
|
|
|
|
// we still have null errors for g
|
|
|
|
expect(currentStateOf([g, g.get('g1')!, g.get('g2')!])).toEqual([
|
|
|
|
{errors: null, pending: false, status: 'INVALID'}, // Group g
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // Group g1
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Group g2
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle failing FormGroup validator resolving before successful child validator',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 2, shouldFail: false}));
|
|
|
|
const g =
|
|
|
|
new FormGroup({'one': c}, null!, simpleAsyncValidator({timeout: 1, shouldFail: true}));
|
|
|
|
|
|
|
|
// Initially, the form group and nested control are in pending state
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Group
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, form group validation fails
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // Group
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, child validation resolves
|
|
|
|
expect(currentStateOf([g, g.get('one')!])).toEqual([
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // Group
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle failing FormArray validator resolving before successful child validator',
|
|
|
|
fakeAsync(() => {
|
|
|
|
const c = new FormControl(
|
|
|
|
'fcValue', null!, simpleAsyncValidator({timeout: 2, shouldFail: false}));
|
|
|
|
const a = new FormArray([c], null!, simpleAsyncValidator({timeout: 1, shouldFail: true}));
|
|
|
|
|
|
|
|
// Initially, the form array and nested control are in pending state
|
|
|
|
expect(currentStateOf([a, a.at(0)!])).toEqual([
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // FormArray
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, form array validation fails
|
|
|
|
expect(currentStateOf([a, a.at(0)!])).toEqual([
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // FormArray
|
|
|
|
{errors: null, pending: true, status: 'PENDING'}, // Control
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
// After 1ms, child validation resolves
|
|
|
|
expect(currentStateOf([a, a.at(0)!])).toEqual([
|
|
|
|
{errors: {async: true}, pending: false, status: 'INVALID'}, // FormArray
|
|
|
|
{errors: null, pending: false, status: 'VALID'}, // Control
|
|
|
|
]);
|
|
|
|
}));
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('disable() & enable()', () => {
|
|
|
|
it('should mark the group as disabled', () => {
|
|
|
|
const g = new FormGroup({'one': new FormControl(null)});
|
|
|
|
expect(g.disabled).toBe(false);
|
|
|
|
expect(g.valid).toBe(true);
|
|
|
|
|
|
|
|
g.disable();
|
|
|
|
expect(g.disabled).toBe(true);
|
|
|
|
expect(g.valid).toBe(false);
|
|
|
|
|
|
|
|
g.enable();
|
|
|
|
expect(g.disabled).toBe(false);
|
|
|
|
expect(g.valid).toBe(true);
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should set the group status as disabled', () => {
|
|
|
|
const g = new FormGroup({'one': new FormControl(null)});
|
|
|
|
expect(g.status).toEqual('VALID');
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.disable();
|
|
|
|
expect(g.status).toEqual('DISABLED');
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.enable();
|
|
|
|
expect(g.status).toBe('VALID');
|
|
|
|
});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should mark children of the group as disabled', () => {
|
|
|
|
const c1 = new FormControl(null);
|
|
|
|
const c2 = new FormControl(null);
|
|
|
|
const g = new FormGroup({'one': c1, 'two': c2});
|
|
|
|
expect(c1.disabled).toBe(false);
|
|
|
|
expect(c2.disabled).toBe(false);
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.disable();
|
|
|
|
expect(c1.disabled).toBe(true);
|
|
|
|
expect(c2.disabled).toBe(true);
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.enable();
|
|
|
|
expect(c1.disabled).toBe(false);
|
|
|
|
expect(c2.disabled).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should ignore disabled controls in validation', () => {
|
|
|
|
const g = new FormGroup({
|
|
|
|
nested: new FormGroup({one: new FormControl(null, Validators.required)}),
|
|
|
|
two: new FormControl('two')
|
2016-08-24 19:58:43 -04:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.valid).toBe(false);
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested')!.disable();
|
|
|
|
expect(g.valid).toBe(true);
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested')!.enable();
|
|
|
|
expect(g.valid).toBe(false);
|
|
|
|
});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should ignore disabled controls when serializing value', () => {
|
|
|
|
const g = new FormGroup(
|
|
|
|
{nested: new FormGroup({one: new FormControl('one')}), two: new FormControl('two')});
|
|
|
|
expect(g.value).toEqual({'nested': {'one': 'one'}, 'two': 'two'});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested')!.disable();
|
|
|
|
expect(g.value).toEqual({'two': 'two'});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested')!.enable();
|
|
|
|
expect(g.value).toEqual({'nested': {'one': 'one'}, 'two': 'two'});
|
|
|
|
});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should update its value when disabled with disabled children', () => {
|
|
|
|
const g = new FormGroup(
|
|
|
|
{nested: new FormGroup({one: new FormControl('one'), two: new FormControl('two')})});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested.two')!.disable();
|
|
|
|
expect(g.value).toEqual({nested: {one: 'one'}});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested')!.disable();
|
|
|
|
expect(g.value).toEqual({nested: {one: 'one', two: 'two'}});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested')!.enable();
|
|
|
|
expect(g.value).toEqual({nested: {one: 'one', two: 'two'}});
|
|
|
|
});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should update its value when enabled with disabled children', () => {
|
|
|
|
const g = new FormGroup(
|
|
|
|
{nested: new FormGroup({one: new FormControl('one'), two: new FormControl('two')})});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested.two')!.disable();
|
|
|
|
expect(g.value).toEqual({nested: {one: 'one'}});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested')!.enable();
|
|
|
|
expect(g.value).toEqual({nested: {one: 'one', two: 'two'}});
|
|
|
|
});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should ignore disabled controls when determining dirtiness', () => {
|
|
|
|
const g = new FormGroup(
|
|
|
|
{nested: new FormGroup({one: new FormControl('one')}), two: new FormControl('two')});
|
|
|
|
g.get('nested.one')!.markAsDirty();
|
|
|
|
expect(g.dirty).toBe(true);
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested')!.disable();
|
|
|
|
expect(g.get('nested')!.dirty).toBe(true);
|
|
|
|
expect(g.dirty).toEqual(false);
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested')!.enable();
|
|
|
|
expect(g.dirty).toEqual(true);
|
|
|
|
});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should ignore disabled controls when determining touched state', () => {
|
|
|
|
const g = new FormGroup(
|
|
|
|
{nested: new FormGroup({one: new FormControl('one')}), two: new FormControl('two')});
|
|
|
|
g.get('nested.one')!.markAsTouched();
|
|
|
|
expect(g.touched).toBe(true);
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested')!.disable();
|
|
|
|
expect(g.get('nested')!.touched).toBe(true);
|
|
|
|
expect(g.touched).toEqual(false);
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.get('nested')!.enable();
|
|
|
|
expect(g.touched).toEqual(true);
|
|
|
|
});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should keep empty, disabled groups disabled when updating validity', () => {
|
|
|
|
const group = new FormGroup({});
|
|
|
|
expect(group.status).toEqual('VALID');
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
group.disable();
|
|
|
|
expect(group.status).toEqual('DISABLED');
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
group.updateValueAndValidity();
|
|
|
|
expect(group.status).toEqual('DISABLED');
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
group.addControl('one', new FormControl({value: '', disabled: true}));
|
|
|
|
expect(group.status).toEqual('DISABLED');
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
group.addControl('two', new FormControl());
|
|
|
|
expect(group.status).toEqual('VALID');
|
|
|
|
});
|
2016-09-08 15:21:48 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should re-enable empty, disabled groups', () => {
|
|
|
|
const group = new FormGroup({});
|
|
|
|
group.disable();
|
|
|
|
expect(group.status).toEqual('DISABLED');
|
2016-09-08 15:21:48 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
group.enable();
|
|
|
|
expect(group.status).toEqual('VALID');
|
|
|
|
});
|
2016-09-08 15:21:48 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not run validators on disabled controls', () => {
|
|
|
|
const validator = jasmine.createSpy('validator');
|
|
|
|
const g = new FormGroup({'one': new FormControl()}, validator);
|
|
|
|
expect(validator.calls.count()).toEqual(1);
|
2016-09-08 15:21:48 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.disable();
|
|
|
|
expect(validator.calls.count()).toEqual(1);
|
2016-09-08 15:21:48 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.setValue({one: 'value'});
|
|
|
|
expect(validator.calls.count()).toEqual(1);
|
2016-09-08 15:21:48 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.enable();
|
|
|
|
expect(validator.calls.count()).toEqual(2);
|
|
|
|
});
|
2016-09-08 15:21:48 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('disabled errors', () => {
|
|
|
|
it('should clear out group errors when disabled', () => {
|
|
|
|
const g = new FormGroup({'one': new FormControl()}, () => ({'expected': true}));
|
|
|
|
expect(g.errors).toEqual({'expected': true});
|
2016-09-09 15:00:38 -04:00
|
|
|
|
|
|
|
g.disable();
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.errors).toEqual(null);
|
2016-09-09 15:00:38 -04:00
|
|
|
|
|
|
|
g.enable();
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.errors).toEqual({'expected': true});
|
2016-09-09 15:00:38 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should re-populate group errors when enabled from a child', () => {
|
|
|
|
const g = new FormGroup({'one': new FormControl()}, () => ({'expected': true}));
|
|
|
|
g.disable();
|
|
|
|
expect(g.errors).toEqual(null);
|
|
|
|
|
|
|
|
g.addControl('two', new FormControl());
|
|
|
|
expect(g.errors).toEqual({'expected': true});
|
2016-09-09 15:00:38 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should clear out async group errors when disabled', fakeAsync(() => {
|
|
|
|
const g = new FormGroup({'one': new FormControl()}, null!, asyncValidator('expected'));
|
|
|
|
tick();
|
|
|
|
expect(g.errors).toEqual({'async': true});
|
2017-12-14 10:51:05 -05:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.disable();
|
|
|
|
expect(g.errors).toEqual(null);
|
|
|
|
|
|
|
|
g.enable();
|
|
|
|
tick();
|
|
|
|
expect(g.errors).toEqual({'async': true});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should re-populate async group errors when enabled from a child', fakeAsync(() => {
|
|
|
|
const g = new FormGroup({'one': new FormControl()}, null!, asyncValidator('expected'));
|
|
|
|
tick();
|
|
|
|
expect(g.errors).toEqual({'async': true});
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
g.disable();
|
|
|
|
expect(g.errors).toEqual(null);
|
|
|
|
|
|
|
|
g.addControl('two', new FormControl());
|
|
|
|
tick();
|
|
|
|
expect(g.errors).toEqual({'async': true});
|
|
|
|
}));
|
2016-08-24 19:58:43 -04:00
|
|
|
});
|
2016-08-29 14:33:49 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('disabled events', () => {
|
2016-08-29 14:33:49 -04:00
|
|
|
let logger: string[];
|
2020-04-06 18:44:00 -04:00
|
|
|
let c: FormControl;
|
|
|
|
let g: FormGroup;
|
|
|
|
let form: FormGroup;
|
2016-08-29 14:33:49 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
logger = [];
|
2020-04-06 18:44:00 -04:00
|
|
|
c = new FormControl('', Validators.required);
|
|
|
|
g = new FormGroup({one: c});
|
|
|
|
form = new FormGroup({g: g});
|
|
|
|
});
|
2016-08-29 14:33:49 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should emit value change events in the right order', () => {
|
|
|
|
c.valueChanges.subscribe(() => logger.push('control'));
|
|
|
|
g.valueChanges.subscribe(() => logger.push('group'));
|
|
|
|
form.valueChanges.subscribe(() => logger.push('form'));
|
|
|
|
|
|
|
|
g.disable();
|
|
|
|
expect(logger).toEqual(['control', 'group', 'form']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should emit status change events in the right order', () => {
|
|
|
|
c.statusChanges.subscribe(() => logger.push('control'));
|
|
|
|
g.statusChanges.subscribe(() => logger.push('group'));
|
2016-08-29 14:33:49 -04:00
|
|
|
form.statusChanges.subscribe(() => logger.push('form'));
|
2020-04-06 18:44:00 -04:00
|
|
|
|
|
|
|
g.disable();
|
|
|
|
expect(logger).toEqual(['control', 'group', 'form']);
|
2016-08-29 14:33:49 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not emit value change events when emitEvent = false', () => {
|
|
|
|
c.valueChanges.subscribe(() => logger.push('control'));
|
|
|
|
g.valueChanges.subscribe(() => logger.push('group'));
|
|
|
|
form.valueChanges.subscribe(() => logger.push('form'));
|
|
|
|
|
|
|
|
g.disable({emitEvent: false});
|
|
|
|
expect(logger).toEqual([]);
|
|
|
|
g.enable({emitEvent: false});
|
|
|
|
expect(logger).toEqual([]);
|
2016-08-29 14:33:49 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not emit status change events when emitEvent = false', () => {
|
|
|
|
c.statusChanges.subscribe(() => logger.push('control'));
|
|
|
|
g.statusChanges.subscribe(() => logger.push('group'));
|
|
|
|
form.statusChanges.subscribe(() => logger.push('form'));
|
|
|
|
|
|
|
|
g.disable({emitEvent: false});
|
|
|
|
expect(logger).toEqual([]);
|
|
|
|
g.enable({emitEvent: false});
|
2016-08-29 14:33:49 -04:00
|
|
|
expect(logger).toEqual([]);
|
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
|
|
|
});
|
2016-08-29 14:33:49 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('updateTreeValidity()', () => {
|
|
|
|
let c: FormControl, c2: FormControl, c3: FormControl;
|
|
|
|
let nested: FormGroup, form: FormGroup;
|
|
|
|
let logger: string[];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
c = new FormControl('one');
|
|
|
|
c2 = new FormControl('two');
|
|
|
|
c3 = new FormControl('three');
|
|
|
|
nested = new FormGroup({one: c, two: c2});
|
|
|
|
form = new FormGroup({nested: nested, three: c3});
|
|
|
|
logger = [];
|
|
|
|
|
|
|
|
c.statusChanges.subscribe(() => logger.push('one'));
|
|
|
|
c2.statusChanges.subscribe(() => logger.push('two'));
|
|
|
|
c3.statusChanges.subscribe(() => logger.push('three'));
|
|
|
|
nested.statusChanges.subscribe(() => logger.push('nested'));
|
|
|
|
form.statusChanges.subscribe(() => logger.push('form'));
|
2016-08-29 14:33:49 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should update tree validity', () => {
|
|
|
|
(form as any)._updateTreeValidity();
|
|
|
|
expect(logger).toEqual(['one', 'two', 'nested', 'three', 'form']);
|
|
|
|
});
|
2016-09-02 18:57:35 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not emit events when turned off', () => {
|
|
|
|
(form as any)._updateTreeValidity({emitEvent: false});
|
|
|
|
expect(logger).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2016-09-02 18:57:35 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
describe('setControl()', () => {
|
|
|
|
let c: FormControl;
|
|
|
|
let g: FormGroup;
|
2016-09-02 18:57:35 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
c = new FormControl('one');
|
|
|
|
g = new FormGroup({one: c});
|
|
|
|
});
|
2016-09-02 18:57:35 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should replace existing control with new control', () => {
|
|
|
|
const c2 = new FormControl('new!', Validators.minLength(10));
|
|
|
|
g.setControl('one', c2);
|
2016-09-02 18:57:35 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.controls['one']).toEqual(c2);
|
|
|
|
expect(g.value).toEqual({one: 'new!'});
|
|
|
|
expect(g.valid).toBe(false);
|
|
|
|
});
|
2016-09-02 18:57:35 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should add control if control did not exist before', () => {
|
|
|
|
const c2 = new FormControl('new!', Validators.minLength(10));
|
|
|
|
g.setControl('two', c2);
|
2016-09-02 18:57:35 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(g.controls['two']).toEqual(c2);
|
|
|
|
expect(g.value).toEqual({one: 'one', two: 'new!'});
|
|
|
|
expect(g.valid).toBe(false);
|
|
|
|
});
|
2016-09-02 18:57:35 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should remove control if new control is null', () => {
|
|
|
|
g.setControl('one', null!);
|
|
|
|
expect(g.controls['one']).not.toBeDefined();
|
|
|
|
expect(g.value).toEqual({});
|
2016-09-02 18:57:35 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should only emit value change event once', () => {
|
|
|
|
const logger: string[] = [];
|
|
|
|
const c2 = new FormControl('new!');
|
|
|
|
g.valueChanges.subscribe(() => logger.push('change!'));
|
|
|
|
g.setControl('one', c2);
|
|
|
|
expect(logger).toEqual(['change!']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('pending', () => {
|
|
|
|
let c: FormControl;
|
|
|
|
let g: FormGroup;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
c = new FormControl('value');
|
|
|
|
g = new FormGroup({'one': c});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be false after creating a control', () => {
|
|
|
|
expect(g.pending).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be true after changing the value of the control', () => {
|
|
|
|
c.markAsPending();
|
|
|
|
expect(g.pending).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not update the parent when onlySelf = true', () => {
|
|
|
|
c.markAsPending({onlySelf: true});
|
|
|
|
expect(g.pending).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('status change events', () => {
|
|
|
|
let logger: string[];
|
2017-11-06 03:59:09 -05:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-04-06 18:44:00 -04:00
|
|
|
logger = [];
|
|
|
|
g.statusChanges.subscribe((status) => logger.push(status));
|
2017-11-06 03:59:09 -05:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should emit event after marking control as pending', () => {
|
2017-11-06 03:59:09 -05:00
|
|
|
c.markAsPending();
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(logger).toEqual(['PENDING']);
|
2017-11-06 03:59:09 -05:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not emit event when onlySelf = true', () => {
|
2017-11-06 03:59:09 -05:00
|
|
|
c.markAsPending({onlySelf: true});
|
2020-04-06 18:44:00 -04:00
|
|
|
expect(logger).toEqual([]);
|
2017-11-06 03:59:09 -05:00
|
|
|
});
|
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should not emit event when emitEvent = false', () => {
|
|
|
|
c.markAsPending({emitEvent: false});
|
|
|
|
expect(logger).toEqual([]);
|
2017-11-06 03:59:09 -05:00
|
|
|
});
|
2016-08-29 14:33:49 -04:00
|
|
|
|
2020-04-06 18:44:00 -04:00
|
|
|
it('should emit event when parent is markedAsPending', () => {
|
|
|
|
g.markAsPending();
|
|
|
|
expect(logger).toEqual(['PENDING']);
|
|
|
|
});
|
|
|
|
});
|
2016-08-13 20:26:08 -04:00
|
|
|
});
|
2020-04-06 18:44:00 -04:00
|
|
|
});
|
2017-12-16 17:42:55 -05:00
|
|
|
})();
|