angular - 下拉选择角 5 的单元测试
问题描述
我正在尝试测试下拉选择,我能够获取 nativeElement 和选项,但我面临调度事件错误
一些未捕获事件的方式。
HTML
<select [(ngModel)]='selectedTemplate' class="gridDropdowns" name="compareDropdown" (ngModelChange)="onTemplateChange($event)">
<option *ngFor='let option of templateDropdown' [attr.id] = "option.text" [ngValue]="option.value" >{{option.text}}</option>
</select>
零件
public templateDropdown: Array<{ text: string, value: string }> =
[
{ text: 'Layer Summary - Final Share', value: 'final' },
{ text: 'Layer Summary - Authorized Share', value: 'authorize' },
{ text: 'Layer Summary - Quote Share', value: 'quote' },
{ text: 'PML Summary (100%)', value: 'pmlSummary' },
{ text: 'Metric Comp Summary', value: 'metricComp' }
];
onTemplateChange(event: any) {
console.log(event);
}
组件.spec.ts
it('Load corresponding data on Template dropdown change', async(() => {
fixture = TestBed.createComponent(CompareComponent);
component = fixture.componentInstance;
spyOn(component, 'onTemplateChange');
component.selectedTemplate= component.templateDropdown[1].value;
fixture.detectChanges();
const templateDropdown = fixture.debugElement.query(By.css('select'));
fixture.whenStable().then(() =>{
templateDropdown.nativeElement.value = 'Layer Summary - Authorized Share'
templateDropdown.nativeElement.dispatchEvent(new Event('change'));
fixture.detectChanges();
expect(component.onTemplateChange).toHaveBeenCalledWith('authorize')
})
}));
我得到的错误
我正在接受这个参考
解决方案
推荐阅读
- c++ - 如何在 MSVC 中抑制所有 3rd 方编译时警告
- python - Pycharm 中的机器人框架。变量的自动完成不起作用
- reactjs - 如何在 NextJS 中创建全局枚举
- google-maps - icCube:地理图表限制区域不起作用
- express - multer 文件上传 - 如何在路由中从 multer 获取值?
- excel - 如何正确使用 Excel VBA 中的 Like 函数?
- azure - 有没有更好的方法来导出 Azure DevOps 项目的模板(字段和状态)?
- macos - 尝试在 macOS 上安装 Qiskit 时超时
- angular - 如何将数据从一个 div 发送到另一个?
- regex - 无法使用 is text end by 设置正则表达式检查