| 
									
										
										
										
											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-03-02 12:12:46 -08:00
										 |  |  | import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-09-09 12:04:38 -07:00
										 |  |  | import {FormBuilder} from '@angular/forms'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should use controls', () => { | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							|  |  |  |       const a = b.array( | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           ['one', ['two', syncValidator], c, b.array(['four'])], syncValidator, asyncValidator); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:36:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(a.value).toEqual(['one', 'two', 'three', ['four']]); | 
					
						
							|  |  |  |       expect(a.validator).toBe(syncValidator); | 
					
						
							|  |  |  |       expect(a.asyncValidator).toBe(asyncValidator); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |