-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add working implementation and tests
- Loading branch information
Anthony Hernandez
committed
Jan 12, 2021
1 parent
2b5718b
commit 6999914
Showing
2 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |