Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

projectScriptKeybindings.test.ts

83 lines2.2 KB

1import { MAX_KEYBINDING_VALUE_LENGTH, type KeybindingCommand } from "@v12/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { commandForProjectScript } from "../projectScripts";
5import {
6 decodeProjectScriptKeybindingRule,
7 keybindingValueForCommand,
8 PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE,
9} from "./projectScriptKeybindings";
10​
11describe("projectScriptKeybindings", () => {
12 it("decodes and trims valid keybinding rules", () => {
13 const rule = decodeProjectScriptKeybindingRule({
14 keybinding: " mod+k ",
15 command: commandForProjectScript("lint"),
16 });
17​
18 expect(rule).toEqual({
19 key: "mod+k",
20 command: "script.lint.run",
21 });
22 });
23​
24 it("returns null when keybinding is empty", () => {
25 expect(
26 decodeProjectScriptKeybindingRule({
27 keybinding: " ",
28 command: commandForProjectScript("lint"),
29 }),
30 ).toBeNull();
31 });
32​
33 it("rejects invalid keybinding values", () => {
34 expect(() =>
35 decodeProjectScriptKeybindingRule({
36 keybinding: "k".repeat(MAX_KEYBINDING_VALUE_LENGTH + 1),
37 command: commandForProjectScript("lint"),
38 }),
39 ).toThrowError(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE);
40 });
41​
42 it("rejects invalid commands", () => {
43 expect(() =>
44 decodeProjectScriptKeybindingRule({
45 keybinding: "mod+k",
46 command: "script.BAD.run" as KeybindingCommand,
47 }),
48 ).toThrowError(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE);
49 });
50​
51 it("reads latest matching keybinding value for a command", () => {
52 const command = commandForProjectScript("test");
53 const value = keybindingValueForCommand(
54 [
55 {
56 command,
57 shortcut: {
58 key: "escape",
59 metaKey: false,
60 ctrlKey: false,
61 shiftKey: false,
62 altKey: false,
63 modKey: true,
64 },
65 },
66 {
67 command,
68 shortcut: {
69 key: "k",
70 metaKey: false,
71 ctrlKey: false,
72 shiftKey: true,
73 altKey: false,
74 modKey: true,
75 },
76 },
77 ],
78 command,
79 );
80​
81 expect(value).toBe("mod+shift+k");
82 });
83});