keyboardShortcuts.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 plain Shift+Tab without modifier keys", () => {
  88. expect(
  89. matchesShiftTab({
  90. key: "Tab",
  91. code: "Tab",
  92. metaKey: false,
  93. ctrlKey: false,
  94. shiftKey: true,
  95. altKey: false,
  96. }),
  97. ).toBe(true);
  98. expect(
  99. matchesShiftTab({
  100. key: "Tab",
  101. code: "Tab",
  102. metaKey: true,
  103. ctrlKey: false,
  104. shiftKey: true,
  105. altKey: false,
  106. }),
  107. ).toBe(false);
  108. expect(
  109. matchesShiftTab({
  110. key: "Tab",
  111. code: "Tab",
  112. metaKey: false,
  113. ctrlKey: true,
  114. shiftKey: true,
  115. altKey: false,
  116. }),
  117. ).toBe(false);
  118. });
  119. it("matches primary shortcuts with the platform primary modifier only", () => {
  120. const mac = { platform: "MacIntel" };
  121. const win = { platform: "Win32" };
  122. expect(
  123. matchesPrimaryShortcut(
  124. {
  125. key: "f",
  126. code: "KeyF",
  127. metaKey: true,
  128. ctrlKey: false,
  129. shiftKey: false,
  130. altKey: false,
  131. },
  132. "f",
  133. mac,
  134. ),
  135. ).toBe(true);
  136. expect(
  137. matchesPrimaryShortcut(
  138. {
  139. key: "f",
  140. code: "KeyF",
  141. metaKey: false,
  142. ctrlKey: true,
  143. shiftKey: false,
  144. altKey: false,
  145. },
  146. "f",
  147. mac,
  148. ),
  149. ).toBe(false);
  150. expect(
  151. matchesPrimaryShortcut(
  152. {
  153. key: "f",
  154. code: "KeyF",
  155. metaKey: false,
  156. ctrlKey: true,
  157. shiftKey: false,
  158. altKey: false,
  159. },
  160. "f",
  161. win,
  162. ),
  163. ).toBe(true);
  164. expect(
  165. matchesPrimaryShortcut(
  166. {
  167. key: "f",
  168. code: "KeyF",
  169. metaKey: true,
  170. ctrlKey: false,
  171. shiftKey: false,
  172. altKey: false,
  173. },
  174. "f",
  175. win,
  176. ),
  177. ).toBe(false);
  178. expect(
  179. matchesPrimaryShortcut(
  180. {
  181. key: "f",
  182. code: "KeyF",
  183. metaKey: true,
  184. ctrlKey: true,
  185. shiftKey: false,
  186. altKey: false,
  187. },
  188. "f",
  189. mac,
  190. ),
  191. ).toBe(false);
  192. });
  193. it("uses the platform primary modifier for shift and alt shortcut variants", () => {
  194. const mac = { platform: "MacIntel" };
  195. const linux = { platform: "Linux x86_64" };
  196. expect(
  197. matchesPrimaryShiftShortcut(
  198. {
  199. key: "P",
  200. code: "KeyP",
  201. metaKey: true,
  202. ctrlKey: false,
  203. shiftKey: true,
  204. altKey: false,
  205. },
  206. "p",
  207. mac,
  208. ),
  209. ).toBe(true);
  210. expect(
  211. matchesPrimaryShiftShortcut(
  212. {
  213. key: "P",
  214. code: "KeyP",
  215. metaKey: false,
  216. ctrlKey: true,
  217. shiftKey: true,
  218. altKey: false,
  219. },
  220. "p",
  221. mac,
  222. ),
  223. ).toBe(false);
  224. expect(
  225. matchesPrimaryAltShortcut(
  226. {
  227. key: "ß",
  228. code: "KeyB",
  229. metaKey: false,
  230. ctrlKey: true,
  231. shiftKey: false,
  232. altKey: true,
  233. },
  234. "b",
  235. linux,
  236. ),
  237. ).toBe(true);
  238. expect(
  239. matchesPrimaryAltShortcut(
  240. {
  241. key: "ß",
  242. code: "KeyB",
  243. metaKey: true,
  244. ctrlKey: false,
  245. shiftKey: false,
  246. altKey: true,
  247. },
  248. "b",
  249. linux,
  250. ),
  251. ).toBe(false);
  252. });
  253. it("formats primary shift shortcuts for each platform", () => {
  254. expect(
  255. formatShiftCommandShortcutLabel("p", {
  256. platform: "MacIntel",
  257. userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0)",
  258. }),
  259. ).toBe("⇧ ⌘ P");
  260. expect(
  261. formatShiftCommandShortcutLabel("p", {
  262. platform: "Linux x86_64",
  263. userAgent: "Mozilla/5.0 (X11; Linux x86_64)",
  264. }),
  265. ).toBe("Ctrl+Shift+P");
  266. expect(
  267. formatShiftCommandShortcutLabel("[", {
  268. platform: "MacIntel",
  269. userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0)",
  270. }),
  271. ).toBe("⇧ ⌘ [");
  272. });
  273. });