简体   繁体   English

KeyboardEvent的Angular 4单元测试

[英]Angular 4 unit test for KeyboardEvent

I'm trying to write a unit test for a utility I've written to limit the characters available to an input field. 我试图为我编写的用于限制输入字段可用字符的实用程序编写单元测试。 The method takes the keyboard event and determines which event.code has been fired and either returns true or event.preventDefault(). 该方法获取键盘事件,并确定触发了哪个event.code并返回true或event.preventDefault()。 This works great but I cannot test it in jasmine / karma. 这很好用,但是我无法在茉莉花/业力中测试它。

Current input from template 模板的当前输入

<input [(ngModel)]="donationValue" formControlName="donationAmount" 
type="tel" class="donation-amount" (keydown)="checkCharacter($event)" 
placeholder="Enter Amount..."/>

Here is my current test 这是我目前的测试

it('should return have defaultPrevented as true', fakeAsync(() => {
        const goalInput = 
fixture.debugElement.query(By.css('input.donation-
amount')).nativeElement;
        const keyEventData = { isTrusted: true, code: 'KeyA' };
        const keyEvent = new KeyboardEvent('keydown', keyEventData);
        goalInput.dispatchEvent(keyEvent);
        tick();
        fixture.detectChanges();                
        expect(keyEvent.defaultPrevented).toBe(true);
    }));

I have other tests that I have spied on the methods and they are getting fired off. 我对方法进行了其他测试,发现它们被解雇了。 My suspicion is that the isTrusted property is set to false even though I'm trying to set it to true. 我怀疑isTrusted属性设置为false,即使我试图将其设置为true。

So - the answer that I ended up using was this: 所以-我最终使用的答案是这样的:

it('Should call prevent default', inject([ManageUtils], (manageUtils: ManageUtils) => {
    const keyEvent = new KeyboardEvent('keydown', { code: 'KeyA' });
    const spy = spyOn(keyEvent, 'preventDefault');        
    manageUtils.checkCharacterForProperCurrency(keyEvent);
    fixture.detectChanges();
    expect(spy).toHaveBeenCalled()
}));

it('Should not call prevent default', inject([ManageUtils], (manageUtils: ManageUtils) => {
    const keyEvent = new KeyboardEvent('keydown', { code: 'Digit0' });
    const spy = spyOn(keyEvent, 'preventDefault');        
    manageUtils.checkCharacterForProperCurrency(keyEvent);
    fixture.detectChanges();
    expect(spy).toHaveBeenCalledTimes(0);
}));

As one of the responders answered, it is impossible to create a true isTrusted keypressed event (from what I've read). 正如一名响应者回答的那样,不可能创建一个真正的isTrusted按键事件(根据我的阅读)。 So to test this, I used a jasmine spy to see if preventDefault was called when I passed the KeyboardEvent to the utility function that I built. 因此,为了对此进行测试,我使用了一个茉莉花间谍,以查看将KeyboardEvent传递给所构建的实用程序函数时是否调用了preventDefault。 Hopefully this can save someone time...took me a while to get here! 希望这可以节省某人的时间...花了我一段时间才能到达这里!

You can't force the value of isTrusted to false when the event is triggred by a dispatchEvent call. 当事件被dispatchEvent调用触发时,不能将isTrusted的值强制设置为false。 Here the documetnation https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted 这里是文档https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM