@@ -447,22 +447,21 @@ async function proveToolbarTracking(page: Page) {
447447 await expect
448448 . poll ( async ( ) => ( await popup . boundingBox ( ) ) ?. width ?? 0 )
449449 . toBeGreaterThan ( header . width + 20 )
450- await expect
451- . poll ( async ( ) => {
452- const caret = await page . evaluate ( ( ) => {
453- const selection = getSelection ( )
454- if ( ! selection ?. rangeCount ) return null
455- const rect = selection . getRangeAt ( 0 ) . getBoundingClientRect ( )
456- return ( rect . left + rect . right ) / 2
457- } )
458- const bounds = await popup . boundingBox ( )
459- return caret === null || ! bounds
460- ? Number . POSITIVE_INFINITY
461- : Math . abs ( bounds . x + bounds . width / 2 - caret )
450+ const distanceFromSelection = ( ) =>
451+ popup . evaluate ( ( element ) => {
452+ const selection = getSelection ( )
453+ if ( ! selection ?. rangeCount ) return Number . POSITIVE_INFINITY
454+ const rect = selection . getRangeAt ( 0 ) . getBoundingClientRect ( )
455+ const bounds = element . getBoundingClientRect ( )
456+ return Math . abs ( ( bounds . left + bounds . right - rect . left - rect . right ) / 2 )
462457 } )
463- . toBeLessThan ( 4 )
464- const before = await popup . boundingBox ( )
465- assert ( before )
458+ await expect
459+ . poll ( distanceFromSelection , { message : 'Toolbar follows selection after controls resize' } )
460+ . toBeLessThan ( 2 )
461+ await page . setViewportSize ( { width : 1400 , height : 1000 } )
462+ await expect
463+ . poll ( distanceFromSelection , { message : 'Toolbar follows selection after viewport resize' } )
464+ . toBeLessThan ( 2 )
466465 const movement = await table . evaluate ( ( element ) => {
467466 const wrapper = element . parentElement
468467 if ( ! wrapper ) throw new Error ( 'Missing table viewport' )
@@ -472,19 +471,13 @@ async function proveToolbarTracking(page: Page) {
472471 } )
473472 assert ( movement . delta > 32 , 'The table did not move with its toolbar open' )
474473 await expect
475- . poll ( async ( ) =>
476- Math . abs (
477- ( ( await popup . boundingBox ( ) ) ?. x ?? Number . POSITIVE_INFINITY ) - before . x + movement . delta
478- )
479- )
474+ . poll ( distanceFromSelection , { message : 'Toolbar follows selection after scrolling forward' } )
480475 . toBeLessThan ( 2 )
481476 await table . evaluate ( ( element , left ) => {
482477 if ( element . parentElement ) element . parentElement . scrollLeft = left
483478 } , movement . from )
484479 await expect
485- . poll ( async ( ) =>
486- Math . abs ( ( ( await popup . boundingBox ( ) ) ?. x ?? Number . POSITIVE_INFINITY ) - before . x )
487- )
480+ . poll ( distanceFromSelection , { message : 'Toolbar follows selection after scrolling back' } )
488481 . toBeLessThan ( 2 )
489482 await page . screenshot ( { path : resolve ( artifactDir , `toolbar-tracking-${ checks . length } .png` ) } )
490483 await page
0 commit comments