Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DraftProjectName.browser.test.tsx

103 lines3.1 KB

1import "~/index.css";
2​
3import { act } from "react";
4import { createRoot, type Root } from "react-dom/client";
5import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6​
7import { InlineButton } from "../ui/button";
8import { DraftProjectName } from "./DraftProjectName";
9​
10let root: Root;
11let host: HTMLDivElement;
12​
13beforeEach(() => {
14 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
15 host = document.body.appendChild(document.createElement("div"));
16 host.style.width = "700px";
17 host.style.zoom = "1.31";
18 root = createRoot(host);
19});
20​
21afterEach(async () => {
22 await act(async () => root.unmount());
23 host.remove();
24 vi.restoreAllMocks();
25 vi.unstubAllGlobals();
26});
27​
28async function render(value: string, placement: "headline" | "row") {
29 await act(async () => {
30 root.render(
31 placement === "headline" ? (
32 <h1 className="w-full text-center text-3xl tracking-tight">
33 What should we build in{" "}
34 <span className="whitespace-nowrap">
35 <InlineButton tone="picker" className="max-w-[calc(100%-1em)] align-baseline">
36 <DraftProjectName value={value} />
37 </InlineButton>
38 ?
39 </span>
40 </h1>
41 ) : (
42 <div className="flex w-full items-center gap-2 text-sm font-medium">
43 <span className="size-4 shrink-0" />
44 <span className="min-w-0 flex-1">
45 <DraftProjectName value={value} />
46 </span>
47 </div>
48 ),
49 );
50 });
52​
53function label() {
54 return host.querySelector<HTMLSpanElement>("span[aria-label]")!;
56​
57it.each(["headline", "row"] as const)(
58 "keeps fitting names in the %s even when canvas metrics disagree with layout",
59 async (placement) => {
60 host.style.zoom = "1";
61 const measureText = CanvasRenderingContext2D.prototype.measureText;
62 vi.spyOn(CanvasRenderingContext2D.prototype, "measureText").mockImplementation(function (
63 this: CanvasRenderingContext2D,
64 text,
65 ) {
66 const metrics = measureText.call(this, text);
67 Object.defineProperty(metrics, "width", { value: metrics.width + 8 });
68 return metrics;
69 });
70​
71 for (const value of ["t3code", "mozart_v2", "No project", "Add project"]) {
72 await render(value, placement);
73 expect(label().textContent).toBe(value);
74 }
75 },
76);
77​
78it.each(["headline", "row"] as const)(
79 "fits long names in the %s and restores them when the available space grows",
80 async (placement) => {
81 const value = "ExampleCompany-CustomerPortal-App";
82 await render(value, placement);
83 expect(label().textContent).toBe(value);
84​
85 await act(async () => {
86 host.style.width = "180px";
87 await expect.poll(() => label().textContent).toContain("…");
88 });
89 const shortened = label().textContent!;
90 expect(shortened.startsWith("Ex")).toBe(true);
91 expect(shortened.endsWith("App")).toBe(true);
92 const text = document.createRange();
93 text.selectNodeContents(label());
94 expect(text.getBoundingClientRect().width).toBeLessThanOrEqual(
95 label().getBoundingClientRect().width,
96 );
97​
98 await act(async () => {
99 host.style.width = "700px";
100 await expect.poll(() => label().textContent).toBe(value);
101 });
102 },
103);