@@ -121,4 +121,66 @@ describe('tooltip state ownership', () => {
121121 }
122122 } ,
123123 )
124+
125+ it ( 'commits the body before the browser paints an interaction' , async ( ) => {
126+ const definition = defineChart ( {
127+ marks : [
128+ dot (
129+ [
130+ { x : 0 , y : 1 } ,
131+ { x : 1 , y : 2 } ,
132+ ] ,
133+ { x : 'x' , y : 'y' } ,
134+ ) ,
135+ ] ,
136+ scales : {
137+ x : { scale : scaleLinear ( ) . domain ( [ 0 , 1 ] ) } ,
138+ y : { scale : scaleLinear ( ) . domain ( [ 0 , 3 ] ) } ,
139+ } ,
140+ tooltip : { use : tooltip } ,
141+ } )
142+ const container = document . createElement ( 'div' )
143+ document . body . append ( container )
144+ const root = createRoot ( container )
145+ const errors = vi . spyOn ( console , 'error' )
146+ const render = ( width : number ) =>
147+ act ( async ( ) =>
148+ root . render (
149+ < Chart
150+ definition = { definition }
151+ width = { width }
152+ height = { 260 }
153+ ariaLabel = "chart"
154+ renderTooltipBody = { ( { primaryPoint } ) => (
155+ < span data-body > { primaryPoint ?. datum . y } </ span >
156+ ) }
157+ /> ,
158+ ) ,
159+ )
160+ try {
161+ await render ( 480 )
162+ const svg = container . querySelector ( 'svg' )
163+ if ( ! svg ) throw new Error ( 'Missing SVG' )
164+ await act ( async ( ) =>
165+ svg . dispatchEvent ( new FocusEvent ( 'focusin' , { bubbles : true } ) ) ,
166+ )
167+ expect ( container . querySelector ( '[data-body]' ) ?. textContent ) . toBe ( '1' )
168+ Object . assign ( globalThis , { IS_REACT_ACT_ENVIRONMENT : false } )
169+ try {
170+ svg . dispatchEvent (
171+ new KeyboardEvent ( 'keydown' , { key : 'ArrowRight' , bubbles : true } ) ,
172+ )
173+ } finally {
174+ Object . assign ( globalThis , { IS_REACT_ACT_ENVIRONMENT : true } )
175+ }
176+ expect ( container . querySelector ( '[data-body]' ) ?. textContent ) . toBe ( '2' )
177+ await render ( 520 )
178+ expect ( container . querySelector ( '[data-body]' ) ?. textContent ) . toBe ( '2' )
179+ expect ( errors ) . not . toHaveBeenCalled ( )
180+ } finally {
181+ errors . mockRestore ( )
182+ await act ( async ( ) => root . unmount ( ) )
183+ container . remove ( )
184+ }
185+ } )
124186} )
0 commit comments