Skip to content

Commit

Permalink
Fixed jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKlauss committed Jul 25, 2020
1 parent 024bc21 commit 9af7ffd
Showing 1 changed file with 40 additions and 39 deletions.
79 changes: 40 additions & 39 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useRef, useState} from "react";
import React, {useState} from "react";
import {useHotkeys} from "./index";
import {act, renderHook} from "@testing-library/react-hooks";
import {act as reactAct, fireEvent, render, waitFor} from "@testing-library/react";
import {act as reactAct, fireEvent, render} from "@testing-library/react";

function useWrapper(keys: string) {
const [count, setCount] = useState(0);
Expand Down Expand Up @@ -56,6 +56,43 @@ const HotkeysOnInput = ({onPress, useTags}: { onPress: () => void, useTags?: boo
);
};

const HotkeysWithRef = ({onPress}: { onPress: () => void }) => {
const ref = useHotkeys<HTMLElement>('a', onPress);

return (
<section ref={ref} tabIndex={0}>
<input type="text" data-testid={'input'}/>
</section>
);
};

test('useHotkeys should only fire when element is focused if a ref is set.', async () => {
let called = false;

const {container} = render(<HotkeysWithRef onPress={() => called = true}/>);

const section = container.querySelector('section');

expect(section).not.toBe(null);

reactAct(() => {
fireEvent.keyDown(section!, {key: 'a', keyCode: 65});
fireEvent.keyUp(section!, {key: 'a', keyCode: 65});
});

expect(called).toBe(false);

reactAct(() => {
section!.focus();
});

reactAct(() => {
fireEvent.keyDown(section!, {key: 'a', keyCode: 65});
});

expect(called).toBe(true);
});

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

Expand Down Expand Up @@ -158,40 +195,4 @@ test('useHotkeys should be enabled on given form tags', async () => {
fireEvent.keyDown(input!, {key: 'a', keyCode: 65});

expect(onPress).toHaveBeenCalled();
});

const HotkeysWithRef = ({onPress}: { onPress: () => void }) => {
const ref = useHotkeys<HTMLDivElement>('a', onPress);

return (
<section ref={ref} tabIndex={0}>
<input type="text" data-testid={'input'}/>
</section>
);
};

/*test('useHotkeys should only fire when element is focused if a ref is set.', async () => {
let called = false;
const {container} = render(<HotkeysWithRef onPress={() => called = true}/>);
const section = container.querySelector('section');
expect(section).not.toBe(null);
act(() => {
fireEvent.keyDown(section!, {key: 'a', keyCode: 65});
});
expect(called).toBe(false);
reactAct(() => {
section!.focus();
});
act(() => {
fireEvent.keyDown(section!, {key: 'a', keyCode: 65});
});
expect(called).toBe(true);
})*/
});

0 comments on commit 9af7ffd

Please sign in to comment.