Using JEST to unit test a component that has a keydown listener attached to the document.
How can I test this in JEST? How do I simulate t
Following @Iris Schaffer answer,
If your code makes use of ctrl/alt/shift keys, you will need to init them, as well as mocking implementation of getModifierState method on KeyboardEvent
const keyboardEvent = new KeyboardEvent('keydown', { keyCode, shiftKey, altKey, ctrlKey });
jest.spyOn(keyboardEvent, 'getModifierState').mockImplementation((modifier) => {
switch (modifier) {
case 'Alt':
return altKey;
case 'Control':
return ctrlKey;
case 'Shift':
return shiftKey;
}
});