@@ -6,33 +6,40 @@ import withFloating from "../with_floating";
66import type { FloatingProps } from "../with_floating" ;
77
88// Mock @floating -ui/react
9+ const mockUseFloating = jest . fn ( ( ) => ( {
10+ placement : "bottom" ,
11+ strategy : "absolute" ,
12+ middlewareData : { } ,
13+ x : 0 ,
14+ y : 0 ,
15+ isPositioned : true ,
16+ update : jest . fn ( ) ,
17+ floatingStyles : { position : "absolute" as const , top : 0 , left : 0 } ,
18+ refs : {
19+ reference : { current : null } ,
20+ floating : { current : null } ,
21+ setFloating : jest . fn ( ) ,
22+ setReference : jest . fn ( ) ,
23+ } ,
24+ elements : {
25+ reference : null ,
26+ floating : null ,
27+ domReference : null ,
28+ } ,
29+ context : { } ,
30+ } ) ) ;
31+
32+ const mockArrow = jest . fn ( ( ) => ( { name : "arrow" , fn : jest . fn ( ) } ) ) ;
33+ const mockOffset = jest . fn ( ( ) => ( { name : "offset" , fn : jest . fn ( ) } ) ) ;
34+ const mockFlip = jest . fn ( ( ) => ( { name : "flip" , fn : jest . fn ( ) } ) ) ;
35+ const mockAutoUpdate = jest . fn ( ) ;
36+
937jest . mock ( "@floating-ui/react" , ( ) => ( {
10- useFloating : jest . fn ( ( ) => ( {
11- placement : "bottom" ,
12- strategy : "absolute" ,
13- middlewareData : { } ,
14- x : 0 ,
15- y : 0 ,
16- isPositioned : true ,
17- update : jest . fn ( ) ,
18- floatingStyles : { position : "absolute" as const , top : 0 , left : 0 } ,
19- refs : {
20- reference : { current : null } ,
21- floating : { current : null } ,
22- setFloating : jest . fn ( ) ,
23- setReference : jest . fn ( ) ,
24- } ,
25- elements : {
26- reference : null ,
27- floating : null ,
28- domReference : null ,
29- } ,
30- context : { } ,
31- } ) ) ,
32- arrow : jest . fn ( ( ) => ( { name : "arrow" , fn : jest . fn ( ) } ) ) ,
33- offset : jest . fn ( ( ) => ( { name : "offset" , fn : jest . fn ( ) } ) ) ,
34- flip : jest . fn ( ( ) => ( { name : "flip" , fn : jest . fn ( ) } ) ) ,
35- autoUpdate : jest . fn ( ) ,
38+ useFloating : mockUseFloating ,
39+ arrow : mockArrow ,
40+ offset : mockOffset ,
41+ flip : mockFlip ,
42+ autoUpdate : mockAutoUpdate ,
3643} ) ) ;
3744
3845interface TestComponentProps extends FloatingProps {
@@ -84,50 +91,46 @@ describe("withFloating", () => {
8491 } ) ;
8592
8693 it ( "sets hidePopper to true by default" , ( ) => {
87- const { useFloating } = require ( "@floating-ui/react" ) ;
8894 const WrappedComponent = withFloating ( TestComponent ) ;
8995 render ( < WrappedComponent /> ) ;
9096
91- expect ( useFloating ) . toHaveBeenCalledWith (
97+ expect ( mockUseFloating ) . toHaveBeenCalledWith (
9298 expect . objectContaining ( {
9399 open : false ,
94100 } ) ,
95101 ) ;
96102 } ) ;
97103
98104 it ( "respects hidePopper prop when set to false" , ( ) => {
99- const { useFloating } = require ( "@floating-ui/react" ) ;
100105 const WrappedComponent = withFloating ( TestComponent ) ;
101106 render ( < WrappedComponent hidePopper = { false } /> ) ;
102107
103- expect ( useFloating ) . toHaveBeenCalledWith (
108+ expect ( mockUseFloating ) . toHaveBeenCalledWith (
104109 expect . objectContaining ( {
105110 open : true ,
106111 } ) ,
107112 ) ;
108113 } ) ;
109114
110115 it ( "passes popperPlacement to useFloating" , ( ) => {
111- const { useFloating } = require ( "@floating-ui/react" ) ;
112116 const WrappedComponent = withFloating ( TestComponent ) ;
113117 render ( < WrappedComponent popperPlacement = "top" /> ) ;
114118
115- expect ( useFloating ) . toHaveBeenCalledWith (
119+ expect ( mockUseFloating ) . toHaveBeenCalledWith (
116120 expect . objectContaining ( {
117121 placement : "top" ,
118122 } ) ,
119123 ) ;
120124 } ) ;
121125
122126 it ( "includes default middleware" , ( ) => {
123- const { useFloating, flip, offset, arrow } = require ( "@floating-ui/react" ) ;
124127 const WrappedComponent = withFloating ( TestComponent ) ;
125128 render ( < WrappedComponent /> ) ;
126129
127- expect ( flip ) . toHaveBeenCalledWith ( { padding : 15 } ) ;
128- expect ( offset ) . toHaveBeenCalledWith ( 10 ) ;
129- expect ( arrow ) . toHaveBeenCalled ( ) ;
130- expect ( useFloating ) . toHaveBeenCalledWith (
130+ expect ( mockFlip ) . toHaveBeenCalledWith ( { padding : 15 } ) ;
131+ expect ( mockOffset ) . toHaveBeenCalledWith ( 10 ) ;
132+ expect ( mockArrow ) . toHaveBeenCalled ( ) ;
133+ expect ( mockUseFloating ) . toHaveBeenCalledWith (
131134 expect . objectContaining ( {
132135 middleware : expect . arrayContaining ( [
133136 expect . objectContaining ( { name : "flip" } ) ,
@@ -139,12 +142,11 @@ describe("withFloating", () => {
139142 } ) ;
140143
141144 it ( "includes custom popperModifiers" , ( ) => {
142- const { useFloating } = require ( "@floating-ui/react" ) ;
143145 const customModifier = { name : "custom" , fn : jest . fn ( ) } ;
144146 const WrappedComponent = withFloating ( TestComponent ) ;
145147 render ( < WrappedComponent popperModifiers = { [ customModifier ] } /> ) ;
146148
147- expect ( useFloating ) . toHaveBeenCalledWith (
149+ expect ( mockUseFloating ) . toHaveBeenCalledWith (
148150 expect . objectContaining ( {
149151 middleware : expect . arrayContaining ( [
150152 expect . objectContaining ( { name : "custom" } ) ,
@@ -154,26 +156,24 @@ describe("withFloating", () => {
154156 } ) ;
155157
156158 it ( "passes popperProps to useFloating" , ( ) => {
157- const { useFloating } = require ( "@floating-ui/react" ) ;
158159 const customProps = { strategy : "fixed" as const } ;
159160 const WrappedComponent = withFloating ( TestComponent ) ;
160161 render ( < WrappedComponent popperProps = { customProps } /> ) ;
161162
162- expect ( useFloating ) . toHaveBeenCalledWith (
163+ expect ( mockUseFloating ) . toHaveBeenCalledWith (
163164 expect . objectContaining ( {
164165 strategy : "fixed" ,
165166 } ) ,
166167 ) ;
167168 } ) ;
168169
169170 it ( "sets whileElementsMounted to autoUpdate" , ( ) => {
170- const { useFloating, autoUpdate } = require ( "@floating-ui/react" ) ;
171171 const WrappedComponent = withFloating ( TestComponent ) ;
172172 render ( < WrappedComponent /> ) ;
173173
174- expect ( useFloating ) . toHaveBeenCalledWith (
174+ expect ( mockUseFloating ) . toHaveBeenCalledWith (
175175 expect . objectContaining ( {
176- whileElementsMounted : autoUpdate ,
176+ whileElementsMounted : mockAutoUpdate ,
177177 } ) ,
178178 ) ;
179179 } ) ;
@@ -205,16 +205,15 @@ describe("withFloating", () => {
205205 } ) ;
206206
207207 it ( "handles hidePopper boolean correctly" , ( ) => {
208- const { useFloating } = require ( "@floating-ui/react" ) ;
209208 const WrappedComponent = withFloating ( TestComponent ) ;
210209
211210 const { rerender } = render ( < WrappedComponent hidePopper = { true } /> ) ;
212- expect ( useFloating ) . toHaveBeenCalledWith (
211+ expect ( mockUseFloating ) . toHaveBeenCalledWith (
213212 expect . objectContaining ( { open : false } ) ,
214213 ) ;
215214
216215 rerender ( < WrappedComponent hidePopper = { false } /> ) ;
217- expect ( useFloating ) . toHaveBeenCalledWith (
216+ expect ( mockUseFloating ) . toHaveBeenCalledWith (
218217 expect . objectContaining ( { open : true } ) ,
219218 ) ;
220219 } ) ;
0 commit comments