I am struggling to comprehend why my test is not passing.
Snapshot of the Class:
export class Viewer implements OnChanges {
// ...
selectedTimePeriod: number;
timePeriods = [20, 30, 40];
constructor( /* ... */) {
this.selectLastDays(15);
}
selectLastDays(days: number): void { // triggered on click
this.selectedTimePeriod = days;
// ...
}
}
Snippet of the HTML Code:
// ...
<ul>
<li *ngFor="let period of timePeriods">
<a [ngClass]="{'active': selectedTimePeriod === period}"
(click)="selectLastDays(period)">{{ period }} days
</a>
</li>
</ul>
Brief Overview of the Test:
beforeEach(() => {
fixture = TestBed.createComponent(HistoryViewerComponent);
component = fixture.componentInstance;
fixture.detectChanges();
dh = new DOMHelper(fixture);
});
it('should change selectedTimePeriod value to 20', async () => {
spyOn(component, 'selectLastDays');
dh.queryOne('li a').triggerEventHandler('click', null); // button value of 20
dh.queryOne('li a').nativeElement.click(); // clicked even twice
await fixture.whenStable();
fixture.detectChanges();
expect(component.selectLastDays).toHaveBeenCalledTimes(2); // <- true
expect(component.selectedTimePeriod).toEqual(20); // <- false
});
it('should change selectedTimePeriod', () => { // this test passes
expect(component.selectedTimePeriod).toEqual(15);
component.selectLastDays(20);
expect(component.selectedTimePeriod).toEqual(20);
});
When the button is clicked, the method selectLastdays
receives parameter 20
. The application functions smoothly, so why does the test fail?