|
| 1 | +import type { KeyboardEvent } from "react"; |
| 2 | +import { renderHook } from "@testing-library/react"; |
| 3 | +import type { FloatingContext } from "@floating-ui/react"; |
| 4 | +import { mockedObject } from "../../../../../test/mocked-object"; |
| 5 | +import { useOpenKey } from "../useOpenKey"; |
| 6 | + |
| 7 | +describe("useOpenKey", () => { |
| 8 | + const onOpenChange = jest.fn(); |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + onOpenChange.mockReset(); |
| 12 | + }); |
| 13 | + |
| 14 | + const render = ({ open }: { open: boolean }) => { |
| 15 | + const context = mockedObject<FloatingContext>({ |
| 16 | + open, |
| 17 | + onOpenChange, |
| 18 | + }); |
| 19 | + |
| 20 | + const { result } = renderHook(() => useOpenKey(context)); |
| 21 | + |
| 22 | + expect(result.current).toEqual({ |
| 23 | + reference: { |
| 24 | + onKeyDown: expect.any(Function), |
| 25 | + }, |
| 26 | + }); |
| 27 | + |
| 28 | + const onKeyDown = result.current.reference?.onKeyDown; |
| 29 | + if (!onKeyDown) { |
| 30 | + throw new Error("onKeyDown is undefined"); |
| 31 | + } |
| 32 | + |
| 33 | + return { |
| 34 | + onKeyDown, |
| 35 | + }; |
| 36 | + }; |
| 37 | + |
| 38 | + const mockKeyboardEvent = ({ |
| 39 | + key = "", |
| 40 | + altKey = false, |
| 41 | + ctrlKey = false, |
| 42 | + metaKey = false, |
| 43 | + shiftKey = false, |
| 44 | + preventDefault = jest.fn(), |
| 45 | + }: Partial<KeyboardEvent>) => |
| 46 | + mockedObject<KeyboardEvent>({ |
| 47 | + key, |
| 48 | + altKey, |
| 49 | + ctrlKey, |
| 50 | + metaKey, |
| 51 | + shiftKey, |
| 52 | + preventDefault, |
| 53 | + }); |
| 54 | + |
| 55 | + const pressKey = (event: Parameters<typeof mockKeyboardEvent>[0]) => { |
| 56 | + const { onKeyDown } = render({ |
| 57 | + open: false, |
| 58 | + }); |
| 59 | + |
| 60 | + const keyboardEvent = mockKeyboardEvent(event); |
| 61 | + |
| 62 | + onKeyDown(keyboardEvent); |
| 63 | + |
| 64 | + return { |
| 65 | + onKeyDown, |
| 66 | + keyboardEvent, |
| 67 | + }; |
| 68 | + }; |
| 69 | + |
| 70 | + it("opens when pressing Ctrl + Space and it is closed", () => { |
| 71 | + const { keyboardEvent } = pressKey({ |
| 72 | + key: " ", |
| 73 | + ctrlKey: true, |
| 74 | + }); |
| 75 | + |
| 76 | + expect(keyboardEvent.preventDefault).toHaveBeenCalledTimes(1); |
| 77 | + expect(onOpenChange).toHaveBeenCalledTimes(1); |
| 78 | + expect(onOpenChange).toHaveBeenCalledWith(true, keyboardEvent); |
| 79 | + }); |
| 80 | + |
| 81 | + it("does not open when pressing Ctrl + Space and it is open", () => { |
| 82 | + const { onKeyDown } = render({ |
| 83 | + open: true, |
| 84 | + }); |
| 85 | + |
| 86 | + // Do not mock any properties to ensure that none of them are used. |
| 87 | + const keyboardEvent = mockedObject<KeyboardEvent>({}); |
| 88 | + |
| 89 | + onKeyDown(keyboardEvent); |
| 90 | + |
| 91 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("does not open when pressing Cmd + Space", () => { |
| 95 | + pressKey({ |
| 96 | + key: " ", |
| 97 | + metaKey: true, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 101 | + }); |
| 102 | + |
| 103 | + it("does not open when pressing Ctrl + Shift + Space", () => { |
| 104 | + pressKey({ |
| 105 | + key: " ", |
| 106 | + ctrlKey: true, |
| 107 | + shiftKey: true, |
| 108 | + }); |
| 109 | + |
| 110 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it("does not open when pressing Ctrl + Alt + Space", () => { |
| 114 | + pressKey({ |
| 115 | + key: " ", |
| 116 | + ctrlKey: true, |
| 117 | + altKey: true, |
| 118 | + }); |
| 119 | + |
| 120 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + it("does not open when pressing Ctrl + Cmd + Space", () => { |
| 124 | + pressKey({ |
| 125 | + key: " ", |
| 126 | + ctrlKey: true, |
| 127 | + metaKey: true, |
| 128 | + }); |
| 129 | + |
| 130 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 131 | + }); |
| 132 | + |
| 133 | + it("does not open when pressing Ctrl + Shift + Alt + Space", () => { |
| 134 | + pressKey({ |
| 135 | + key: " ", |
| 136 | + ctrlKey: true, |
| 137 | + altKey: true, |
| 138 | + shiftKey: true, |
| 139 | + }); |
| 140 | + |
| 141 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 142 | + }); |
| 143 | + |
| 144 | + it("does not open when pressing Space", () => { |
| 145 | + pressKey({ |
| 146 | + key: " ", |
| 147 | + }); |
| 148 | + |
| 149 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 150 | + }); |
| 151 | + |
| 152 | + it("does not open when pressing Ctrl + Tab", () => { |
| 153 | + pressKey({ |
| 154 | + key: "Tab", |
| 155 | + ctrlKey: true, |
| 156 | + }); |
| 157 | + |
| 158 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 159 | + }); |
| 160 | + |
| 161 | + it("does not open when pressing Ctrl + a letter", () => { |
| 162 | + pressKey({ |
| 163 | + key: "a", |
| 164 | + ctrlKey: true, |
| 165 | + }); |
| 166 | + |
| 167 | + expect(onOpenChange).not.toHaveBeenCalled(); |
| 168 | + }); |
| 169 | + |
| 170 | + it("does not change reference when the context changes", () => { |
| 171 | + const context = mockedObject<FloatingContext>({ |
| 172 | + open: false, |
| 173 | + onOpenChange, |
| 174 | + }); |
| 175 | + |
| 176 | + const { result, rerender } = renderHook((context) => useOpenKey(context), { |
| 177 | + initialProps: context, |
| 178 | + }); |
| 179 | + |
| 180 | + const firstOnKeyDown = result.current.reference?.onKeyDown; |
| 181 | + expect(firstOnKeyDown).toBeDefined(); |
| 182 | + |
| 183 | + rerender( |
| 184 | + mockedObject<FloatingContext>({ |
| 185 | + open: true, |
| 186 | + onOpenChange: jest.fn(), |
| 187 | + }), |
| 188 | + ); |
| 189 | + |
| 190 | + const secondOnKeyDown = result.current.reference?.onKeyDown; |
| 191 | + // test that useEffectEvent is used correctly and the reference doesn't change |
| 192 | + expect(secondOnKeyDown).toBe(firstOnKeyDown); |
| 193 | + }); |
| 194 | +}); |
0 commit comments