@@ -162,7 +162,7 @@ describe("useFetch", () => {
162162 expect ( json ) . toHaveBeenCalled ( )
163163 } )
164164
165- test ( "calling `run` with an argument allows to override `init` parameters" , ( ) => {
165+ test ( "calling `run` with a method argument allows to override `init` parameters" , ( ) => {
166166 const component = (
167167 < Fetch input = "/test" init = { { method : "POST" } } >
168168 { ( { run } ) => (
@@ -178,4 +178,42 @@ describe("useFetch", () => {
178178 expect . objectContaining ( { method : "POST" , signal : abortCtrl . signal , body : '{"name":"test"}' } )
179179 )
180180 } )
181+
182+
183+ test ( "calling `run` with an object as argument allows to override `init` parameters" , ( ) => {
184+ const component = (
185+ < Fetch input = "/test" init = { { method : "POST" } } >
186+ { ( { run } ) => (
187+ < button onClick = { ( ) => run ( { body : '{"name":"test"}' } ) } > run</ button >
188+ ) }
189+ </ Fetch >
190+ )
191+ const { getByText } = render ( component )
192+ expect ( globalScope . fetch ) . not . toHaveBeenCalled ( )
193+ fireEvent . click ( getByText ( "run" ) )
194+ expect ( globalScope . fetch ) . toHaveBeenCalledWith (
195+ "/test" ,
196+ expect . objectContaining ( { method : "POST" , signal : abortCtrl . signal , body : '{"name":"test"}' } )
197+ )
198+ } )
199+
200+
201+ test ( "passing `run` directly as a click handler will not spread the event over init" , ( ) => {
202+ const component = (
203+ < Fetch input = "/test" init = { { method : "POST" } } >
204+ { ( { run } ) => ( < button onClick = { run } > run</ button > ) }
205+ </ Fetch >
206+ )
207+ const { getByText } = render ( component )
208+ expect ( globalScope . fetch ) . not . toHaveBeenCalled ( )
209+ fireEvent . click ( getByText ( "run" ) )
210+ expect ( globalScope . fetch ) . toHaveBeenCalledWith (
211+ "/test" ,
212+ expect . any ( Object )
213+ ) ;
214+ expect ( globalScope . fetch ) . not . toHaveBeenCalledWith (
215+ "/test" ,
216+ expect . objectContaining ( { preventDefault : expect . any ( Function ) } )
217+ )
218+ } )
181219} )
0 commit comments