showing results for - "jest async test fetch api"
Oona
22 Jan 2019
1// fetch.js
2async function fetchUser() {
3  const url = 'https://jsonplaceholder.typicode.com/users'
4  const response = await fetch(url)
5  return await response.json()
6}
7
8const fetchPosts = {
9   async postAPI() {
10   const url = 'https://jsonplaceholder.typicode.com/posts'
11   const response = await fetch(url)
12   return await response.json()
13  }
14}
15
16
17// fetch.test.js
18test('async fetch action users', async (done) => {
19  const response = await fetchUser()
20  expect(response.length).toBe(10);
21  done()
22});
23
24test('async fetch action posts using Jetst Spy', async (done) => {
25   const spyOn = jest.spyOn(fetchPosts, 'postAPI')
26   const response = await fetchPosts.postAPI()
27   expect(spyOn).toHaveBeenCalled()
28   expect(response.length).toBe(100);
29   done()
30})