code-client.test.tsx
This file
Path
apps/web/test/code-client.test.tsx
143 lines · 4.1 KiB · TypeScript
Last changed by
Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248
History of this fileTake it
Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 1why, one step back1import { describe, expect, it } from "@effect/vitest";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 2why, one step back2import { FileTree } from "@pierre/trees";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 3–8why, one step back3import { ChangedFile } from "@repo/protocol/domain/code";4import { Schema } from "effect";5import { renderToStaticMarkup } from "react-dom/server";67import { FileCode } from "../src/features/code/code-view.tsx";8import { DiffCode } from "../src/features/code/diff-view.tsx";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 9–11why, one step back9import {10 followChoices,11 RepoTree,Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
line 12why, one step back12 filterTree,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 13–14why, one step back13 treeOptionsFor,14} from "../src/features/code/repo-tree.tsx";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 15–34why, one step back1516const files = Schema.decodeSync(Schema.Array(ChangedFile))([17 {18 after: "a".repeat(40),19 before: "b".repeat(40),20 binary: false,21 newText: "export const parse = fromIso;\n",22 oldText: "export const parse = Date;\n",23 path: "src/parse.ts",24 status: "modified",25 },26 {27 after: "c".repeat(40),28 binary: false,29 newText: "export const week = 1;\n",30 path: "src/week.ts",31 status: "added",32 },33]);34Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 35–36why, one step back35const PATHS = ["README.md", "src/parse.ts", "src/week.ts"];36Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 37–47why, one step back37describe("the client-only drawings", () => {38 it("draw a file with @pierre/diffs", () => {39 const markup = renderToStaticMarkup(40 <FileCode41 path="src/parse.ts"42 sha={"e".repeat(40)}43 text={"one\ntwo\nthree\n"}44 />45 );4647 expect(markup).toContain("<diffs-container");Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 48why, one step back48 expect(markup).toContain('data-code="pierre"');Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 49–50why, one step back49 });50Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 51why, one step back51 it("draw a commit's diff with one CodeView, which fills the pane and scrolls in it", () => {Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 52–55why, one step back52 const markup = renderToStaticMarkup(53 <DiffCode files={files} split={false} />54 );55Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 56–57why, one step back56 expect(markup).toContain('data-diff="pierre"');57 expect(markup).toMatch(/class="[^"]*\bflex-1\b[^"]*\boverflow-auto\b/u);Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 58–61why, one step back58 });5960 it("draw the repo's file tree with @pierre/trees", () => {61 const markup = renderToStaticMarkup(Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248
line 62why, one step back62 <RepoTree current="src/parse.ts" onOpen={() => {}} paths={PATHS} />Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 63–65why, one step back63 );6465 expect(markup).toContain("<file-tree-container");Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 66–95why, one step back66 });67});6869const treeAt = (current: string) => {70 const opened: string[] = [];71 const tree = new FileTree(treeOptionsFor({ current, paths: PATHS }));7273 followChoices(tree, current, (path) => {74 opened.push(path);75 });7677 return { opened, tree };78};7980describe("the file tree", () => {81 it("hands a chosen file to the page, which moves with the router", () => {82 const { opened, tree } = treeAt("src/parse.ts");8384 tree.getItem("src/week.ts")?.select();8586 expect(opened).toStrictEqual(["src/week.ts"]);87 });8889 it("asks nothing for the page's own file, or a folder", () => {90 const { opened, tree } = treeAt("src/parse.ts");9192 tree.getItem("src/parse.ts")?.select();93 tree.getItem("src/")?.select();9495 expect(opened).toStrictEqual([]);Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
lines 96–97why, one step back96 });97Merge build/1500-design-draws into mainoscabrielbefore adoption9bc46b7
lines 98–108why, one step back98 it("sets a flattened row's leading folders muted and its last name in ink, so the name reads first", () => {99 const css = treeOptionsFor({ paths: PATHS }).unsafeCSS ?? "";100101 expect(css).toMatch(102 /\[data-item-flattened-subitems\]\s*\{[^}]*color:\s*var\(--muted\)/u103 );104 expect(css).toMatch(105 /\[data-item-flattened-subitem\]:last-child\s*\{[^}]*color:\s*var\(--foreground\)/u106 );107 });108Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
lines 109–122why, one step back109 it("keeps its filter outside the tree, which holds only rows", () => {110 const { tree } = treeAt("src/parse.ts");111112 // A field inside a tree's role is not one of its rows to a screen113 // reader: the filter sits above the tree and narrows it from there.114 expect(treeOptionsFor({ paths: PATHS }).search).toBeFalsy();115116 filterTree(tree, "week");117118 expect(tree.getSearchValue()).toBe("week");119120 filterTree(tree, "");121122 expect(tree.isSearchOpen()).toBeFalsy();Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 123–141why, one step back123 });124125 it("opens the folders above the page's file, and selects it", () => {126 const { tree } = treeAt("src/parse.ts");127128 expect(tree.getSelectedPaths()).toStrictEqual(["src/parse.ts"]);129 expect(tree.getItem("src/")?.isDirectory()).toBeTruthy();130 });131132 const sources: Readonly<Record<string, string>> = import.meta.glob(133 "../src/features/code/repo-tree.tsx",134 { eager: true, import: "default", query: "?raw" }135 );136137 it("never reloads the document to open a file", () => {138 const [source = ""] = Object.values(sources);139140 expect(source).toContain("onOpen");141 expect(source).not.toMatch(/location\.(?:assign|href\s*=|replace)/u);Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 142–143why, one step back142 });143});