|
1 | | -import { it, expect } from "vitest"; |
2 | | -import { createHooks } from "atomico/test-hooks"; |
3 | | -import { useKeyboard } from "../src/"; |
4 | | - |
5 | | -it("useKeyboard", () => { |
6 | | - const ref = { current: document }; |
7 | | - const hooks = createHooks(); |
8 | | - const capture = []; |
9 | | - hooks.load(() => { |
10 | | - useKeyboard(ref, ["KeyA", "KeyQ"], (event) => { |
11 | | - capture.push(event); |
12 | | - }); |
13 | | - }); |
14 | | - |
15 | | - hooks.cleanEffects()()(); |
16 | | - |
17 | | - ref.current.dispatchEvent( |
18 | | - new KeyboardEvent("keydown", { code: "KeyQ", key: "1" }), |
19 | | - ); |
20 | | - |
21 | | - ref.current.dispatchEvent( |
22 | | - new KeyboardEvent("keydown", { code: "KeyA", key: "1" }), |
23 | | - ); |
24 | | - |
25 | | - ref.current.dispatchEvent( |
26 | | - new KeyboardEvent("keyup", { code: "KeyQ", key: "1" }), |
27 | | - ); |
28 | | - |
29 | | - ref.current.dispatchEvent( |
30 | | - new KeyboardEvent("keyup", { code: "KeyA", key: "1" }), |
31 | | - ); |
32 | | - |
33 | | - ref.current.dispatchEvent( |
34 | | - new KeyboardEvent("keydown", { code: "KeyA", key: "2" }), |
35 | | - ); |
36 | | - ref.current.dispatchEvent( |
37 | | - new KeyboardEvent("keydown", { code: "KeyQ", key: "2" }), |
38 | | - ); |
39 | | - |
40 | | - expect(capture.length).to.equal(1); |
41 | | - |
42 | | - const [event] = capture; |
43 | | - |
44 | | - // The captured event defines key as 2, if it is 1 the hook ignores the convination |
45 | | - expect(event.key).to.equal("2"); |
| 1 | +import { asyncEventListener } from "atomico/test-dom"; |
| 2 | +import { it } from "vitest"; |
| 3 | +import { Element } from "./element"; |
| 4 | + |
| 5 | +it("useKeyboard", async () => { |
| 6 | + const element = new Element(); |
| 7 | + |
| 8 | + document.body.append(element); |
| 9 | + |
| 10 | + await element.updated; |
| 11 | + |
| 12 | + const input = element.querySelector("input"); |
| 13 | + |
| 14 | + setTimeout(() => { |
| 15 | + input.dispatchEvent( |
| 16 | + new KeyboardEvent("keydown", { code: "KeyQ", key: "1" }), |
| 17 | + ); |
| 18 | + |
| 19 | + input.dispatchEvent( |
| 20 | + new KeyboardEvent("keydown", { code: "KeyA", key: "1" }), |
| 21 | + ); |
| 22 | + |
| 23 | + input.dispatchEvent( |
| 24 | + new KeyboardEvent("keyup", { code: "KeyQ", key: "1" }), |
| 25 | + ); |
| 26 | + |
| 27 | + input.dispatchEvent( |
| 28 | + new KeyboardEvent("keyup", { code: "KeyA", key: "1" }), |
| 29 | + ); |
| 30 | + |
| 31 | + input.dispatchEvent( |
| 32 | + new KeyboardEvent("keydown", { code: "KeyA", key: "2" }), |
| 33 | + ); |
| 34 | + input.dispatchEvent( |
| 35 | + new KeyboardEvent("keydown", { code: "KeyQ", key: "2" }), |
| 36 | + ); |
| 37 | + }, 100); |
| 38 | + |
| 39 | + await asyncEventListener(element, "matchKeys"); |
46 | 40 | }); |
0 commit comments