Hey folks, I’m working on a little pixel-art canvas editor and I’m trying to add keyboard shortcuts, but my Jest tests started randomly failing after I introduced a global keydown listener; sometimes it fires twice and my undo stack ends up off-by-one.
// shortcuts.js
export function attachShortcuts(store) {
const onKeyDown = (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === "z") {
store.undo();
}
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}
// shortcuts.test.js
import { attachShortcuts } from "./shortcuts";
test("ctrl+z undoes once", () => {
const store = { undo: jest.fn() };
attachShortcuts(store);
window.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true }));
expect(store.undo).toHaveBeenCalledTimes(1);
});
What’s the cleanest pattern to make this reliable in tests without leaking handlers between tests or over-mocking the browser event system?
Sora ![]()