| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-12-03 21:48:45 +01:00
										 |  |  | import {fakeAsync, tick} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testing_internal'; | 
					
						
							| 
									
										
										
										
											2018-06-20 19:21:46 +02:00
										 |  |  | import {FormBuilder, Validators} from '@angular/forms'; | 
					
						
							| 
									
										
										
										
											2018-02-27 17:06:06 -05:00
										 |  |  | import {of } from 'rxjs'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 15:10:54 -08:00
										 |  |  | (function() { | 
					
						
							| 
									
										
										
										
											2016-06-08 17:08:59 -07:00
										 |  |  |   function syncValidator(_: any /** TODO #9100 */): any /** TODO #9100 */ { return null; } | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |   function asyncValidator(_: any /** TODO #9100 */) { return Promise.resolve(null); } | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   describe('Form Builder', () => { | 
					
						
							| 
									
										
										
										
											2016-08-24 16:58:43 -07:00
										 |  |  |     let b: FormBuilder; | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { b = new FormBuilder(); }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should create controls from a value', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const g = b.group({'login': 'some value'}); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       expect(g.controls['login'].value).toEqual('some value'); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-24 16:58:43 -07:00
										 |  |  |     it('should create controls from a boxed value', () => { | 
					
						
							|  |  |  |       const g = b.group({'login': {value: 'some value', disabled: true}}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(g.controls['login'].value).toEqual('some value'); | 
					
						
							|  |  |  |       expect(g.controls['login'].disabled).toEqual(true); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should create controls from an array', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const g = b.group( | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           {'login': ['some value'], 'password': ['some value', syncValidator, asyncValidator]}); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       expect(g.controls['login'].value).toEqual('some value'); | 
					
						
							|  |  |  |       expect(g.controls['password'].value).toEqual('some value'); | 
					
						
							|  |  |  |       expect(g.controls['password'].validator).toEqual(syncValidator); | 
					
						
							|  |  |  |       expect(g.controls['password'].asyncValidator).toEqual(asyncValidator); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 20:02:39 +01:00
										 |  |  |     it('should use controls whose form state is a primitive value', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const g = b.group({'login': b.control('some value', syncValidator, asyncValidator)}); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       expect(g.controls['login'].value).toEqual('some value'); | 
					
						
							|  |  |  |       expect(g.controls['login'].validator).toBe(syncValidator); | 
					
						
							|  |  |  |       expect(g.controls['login'].asyncValidator).toBe(asyncValidator); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 19:17:49 +01:00
										 |  |  |     it('should support controls with no validators and whose form state is null', () => { | 
					
						
							|  |  |  |       const g = b.group({'login': b.control(null)}); | 
					
						
							|  |  |  |       expect(g.controls['login'].value).toBeNull(); | 
					
						
							|  |  |  |       expect(g.controls['login'].validator).toBeNull(); | 
					
						
							|  |  |  |       expect(g.controls['login'].asyncValidator).toBeNull(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should support controls with validators and whose form state is null', () => { | 
					
						
							|  |  |  |       const g = b.group({'login': b.control(null, syncValidator, asyncValidator)}); | 
					
						
							|  |  |  |       expect(g.controls['login'].value).toBeNull(); | 
					
						
							|  |  |  |       expect(g.controls['login'].validator).toBe(syncValidator); | 
					
						
							|  |  |  |       expect(g.controls['login'].asyncValidator).toBe(asyncValidator); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should support controls with no validators and whose form state is undefined', () => { | 
					
						
							|  |  |  |       const g = b.group({'login': b.control(undefined)}); | 
					
						
							|  |  |  |       expect(g.controls['login'].value).toBeNull(); | 
					
						
							|  |  |  |       expect(g.controls['login'].validator).toBeNull(); | 
					
						
							|  |  |  |       expect(g.controls['login'].asyncValidator).toBeNull(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should support controls with validators and whose form state is undefined', () => { | 
					
						
							|  |  |  |       const g = b.group({'login': b.control(undefined, syncValidator, asyncValidator)}); | 
					
						
							|  |  |  |       expect(g.controls['login'].value).toBeNull(); | 
					
						
							|  |  |  |       expect(g.controls['login'].validator).toBe(syncValidator); | 
					
						
							|  |  |  |       expect(g.controls['login'].asyncValidator).toBe(asyncValidator); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should create groups with a custom validator', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const g = b.group( | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           {'login': 'some value'}, {'validator': syncValidator, 'asyncValidator': asyncValidator}); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(g.validator).toBe(syncValidator); | 
					
						
							|  |  |  |       expect(g.asyncValidator).toBe(asyncValidator); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should create control arrays', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const c = b.control('three'); | 
					
						
							| 
									
										
										
										
											2017-12-09 19:17:49 +01:00
										 |  |  |       const e = b.control(null); | 
					
						
							|  |  |  |       const f = b.control(undefined); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const a = b.array( | 
					
						
							| 
									
										
										
										
											2017-12-09 19:17:49 +01:00
										 |  |  |           ['one', ['two', syncValidator], c, b.array(['four']), e, f], syncValidator, | 
					
						
							|  |  |  |           asyncValidator); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 19:17:49 +01:00
										 |  |  |       expect(a.value).toEqual(['one', 'two', 'three', ['four'], null, null]); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  |       expect(a.validator).toBe(syncValidator); | 
					
						
							|  |  |  |       expect(a.asyncValidator).toBe(asyncValidator); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-12-03 21:48:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should create control arrays with multiple async validators', fakeAsync(() => { | 
					
						
							|  |  |  |          function asyncValidator1() { return of ({'async1': true}); } | 
					
						
							|  |  |  |          function asyncValidator2() { return of ({'async2': true}); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          const a = b.array(['one', 'two'], null, [asyncValidator1, asyncValidator2]); | 
					
						
							|  |  |  |          expect(a.value).toEqual(['one', 'two']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          expect(a.errors).toEqual({'async1': true, 'async2': true}); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should create control arrays with multiple sync validators', () => { | 
					
						
							|  |  |  |       function syncValidator1() { return {'sync1': true}; } | 
					
						
							|  |  |  |       function syncValidator2() { return {'sync2': true}; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const a = b.array(['one', 'two'], [syncValidator1, syncValidator2]); | 
					
						
							|  |  |  |       expect(a.value).toEqual(['one', 'two']); | 
					
						
							|  |  |  |       expect(a.errors).toEqual({'sync1': true, 'sync2': true}); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-06-20 19:21:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     describe('updateOn', () => { | 
					
						
							|  |  |  |       it('should default to on change', () => { | 
					
						
							|  |  |  |         const c = b.control(''); | 
					
						
							|  |  |  |         expect(c.updateOn).toEqual('change'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should default to on change with an options obj', () => { | 
					
						
							|  |  |  |         const c = b.control('', {validators: Validators.required}); | 
					
						
							|  |  |  |         expect(c.updateOn).toEqual('change'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should set updateOn when updating on blur', () => { | 
					
						
							|  |  |  |         const c = b.control('', {updateOn: 'blur'}); | 
					
						
							|  |  |  |         expect(c.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       describe('in groups and arrays', () => { | 
					
						
							|  |  |  |         it('should default to group updateOn when not set in control', () => { | 
					
						
							|  |  |  |           const g = b.group({one: b.control(''), two: b.control('')}, {updateOn: 'blur'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(g.get('one') !.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |           expect(g.get('two') !.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should default to array updateOn when not set in control', () => { | 
					
						
							|  |  |  |           const a = b.array([b.control(''), b.control('')], {updateOn: 'blur'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(a.get([0]) !.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |           expect(a.get([1]) !.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should set updateOn with nested groups', () => { | 
					
						
							|  |  |  |           const g = b.group( | 
					
						
							|  |  |  |               { | 
					
						
							|  |  |  |                 group: b.group({one: b.control(''), two: b.control('')}), | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |               {updateOn: 'blur'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(g.get('group.one') !.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |           expect(g.get('group.two') !.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |           expect(g.get('group') !.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should set updateOn with nested arrays', () => { | 
					
						
							|  |  |  |           const g = b.group( | 
					
						
							|  |  |  |               { | 
					
						
							|  |  |  |                 arr: b.array([b.control(''), b.control('')]), | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |               {updateOn: 'blur'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(g.get(['arr', 0]) !.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |           expect(g.get(['arr', 1]) !.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |           expect(g.get('arr') !.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should allow control updateOn to override group updateOn', () => { | 
					
						
							|  |  |  |           const g = b.group( | 
					
						
							|  |  |  |               {one: b.control('', {updateOn: 'change'}), two: b.control('')}, {updateOn: 'blur'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(g.get('one') !.updateOn).toEqual('change'); | 
					
						
							|  |  |  |           expect(g.get('two') !.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should set updateOn with complex setup', () => { | 
					
						
							|  |  |  |           const g = b.group({ | 
					
						
							|  |  |  |             group: b.group( | 
					
						
							|  |  |  |                 {one: b.control('', {updateOn: 'change'}), two: b.control('')}, {updateOn: 'blur'}), | 
					
						
							|  |  |  |             groupTwo: b.group({one: b.control('')}, {updateOn: 'submit'}), | 
					
						
							|  |  |  |             three: b.control('') | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(g.get('group.one') !.updateOn).toEqual('change'); | 
					
						
							|  |  |  |           expect(g.get('group.two') !.updateOn).toEqual('blur'); | 
					
						
							|  |  |  |           expect(g.get('groupTwo.one') !.updateOn).toEqual('submit'); | 
					
						
							|  |  |  |           expect(g.get('three') !.updateOn).toEqual('change'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | })(); |