Using jasmine and karma, I can pass the unit test using fake async and using a real event, but the coverage is not correct, the settimeout call back is ignored.
@Component({
selector: 'slider',
templateUrl: './slider.component.html',
styleUrls: ['./slider.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [AgGridModule],
})
export class SliderComponent {
otherMethod() {}
onSliderReady() {
window.addEventListener('resize', () => {
setTimeout(() => {
this.otherMethod();
});
});
}
}
The unit test is working as expecting, the tick could be optional (not affecting the assertion).
describe('onSliderReady', () => {
it('should call otherMethod', fakeAsync(() => {
spyOn(component, 'otherMethod')
window.dispatchEvent(new Event('resize'));
component.onSliderReady();
tick(500);
expect(component.otherMethod).toHaveBeenCalled();
}));
});

otherMethod()via theonSliderReady()method? So the test "onSliderReady" fails?