Skip to content

Commit d0b36a7

Browse files
committed
test: update useAnchoredPosition test and snapshot
1 parent 72d709b commit d0b36a7

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`should should return a position 1`] = `
4+
{
5+
"anchorAlign": "center",
6+
"anchorSide": "outside-bottom",
7+
"left": 8.550800000000002,
8+
"top": 24.5,
9+
}
10+
`;

packages/react/src/hooks/__tests__/useAnchoredPosition.test.tsx

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,34 @@ import {it, expect, vi} from 'vitest'
33
import React from 'react'
44
import {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

Comments
 (0)