@@ -203,11 +203,11 @@ describe("useFetch", () => {
203203 expect ( json ) . toHaveBeenCalled ( )
204204 } )
205205
206- test ( "calling `run` with a method argument allows to override `init` parameters" , ( ) => {
206+ test ( "calling `run` with a callback as argument allows to override `init` parameters" , ( ) => {
207207 const component = (
208- < Fetch input = "/test" init = { { method : "POST" } } >
208+ < Fetch input = "/test" init = { { method : "POST" , body : '{"name":"foo"}' } } >
209209 { ( { run } ) => (
210- < button onClick = { ( ) => run ( init => ( { ...init , body : '{"name":"test "}' } ) ) } > run</ button >
210+ < button onClick = { ( ) => run ( init => ( { ...init , body : '{"name":"bar "}' } ) ) } > run</ button >
211211 ) }
212212 </ Fetch >
213213 )
@@ -216,22 +216,56 @@ describe("useFetch", () => {
216216 fireEvent . click ( getByText ( "run" ) )
217217 expect ( globalScope . fetch ) . toHaveBeenCalledWith (
218218 "/test" ,
219- expect . objectContaining ( { method : "POST" , signal : abortCtrl . signal , body : '{"name":"test "}' } )
219+ expect . objectContaining ( { method : "POST" , signal : abortCtrl . signal , body : '{"name":"bar "}' } )
220220 )
221221 } )
222222
223223 test ( "calling `run` with an object as argument allows to override `init` parameters" , ( ) => {
224224 const component = (
225- < Fetch input = "/test" init = { { method : "POST" } } >
226- { ( { run } ) => < button onClick = { ( ) => run ( { body : '{"name":"test "}' } ) } > run</ button > }
225+ < Fetch input = "/test" init = { { method : "POST" , body : '{"name":"foo"}' } } >
226+ { ( { run } ) => < button onClick = { ( ) => run ( { body : '{"name":"bar "}' } ) } > run</ button > }
227227 </ Fetch >
228228 )
229229 const { getByText } = render ( component )
230230 expect ( globalScope . fetch ) . not . toHaveBeenCalled ( )
231231 fireEvent . click ( getByText ( "run" ) )
232232 expect ( globalScope . fetch ) . toHaveBeenCalledWith (
233233 "/test" ,
234- expect . objectContaining ( { method : "POST" , signal : abortCtrl . signal , body : '{"name":"test"}' } )
234+ expect . objectContaining ( { method : "POST" , signal : abortCtrl . signal , body : '{"name":"bar"}' } )
235+ )
236+ } )
237+
238+ test ( "calling `run` with a url allows to override fetch's `resource` parameter" , ( ) => {
239+ const component = (
240+ < Fetch input = "/foo" options = { { defer : true } } >
241+ { ( { run } ) => < button onClick = { ( ) => run ( "/bar" ) } > run</ button > }
242+ </ Fetch >
243+ )
244+ const { getByText } = render ( component )
245+ expect ( globalScope . fetch ) . not . toHaveBeenCalled ( )
246+ fireEvent . click ( getByText ( "run" ) )
247+ expect ( globalScope . fetch ) . toHaveBeenCalledWith (
248+ "/bar" ,
249+ expect . objectContaining ( { signal : abortCtrl . signal } )
250+ )
251+ } )
252+
253+ test ( "overriding the `resource` can be combined with overriding `init`" , ( ) => {
254+ const component = (
255+ < Fetch input = "/foo" init = { { method : "POST" , body : '{"name":"foo"}' } } >
256+ { ( { run } ) => (
257+ < button onClick = { ( ) => run ( "/bar" , init => ( { ...init , body : '{"name":"bar"}' } ) ) } >
258+ run
259+ </ button >
260+ ) }
261+ </ Fetch >
262+ )
263+ const { getByText } = render ( component )
264+ expect ( globalScope . fetch ) . not . toHaveBeenCalled ( )
265+ fireEvent . click ( getByText ( "run" ) )
266+ expect ( globalScope . fetch ) . toHaveBeenCalledWith (
267+ "/bar" ,
268+ expect . objectContaining ( { method : "POST" , signal : abortCtrl . signal , body : '{"name":"bar"}' } )
235269 )
236270 } )
237271
0 commit comments