|
| 1 | +import { act, fireEvent, render, renderHook, screen, waitFor } from "@testing-library/react"; |
| 2 | +import * as React from "react"; |
| 3 | + |
| 4 | +import { useKeyCombination, useKeyCombinations } from "../src"; |
| 5 | +import { Default } from "../stories/index.stories"; |
| 6 | + |
| 7 | +describe("Hooks", () => { |
| 8 | + it("should handle pressed styling", async () => { |
| 9 | + const { getAllByText } = render(<Default />); |
| 10 | + |
| 11 | + const controlKey = getAllByText("Control")[0]; |
| 12 | + const bKey = getAllByText("B")[0]; |
| 13 | + expect(controlKey).toBeDefined(); |
| 14 | + expect(controlKey).toHaveStyleRule("background-color", "rgb(249, 249, 250)"); |
| 15 | + expect(bKey).toHaveStyleRule("background-color", "rgb(249, 249, 250)"); |
| 16 | + |
| 17 | + await act(async () => { |
| 18 | + fireEvent.keyDown(controlKey, { key: "Control" }); |
| 19 | + }); |
| 20 | + |
| 21 | + expect(controlKey).toHaveStyleRule("background-color", "rgb(225, 227, 234)"); |
| 22 | + expect(bKey).toHaveStyleRule("background-color", "rgb(249, 249, 250)"); |
| 23 | + |
| 24 | + await act(async () => { |
| 25 | + fireEvent.keyUp(controlKey, { key: "Control" }); |
| 26 | + }); |
| 27 | + |
| 28 | + expect(controlKey).toHaveStyleRule("background-color", "rgb(249, 249, 250)"); |
| 29 | + expect(bKey).toHaveStyleRule("background-color", "rgb(249, 249, 250)"); |
| 30 | + }); |
| 31 | + |
| 32 | + describe("useKeyCombination", () => { |
| 33 | + it("should update activeKeys on keydown and keyup", async () => { |
| 34 | + const { result } = renderHook(() => useKeyCombination({ keys: ["Control", "b"], onCombinationPress: jest.fn() })); |
| 35 | + |
| 36 | + expect(result.current?.activeKeys).toEqual([]); |
| 37 | + |
| 38 | + await act(async () => { |
| 39 | + fireEvent.keyDown(window, { key: "Control" }); |
| 40 | + }); |
| 41 | + |
| 42 | + await waitFor(() => { |
| 43 | + expect(result.current?.activeKeys).toEqual(["Control"]); |
| 44 | + }); |
| 45 | + |
| 46 | + await act(async () => { |
| 47 | + fireEvent.keyDown(window, { key: "d" }); |
| 48 | + fireEvent.keyDown(window, { key: "v" }); |
| 49 | + }); |
| 50 | + |
| 51 | + await waitFor(() => { |
| 52 | + expect(result.current?.activeKeys).toEqual(["Control", "d", "v"]); |
| 53 | + }); |
| 54 | + |
| 55 | + await act(async () => { |
| 56 | + fireEvent.keyUp(window, { key: "Control" }); |
| 57 | + fireEvent.keyUp(window, { key: "d" }); |
| 58 | + }); |
| 59 | + |
| 60 | + await waitFor(() => { |
| 61 | + expect(result.current?.activeKeys).toEqual(["v"]); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should call onCombinationPress when keys match", async () => { |
| 66 | + const onCombinationPress = jest.fn(); |
| 67 | + const { result } = renderHook(() => useKeyCombination({ keys: ["Control", "b"], onCombinationPress }), {}); |
| 68 | + |
| 69 | + expect(result.current?.activeKeys).toEqual([]); |
| 70 | + |
| 71 | + await act(async () => { |
| 72 | + fireEvent.keyDown(window, { key: "Control" }); |
| 73 | + }); |
| 74 | + |
| 75 | + await waitFor(() => { |
| 76 | + expect(result.current?.activeKeys).toEqual(["Control"]); |
| 77 | + }); |
| 78 | + |
| 79 | + await act(async () => { |
| 80 | + fireEvent.keyDown(window, { key: "b" }); |
| 81 | + }); |
| 82 | + |
| 83 | + await waitFor(() => { |
| 84 | + expect(result.current?.activeKeys).toEqual(expect.arrayContaining(["Control", "b"])); |
| 85 | + expect(onCombinationPress).toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should not call onCombinationPress when keys do not match", async () => { |
| 90 | + const onCombinationPress = jest.fn(); |
| 91 | + const { result } = renderHook(() => useKeyCombination({ keys: ["Control", "b"], onCombinationPress }), {}); |
| 92 | + |
| 93 | + expect(result.current?.activeKeys).toEqual([]); |
| 94 | + |
| 95 | + await act(async () => { |
| 96 | + fireEvent.keyDown(window, { key: "Control" }); |
| 97 | + }); |
| 98 | + |
| 99 | + await waitFor(() => { |
| 100 | + expect(result.current?.activeKeys).toEqual(["Control"]); |
| 101 | + }); |
| 102 | + |
| 103 | + await act(async () => { |
| 104 | + fireEvent.keyDown(window, { key: "d" }); |
| 105 | + }); |
| 106 | + |
| 107 | + await waitFor(() => { |
| 108 | + expect(result.current?.activeKeys).toEqual(["Control", "d"]); |
| 109 | + expect(onCombinationPress).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + it("should not call onCombinationPress when keys are present but more are pressed", async () => { |
| 114 | + const onCombinationPress = jest.fn(); |
| 115 | + const { result } = renderHook(() => useKeyCombination({ keys: ["Control", "b"], onCombinationPress }), {}); |
| 116 | + |
| 117 | + expect(result.current?.activeKeys).toEqual([]); |
| 118 | + |
| 119 | + await act(async () => { |
| 120 | + fireEvent.keyDown(window, { key: "Control" }); |
| 121 | + }); |
| 122 | + |
| 123 | + await waitFor(() => { |
| 124 | + expect(result.current?.activeKeys).toEqual(["Control"]); |
| 125 | + }); |
| 126 | + |
| 127 | + await act(async () => { |
| 128 | + fireEvent.keyDown(window, { key: "v" }); |
| 129 | + fireEvent.keyDown(window, { key: "b" }); |
| 130 | + }); |
| 131 | + |
| 132 | + await waitFor(() => { |
| 133 | + expect(result.current?.activeKeys).toEqual(expect.arrayContaining(["Control", "v", "b"])); |
| 134 | + expect(onCombinationPress).not.toHaveBeenCalled(); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + it("should not call onCombinationPress when disabled", async () => { |
| 139 | + const onCombinationPress = jest.fn(); |
| 140 | + const { result } = renderHook(() => |
| 141 | + useKeyCombination({ keys: ["Control", "b"], onCombinationPress, disabled: true }), |
| 142 | + ); |
| 143 | + |
| 144 | + expect(result.current?.activeKeys).toEqual([]); |
| 145 | + |
| 146 | + await act(async () => { |
| 147 | + fireEvent.keyDown(window, { key: "Control" }); |
| 148 | + }); |
| 149 | + |
| 150 | + await waitFor(() => { |
| 151 | + expect(result.current?.activeKeys).toEqual(["Control"]); |
| 152 | + }); |
| 153 | + |
| 154 | + await act(async () => { |
| 155 | + fireEvent.keyDown(window, { key: "b" }); |
| 156 | + }); |
| 157 | + |
| 158 | + await waitFor(() => { |
| 159 | + expect(result.current?.activeKeys).toEqual(expect.arrayContaining(["Control", "b"])); |
| 160 | + expect(onCombinationPress).not.toHaveBeenCalled(); |
| 161 | + }); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe("useKeyCombinations", () => { |
| 166 | + it("should update activeKeys on keydown and keyup", async () => { |
| 167 | + const { result } = renderHook(() => |
| 168 | + useKeyCombinations({ |
| 169 | + combinations: [ |
| 170 | + { keys: ["Control", "b"], onCombinationPress: jest.fn() }, |
| 171 | + { keys: ["Control", "c"], onCombinationPress: jest.fn() }, |
| 172 | + ], |
| 173 | + }), |
| 174 | + ); |
| 175 | + |
| 176 | + expect(result.current?.activeKeys).toEqual([]); |
| 177 | + |
| 178 | + await act(async () => { |
| 179 | + fireEvent.keyDown(window, { key: "Control" }); |
| 180 | + }); |
| 181 | + |
| 182 | + await waitFor(() => { |
| 183 | + expect(result.current?.activeKeys).toEqual(["Control"]); |
| 184 | + }); |
| 185 | + |
| 186 | + await act(async () => { |
| 187 | + fireEvent.keyDown(window, { key: "d" }); |
| 188 | + fireEvent.keyDown(window, { key: "v" }); |
| 189 | + }); |
| 190 | + |
| 191 | + await waitFor(() => { |
| 192 | + expect(result.current?.activeKeys).toEqual(["Control", "d", "v"]); |
| 193 | + }); |
| 194 | + |
| 195 | + await act(async () => { |
| 196 | + fireEvent.keyUp(window, { key: "Control" }); |
| 197 | + fireEvent.keyUp(window, { key: "d" }); |
| 198 | + }); |
| 199 | + |
| 200 | + await waitFor(() => { |
| 201 | + expect(result.current?.activeKeys).toEqual(["v"]); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + it("should call onCombinationPress when keys match", async () => { |
| 206 | + const onCombinationPress1 = jest.fn(); |
| 207 | + const onCombinationPress2 = jest.fn(); |
| 208 | + |
| 209 | + const { result } = renderHook(() => |
| 210 | + useKeyCombinations({ |
| 211 | + combinations: [ |
| 212 | + { keys: ["Control", "b"], onCombinationPress: onCombinationPress1 }, |
| 213 | + { keys: ["Control", "c"], onCombinationPress: onCombinationPress2 }, |
| 214 | + ], |
| 215 | + }), |
| 216 | + ); |
| 217 | + |
| 218 | + expect(result.current?.activeKeys).toEqual([]); |
| 219 | + |
| 220 | + await act(async () => { |
| 221 | + fireEvent.keyDown(window, { key: "Control" }); |
| 222 | + }); |
| 223 | + |
| 224 | + await waitFor(() => { |
| 225 | + expect(result.current?.activeKeys).toEqual(["Control"]); |
| 226 | + }); |
| 227 | + |
| 228 | + await act(async () => { |
| 229 | + fireEvent.keyDown(window, { key: "b" }); |
| 230 | + }); |
| 231 | + |
| 232 | + await waitFor(() => { |
| 233 | + expect(result.current?.activeKeys).toEqual(expect.arrayContaining(["Control", "b"])); |
| 234 | + expect(onCombinationPress1).toHaveBeenCalled(); |
| 235 | + expect(onCombinationPress2).not.toHaveBeenCalled(); |
| 236 | + }); |
| 237 | + |
| 238 | + await act(async () => { |
| 239 | + onCombinationPress1.mockClear(); |
| 240 | + fireEvent.keyDown(window, { key: "c" }); |
| 241 | + }); |
| 242 | + |
| 243 | + await waitFor(() => { |
| 244 | + expect(result.current?.activeKeys).toEqual(expect.arrayContaining(["Control", "b", "c"])); |
| 245 | + expect(onCombinationPress1).not.toHaveBeenCalled(); |
| 246 | + expect(onCombinationPress2).not.toHaveBeenCalled(); |
| 247 | + }); |
| 248 | + |
| 249 | + await act(async () => { |
| 250 | + fireEvent.keyUp(window, { key: "b" }); |
| 251 | + }); |
| 252 | + |
| 253 | + await waitFor(() => { |
| 254 | + expect(result.current?.activeKeys).toEqual(expect.arrayContaining(["Control", "c"])); |
| 255 | + expect(onCombinationPress1).not.toHaveBeenCalled(); |
| 256 | + expect(onCombinationPress2).toHaveBeenCalled(); |
| 257 | + }); |
| 258 | + }); |
| 259 | + |
| 260 | + it("should not call onCOmbinationPress when disabled", async () => { |
| 261 | + const onCombinationPress1 = jest.fn(); |
| 262 | + const onCombinationPress2 = jest.fn(); |
| 263 | + |
| 264 | + const { result } = renderHook(() => |
| 265 | + useKeyCombinations({ |
| 266 | + combinations: [ |
| 267 | + { keys: ["Control", "b"], onCombinationPress: onCombinationPress1 }, |
| 268 | + { keys: ["Control", "c"], onCombinationPress: onCombinationPress2, disabled: true }, |
| 269 | + ], |
| 270 | + }), |
| 271 | + ); |
| 272 | + |
| 273 | + expect(result.current?.activeKeys).toEqual([]); |
| 274 | + |
| 275 | + await act(async () => { |
| 276 | + fireEvent.keyDown(window, { key: "Control" }); |
| 277 | + }); |
| 278 | + |
| 279 | + await waitFor(() => { |
| 280 | + expect(result.current?.activeKeys).toEqual(["Control"]); |
| 281 | + }); |
| 282 | + |
| 283 | + await act(async () => { |
| 284 | + fireEvent.keyDown(window, { key: "b" }); |
| 285 | + }); |
| 286 | + |
| 287 | + await waitFor(() => { |
| 288 | + expect(result.current?.activeKeys).toEqual(expect.arrayContaining(["Control", "b"])); |
| 289 | + expect(onCombinationPress1).toHaveBeenCalled(); |
| 290 | + expect(onCombinationPress2).not.toHaveBeenCalled(); |
| 291 | + }); |
| 292 | + |
| 293 | + await act(async () => { |
| 294 | + onCombinationPress1.mockClear(); |
| 295 | + fireEvent.keyDown(window, { key: "c" }); |
| 296 | + }); |
| 297 | + |
| 298 | + await waitFor(() => { |
| 299 | + expect(result.current?.activeKeys).toEqual(expect.arrayContaining(["Control", "b", "c"])); |
| 300 | + expect(onCombinationPress1).not.toHaveBeenCalled(); |
| 301 | + expect(onCombinationPress2).not.toHaveBeenCalled(); |
| 302 | + }); |
| 303 | + |
| 304 | + await act(async () => { |
| 305 | + fireEvent.keyUp(window, { key: "b" }); |
| 306 | + }); |
| 307 | + |
| 308 | + await waitFor(() => { |
| 309 | + expect(result.current?.activeKeys).toEqual(expect.arrayContaining(["Control", "c"])); |
| 310 | + expect(onCombinationPress1).not.toHaveBeenCalled(); |
| 311 | + expect(onCombinationPress2).not.toHaveBeenCalled(); |
| 312 | + }); |
| 313 | + }); |
| 314 | + }); |
| 315 | +}); |
0 commit comments