Reputation: 13
Consider this part of code :
@ViewChild('amountSlider') amountSlider: any;
@ViewChild('amountInput') amountInput: any;
** Some Code **
setvariables()
{
const updateStep = this.renderer.listen(this.amountInput.input.nativeElement, 'keydown', (evt) => {
this.amountSlider.step = 1;
const keyName = evt.key;
if (keyName == 'Tab') {
this.amountSlider.step = 100;
}
});
const resetStep = this.renderer.listen(this.amountSlider.slider.nativeElement, 'mouseover', (evt) => {
this.amountSlider.step = 100;
});
}
Following part of (Event Listener) my code bothers me in Unit Testing I am not calling keydown or mouseover directly from my DOM. Can someone help me to write Unit Testing to cover this ???
test.spec.ts
describe('setVariables', () => {
it('makes expected calls', () => {
const renderer2Stub: Renderer2 = fixture.debugElement.injector.get(Renderer2);
spyOn(renderer2Stub, 'listen');
comp.setVariables();
expect(renderer2Stub.listen).toHaveBeenCalled();
})
});
Upvotes: 1
Views: 2053
Reputation: 21397
try this using KeyboardEvent with key codes
describe('setVariables', () => {
it('makes expected calls', () => {
const renderer2Stub: Renderer2 = fixture.debugElement.injector.get(Renderer2);
spyOn(renderer2Stub, 'listen');
// for down arrow event for example
comp.setVariables();
const event: Event = new KeyboardEvent('keydown', {
'code': '40'
});
window.dispatchEvent(event);
fixture.detectChanges();
expect(renderer2Stub.listen).toHaveBeenCalled();
})
});
Upvotes: 2