@@ -3,17 +3,34 @@ import {it, expect, vi} from 'vitest'
33import React from 'react'
44import { useAnchoredPosition } from '../../hooks/useAnchoredPosition'
55
6- const Component = ( { callback} : { callback : ( hookReturnValue : ReturnType < typeof useAnchoredPosition > ) => void } ) => {
6+ const Component = ( {
7+ callback,
8+ } : {
9+ callback : ( { position} : { position : ReturnType < typeof useAnchoredPosition > [ 'position' ] } ) => void
10+ } ) => {
711 const floatingElementRef = React . useRef < HTMLDivElement > ( null )
8- const anchorElementRef = React . useRef < HTMLDivElement > ( null )
9- callback ( useAnchoredPosition ( { floatingElementRef, anchorElementRef} ) )
12+ const anchorElementRef = React . useRef < HTMLButtonElement > ( null )
13+ const { position} = useAnchoredPosition ( { floatingElementRef, anchorElementRef} )
14+ callback ( { position} )
15+ const floatStyle : React . CSSProperties = {
16+ position : 'absolute' ,
17+ height : '50px' ,
18+ width : '50px' ,
19+ ...( position
20+ ? {
21+ top : position . top ,
22+ left : position . left ,
23+ }
24+ : { top : '20px' , left : '20px' } ) ,
25+ }
1026 return (
1127 < div style = { { position : 'absolute' } } >
12- < div
13- style = { { position : 'absolute' , top : '20px' , left : '20px' , height : '50px' , width : '50px' } }
14- ref = { floatingElementRef }
15- />
16- < div ref = { anchorElementRef } />
28+ < div style = { floatStyle } ref = { floatingElementRef } >
29+ Floating element
30+ </ div >
31+ < button ref = { anchorElementRef } type = "button" >
32+ Click me
33+ </ button >
1734 </ div >
1835 )
1936}
@@ -23,14 +40,6 @@ it('should should return a position', async () => {
2340 render ( < Component callback = { cb } /> )
2441
2542 await waitFor ( ( ) => {
26- expect ( cb ) . toHaveBeenCalledTimes ( 2 )
27- expect ( cb . mock . calls [ 1 ] [ 0 ] [ 'position' ] ) . toMatchInlineSnapshot ( `
28- {
29- "anchorAlign": "start",
30- "anchorSide": "outside-bottom",
31- "left": 0,
32- "top": 4,
33- }
34- ` )
43+ expect ( cb . mock . calls [ 1 ] [ 0 ] . position ) . toMatchSnapshot ( )
3544 } )
3645} )
0 commit comments