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(). 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.