'Test in Karma/Jasmine getModifierState
How to test this method (angular):
public detectCapslock(event: KeyboardEvent): void {
let capsOn: boolean = event.getModifierState && event.getModifierState("CapsLock");
if (capsOn) {
this.capsOn = "Caps Lock on";
} else {
this.capsOn = "";
}
}
Solution 1:[1]
We can do something like this:
// Assemble
const event = new KeyboardEvent('keyup', {
key: 'CapsLock',
code: 'CapsLock',
modifierCapsLock: true,
});
// Act
document.dispatchEvent(event);
// Assert
expect(component.capsOn).toEqual('Caps Lock on');
or in your case:
// Assemble
const event = new KeyboardEvent('keyup', {
key: 'CapsLock',
code: 'CapsLock',
modifierCapsLock: true,
});
// Act
compoent.detectCapslock(event);
// Assert
expect(component.capsOn).toEqual('Caps Lock on');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Brendan |
