Skip to content

Commit

Permalink
Added first tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKlauss committed Apr 5, 2020
1 parent bc613b3 commit 5298cf2
Show file tree
Hide file tree
Showing 3 changed files with 1,522 additions and 51 deletions.
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"license": "MIT",
"scripts": {
"build": "pika build",
"test": "jest",
"publish": "pika publish",
"docz:dev": "docz dev",
"docz:build": "docz build",
Expand All @@ -30,6 +31,13 @@
"@babel/preset-react"
]
},
"jest": {
"testPathIgnorePatterns": [
"pkg",
".docz",
"docs"
]
},
"@pika/pack": {
"pipeline": [
[
Expand Down Expand Up @@ -60,13 +68,20 @@
"@pika/plugin-build-types": "0.9.2",
"@pika/plugin-build-web": "0.9.2",
"@pika/plugin-ts-standard-pkg": "0.9.2",
"@testing-library/react": "^10.0.2",
"@testing-library/react-hooks": "3.2.1",
"@types/jest": "^25.2.1",
"@types/react": "16.9.32",
"@types/react-dom": "16.9.6",
"@types/testing-library__react": "^10.0.1",
"@types/testing-library__react-hooks": "^3.2.0",
"docz": "2.3.0",
"emotion-theming": "10.0.27",
"gatsby-plugin-emotion": "4.2.1",
"jest": "25.2.7",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-test-renderer": "16.13.1",
"typescript": "3.8.3"
},
"peerDependencies": {
Expand Down
41 changes: 41 additions & 0 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, {useState} from "react";
import {useHotkeys} from "./index";
import {act, renderHook} from "@testing-library/react-hooks";
import { fireEvent } from "@testing-library/react";

export default function useWrapper(keys: string) {
const [count, setCount] = useState(0);
const increment = () => setCount((x) => x + 1);

useHotkeys(keys, increment);

return count;
}

test('useHotkeys should listen to key presses', () => {
const { result } = renderHook(() => useWrapper('a'));

expect(result.current).toBe(0);

act(() => {
fireEvent.keyDown(document.body, { key: 'a', keyCode: 65 });

return undefined;
});

expect(result.current).toBe(1);
});

test('useHotkeys should listen to its own context', function () {
const resultA = renderHook(() => useWrapper('a'));
const resultB = renderHook(() => useWrapper('b'));

act(() => {
fireEvent.keyDown(document.body, { key: 'a', keyCode: 65 });

return undefined;
});

expect(resultA.result.current).toBe(1);
expect(resultB.result.current).toBe(0);
});
Loading

0 comments on commit 5298cf2

Please sign in to comment.