Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { render } from "../../../test/render";
4import { MiddleTruncate } from "./middle-truncate";
5
6import "~/index.css";
7
8const narrow = (value: string) => (
9 <div style={{ width: 80 }}>
10 <MiddleTruncate value={value} className="w-full" />
11 </div>
12);
13
14describe("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.each([
28 ["Fix the sidebar", 7],
29 ["report\u2003\u2003draft.txt", 9],
30 ])("renders %j split as wide as whole", async (value, tail) => {
31 await using screen = await render(
32 <div>
33 <MiddleTruncate value={value} tail={tail} data-testid="split" />
34 <span data-testid="whole" className="whitespace-nowrap">
35 {value}
36 </span>
37 </div>,
38 );
39 const width = (id: string) => screen.getByTestId(id).element().getBoundingClientRect().width;
40
41 expect(width("split")).toBeCloseTo(width("whole"), 0);
42 });
43
44 it("keeps the raw value's tail when collapsing its spaces", async () => {
45 await using screen = await render(<MiddleTruncate value={"longprefix a b.txt"} />);
46
47 expect(screen.container.querySelector("[title] > span:last-child")?.textContent).toBe(
48 "x a b.txt",
49 );
50 });
51});