@@ -203,11 +203,11 @@ describe("useFetch", () => {
203
203
expect ( json ) . toHaveBeenCalled ( )
204
204
} )
205
205
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" , ( ) => {
207
207
const component = (
208
- < Fetch input = "/test" init = { { method : "POST" } } >
208
+ < Fetch input = "/test" init = { { method : "POST" , body : '{"name":"foo"}' } } >
209
209
{ ( { run } ) => (
210
- < button onClick = { ( ) => run ( init => ( { ...init , body : '{"name":"test "}' } ) ) } > run</ button >
210
+ < button onClick = { ( ) => run ( init => ( { ...init , body : '{"name":"bar "}' } ) ) } > run</ button >
211
211
) }
212
212
</ Fetch >
213
213
)
@@ -216,22 +216,56 @@ describe("useFetch", () => {
216
216
fireEvent . click ( getByText ( "run" ) )
217
217
expect ( globalScope . fetch ) . toHaveBeenCalledWith (
218
218
"/test" ,
219
- expect . objectContaining ( { method : "POST" , signal : abortCtrl . signal , body : '{"name":"test "}' } )
219
+ expect . objectContaining ( { method : "POST" , signal : abortCtrl . signal , body : '{"name":"bar "}' } )
220
220
)
221
221
} )
222
222
223
223
test ( "calling `run` with an object as argument allows to override `init` parameters" , ( ) => {
224
224
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 > }
227
227
</ Fetch >
228
228
)
229
229
const { getByText } = render ( component )
230
230
expect ( globalScope . fetch ) . not . toHaveBeenCalled ( )
231
231
fireEvent . click ( getByText ( "run" ) )
232
232
expect ( globalScope . fetch ) . toHaveBeenCalledWith (
233
233
"/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"}' } )
235
269
)
236
270
} )
237
271
0 commit comments