【发布时间】:2021-06-28 08:17:26
【问题描述】:
我正在尝试单元测试(Karma/Jasmine Angular 9.15)当输入被填充(非空或空)时,选择的禁用属性从 true 变为 false。 Jasmine 似乎没有检测到 de 变化,因为当我模拟输入的值变化时,我没有通过处理布尔值“禁用”的函数。
所以我想知道为什么它没有检测到更改,并且由于我没有找到有效的解决方案,所以我质疑我的测试策略:我做对了吗?
代码如下:
filter.spec.ts
fdescribe('FilterComponent', () => {
let component: FilterComponent;
let fixture: ComponentFixture<FilterComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ FilterComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(FilterComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should able the select when input is filled', () => {
let inputFilter = fixture.debugElement.nativeElement.querySelector('#my-filter');
inputFilter.value = "testing";
inputFilter.dispatchEvent(new Event('input'));
fixture.detectChanges();
fixture.whenStable().then(fix=>{
expect(fixture.debugElement.nativeElement.querySelector('#my-
select').disabled).toBeFalse();
});
});
filter.component.html
<!-- Filter -->
<div class="flex-container-h flex-align-center flex-nowrap mlm">
<label class="mt-label txtc-15 mrs" for="my-filter">Contract number</label>
<div class="flex-item-fluid">
<input [(ngModel)]="contractFilter" id="my-filter" type="text"
class="form-control">
</div>
</div>
<!-- Filtre select-->
<div class="flex-container-h flex-align-center flex-nowrap mlm">
<label class="mt-label txtc-15 mrs" for="my-select">Statut</label>
<div class="flex-item-fluid">
<select id="my-select" name="statuts" [disabled]="isSelectDisabled()">
<option value="" selected disabled hidden>Select an option</option>
<option *ngFor="let status of statusesList" [value]="status.code">{{status.libelle}}</option>
</select>
</div>
</div>
filter.component.ts
public isSelectDisabled(): boolean {
return (!this.contractFilter || this.contractFilter === "");
}
感谢阅读
【问题讨论】: