Skip to content

Commit

Permalink
Add working implementation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Hernandez committed Jan 12, 2021
1 parent 2b5718b commit 6999914
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
const kekos = () => console.log("Kekos!");
// https://keycode.info/
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code

export default kekos;
const kekos = ({
keyCodesPermitted = ["Enter", "Space"],
callback = () => {},
}) => (event) => {
try {
if (keyCodesPermitted.includes(event.code)) callback(event);
} catch (_) {}
};

module.exports = kekos;
32 changes: 32 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const kekos = require("./");

describe("Kekos", () => {
test("will not invoke the callback on configuration", () => {
const callback = jest.fn();
kekos({ callback });

expect(callback).not.toHaveBeenCalled();
});

test("will invoke the callback when permitted key code provided", () => {
const callback = jest.fn();
const event = { code: "Enter" };
const onKeyDown = kekos({ callback });

expect(callback).not.toHaveBeenCalled();

onKeyDown(event);

expect(callback).toHaveBeenCalledWith(event);
});

test("will not invoke the callback when unpermitted key code provided", () => {
const callback = jest.fn();
const event = { code: "ControlLeft" };
const onKeyDown = kekos({ callback });

onKeyDown(event);

expect(callback).not.toHaveBeenCalled();
});
});

0 comments on commit 6999914

Please sign in to comment.