@@ -100,6 +100,28 @@ async function click(label: string) {
100100 await act ( ( ) => userEvent . click ( button ! ) ) ;
101101}
102102
103+ /**
104+ * Runs `change`, which takes the open popup's trigger out of the page, then watches the popup
105+ * leave. A detached trigger has no rect, so a popup that keeps positioning against it slides
106+ * to the viewport origin on its way out.
107+ */
108+ async function expectDismissedInPlace ( change : ( ) => Promise < void > | void ) {
109+ const positioner = ( ) => document . querySelector ( '[data-slot="popover-positioner"]' ) ;
110+ const place = ( node : Element ) => {
111+ const rect = node . getBoundingClientRect ( ) ;
112+ return [ rect . left , rect . top ] ;
113+ } ;
114+ const before = place ( positioner ( ) ! ) ;
115+ await change ( ) ;
116+ const seen : number [ ] [ ] = [ ] ;
117+ for ( let frame = 0 ; frame < 60 && positioner ( ) ; frame += 1 ) {
118+ seen . push ( place ( positioner ( ) ! ) ) ;
119+ await new Promise ( requestAnimationFrame ) ;
120+ }
121+ expect ( positioner ( ) ) . toBeNull ( ) ;
122+ expect ( seen ) . toEqual ( seen . map ( ( ) => before ) ) ;
123+ }
124+
103125it . each ( [
104126 {
105127 label : "success" ,
@@ -221,8 +243,9 @@ it("moves the one limit popover between windows on hover", async () => {
221243 await act ( ( ) => userEvent . hover ( session , { position : { x : 4 , y : 4 } } ) ) ;
222244 await expect . poll ( popups ) . toEqual ( [ expect . stringContaining ( "Left60%" ) ] ) ;
223245
224- await act ( ( ) => root . render ( < UsageLimitsPooled presentations = { new Map ( ) } now = { 0 } /> ) ) ;
225- await expect . poll ( popups ) . toEqual ( [ ] ) ;
246+ await expectDismissedInPlace ( ( ) =>
247+ act ( ( ) => root . render ( < UsageLimitsPooled presentations = { new Map ( ) } now = { 0 } /> ) ) ,
248+ ) ;
226249 await act ( ( ) => userEvent . hover ( container , { position : { x : 4 , y : 4 } } ) ) ;
227250
228251 await act ( ( ) => root . render ( < UsageLimitsPooled presentations = { twoWindows } now = { 0 } /> ) ) ;
@@ -272,13 +295,15 @@ it("closes an open source-account popover when reset availability replaces its t
272295 await click ( "Alpha: 60%" ) ;
273296 await expect . poll ( ( ) => popup ( ) ?. textContent ) . toContain ( "Use reset" ) ;
274297
275- await act ( ( ) => root . render ( < UsageLimitsPooled presentations = { source ( ) } now = { 0 } /> ) ) ;
276- await expect . poll ( popup ) . toBeNull ( ) ;
298+ await expectDismissedInPlace ( ( ) =>
299+ act ( ( ) => root . render ( < UsageLimitsPooled presentations = { source ( ) } now = { 0 } /> ) ) ,
300+ ) ;
277301 await click ( "Alpha: 60%" ) ;
278302 await expect . poll ( ( ) => popup ( ) ?. textContent ) . toContain ( "Left60%" ) ;
279303
280- await act ( ( ) => root . render ( < UsageLimitsPooled presentations = { source ( "credit-2" ) } now = { 0 } /> ) ) ;
281- await expect . poll ( popup ) . toBeNull ( ) ;
304+ await expectDismissedInPlace ( ( ) =>
305+ act ( ( ) => root . render ( < UsageLimitsPooled presentations = { source ( "credit-2" ) } now = { 0 } /> ) ) ,
306+ ) ;
282307 await click ( "Alpha: 60%" ) ;
283308 await expect . poll ( ( ) => popup ( ) ?. textContent ) . toContain ( "Use reset" ) ;
284309 await click ( "Use reset" ) ;
0 commit comments