The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import { render } from "../../../test/render"; |
| 4 | import { MiddleTruncate } from "./middle-truncate"; |
| 5 | |
| 6 | import "~/index.css"; |
| 7 | |
| 8 | const narrow = (value: string) => ( |
| 9 | <div style={{ width: 80 }}> |
| 10 | <MiddleTruncate value={value} className="w-full" /> |
| 11 | </div> |
| 12 | ); |
| 13 | |
| 14 | describe("MiddleTruncate", () => { |
| 15 | it("does not clip a short value that replaces a truncated one", async () => { |
| 16 | await using screen = await render(narrow("t3/investigate-release-build-times")); |
| 17 | const label = () => screen.container.querySelector("[title] > span"); |
| 18 | await expect.poll(() => label()?.hasAttribute("data-overflow")).toBe(true); |
| 19 | |
| 20 | await screen.rerender(narrow("main")); |
| 21 | |
| 22 | expect(label()).toHaveTextContent("main"); |
| 23 | expect(label()).not.toHaveAttribute("data-overflow"); |
| 24 | expect(label()).toHaveStyle({ clipPath: "none" }); |
| 25 | }); |
| 26 | |
| 27 | it("fits every head mounted in the same commit", async () => { |
| 28 | const values = ["t3/investigate-release-build-times", "t3/remove-stale-worktree-branches"]; |
| 29 | await using screen = await render( |
| 30 | <> |
| 31 | {values.map((value) => ( |
| 32 | <div key={value}>{narrow(value)}</div> |
| 33 | ))} |
| 34 | </>, |
| 35 | ); |
| 36 | const heads = () => [ |
| 37 | ...screen.container.querySelectorAll<HTMLElement>("[title] > span:first-child"), |
| 38 | ]; |
| 39 | |
| 40 | await vi.waitFor(() => |
| 41 | expect(heads().filter((head) => head.hasAttribute("data-overflow"))).toHaveLength(2), |
| 42 | ); |
| 43 | }); |
| 44 | |
| 45 | it.each([ |
| 46 | ["Fix the sidebar", 7], |
| 47 | ["report\u2003\u2003draft.txt", 9], |
| 48 | ])("renders %j split as wide as whole", async (value, tail) => { |
| 49 | await using screen = await render( |
| 50 | <div> |
| 51 | <MiddleTruncate value={value} tail={tail} data-testid="split" /> |
| 52 | <span data-testid="whole" className="whitespace-nowrap"> |
| 53 | {value} |
| 54 | </span> |
| 55 | </div>, |
| 56 | ); |
| 57 | const width = (id: string) => screen.getByTestId(id).element().getBoundingClientRect().width; |
| 58 | |
| 59 | expect(width("split")).toBeCloseTo(width("whole"), 0); |
| 60 | }); |
| 61 | |
| 62 | it("keeps the raw value's tail when collapsing its spaces", async () => { |
| 63 | await using screen = await render(<MiddleTruncate value={"longprefix a b.txt"} />); |
| 64 | |
| 65 | expect(screen.container.querySelector("[title] > span:last-child")?.textContent).toBe( |
| 66 | "x a b.txt", |
| 67 | ); |
| 68 | }); |
| 69 | }); |