Elffuss BraidHub

kikocis/continue public

7e7b3d18f689 · 4.9 KiBRawPermalink
1import { render } from "@testing-library/react";
2import { beforeEach, describe, expect, it, vi } from "vitest";
3import * as util from "../../util";
4import { Platform } from "../../util";
5import Shortcut from "./Shortcut";
6
7vi.mock("../../util", () => ({
8 ...vi.importActual("../../util"),
9 getPlatform: vi.fn(),
10 getMetaKeyLabel: vi.fn(),
11 getAltKeyLabel: vi.fn(),
12 getFontSize: vi.fn(),
13}));
14
15describe("Shortcut component", () => {
16 beforeEach(() => {
17 vi.resetAllMocks();
18 vi.mocked(util.getPlatform).mockReturnValue("windows");
19 vi.mocked(util.getFontSize).mockReturnValue(14);
20 });
21 describe("Meta key rendering across platforms", () => {
22 const metaKeyInputs = ["meta", "cmd", "ctrl", "^", "⌘"];
23 const platforms: Platform[] = ["windows", "mac", "linux"];
24
25 platforms.forEach((platform) => {
26 metaKeyInputs.forEach((metaKey) => {
27 it(`should render "${metaKey}" as "${
28 platform === "mac" ? "⌘" : "Ctrl"
29 }" on ${platform}`, () => {
30 vi.mocked(util.getPlatform).mockReturnValue(platform);
31 vi.mocked(util.getMetaKeyLabel).mockImplementation(() => {
32 if (platform === "mac") return "⌘";
33 return "Ctrl";
34 });
35 const { container } = render(<Shortcut>{metaKey}</Shortcut>);
36 const kbdElement = container.querySelector("kbd");
37 expect(kbdElement?.textContent).toBe(
38 platform === "mac" ? "⌘" : "Ctrl",
39 );
40 });
41 });
42 });
43 });
44
45 describe("Font class application", () => {
46 it("should apply 'keyboard-key-normal' class for single characters", () => {
47 const { container } = render(<Shortcut>a</Shortcut>);
48 const kbdElement = container.querySelector("kbd");
49 expect(kbdElement).toHaveClass("keyboard-key-normal");
50 });
51
52 it("should apply 'keyboard-key-special' class for special keys", () => {
53 const { container } = render(<Shortcut>Enter</Shortcut>);
54 const kbdElement = container.querySelector("kbd");
55 expect(kbdElement).toHaveClass("keyboard-key-special");
56 });
57 });
58
59 describe("Delete key rendering across platforms", () => {
60 const deleteKeyInputs = ["backspace", "delete", "⌫"];
61 const platforms: Platform[] = ["windows", "mac", "linux"];
62
63 platforms.forEach((platform) => {
64 deleteKeyInputs.forEach((deleteKey) => {
65 it(`should render "${deleteKey}" correctly on ${platform}`, () => {
66 vi.mocked(util.getPlatform).mockReturnValue(platform);
67 const { container } = render(<Shortcut>{deleteKey}</Shortcut>);
68 const kbdElement = container.querySelector("kbd");
69 const expectedText = platform === "mac" ? "Delete ⌫" : "Backspace ⌫";
70 expect(kbdElement?.textContent).toBe(expectedText);
71 expect(kbdElement).toHaveClass("keyboard-key-special");
72 });
73 });
74 });
75 });
76
77 describe("Invalid input handling", () => {
78 it("should render error for empty shortcut string", () => {
79 const { container } = render(<Shortcut>{""}</Shortcut>);
80 expect(container.textContent).toBe("Error: Invalid shortcut key");
81 });
82
83 it("should render error for null input", () => {
84 const { container } = render(<Shortcut>{null as any}</Shortcut>);
85 expect(container.textContent).toBe("Error: Invalid shortcut key");
86 });
87
88 it("should gracefully handle invalid formatted input", () => {
89 const { container } = render(<Shortcut>{"abc ,, def"}</Shortcut>);
90 expect(container.querySelectorAll("kbd").length).toBeGreaterThan(0);
91 });
92 });
93
94 describe("Option/Alt key rendering across platforms", () => {
95 const optionKeyInputs = ["⌥", "option", "opt", "alt"];
96 const platforms: util.Platform[] = ["windows", "mac"];
97
98 platforms.forEach((platform) => {
99 optionKeyInputs.forEach((optionKey) => {
100 it(`should render "${optionKey}" correctly on ${platform}`, () => {
101 vi.mocked(util.getPlatform).mockReturnValue(platform);
102 vi.mocked(util.getAltKeyLabel).mockImplementation(() => {
103 return platform === "mac" ? "Option ⌥" : "Alt";
104 });
105
106 const { container } = render(<Shortcut>{optionKey}</Shortcut>);
107 const kbdElement = container.querySelector("kbd");
108
109 const expectedText = platform === "mac" ? "Option ⌥" : "Alt";
110 expect(kbdElement?.textContent).toBe(expectedText);
111 expect(kbdElement).toHaveClass("keyboard-key-special");
112 });
113 });
114 });
115 });
116
117 describe("Font class application for option/alt key", () => {
118 it("should apply 'keyboard-key-special' class for option/alt key", () => {
119 vi.mocked(util.getPlatform).mockReturnValue("windows");
120 vi.mocked(util.getFontSize).mockReturnValue(14);
121 vi.mocked(util.getAltKeyLabel).mockImplementation(() => "Alt");
122 const { container } = render(<Shortcut>alt</Shortcut>);
123 const kbdElement = container.querySelector("kbd");
124 expect(kbdElement).toHaveClass("keyboard-key-special");
125 });
126 });
127});