File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -355,6 +355,32 @@ describe('useSWR', () => {
355
355
`"hello, SWR"`
356
356
)
357
357
} )
358
+
359
+ it ( 'should use fetch api as default fetcher' , async ( ) => {
360
+ const users = [ { name : 'bob' } , { name : 'sue' } ]
361
+ global [ 'fetch' ] = ( ) => Promise . resolve ( )
362
+ const mockFetch = body =>
363
+ Promise . resolve ( { json : ( ) => Promise . resolve ( body ) } as any )
364
+ const fn = jest
365
+ . spyOn ( window , 'fetch' )
366
+ . mockImplementation ( ( ) => mockFetch ( users ) )
367
+
368
+ function Users ( ) {
369
+ const { data } = useSWR ( 'http://localhost:3000/api/users' )
370
+
371
+ return < div > hello, { data && data . map ( u => u . name ) . join ( ' and ' ) } </ div >
372
+ }
373
+
374
+ const { container } = render ( < Users /> )
375
+
376
+ expect ( container . firstChild . textContent ) . toMatchInlineSnapshot ( `"hello, "` )
377
+ expect ( fn ) . toBeCalled ( )
378
+ await waitForDomChange ( { container } )
379
+ expect ( container . firstChild . textContent ) . toMatchInlineSnapshot (
380
+ `"hello, bob and sue"`
381
+ )
382
+ delete global [ 'fetch' ]
383
+ } )
358
384
} )
359
385
360
386
describe ( 'useSWR - loading' , ( ) => {
You can’t perform that action at this time.
0 commit comments