From 6999914f02c2c04767aa4a3adeb2d613383eba80 Mon Sep 17 00:00:00 2001 From: Anthony Hernandez Date: Tue, 12 Jan 2021 11:42:55 -0800 Subject: [PATCH] Add working implementation and tests --- index.js | 14 ++++++++++++-- index.test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 index.test.js diff --git a/index.js b/index.js index 02d25c1..af5b1f1 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/index.test.js b/index.test.js new file mode 100644 index 0000000..68155b0 --- /dev/null +++ b/index.test.js @@ -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(); + }); +});