|
1 | | -import {Component, Directive, View} from 'angular2/angular2'; |
| 1 | +import {Component, Directive, View, Output, EventEmitter} from 'angular2/angular2'; |
2 | 2 | import { |
3 | 3 | RootTestComponent, |
4 | 4 | afterEach, |
@@ -32,6 +32,7 @@ import { |
32 | 32 | } from 'angular2/core'; |
33 | 33 | import {By} from 'angular2/src/core/debug'; |
34 | 34 | import {ListWrapper} from 'angular2/src/core/facade/collection'; |
| 35 | +import {ObservableWrapper} from 'angular2/src/core/facade/async'; |
35 | 36 |
|
36 | 37 | export function main() { |
37 | 38 | describe("integration tests", () => { |
@@ -366,6 +367,29 @@ export function main() { |
366 | 367 | async.done(); |
367 | 368 | }); |
368 | 369 | })); |
| 370 | + |
| 371 | + it("should support custom value accessors on non builtin input elements that fire a change event without a 'target' property", |
| 372 | + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { |
| 373 | + var t = `<div [ng-form-model]="form"> |
| 374 | + <my-input ng-control="name"></my-input> |
| 375 | + </div>`; |
| 376 | + |
| 377 | + tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((rootTC) => { |
| 378 | + rootTC.debugElement.componentInstance.form = |
| 379 | + new ControlGroup({"name": new Control("aa")}); |
| 380 | + rootTC.detectChanges(); |
| 381 | + var input = rootTC.debugElement.query(By.css("my-input")); |
| 382 | + expect(input.componentInstance.value).toEqual("!aa!"); |
| 383 | + |
| 384 | + input.componentInstance.value = "!bb!"; |
| 385 | + ObservableWrapper.subscribe(input.componentInstance.onChange, (value) => { |
| 386 | + expect(rootTC.debugElement.componentInstance.form.value).toEqual({"name": "bb"}); |
| 387 | + async.done(); |
| 388 | + }); |
| 389 | + input.componentInstance.dispatchChangeEvent(); |
| 390 | + }); |
| 391 | + })); |
| 392 | + |
369 | 393 | }); |
370 | 394 |
|
371 | 395 | describe("validations", () => { |
@@ -872,8 +896,26 @@ class WrappedValue implements ControlValueAccessor { |
872 | 896 | handleOnChange(value) { this.onChange(value.substring(1, value.length - 1)); } |
873 | 897 | } |
874 | 898 |
|
| 899 | +@Component({selector: "my-input", template: ''}) |
| 900 | +class MyInput implements ControlValueAccessor { |
| 901 | + @Output('change') onChange: EventEmitter = new EventEmitter(); |
| 902 | + value: string; |
| 903 | + |
| 904 | + constructor(cd: NgControl) { cd.valueAccessor = this; } |
| 905 | + |
| 906 | + writeValue(value) { this.value = `!${value}!`; } |
| 907 | + |
| 908 | + registerOnChange(fn) { ObservableWrapper.subscribe(this.onChange, fn); } |
| 909 | + |
| 910 | + registerOnTouched(fn) {} |
| 911 | + |
| 912 | + dispatchChangeEvent() { |
| 913 | + ObservableWrapper.callNext(this.onChange, this.value.substring(1, this.value.length - 1)); |
| 914 | + } |
| 915 | +} |
| 916 | + |
875 | 917 | @Component({selector: "my-comp"}) |
876 | | -@View({directives: [FORM_DIRECTIVES, WrappedValue, NgIf, NgFor]}) |
| 918 | +@View({directives: [FORM_DIRECTIVES, WrappedValue, MyInput, NgIf, NgFor]}) |
877 | 919 | class MyComp { |
878 | 920 | form: any; |
879 | 921 | name: string; |
|
0 commit comments