@@ -3,8 +3,10 @@ import "~/index.css";
33import { act } from "react" ;
44import { createRoot , type Root } from "react-dom/client" ;
55import { afterEach , beforeEach , expect , it , vi } from "vite-plus/test" ;
6+ import { page } from "vite-plus/test/browser" ;
67
78import { InlineButton } from "../ui/button" ;
9+ import { DraftHeroProjectHeading } from "./DraftHeroProjectHeading" ;
810import { DraftProjectName } from "./DraftProjectName" ;
911
1012let root : Root ;
@@ -14,30 +16,29 @@ beforeEach(() => {
1416 vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
1517 host = document . body . appendChild ( document . createElement ( "div" ) ) ;
1618 host . style . width = "700px" ;
17- host . style . zoom = "1.31 " ;
19+ host . style . maxWidth = "100% " ;
1820 root = createRoot ( host ) ;
1921} ) ;
2022
2123afterEach ( async ( ) => {
2224 await act ( async ( ) => root . unmount ( ) ) ;
2325 host . remove ( ) ;
24- vi . restoreAllMocks ( ) ;
2526 vi . unstubAllGlobals ( ) ;
2627} ) ;
2728
28- async function render ( value : string , placement : "headline" | "row" ) {
29+ async function render ( value : string , placement : "headline" | "row" = "headline" ) {
2930 await act ( async ( ) => {
3031 root . render (
3132 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 >
33+ < div data-testid = "composer" className = "relative mt-64 h-32 w-full " >
34+ < div className = "absolute inset-x-0 bottom-full pb-4" >
35+ < DraftHeroProjectHeading projectName = { value } >
36+ < InlineButton tone = "picker" className = "min -w-0 max-w-full align-baseline" >
37+ < DraftProjectName value = { value } singleLine />
38+ </ InlineButton >
39+ </ DraftHeroProjectHeading >
40+ </ div >
41+ </ div >
4142 ) : (
4243 < div className = "flex w-full items-center gap-2 text-sm font-medium" >
4344 < span className = "size-4 shrink-0" />
@@ -50,54 +51,135 @@ async function render(value: string, placement: "headline" | "row") {
5051 } ) ;
5152}
5253
53- function label ( ) {
54- return host . querySelector < HTMLSpanElement > ( "span[aria-label]" ) ! ;
54+ const label = ( ) => host . querySelector < HTMLSpanElement > ( "span[aria-label]" ) ! ;
55+ const title = ( ) => host . querySelector < HTMLSpanElement > ( "h1 > span" ) ! ;
56+
57+ function expectWithinHost ( element : Element ) {
58+ const bounds = element . getBoundingClientRect ( ) ;
59+ const hostBounds = host . getBoundingClientRect ( ) ;
60+ expect ( bounds . left ) . toBeGreaterThanOrEqual ( hostBounds . left ) ;
61+ expect ( bounds . right ) . toBeLessThanOrEqual ( hostBounds . right ) ;
62+ }
63+
64+ function expectAboveComposer ( ) {
65+ const heading = host . querySelector ( "h1" ) ! ;
66+ const composer = host . querySelector ( '[data-testid="composer"]' ) ! ;
67+ expectWithinHost ( heading ) ;
68+ expectWithinHost ( label ( ) ) ;
69+ expect ( heading . getBoundingClientRect ( ) . bottom ) . toBeLessThan ( composer . getBoundingClientRect ( ) . top ) ;
5570}
5671
57- it . 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 ) ;
72+ it . each ( [ 320 , 390 , 822 ] ) (
73+ "places the full short project name below the title in smaller text at %ipx" ,
74+ async ( width ) => {
75+ await page . viewport ( width , 800 ) ;
76+ host . style . width = `${ width - 32 } px` ;
77+ for ( const value of [ "t3code" , "obsidian-arr" , "No project" ] ) {
78+ await render ( value ) ;
79+ expect ( title ( ) . innerText ) . toBe ( "What should we build?" ) ;
7380 expect ( label ( ) . textContent ) . toBe ( value ) ;
81+ expect ( label ( ) . scrollWidth ) . toBeLessThanOrEqual ( label ( ) . clientWidth ) ;
82+ expect ( label ( ) . scrollHeight ) . toBeLessThanOrEqual ( label ( ) . clientHeight ) ;
83+ expect ( label ( ) . getBoundingClientRect ( ) . top ) . toBeGreaterThanOrEqual (
84+ title ( ) . getBoundingClientRect ( ) . bottom ,
85+ ) ;
86+ expect ( Number . parseFloat ( getComputedStyle ( label ( ) ) . fontSize ) ) . toBeLessThan (
87+ Number . parseFloat ( getComputedStyle ( title ( ) ) . fontSize ) ,
88+ ) ;
89+ expectAboveComposer ( ) ;
7490 }
7591 } ,
7692) ;
7793
78- it . 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 ) ;
94+ it ( "uses the inline heading only when the viewport and chat lane both have room ", async ( ) => {
95+ await page . viewport ( 1440 , 900 ) ;
96+ await render ( "obsidian-arr" ) ;
97+ expect ( title ( ) . innerText ) . toBe ( "What should we build in" ) ;
98+ expect ( label ( ) . getBoundingClientRect ( ) . top ) . toBeLessThan ( title ( ) . getBoundingClientRect ( ) . bottom ) ;
99+ expectAboveComposer ( ) ;
84100
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- ) ;
101+ host . style . width = "350px" ;
102+ await expect . poll ( ( ) => title ( ) . innerText ) . toBe ( "What should we build?" ) ;
103+ expect ( label ( ) . getBoundingClientRect ( ) . top ) . toBeGreaterThanOrEqual (
104+ title ( ) . getBoundingClientRect ( ) . bottom ,
105+ ) ;
106+ expectAboveComposer ( ) ;
107+ } ) ;
97108
98- await act ( async ( ) => {
99- host . style . width = "700px" ;
100- await expect . poll ( ( ) => label ( ) . textContent ) . toBe ( value ) ;
101- } ) ;
109+ it . each ( [ 390 , 1440 ] ) (
110+ "uses a single smaller row and truncates only its remaining overflow at %ipx" ,
111+ async ( width ) => {
112+ await page . viewport ( width , 900 ) ;
113+ host . style . width = `${ Math . min ( width - 32 , 700 ) } px` ;
114+ const value = "ExampleCompany-CustomerPortal-Production-" . repeat ( 5 ) ;
115+ await render ( value ) ;
116+ expect ( label ( ) . textContent ) . toBe ( value ) ;
117+ expect ( label ( ) . getAttribute ( "aria-label" ) ) . toBe ( value ) ;
118+ expect ( title ( ) . innerText ) . toBe ( "What should we build?" ) ;
119+ expect ( label ( ) . getBoundingClientRect ( ) . top ) . toBeGreaterThanOrEqual (
120+ title ( ) . getBoundingClientRect ( ) . bottom ,
121+ ) ;
122+ expect ( Number . parseFloat ( getComputedStyle ( label ( ) ) . fontSize ) ) . toBeLessThan (
123+ Number . parseFloat ( getComputedStyle ( title ( ) ) . fontSize ) ,
124+ ) ;
125+ expect ( label ( ) . scrollWidth ) . toBeGreaterThan ( label ( ) . clientWidth ) ;
126+ expect ( label ( ) . clientHeight ) . toBe ( Number . parseFloat ( getComputedStyle ( label ( ) ) . lineHeight ) ) ;
127+ expect ( getComputedStyle ( label ( ) ) . textOverflow ) . toBe ( "ellipsis" ) ;
128+ expect ( document . documentElement . scrollWidth ) . toBeLessThanOrEqual ( window . innerWidth ) ;
129+ expectAboveComposer ( ) ;
102130 } ,
103131) ;
132+
133+ it ( "keeps short picker rows intact and bounds long rows at fractional zoom" , async ( ) => {
134+ await page . viewport ( 1280 , 800 ) ;
135+ host . style . width = "240px" ;
136+ host . style . zoom = "1.31" ;
137+ for ( const value of [ "t3code" , "obsidian-arr" , "Add project" , "LongProjectName-" . repeat ( 10 ) ] ) {
138+ await render ( value , "row" ) ;
139+ expect ( label ( ) . textContent ) . toBe ( value ) ;
140+ expectWithinHost ( label ( ) ) ;
141+ expect ( label ( ) . clientHeight ) . toBeLessThanOrEqual (
142+ Number . parseFloat ( getComputedStyle ( label ( ) ) . lineHeight ) * 2 ,
143+ ) ;
144+ }
145+ } ) ;
146+
147+ it ( "moves a name below the heading when it stops fitting and returns it inline when space recovers" , async ( ) => {
148+ await page . viewport ( 1440 , 900 ) ;
149+ host . style . width = "900px" ;
150+ const value = "CustomerPortal-Production" ;
151+ await render ( value ) ;
152+ expect ( title ( ) . innerText ) . toBe ( "What should we build in" ) ;
153+
154+ await act ( async ( ) => {
155+ host . style . width = "650px" ;
156+ await expect . poll ( ( ) => title ( ) . innerText ) . toBe ( "What should we build?" ) ;
157+ } ) ;
158+ expect ( label ( ) . textContent ) . toBe ( value ) ;
159+ expect ( label ( ) . scrollWidth ) . toBeLessThanOrEqual ( label ( ) . clientWidth ) ;
160+ expect ( label ( ) . scrollHeight ) . toBeLessThanOrEqual ( label ( ) . clientHeight ) ;
161+ expectAboveComposer ( ) ;
162+
163+ await act ( async ( ) => {
164+ host . style . width = "900px" ;
165+ await expect . poll ( ( ) => title ( ) . innerText ) . toBe ( "What should we build in" ) ;
166+ } ) ;
167+ expect ( label ( ) . textContent ) . toBe ( value ) ;
168+ expectAboveComposer ( ) ;
169+ } ) ;
170+
171+ it ( "rechecks inline fit when the selected project changes" , async ( ) => {
172+ await page . viewport ( 1440 , 900 ) ;
173+ await render ( "ExampleCompany-CustomerPortal-Production-Environment" ) ;
174+ expect ( title ( ) . innerText ) . toBe ( "What should we build?" ) ;
175+
176+ await render ( "obsidian-arr" ) ;
177+ await expect . poll ( ( ) => title ( ) . innerText ) . toBe ( "What should we build in" ) ;
178+ expect ( label ( ) . textContent ) . toBe ( "obsidian-arr" ) ;
179+
180+ await render ( "ExampleCompany-CustomerPortal-Production-Environment" ) ;
181+ await expect . poll ( ( ) => title ( ) . innerText ) . toBe ( "What should we build?" ) ;
182+ expect ( label ( ) . scrollWidth ) . toBeLessThanOrEqual ( label ( ) . clientWidth ) ;
183+ expect ( label ( ) . scrollHeight ) . toBeLessThanOrEqual ( label ( ) . clientHeight ) ;
184+ expectAboveComposer ( ) ;
185+ } ) ;
0 commit comments