【发布时间】:2016-04-15 11:26:01
【问题描述】:
我正在尝试围绕文本输入测试包装器组件。
我的组件如下所示:
@Component({
selector: 'my-textbox',
inputs: ['inputModel', 'label'],
outputs: ['inputModelChange'],
template: `
<div ngForm class="field">
<label>{{ label }}</label>
<input type="text" ngControl="{{ control }}" #item="ngForm" [ngModel]="inputModel" (ngModelChange)="setTextValue($event)">
</div>
`
})
export class IslTextboxComponent {
control;
inputModel: Object;
inputModelChange: EventEmitter<any> = new EventEmitter();
label: string;
constructor() {
this.control = 'text'+ Math.floor(Math.random()*11);
}
setTextValue(newValue): void {
this.inputModel = newValue;
this.inputModelChange.emit(newValue);
}
}
这是我的测试:
describe('My Textbox Component', () => {
it('should show modify input model',
injectAsync([TestComponentBuilder], (tcb) => {
return tcb
.createAsync(MyTextboxComponent)
.then((fixture) => {
let nativeElement = fixture.nativeElement;
fixture.detectChanges();
let input = nativeElement.querySelector('input');
input.value = 'VALUE_TO_TEST';
//???
input.dispatchEvent(new Event('input'));
fixture.detectChanges();
expect(nativeElement.inputModel).toBe('VALUE_TO_TEST');
});
}));
});
我想在文本框中输入文本并让 inputModel 自行更新,但没有触发事件。如何使 ngModelChange 事件触发? inputModel 永远不会更新...
【问题讨论】:
标签: angular