mrk*_*nic 3 jasmine angular angular-reactive-forms
我在 Angular 5 中有一个反应式形式,它正在工作。该表单有 eq 一个输入字段和一个复选框。
<div class="form-group" *ngIf="!myForm.controls.myCheckbox.value">
<input class="form-control" formControlName="myField"
</div>
<div class="form-group">
<input type="checkbox" formControlName="myCheckbox">
</div>
Run Code Online (Sandbox Code Playgroud)
复选框用于处理输入字段的可见性:如果选中的输入字段是不可见的,反之亦然。
我正在使用 TestBed 使用 jasmine 编写测试来配置 componentFixture。
我尝试获取 formControl 并检查是否存在,例如使用以下语句,但它不起作用。
let myField= component.driverForm.controls['myField'];
let myCheckbox= component.driverForm.controls['myCheckbox'];
myCheckbox.setValue(true);
fixture.detectChanges();
expect(myField).toBeFalsy("myField not existing");
Run Code Online (Sandbox Code Playgroud)
预期 FormControl( ... ) 为假“myField 不存在”。
我现在的问题是,当单击茉莉花测试中的复选框时,如何测试表单控件“myField”是否可见/不可见?
好吧,我找到了自己的解决方案。首先我需要通过以下方式获取元素:
const el = fixture.debugElement.nativeElement;
let myField= el.querySelector('input[formControlName=myField]');
expect(myField).toBeTruthy();
let myCheckboxControl= component.driverForm.controls['myCheckbox'];
expect(myCheckboxControl.value).toEqual(false);
//set checkbox state to true
myCheckboxControl.setValue(true);
fixture.detectChanges();
Run Code Online (Sandbox Code Playgroud)
然后我需要在更新复选框后再次获取输入字段:
myField = el.querySelector('input[formControlName=myField]');
expect(myField).toBeNull("myField not existing");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9348 次 |
| 最近记录: |