|
| 1 | +import { expect, it, vi } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { render } from "../../../test/render"; |
| 4 | +import "../../index.css"; |
| 5 | + |
| 6 | +import { MarkdownLinkContext } from "./ChatMarkdownMedia"; |
| 7 | +import { MarkdownExternalLinkContent } from "./MarkdownExternalLink"; |
| 8 | + |
| 9 | +it.each(["plain", "formatted"] as const)( |
| 10 | + "keeps %s long links bounded in the DOM without losing text or wrapping", |
| 11 | + async (kind) => { |
| 12 | + const link = (url: string) => ( |
| 13 | + <div className="chat-markdown w-64"> |
| 14 | + <table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td> |
| 18 | + <a href={url}> |
| 19 | + <MarkdownExternalLinkContent |
| 20 | + host="github.com" |
| 21 | + plainText={kind === "plain" ? url : null} |
| 22 | + > |
| 23 | + <MarkdownLinkContext value> |
| 24 | + {url} |
| 25 | + {kind === "formatted" ? <strong> details</strong> : null} |
| 26 | + </MarkdownLinkContext> |
| 27 | + </MarkdownExternalLinkContent> |
| 28 | + </a> |
| 29 | + </td> |
| 30 | + </tr> |
| 31 | + </tbody> |
| 32 | + </table> |
| 33 | + </div> |
| 34 | + ); |
| 35 | + const url = (length: number) => `https://github.com/search?q=${"a".repeat(length)}`; |
| 36 | + await using screen = await render(link(url(128))); |
| 37 | + const initialElementCount = screen.container.querySelectorAll("a *").length; |
| 38 | + |
| 39 | + const longUrl = url(16_000); |
| 40 | + await screen.rerender(link(longUrl)); |
| 41 | + await vi.waitFor(() => { |
| 42 | + const anchor = screen.container.querySelector("a")!; |
| 43 | + const table = screen.container.querySelector("table")!; |
| 44 | + expect(anchor.getAttribute("href")).toBe(longUrl); |
| 45 | + expect(anchor.textContent).toBe(longUrl + (kind === "formatted" ? " details" : "")); |
| 46 | + expect(anchor.querySelectorAll("*")).toHaveLength(initialElementCount); |
| 47 | + if (kind === "formatted") |
| 48 | + expect(anchor.querySelector("strong")?.textContent).toBe(" details"); |
| 49 | + expect(table.scrollWidth).toBeLessThanOrEqual( |
| 50 | + screen.container.firstElementChild!.clientWidth, |
| 51 | + ); |
| 52 | + }); |
| 53 | + }, |
| 54 | +); |
0 commit comments