ngOnInit() { this.validator = equalTo(this.equalTo); }
it('should not set the parent when explicitly specified', () => { c.patchValue('newValue', {onlySelf: true}); expect(g.value).toEqual({'one': 'oldValue'}); });
describe('reset()', () => { let c: FormControl; beforeEach(() => { c = new FormControl('initial value'); }); it('should reset to a specific value if passed', () => { c.setValue('new value'); expect(c.value).toBe('new value'); c.reset('initial value'); expect(c.value).toBe('initial value'); }); it('should not set the parent when explicitly specified', () => { const g = new FormGroup({'one': c}); c.patchValue('newValue', {onlySelf: true}); expect(g.value).toEqual({'one': 'initial value'}); }); it('should reset to a specific value if passed with boxed value', () => { c.setValue('new value'); expect(c.value).toBe('new value'); c.reset({value: 'initial value', disabled: false}); expect(c.value).toBe('initial value'); }); it('should clear the control value if no value is passed', () => { c.setValue('new value'); expect(c.value).toBe('new value'); c.reset(); expect(c.value).toBe(null); }); it('should update the value of any parent controls with passed value', () => { const g = new FormGroup({'one': c}); c.setValue('new value'); expect(g.value).toEqual({'one': 'new value'}); c.reset('initial value'); expect(g.value).toEqual({'one': 'initial value'}); }); it('should update the value of any parent controls with null value', () => { const g = new FormGroup({'one': c}); c.setValue('new value'); expect(g.value).toEqual({'one': 'new value'}); c.reset(); expect(g.value).toEqual({'one': null}); }); it('should mark the control as pristine', () => { c.markAsDirty(); expect(c.pristine).toBe(false); c.reset(); expect(c.pristine).toBe(true); }); it('should set the parent pristine state if all pristine', () => { const g = new FormGroup({'one': c}); c.markAsDirty(); expect(g.pristine).toBe(false); c.reset(); expect(g.pristine).toBe(true); }); it('should not set the parent pristine state if it has other dirty controls', () => { const c2 = new FormControl('two'); const g = new FormGroup({'one': c, 'two': c2}); c.markAsDirty(); c2.markAsDirty(); c.reset(); expect(g.pristine).toBe(false); }); it('should mark the control as untouched', () => { c.markAsTouched(); expect(c.untouched).toBe(false); c.reset(); expect(c.untouched).toBe(true); }); it('should set the parent untouched state if all untouched', () => { const g = new FormGroup({'one': c}); c.markAsTouched(); expect(g.untouched).toBe(false); c.reset(); expect(g.untouched).toBe(true); }); it('should not set the parent untouched state if other touched controls', () => { const c2 = new FormControl('two'); const g = new FormGroup({'one': c, 'two': c2}); c.markAsTouched(); c2.markAsTouched(); c.reset(); expect(g.untouched).toBe(false); }); it('should retain the disabled state of the control', () => { c.disable(); c.reset(); expect(c.disabled).toBe(true); }); it('should set disabled state based on boxed value if passed', () => { c.disable(); c.reset({value: null, disabled: false}); expect(c.disabled).toBe(false); }); describe('reset() events', () => { let g: FormGroup, c2: FormControl, logger: any[]; beforeEach(() => { c2 = new FormControl('two'); g = new FormGroup({'one': c, 'two': c2}); logger = []; }); it('should emit one valueChange event per reset control', () => { g.valueChanges.subscribe(() => logger.push('group')); c.valueChanges.subscribe(() => logger.push('control1')); c2.valueChanges.subscribe(() => logger.push('control2')); c.reset(); expect(logger).toEqual(['control1', 'group']); }); it('should not fire an event when explicitly specified', fakeAsync(() => { g.valueChanges.subscribe((value) => { throw 'Should not happen'; }); c.valueChanges.subscribe((value) => { throw 'Should not happen'; }); c2.valueChanges.subscribe((value) => { throw 'Should not happen'; }); c.reset(null, {emitEvent: false}); tick(); })); it('should emit one statusChange event per reset control', () => { g.statusChanges.subscribe(() => logger.push('group')); c.statusChanges.subscribe(() => logger.push('control1')); c2.statusChanges.subscribe(() => logger.push('control2')); c.reset(); expect(logger).toEqual(['control1', 'group']); }); it('should emit one statusChange event per disabled control', () => { g.statusChanges.subscribe(() => logger.push('group')); c.statusChanges.subscribe(() => logger.push('control1')); c2.statusChanges.subscribe(() => logger.push('control2')); c.reset({value: null, disabled: true}); expect(logger).toEqual(['control1', 'group']); }); }); });
it('should not fire an event when explicitly specified', fakeAsync(() => { c.valueChanges.subscribe((value) => { throw 'Should not happen'; }); c.setValue('newValue', {emitEvent: false}); tick(); }));
it('should set the value of the control', () => { c.patchValue('newValue'); expect(c.value).toEqual('newValue'); });
it('should rerun the validator when the value changes', () => { const c = new FormControl('value', Validators.required); c.setValue(null); expect(c.valid).toEqual(false); });
it('should be true after changing the value of the control', () => { const c = new FormControl('value'); c.markAsDirty(); expect(c.dirty).toEqual(true); });
it('should unset error when values are equal', () => { y.setErrors({ match: true }); CdValidators.match('x', 'y')(form); formHelper.expectValid('x'); formHelper.expectValid('y'); });
it('should keep other existing errors', () => { y.setErrors({ match: true, notUnique: true }); CdValidators.match('x', 'y')(form); formHelper.expectValid('x'); formHelper.expectError('y', 'notUnique'); });
ngAfterViewInit(): void { this.control.updateValueAndValidity(this.value || undefined); super.ngAfterViewInit(); }