keys.test.tsx
This file
Path
apps/web/test/keys.test.tsx
200 lines · 5.3 KiB · TypeScript
Last changed by
Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248
History of this fileTake it
Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e
lines 1–134why, one step back1import { describe, expect, it } from "@effect/vitest";2import { renderToStaticMarkup } from "react-dom/server";34import {5 intentOf,6 KeysHelp,7 readShortcuts,8 writeShortcuts,9} from "../src/features/shared/keys.tsx";1011const press = (key: string, inField = false) => ({12 altKey: false,13 ctrlKey: false,14 inField,15 key,16 metaKey: false,17});1819const refusing: Storage = {20 clear: () => {21 throw new Error("blocked");22 },23 getItem: () => {24 throw new Error("blocked");25 },26 key: () => {27 throw new Error("blocked");28 },29 get length(): number {30 throw new Error("blocked");31 },32 removeItem: () => {33 throw new Error("blocked");34 },35 setItem: () => {36 throw new Error("blocked");37 },38};3940const kept = (): Storage => {41 const items = new Map<string, string>();4243 return {44 clear: () => {45 items.clear();46 },47 getItem: (name) => items.get(name) ?? null,48 key: (index) => [...items.keys()][index] ?? null,49 get length() {50 return items.size;51 },52 removeItem: (name) => {53 items.delete(name);54 },55 setItem: (name, value) => {56 items.set(name, value);57 },58 };59};6061describe("a single key", () => {62 it("opens the keys dialog with ?", () => {63 expect(intentOf(press("?"), true)).toStrictEqual({ kind: "keys" });64 });6566 it("moves through the page's list with j and k, and its files with J and K", () => {67 expect(68 ["j", "k", "J", "K"].map((key) => intentOf(press(key), true))69 ).toStrictEqual([70 { kind: "next" },71 { kind: "previous" },72 { kind: "nextFile" },73 { kind: "previousFile" },74 ]);75 });7677 it("copies the page's numbered commands with 1 to 9, and nothing with 0", () => {78 expect(intentOf(press("1"), true)).toStrictEqual({79 index: 1,80 kind: "copy",81 });82 expect(intentOf(press("9"), true)).toStrictEqual({83 index: 9,84 kind: "copy",85 });86 expect(intentOf(press("0"), true)).toBeUndefined();87 });8889 it("clears with Escape", () => {90 expect(intentOf(press("Escape"), true)).toStrictEqual({ kind: "clear" });91 });9293 it("does nothing once the switch is off, but ? still opens the dialog that holds it", () => {94 expect(intentOf(press("j"), false)).toBeUndefined();95 expect(intentOf(press("1"), false)).toBeUndefined();96 expect(intentOf(press("?"), false)).toStrictEqual({ kind: "keys" });97 });9899 it("does nothing while focus is in a text field, ? and Escape included", () => {100 expect(101 ["j", "?", "1", "Escape"].map((key) => intentOf(press(key, true), true))102 ).toStrictEqual([undefined, undefined, undefined, undefined]);103 });104105 it("leaves a press with Ctrl, Alt or Meta to the browser", () => {106 expect(intentOf({ ...press("k"), ctrlKey: true }, true)).toBeUndefined();107 expect(intentOf({ ...press("j"), metaKey: true }, true)).toBeUndefined();108 expect(intentOf({ ...press("1"), altKey: true }, true)).toBeUndefined();109 });110});111112describe("the switch", () => {113 it("is on until it is turned off, and stays as it was left", () => {114 const storage = kept();115116 expect(readShortcuts(storage)).toBeTruthy();117118 writeShortcuts(storage, false);119120 expect(readShortcuts(storage)).toBeFalsy();121122 writeShortcuts(storage, true);123124 expect(readShortcuts(storage)).toBeTruthy();125 });126127 it("is on when the storage refuses to read, and a refused write changes nothing", () => {128 expect(readShortcuts(refusing)).toBeTruthy();129 expect(() => {130 writeShortcuts(refusing, false);131 }).not.toThrow();132 expect(readShortcuts()).toBeTruthy();133 });134Merge build/1500-design-draws into mainoscabrielbefore adoption9bc46b7
lines 135–166why, one step back135 it("draws each key as a key: label type on the paper, a hairline border, the 2 px radius, 4 px of side padding", () => {136 const markup = renderToStaticMarkup(137 <KeysHelp138 on139 onChange={() => {140 // A static render never presses it.141 }}142 />143 );144145 const caps = [146 ...markup.matchAll(/<kbd\b[^>]*class="(?<classes>[^"]*)"/gu),147 ].map(({ groups }) => groups?.classes?.split(" ") ?? []);148149 expect(caps.length).toBeGreaterThan(0);150 expect(151 caps.filter(152 (classes) =>153 ![154 "bg-background",155 "border",156 "border-rule",157 "rounded-md",158 "px-1",159 "text-label",160 "font-sans",161 ].every((one) => classes.includes(one)) ||162 classes.includes("font-mono")163 )164 ).toStrictEqual([]);165 });166Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e
lines 167–186why, one step back167 it("is in the dialog, with every key and what it does", () => {168 const markup = renderToStaticMarkup(169 <KeysHelp170 on={false}171 onChange={() => {172 // A static render never presses it.173 }}174 />175 );176177 const text = markup.replaceAll(/<[^>]+>/gu, " ");178179 expect(text).toContain("Single-key shortcuts");180 expect(markup).toMatch(181 /<button\b[^>]*aria-pressed="true"[^>]*>Off<\/button>/u182 );183 expect(markup).toMatch(184 /<button\b[^>]*aria-pressed="false"[^>]*>On<\/button>/u185 );186Review fixes for 1203oscabrielbefore adoption950d5e0
lines 187–194why, one step back187 // Each key cap at the label size, whatever face it is set in.188 expect(189 [...markup.matchAll(/<kbd\b[^>]*class="(?<classes>[^"]*)"/gu)].filter(190 ({ groups }) =>191 groups?.classes?.split(" ").includes("text-label") !== true192 )193 ).toStrictEqual([]);194Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e
lines 195–200why, one step back195 for (const key of ["?", "j", "k", "J", "K", "1", "9", "Esc"]) {196 expect(markup).toContain(`<kbd`);197 expect(text).toContain(key);198 }199 });200});