| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, inject,} from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-05-01 22:50:37 -07:00
										 |  |  | import {fakeAsync, flushMicrotasks, Log, tick} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {AsyncTestCompleter} from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-06-10 11:15:59 -07:00
										 |  |  | import {ControlGroup, Control, ControlArray, Validators} from '@angular/common/src/forms-deprecated'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {IS_DART, isPresent} from '../../src/facade/lang'; | 
					
						
							|  |  |  | import {PromiseWrapper} from '../../src/facade/promise'; | 
					
						
							|  |  |  | import {TimerWrapper, ObservableWrapper, EventEmitter} from '../../src/facade/async'; | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |   function asyncValidator(expected: any /** TODO #9100 */, timeouts = /*@ts2dart_const*/ {}) { | 
					
						
							|  |  |  |     return (c: any /** TODO #9100 */) => { | 
					
						
							| 
									
										
										
										
											2015-11-02 10:00:42 -08:00
										 |  |  |       var completer = PromiseWrapper.completer(); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       var t = isPresent((timeouts as any /** TODO #9100 */)[c.value]) ? | 
					
						
							|  |  |  |           (timeouts as any /** TODO #9100 */)[c.value] : | 
					
						
							|  |  |  |           0; | 
					
						
							|  |  |  |       var res = c.value != expected ? {'async': true} : null; | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (t == 0) { | 
					
						
							| 
									
										
										
										
											2015-11-02 10:00:42 -08:00
										 |  |  |         completer.resolve(res); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2015-11-02 10:00:42 -08:00
										 |  |  |         TimerWrapper.setTimeout(() => { completer.resolve(res); }, t); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-11-02 10:00:42 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       return completer.promise; | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |   function asyncValidatorReturningObservable(c: any /** TODO #9100 */) { | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |     var e = new EventEmitter(); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     PromiseWrapper.scheduleMicrotask(() => ObservableWrapper.callEmit(e, {'async': true})); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |     return e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   describe('Form Model', () => { | 
					
						
							|  |  |  |     describe('Control', () => { | 
					
						
							|  |  |  |       it('should default the value to null', () => { | 
					
						
							| 
									
										
										
										
											2015-07-16 18:34:03 -07:00
										 |  |  |         var c = new Control(); | 
					
						
							|  |  |  |         expect(c.value).toBe(null); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('validator', () => { | 
					
						
							|  |  |  |         it('should run validator with the initial value', () => { | 
					
						
							|  |  |  |           var c = new Control('value', Validators.required); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:10:27 -08:00
										 |  |  |           expect(c.valid).toEqual(true); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should rerun the validator when the value changes', () => { | 
					
						
							|  |  |  |           var c = new Control('value', Validators.required); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:10:27 -08:00
										 |  |  |           c.updateValue(null); | 
					
						
							|  |  |  |           expect(c.valid).toEqual(false); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should return errors', () => { | 
					
						
							| 
									
										
										
										
											2015-03-19 14:21:40 -07:00
										 |  |  |           var c = new Control(null, Validators.required); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(c.errors).toEqual({'required': true}); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:10:27 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-19 10:51:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('asyncValidator', () => { | 
					
						
							|  |  |  |         it('should run validator with the initial value', fakeAsync(() => { | 
					
						
							|  |  |  |              var c = new Control('value', null, asyncValidator('expected')); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |              tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(c.valid).toEqual(false); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              expect(c.errors).toEqual({'async': true}); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should support validators returning observables', fakeAsync(() => { | 
					
						
							|  |  |  |              var c = new Control('value', null, asyncValidatorReturningObservable); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |              tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(c.valid).toEqual(false); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              expect(c.errors).toEqual({'async': true}); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should rerun the validator when the value changes', fakeAsync(() => { | 
					
						
							|  |  |  |              var c = new Control('value', null, asyncValidator('expected')); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c.updateValue('expected'); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |              tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(c.valid).toEqual(true); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should run the async validator only when the sync validator passes', fakeAsync(() => { | 
					
						
							|  |  |  |              var c = new Control('', Validators.required, asyncValidator('expected')); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |              tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              expect(c.errors).toEqual({'required': true}); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c.updateValue('some value'); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |              tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              expect(c.errors).toEqual({'async': true}); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should mark the control as pending while running the async validation', | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |            fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              var c = new Control('', null, asyncValidator('expected')); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |              expect(c.pending).toEqual(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(c.pending).toEqual(false); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should only use the latest async validation run', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |              var c = | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |                  new Control('', null, asyncValidator('expected', {'long': 200, 'expected': 100})); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c.updateValue('long'); | 
					
						
							|  |  |  |              c.updateValue('expected'); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |              tick(300); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(c.valid).toEqual(true); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('dirty', () => { | 
					
						
							|  |  |  |         it('should be false after creating a control', () => { | 
					
						
							|  |  |  |           var c = new Control('value'); | 
					
						
							| 
									
										
										
										
											2015-03-19 10:51:49 -07:00
										 |  |  |           expect(c.dirty).toEqual(false); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should be true after changing the value of the control', () => { | 
					
						
							|  |  |  |           var c = new Control('value'); | 
					
						
							| 
									
										
										
										
											2015-06-03 11:54:39 -07:00
										 |  |  |           c.markAsDirty(); | 
					
						
							| 
									
										
										
										
											2015-03-19 10:51:49 -07:00
										 |  |  |           expect(c.dirty).toEqual(true); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('updateValue', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |         var g: any /** TODO #9100 */, c: any /** TODO #9100 */; | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  |         beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c = new Control('oldValue'); | 
					
						
							|  |  |  |           g = new ControlGroup({'one': c}); | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should update the value of the control', () => { | 
					
						
							|  |  |  |           c.updateValue('newValue'); | 
					
						
							|  |  |  |           expect(c.value).toEqual('newValue'); | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should invoke ngOnChanges if it is present', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |           var ngOnChanges: any /** TODO #9100 */; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c.registerOnChange((v: any /** TODO #9100 */) => ngOnChanges = ['invoked', v]); | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c.updateValue('newValue'); | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(ngOnChanges).toEqual(['invoked', 'newValue']); | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should not invoke on change when explicitly specified', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |           var onChange: any /** TODO #9100 */ = null; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c.registerOnChange((v: any /** TODO #9100 */) => onChange = ['invoked', v]); | 
					
						
							| 
									
										
										
										
											2015-07-15 18:16:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c.updateValue('newValue', {emitModelToViewChange: false}); | 
					
						
							| 
									
										
										
										
											2015-07-15 18:16:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(onChange).toBeNull(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should update the parent', () => { | 
					
						
							|  |  |  |           c.updateValue('newValue'); | 
					
						
							|  |  |  |           expect(g.value).toEqual({'one': 'newValue'}); | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should not update the parent when explicitly specified', () => { | 
					
						
							|  |  |  |           c.updateValue('newValue', {onlySelf: true}); | 
					
						
							|  |  |  |           expect(g.value).toEqual({'one': 'oldValue'}); | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event', fakeAsync(() => { | 
					
						
							|  |  |  |              ObservableWrapper.subscribe( | 
					
						
							|  |  |  |                  c.valueChanges, (value) => { expect(value).toEqual('newValue'); }); | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c.updateValue('newValue'); | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  |              tick(); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should not fire an event when explicitly specified', fakeAsync(() => { | 
					
						
							|  |  |  |              ObservableWrapper.subscribe(c.valueChanges, (value) => { throw 'Should not happen'; }); | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c.updateValue('newValue', {emitEvent: false}); | 
					
						
							| 
									
										
										
										
											2015-06-01 10:41:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |              tick(); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('valueChanges & statusChanges', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |         var c: any /** TODO #9100 */; | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         beforeEach(() => { c = new Control('old', Validators.required); }); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event after the value has been updated', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |            inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |              ObservableWrapper.subscribe(c.valueChanges, (value) => { | 
					
						
							|  |  |  |                expect(c.value).toEqual('new'); | 
					
						
							|  |  |  |                expect(value).toEqual('new'); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c.updateValue('new'); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event after the status has been updated to invalid', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |              ObservableWrapper.subscribe(c.statusChanges, (status) => { | 
					
						
							|  |  |  |                expect(c.status).toEqual('INVALID'); | 
					
						
							|  |  |  |                expect(status).toEqual('INVALID'); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c.updateValue(''); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |              tick(); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event after the status has been updated to pending', fakeAsync(() => { | 
					
						
							|  |  |  |              var c = new Control('old', Validators.required, asyncValidator('expected')); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |              var log: any[] /** TODO #9100 */ = []; | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |              ObservableWrapper.subscribe(c.valueChanges, (value) => log.push(`value: '${value}'`)); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              ObservableWrapper.subscribe( | 
					
						
							|  |  |  |                  c.statusChanges, (status) => log.push(`status: '${status}'`)); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c.updateValue(''); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |              tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c.updateValue('nonEmpty'); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |              tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c.updateValue('expected'); | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |              tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(log).toEqual([ | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |                '' + | 
					
						
							|  |  |  |                    'value: \'\'', | 
					
						
							|  |  |  |                'status: \'INVALID\'', | 
					
						
							|  |  |  |                'value: \'nonEmpty\'', | 
					
						
							|  |  |  |                'status: \'PENDING\'', | 
					
						
							|  |  |  |                'status: \'INVALID\'', | 
					
						
							|  |  |  |                'value: \'expected\'', | 
					
						
							|  |  |  |                'status: \'PENDING\'', | 
					
						
							|  |  |  |                'status: \'VALID\'', | 
					
						
							| 
									
										
										
										
											2015-11-05 17:18:16 -08:00
										 |  |  |              ]); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-13 18:27:30 -07:00
										 |  |  |         // TODO: remove the if statement after making observable delivery sync
 | 
					
						
							|  |  |  |         if (!IS_DART) { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           it('should update set errors and status before emitting an event', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |              inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |                c.valueChanges.subscribe((value: any /** TODO #9100 */) => { | 
					
						
							| 
									
										
										
										
											2015-10-13 18:27:30 -07:00
										 |  |  |                  expect(c.valid).toEqual(false); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |                  expect(c.errors).toEqual({'required': true}); | 
					
						
							| 
									
										
										
										
											2015-10-13 18:27:30 -07:00
										 |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |                c.updateValue(''); | 
					
						
							| 
									
										
										
										
											2015-10-13 18:27:30 -07:00
										 |  |  |              })); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should return a cold observable', | 
					
						
							|  |  |  |            inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |              c.updateValue('will be ignored'); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |              ObservableWrapper.subscribe(c.valueChanges, (value) => { | 
					
						
							|  |  |  |                expect(value).toEqual('new'); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c.updateValue('new'); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('setErrors', () => { | 
					
						
							|  |  |  |         it('should set errors on a control', () => { | 
					
						
							|  |  |  |           var c = new Control('someValue'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c.setErrors({'someError': true}); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(c.valid).toEqual(false); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(c.errors).toEqual({'someError': true}); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should reset the errors and validity when the value changes', () => { | 
					
						
							|  |  |  |           var c = new Control('someValue', Validators.required); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c.setErrors({'someError': true}); | 
					
						
							|  |  |  |           c.updateValue(''); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(c.errors).toEqual({'required': true}); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should update the parent group\'s validity', () => { | 
					
						
							|  |  |  |           var c = new Control('someValue'); | 
					
						
							|  |  |  |           var g = new ControlGroup({'one': c}); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(g.valid).toEqual(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c.setErrors({'someError': true}); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(g.valid).toEqual(false); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should not reset parent\'s errors', () => { | 
					
						
							|  |  |  |           var c = new Control('someValue'); | 
					
						
							|  |  |  |           var g = new ControlGroup({'one': c}); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           g.setErrors({'someGroupError': true}); | 
					
						
							|  |  |  |           c.setErrors({'someError': true}); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(g.errors).toEqual({'someGroupError': true}); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should reset errors when updating a value', () => { | 
					
						
							|  |  |  |           var c = new Control('oldValue'); | 
					
						
							|  |  |  |           var g = new ControlGroup({'one': c}); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           g.setErrors({'someGroupError': true}); | 
					
						
							|  |  |  |           c.setErrors({'someError': true}); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c.updateValue('newValue'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(c.errors).toEqual(null); | 
					
						
							|  |  |  |           expect(g.errors).toEqual(null); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     describe('ControlGroup', () => { | 
					
						
							|  |  |  |       describe('value', () => { | 
					
						
							|  |  |  |         it('should be the reduced value of the child controls', () => { | 
					
						
							|  |  |  |           var g = new ControlGroup({'one': new Control('111'), 'two': new Control('222')}); | 
					
						
							|  |  |  |           expect(g.value).toEqual({'one': '111', 'two': '222'}); | 
					
						
							| 
									
										
										
										
											2015-02-03 07:27:09 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-02-11 11:10:31 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should be empty when there are no child controls', () => { | 
					
						
							| 
									
										
										
										
											2015-02-25 15:10:27 -08:00
										 |  |  |           var g = new ControlGroup({}); | 
					
						
							|  |  |  |           expect(g.value).toEqual({}); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should support nested groups', () => { | 
					
						
							| 
									
										
										
										
											2015-06-03 13:42:57 -07:00
										 |  |  |           var g = new ControlGroup( | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |               {'one': new Control('111'), 'nested': new ControlGroup({'two': new Control('222')})}); | 
					
						
							|  |  |  |           expect(g.value).toEqual({'one': '111', 'nested': {'two': '222'}}); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           (<Control>(g.controls['nested'].find('two'))).updateValue('333'); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:10:27 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(g.value).toEqual({'one': '111', 'nested': {'two': '333'}}); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:10:27 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('adding and removing controls', () => { | 
					
						
							|  |  |  |         it('should update value and validity when control is added', () => { | 
					
						
							|  |  |  |           var g = new ControlGroup({'one': new Control('1')}); | 
					
						
							|  |  |  |           expect(g.value).toEqual({'one': '1'}); | 
					
						
							| 
									
										
										
										
											2016-06-08 14:06:20 -07:00
										 |  |  |           expect(g.valid).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           g.addControl('two', new Control('2', Validators.minLength(10))); | 
					
						
							| 
									
										
										
										
											2016-06-08 14:06:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(g.value).toEqual({'one': '1', 'two': '2'}); | 
					
						
							| 
									
										
										
										
											2016-06-08 14:06:20 -07:00
										 |  |  |           expect(g.valid).toBe(false); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should update value and validity when control is removed', () => { | 
					
						
							|  |  |  |           var g = new ControlGroup( | 
					
						
							|  |  |  |               {'one': new Control('1'), 'two': new Control('2', Validators.minLength(10))}); | 
					
						
							|  |  |  |           expect(g.value).toEqual({'one': '1', 'two': '2'}); | 
					
						
							| 
									
										
										
										
											2016-06-08 14:06:20 -07:00
										 |  |  |           expect(g.valid).toBe(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           g.removeControl('two'); | 
					
						
							| 
									
										
										
										
											2016-06-08 14:06:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(g.value).toEqual({'one': '1'}); | 
					
						
							| 
									
										
										
										
											2016-06-08 14:06:20 -07:00
										 |  |  |           expect(g.valid).toBe(true); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('errors', () => { | 
					
						
							|  |  |  |         it('should run the validator when the value changes', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |           var simpleValidator = (c: any /** TODO #9100 */) => | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |               c.controls['one'].value != 'correct' ? {'broken': true} : null; | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           var c = new Control(null); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           var g = new ControlGroup({'one': c}, null, simpleValidator); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c.updateValue('correct'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 15:10:27 -08:00
										 |  |  |           expect(g.valid).toEqual(true); | 
					
						
							|  |  |  |           expect(g.errors).toEqual(null); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c.updateValue('incorrect'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(g.valid).toEqual(false); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(g.errors).toEqual({'broken': true}); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:10:27 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('dirty', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |         var c: any /** TODO #9100 */, g: any /** TODO #9100 */; | 
					
						
							| 
									
										
										
										
											2015-03-19 10:51:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 11:54:39 -07:00
										 |  |  |         beforeEach(() => { | 
					
						
							|  |  |  |           c = new Control('value'); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           g = new ControlGroup({'one': c}); | 
					
						
							| 
									
										
										
										
											2015-06-03 11:54:39 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should be false after creating a control', () => { expect(g.dirty).toEqual(false); }); | 
					
						
							| 
									
										
										
										
											2015-03-19 10:51:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should be false after changing the value of the control', () => { | 
					
						
							| 
									
										
										
										
											2015-06-03 11:54:39 -07:00
										 |  |  |           c.markAsDirty(); | 
					
						
							| 
									
										
										
										
											2015-03-19 10:51:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 11:54:39 -07:00
										 |  |  |           expect(g.dirty).toEqual(true); | 
					
						
							| 
									
										
										
										
											2015-03-19 10:51:49 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('optional components', () => { | 
					
						
							|  |  |  |         describe('contains', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |           var group: any /** TODO #9100 */; | 
					
						
							| 
									
										
										
										
											2015-03-10 18:12:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |             group = new ControlGroup( | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |                   'required': new Control('requiredValue'), | 
					
						
							|  |  |  |                   'optional': new Control('optionalValue') | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |                 }, | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |                 {'optional': false}); | 
					
						
							| 
									
										
										
										
											2015-03-10 18:12:30 -07:00
										 |  |  |           }); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 18:12:30 -07:00
										 |  |  |           // rename contains into has
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           it('should return false when the component is not included', | 
					
						
							|  |  |  |              () => { expect(group.contains('optional')).toEqual(false); }) | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           it('should return false when there is no component with the given name', | 
					
						
							|  |  |  |              () => { expect(group.contains('something else')).toEqual(false); }); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           it('should return true when the component is included', () => { | 
					
						
							|  |  |  |             expect(group.contains('required')).toEqual(true); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |             group.include('optional'); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |             expect(group.contains('optional')).toEqual(true); | 
					
						
							| 
									
										
										
										
											2015-03-10 18:12:30 -07:00
										 |  |  |           }); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:10:27 -08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should not include an inactive component into the group value', () => { | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |           var group = new ControlGroup( | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |               {'required': new Control('requiredValue'), 'optional': new Control('optionalValue')}, | 
					
						
							|  |  |  |               {'optional': false}); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(group.value).toEqual({'required': 'requiredValue'}); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           group.include('optional'); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(group.value).toEqual({'required': 'requiredValue', 'optional': 'optionalValue'}); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:10:27 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should not run Validators on an inactive component', () => { | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |           var group = new ControlGroup( | 
					
						
							|  |  |  |               { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |                 'required': new Control('requiredValue', Validators.required), | 
					
						
							|  |  |  |                 'optional': new Control('', Validators.required) | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  |               }, | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |               {'optional': false}); | 
					
						
							| 
									
										
										
										
											2015-03-10 18:12:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(group.valid).toEqual(true); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           group.include('optional'); | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 18:12:30 -07:00
										 |  |  |           expect(group.valid).toEqual(false); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-24 13:45:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('valueChanges', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |         var g: any /** TODO #9100 */, c1: any /** TODO #9100 */, c2: any /** TODO #9100 */; | 
					
						
							| 
									
										
										
										
											2015-03-24 13:45:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |         beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c1 = new Control('old1'); | 
					
						
							|  |  |  |           c2 = new Control('old2'); | 
					
						
							|  |  |  |           g = new ControlGroup({'one': c1, 'two': c2}, {'two': true}); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-24 13:45:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event after the value has been updated', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |            inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |              ObservableWrapper.subscribe(g.valueChanges, (value) => { | 
					
						
							|  |  |  |                expect(g.value).toEqual({'one': 'new1', 'two': 'old2'}); | 
					
						
							|  |  |  |                expect(value).toEqual({'one': 'new1', 'two': 'old2'}); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c1.updateValue('new1'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event after the control\'s observable fired an event', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |            inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |              var controlCallbackIsCalled = false; | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              ObservableWrapper.subscribe( | 
					
						
							|  |  |  |                  c1.valueChanges, (value) => { controlCallbackIsCalled = true; }); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |              ObservableWrapper.subscribe(g.valueChanges, (value) => { | 
					
						
							|  |  |  |                expect(controlCallbackIsCalled).toBe(true); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c1.updateValue('new1'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event when a control is excluded', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |            inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |              ObservableWrapper.subscribe(g.valueChanges, (value) => { | 
					
						
							|  |  |  |                expect(value).toEqual({'one': 'old1'}); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              g.exclude('two'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event when a control is included', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |            inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              g.exclude('two'); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |              ObservableWrapper.subscribe(g.valueChanges, (value) => { | 
					
						
							|  |  |  |                expect(value).toEqual({'one': 'old1', 'two': 'old2'}); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              g.include('two'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event every time a control is updated', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |            inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |              var loggedValues: any[] /** TODO #9100 */ = []; | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |              ObservableWrapper.subscribe(g.valueChanges, (value) => { | 
					
						
							|  |  |  |                loggedValues.push(value); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |                if (loggedValues.length == 2) { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |                  expect(loggedValues).toEqual([ | 
					
						
							|  |  |  |                    {'one': 'new1', 'two': 'old2'}, {'one': 'new1', 'two': 'new2'} | 
					
						
							|  |  |  |                  ]); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |                  async.done(); | 
					
						
							|  |  |  |                } | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c1.updateValue('new1'); | 
					
						
							|  |  |  |              c2.updateValue('new2'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         xit('should not fire an event when an excluded control is updated', | 
					
						
							|  |  |  |             inject( | 
					
						
							|  |  |  |                 [AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |                                           // hard to test without hacking zones
 | 
					
						
							|  |  |  |                                       })); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('getError', () => { | 
					
						
							|  |  |  |         it('should return the error when it is present', () => { | 
					
						
							|  |  |  |           var c = new Control('', Validators.required); | 
					
						
							|  |  |  |           var g = new ControlGroup({'one': c}); | 
					
						
							|  |  |  |           expect(c.getError('required')).toEqual(true); | 
					
						
							|  |  |  |           expect(g.getError('required', ['one'])).toEqual(true); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should return null otherwise', () => { | 
					
						
							|  |  |  |           var c = new Control('not empty', Validators.required); | 
					
						
							|  |  |  |           var g = new ControlGroup({'one': c}); | 
					
						
							|  |  |  |           expect(c.getError('invalid')).toEqual(null); | 
					
						
							|  |  |  |           expect(g.getError('required', ['one'])).toEqual(null); | 
					
						
							|  |  |  |           expect(g.getError('required', ['invalid'])).toEqual(null); | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('asyncValidator', () => { | 
					
						
							|  |  |  |         it('should run the async validator', fakeAsync(() => { | 
					
						
							|  |  |  |              var c = new Control('value'); | 
					
						
							|  |  |  |              var g = new ControlGroup({'one': c}, null, null, asyncValidator('expected')); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |              expect(g.pending).toEqual(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tick(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              expect(g.errors).toEqual({'async': true}); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |              expect(g.pending).toEqual(false); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should set the parent group\'s status to pending', fakeAsync(() => { | 
					
						
							|  |  |  |              var c = new Control('value', null, asyncValidator('expected')); | 
					
						
							|  |  |  |              var g = new ControlGroup({'one': c}); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |              expect(g.pending).toEqual(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tick(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(g.pending).toEqual(false); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should run the parent group\'s async validator when children are pending', | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |            fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              var c = new Control('value', null, asyncValidator('expected')); | 
					
						
							|  |  |  |              var g = new ControlGroup({'one': c}, null, null, asyncValidator('expected')); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |              tick(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              expect(g.errors).toEqual({'async': true}); | 
					
						
							|  |  |  |              expect(g.find(['one']).errors).toEqual({'async': true}); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |            })); | 
					
						
							|  |  |  |       }) | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     describe('ControlArray', () => { | 
					
						
							|  |  |  |       describe('adding/removing', () => { | 
					
						
							| 
									
										
										
										
											2015-06-11 18:50:41 -07:00
										 |  |  |         var a: ControlArray; | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |         var c1: any /** TODO #9100 */, c2: any /** TODO #9100 */, c3: any /** TODO #9100 */; | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |         beforeEach(() => { | 
					
						
							|  |  |  |           a = new ControlArray([]); | 
					
						
							|  |  |  |           c1 = new Control(1); | 
					
						
							|  |  |  |           c2 = new Control(2); | 
					
						
							|  |  |  |           c3 = new Control(3); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should support pushing', () => { | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |           a.push(c1); | 
					
						
							|  |  |  |           expect(a.length).toEqual(1); | 
					
						
							|  |  |  |           expect(a.controls).toEqual([c1]); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should support removing', () => { | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |           a.push(c1); | 
					
						
							|  |  |  |           a.push(c2); | 
					
						
							|  |  |  |           a.push(c3); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |           a.removeAt(1); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |           expect(a.controls).toEqual([c1, c3]); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should support inserting', () => { | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |           a.push(c1); | 
					
						
							|  |  |  |           a.push(c3); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |           a.insert(1, c2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(a.controls).toEqual([c1, c2, c3]); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('value', () => { | 
					
						
							|  |  |  |         it('should be the reduced value of the child controls', () => { | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |           var a = new ControlArray([new Control(1), new Control(2)]); | 
					
						
							|  |  |  |           expect(a.value).toEqual([1, 2]); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should be an empty array when there are no child controls', () => { | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |           var a = new ControlArray([]); | 
					
						
							|  |  |  |           expect(a.value).toEqual([]); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('errors', () => { | 
					
						
							|  |  |  |         it('should run the validator when the value changes', () => { | 
					
						
							|  |  |  |           var simpleValidator = (c: any /** TODO #9100 */) => | 
					
						
							|  |  |  |               c.controls[0].value != 'correct' ? {'broken': true} : null; | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           var c = new Control(null); | 
					
						
							|  |  |  |           var g = new ControlArray([c], simpleValidator); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c.updateValue('correct'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(g.valid).toEqual(true); | 
					
						
							|  |  |  |           expect(g.errors).toEqual(null); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c.updateValue('incorrect'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(g.valid).toEqual(false); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(g.errors).toEqual({'broken': true}); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('dirty', () => { | 
					
						
							| 
									
										
										
										
											2015-06-11 18:50:41 -07:00
										 |  |  |         var c: Control; | 
					
						
							|  |  |  |         var a: ControlArray; | 
					
						
							| 
									
										
										
										
											2015-06-03 11:54:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |         beforeEach(() => { | 
					
						
							|  |  |  |           c = new Control('value'); | 
					
						
							|  |  |  |           a = new ControlArray([c]); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should be false after creating a control', () => { expect(a.dirty).toEqual(false); }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should be false after changing the value of the control', () => { | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |           c.markAsDirty(); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |           expect(a.dirty).toEqual(true); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-10-16 12:04:33 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('pending', () => { | 
					
						
							| 
									
										
										
										
											2015-10-16 12:04:33 -07:00
										 |  |  |         var c: Control; | 
					
						
							|  |  |  |         var a: ControlArray; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         beforeEach(() => { | 
					
						
							|  |  |  |           c = new Control('value'); | 
					
						
							|  |  |  |           a = new ControlArray([c]); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should be false after creating a control', () => { | 
					
						
							| 
									
										
										
										
											2015-10-26 11:50:30 -07:00
										 |  |  |           expect(c.pending).toEqual(false); | 
					
						
							|  |  |  |           expect(a.pending).toEqual(false); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-10-16 12:04:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should be true after changing the value of the control', () => { | 
					
						
							| 
									
										
										
										
											2015-10-16 12:04:33 -07:00
										 |  |  |           c.markAsPending(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 11:50:30 -07:00
										 |  |  |           expect(c.pending).toEqual(true); | 
					
						
							| 
									
										
										
										
											2015-10-16 12:04:33 -07:00
										 |  |  |           expect(a.pending).toEqual(true); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-10-26 11:50:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should not update the parent when onlySelf = true', () => { | 
					
						
							| 
									
										
										
										
											2015-10-26 11:50:30 -07:00
										 |  |  |           c.markAsPending({onlySelf: true}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expect(c.pending).toEqual(true); | 
					
						
							|  |  |  |           expect(a.pending).toEqual(false); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('valueChanges', () => { | 
					
						
							| 
									
										
										
										
											2015-06-11 18:50:41 -07:00
										 |  |  |         var a: ControlArray; | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |         var c1: any /** TODO #9100 */, c2: any /** TODO #9100 */; | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |         beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           c1 = new Control('old1'); | 
					
						
							|  |  |  |           c2 = new Control('old2'); | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |           a = new ControlArray([c1, c2]); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event after the value has been updated', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |            inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |              ObservableWrapper.subscribe(a.valueChanges, (value) => { | 
					
						
							|  |  |  |                expect(a.value).toEqual(['new1', 'old2']); | 
					
						
							|  |  |  |                expect(value).toEqual(['new1', 'old2']); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c1.updateValue('new1'); | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event after the control\'s observable fired an event', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |            inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |              var controlCallbackIsCalled = false; | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              ObservableWrapper.subscribe( | 
					
						
							|  |  |  |                  c1.valueChanges, (value) => { controlCallbackIsCalled = true; }); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |              ObservableWrapper.subscribe(a.valueChanges, (value) => { | 
					
						
							|  |  |  |                expect(controlCallbackIsCalled).toBe(true); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              c1.updateValue('new1'); | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event when a control is removed', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |            inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |              ObservableWrapper.subscribe(a.valueChanges, (value) => { | 
					
						
							|  |  |  |                expect(value).toEqual(['old1']); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |              a.removeAt(1); | 
					
						
							|  |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should fire an event when a control is added', | 
					
						
							|  |  |  |            inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |              a.removeAt(1); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |              ObservableWrapper.subscribe(a.valueChanges, (value) => { | 
					
						
							|  |  |  |                expect(value).toEqual(['old1', 'old2']); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:32:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  |              a.push(c2); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('find', () => { | 
					
						
							|  |  |  |         it('should return null when path is null', () => { | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |           var g = new ControlGroup({}); | 
					
						
							|  |  |  |           expect(g.find(null)).toEqual(null); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should return null when path is empty', () => { | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |           var g = new ControlGroup({}); | 
					
						
							|  |  |  |           expect(g.find([])).toEqual(null); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should return null when path is invalid', () => { | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |           var g = new ControlGroup({}); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(g.find(['one', 'two'])).toEqual(null); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should return a child of a control group', () => { | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |           var g = new ControlGroup( | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |               {'one': new Control('111'), 'nested': new ControlGroup({'two': new Control('222')})}); | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(g.find(['nested', 'two']).value).toEqual('222'); | 
					
						
							|  |  |  |           expect(g.find(['one']).value).toEqual('111'); | 
					
						
							|  |  |  |           expect(g.find('nested/two').value).toEqual('222'); | 
					
						
							|  |  |  |           expect(g.find('one').value).toEqual('111'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         it('should return an element of an array', () => { | 
					
						
							|  |  |  |           var g = new ControlGroup({'array': new ControlArray([new Control('111')])}); | 
					
						
							| 
									
										
										
										
											2015-06-05 14:28:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |           expect(g.find(['array', 0]).value).toEqual('111'); | 
					
						
							| 
									
										
										
										
											2015-10-27 11:20:07 -07:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-03-25 10:51:05 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       describe('asyncValidator', () => { | 
					
						
							|  |  |  |         it('should run the async validator', fakeAsync(() => { | 
					
						
							|  |  |  |              var c = new Control('value'); | 
					
						
							|  |  |  |              var g = new ControlArray([c], null, asyncValidator('expected')); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |              expect(g.pending).toEqual(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tick(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              expect(g.errors).toEqual({'async': true}); | 
					
						
							| 
									
										
										
										
											2015-10-29 17:45:15 -07:00
										 |  |  |              expect(g.pending).toEqual(false); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  |       }) | 
					
						
							| 
									
										
										
										
											2015-02-24 11:59:10 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:10:27 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-03-26 17:32:48 +01:00
										 |  |  | } |