【发布时间】:2018-03-04 22:11:04
【问题描述】:
试图让我的选择菜单更改事件以正确的值正确触发。它似乎正在触发,但值没有改变。
我的组件中有这个选择菜单:
<select id="seasonDropDown" style="width:200px;height: 36px;" aria-label="Select a Season" (change)="changeSeason($event.target.value)">
<option *ngFor="let item of seasons" [value]="item.id" [selected]="item.id === seasonId">Season {{item.id}}</option>
</select>
我有这个更改事件:
public changeSeason(seasonId: number) {
this.test = 'blah'; //for testing sake
console.log('change season ' + seasonId)
this.seasonId = seasonId;
this.notify.emit(this.seasonId);
}
我已经尝试像下面的代码一样对其进行测试,但 commponent.seasonId 永远不会改变其默认值。它应该在 changeSeason 方法中进行更改。我知道该方法正在触发,因为当我测试 expect(component.test).toEqual('blah') 它会通过:
it('should emit new season on change event', fakeAsync(() => {
let select = fixture.debugElement.query(By.css('#seasonDropDown')).nativeElement;
select.value = 2;
select.selectedValue = 2;
fixture.detectChanges();
select.dispatchEvent(new Event('change'));
tick();
fixture.detectChanges();
expect(component.seasonId).toEqual(2);
//expect(component.test).toEqual('blah'); this will pass so I know the
//changeSeason event is actually getting called
}));
【问题讨论】:
标签: angular jasmine karma-jasmine