keyboardShortcuts.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. formatShiftCommandShortcutLabel,
  4. matchesCtrlShiftShortcut,
  5. matchesCtrlShortcut,
  6. matchesPrimaryAltShortcut,
  7. matchesPrimaryShiftShortcut,
  8. matchesPrimaryShortcut,
  9. matchesShiftTab,
  10. } from "@/lib/keyboardShortcuts.js";
  11. describe("keyboardShortcuts", () => {
  12. it("uses code as a fallback so macOS option-modified keys still match", () => {
  13. expect(
  14. matchesPrimaryAltShortcut(
  15. {
  16. key: "ß",
  17. code: "KeyB",
  18. metaKey: true,
  19. ctrlKey: false,
  20. shiftKey: false,
  21. altKey: true,
  22. },
  23. "b",
  24. { platform: "MacIntel" },
  25. ),
  26. ).toBe(true);
  27. });
  28. it("still matches plain primary shortcuts by key or code", () => {
  29. expect(
  30. matchesPrimaryShortcut(
  31. {
  32. key: "b",
  33. code: "KeyB",
  34. metaKey: true,
  35. ctrlKey: false,
  36. shiftKey: false,
  37. altKey: false,
  38. },
  39. "b",
  40. { platform: "MacIntel" },
  41. ),
  42. ).toBe(true);
  43. });
  44. it("still matches ctrl shortcuts by key or code", () => {
  45. expect(
  46. matchesCtrlShortcut(
  47. {
  48. key: "m",
  49. code: "KeyM",
  50. metaKey: false,
  51. ctrlKey: true,
  52. shiftKey: false,
  53. altKey: false,
  54. },
  55. "m",
  56. ),
  57. ).toBe(true);
  58. });
  59. it("matches ctrl shift shortcuts by key or code", () => {
  60. expect(
  61. matchesCtrlShiftShortcut(
  62. {
  63. key: "M",
  64. code: "KeyM",
  65. metaKey: false,
  66. ctrlKey: true,
  67. shiftKey: true,
  68. altKey: false,
  69. },
  70. "m",
  71. ),
  72. ).toBe(true);
  73. expect(
  74. matchesCtrlShiftShortcut(
  75. {
  76. key: "M",
  77. code: "KeyM",
  78. metaKey: true,
  79. ctrlKey: true,
  80. shiftKey: true,
  81. altKey: false,
  82. },
  83. "m",
  84. ),
  85. ).toBe(false);
  86. });
  87. it("matches primary shift shortcuts by key", () => {
  88. expect(
  89. matchesPrimaryShiftShortcut(
  90. {
  91. key: "P",
  92. code: "KeyP",
  93. metaKey: true,
  94. ctrlKey: false,
  95. shiftKey: true,
  96. altKey: false,
  97. },
  98. "p",
  99. { platform: "MacIntel" },
  100. ),
  101. ).toBe(true);
  102. });
  103. it("matches plain Shift+Tab without modifier keys", () => {
  104. expect(
  105. matchesShiftTab({
  106. key: "Tab",
  107. code: "Tab",
  108. metaKey: false,
  109. ctrlKey: false,
  110. shiftKey: true,
  111. altKey: false,
  112. }),
  113. ).toBe(true);
  114. expect(
  115. matchesShiftTab({
  116. key: "Tab",
  117. code: "Tab",
  118. metaKey: true,
  119. ctrlKey: false,
  120. shiftKey: true,
  121. altKey: false,
  122. }),
  123. ).toBe(false);
  124. expect(
  125. matchesShiftTab({
  126. key: "Tab",
  127. code: "Tab",
  128. metaKey: false,
  129. ctrlKey: true,
  130. shiftKey: true,
  131. altKey: false,
  132. }),
  133. ).toBe(false);
  134. });
  135. it("matches primary shortcuts with the platform primary modifier only", () => {
  136. const mac = { platform: "MacIntel" };
  137. const win = { platform: "Win32" };
  138. expect(
  139. matchesPrimaryShortcut(
  140. {
  141. key: "f",
  142. code: "KeyF",
  143. metaKey: true,
  144. ctrlKey: false,
  145. shiftKey: false,
  146. altKey: false,
  147. },
  148. "f",
  149. mac,
  150. ),
  151. ).toBe(true);
  152. expect(
  153. matchesPrimaryShortcut(
  154. {
  155. key: "f",
  156. code: "KeyF",
  157. metaKey: false,
  158. ctrlKey: true,
  159. shiftKey: false,
  160. altKey: false,
  161. },
  162. "f",
  163. mac,
  164. ),
  165. ).toBe(false);
  166. expect(
  167. matchesPrimaryShortcut(
  168. {
  169. key: "f",
  170. code: "KeyF",
  171. metaKey: false,
  172. ctrlKey: true,
  173. shiftKey: false,
  174. altKey: false,
  175. },
  176. "f",
  177. win,
  178. ),
  179. ).toBe(true);
  180. expect(
  181. matchesPrimaryShortcut(
  182. {
  183. key: "f",
  184. code: "KeyF",
  185. metaKey: true,
  186. ctrlKey: false,
  187. shiftKey: false,
  188. altKey: false,
  189. },
  190. "f",
  191. win,
  192. ),
  193. ).toBe(false);
  194. expect(
  195. matchesPrimaryShortcut(
  196. {
  197. key: "f",
  198. code: "KeyF",
  199. metaKey: true,
  200. ctrlKey: true,
  201. shiftKey: false,
  202. altKey: false,
  203. },
  204. "f",
  205. mac,
  206. ),
  207. ).toBe(false);
  208. });
  209. it("uses the platform primary modifier for shift and alt shortcut variants", () => {
  210. const mac = { platform: "MacIntel" };
  211. const linux = { platform: "Linux x86_64" };
  212. expect(
  213. matchesPrimaryShiftShortcut(
  214. {
  215. key: "P",
  216. code: "KeyP",
  217. metaKey: true,
  218. ctrlKey: false,
  219. shiftKey: true,
  220. altKey: false,
  221. },
  222. "p",
  223. mac,
  224. ),
  225. ).toBe(true);
  226. expect(
  227. matchesPrimaryShiftShortcut(
  228. {
  229. key: "P",
  230. code: "KeyP",
  231. metaKey: false,
  232. ctrlKey: true,
  233. shiftKey: true,
  234. altKey: false,
  235. },
  236. "p",
  237. mac,
  238. ),
  239. ).toBe(false);
  240. expect(
  241. matchesPrimaryAltShortcut(
  242. {
  243. key: "ß",
  244. code: "KeyB",
  245. metaKey: false,
  246. ctrlKey: true,
  247. shiftKey: false,
  248. altKey: true,
  249. },
  250. "b",
  251. linux,
  252. ),
  253. ).toBe(true);
  254. expect(
  255. matchesPrimaryAltShortcut(
  256. {
  257. key: "ß",
  258. code: "KeyB",
  259. metaKey: true,
  260. ctrlKey: false,
  261. shiftKey: false,
  262. altKey: true,
  263. },
  264. "b",
  265. linux,
  266. ),
  267. ).toBe(false);
  268. });
  269. it("formats primary shift shortcuts for each platform", () => {
  270. expect(
  271. formatShiftCommandShortcutLabel("p", {
  272. platform: "MacIntel",
  273. userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0)",
  274. }),
  275. ).toBe("⇧ ⌘ P");
  276. expect(
  277. formatShiftCommandShortcutLabel("p", {
  278. platform: "Linux x86_64",
  279. userAgent: "Mozilla/5.0 (X11; Linux x86_64)",
  280. }),
  281. ).toBe("Ctrl+Shift+P");
  282. expect(
  283. formatShiftCommandShortcutLabel("[", {
  284. platform: "MacIntel",
  285. userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0)",
  286. }),
  287. ).toBe("⇧ ⌘ [");
  288. });
  289. });