|
| 1 | +import { |
| 2 | + isBareLetterKey, |
| 3 | + keyboardKey, |
| 4 | + normalizedKeyboardKey, |
| 5 | +} from "@web/shortcuts/is-bare-letter-key"; |
| 6 | +import { describe, expect, it } from "bun:test"; |
| 7 | + |
| 8 | +const eventWithKey = (key: unknown): KeyboardEvent => { |
| 9 | + const event = new KeyboardEvent("keydown", { |
| 10 | + bubbles: true, |
| 11 | + cancelable: true, |
| 12 | + }); |
| 13 | + Object.defineProperty(event, "key", { get: () => key }); |
| 14 | + return event; |
| 15 | +}; |
| 16 | + |
| 17 | +describe("keyboardKey", () => { |
| 18 | + it("returns the key when it is a string", () => { |
| 19 | + expect(keyboardKey(eventWithKey("e"))).toBe("e"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("returns empty string when key is missing", () => { |
| 23 | + expect(keyboardKey(eventWithKey(undefined))).toBe(""); |
| 24 | + expect(keyboardKey(eventWithKey(null))).toBe(""); |
| 25 | + }); |
| 26 | +}); |
| 27 | + |
| 28 | +describe("normalizedKeyboardKey", () => { |
| 29 | + it("lowercases single-character keys", () => { |
| 30 | + expect(normalizedKeyboardKey(eventWithKey("E"))).toBe("e"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("leaves named keys unchanged", () => { |
| 34 | + expect(normalizedKeyboardKey(eventWithKey("Escape"))).toBe("Escape"); |
| 35 | + }); |
| 36 | + |
| 37 | + it("does not throw when key is missing", () => { |
| 38 | + expect(normalizedKeyboardKey(eventWithKey(undefined))).toBe(""); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe("isBareLetterKey", () => { |
| 43 | + it("matches an unmodified letter", () => { |
| 44 | + expect(isBareLetterKey(eventWithKey("s"), "s")).toBe(true); |
| 45 | + expect(isBareLetterKey(eventWithKey("S"), "s")).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it("returns false instead of throwing when key is missing", () => { |
| 49 | + expect(isBareLetterKey(eventWithKey(undefined), "s")).toBe(false); |
| 50 | + }); |
| 51 | +}); |
0 commit comments