Skip to content

Commit fba282a

Browse files
test(fetch) add test for default fetcher
1 parent 173a615 commit fba282a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

test/use-swr.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,32 @@ describe('useSWR', () => {
355355
`"hello, SWR"`
356356
)
357357
})
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+
})
358384
})
359385

360386
describe('useSWR - loading', () => {

0 commit comments

Comments
 (0)