Skip to content

Instantly share code, notes, and snippets.

@devbkhadka
Created April 12, 2020 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devbkhadka/5525106a6bb10197b8598351aa880a7a to your computer and use it in GitHub Desktop.
Save devbkhadka/5525106a6bb10197b8598351aa880a7a to your computer and use it in GitHub Desktop.
jest tutorial
import {fetchRestaurants, RESTAURANTS_URL} from '../utils'
import * as fixtures from '../fixtures'
jest.spyOn(global, 'fetch')
describe('fetchRestaurants', ()=>{
beforeEach(()=>{
global.fetch.mockClear()
global.fetch.mockResolvedValue({text: ()=>JSON.stringify(fixtures.dummyRestaurants)})
})
it('should call fetch api with correct parameters', ()=>{
fetchRestaurants()
expect(global.fetch).toBeCalledWith(RESTAURANTS_URL)
})
it("should return response on fetch success", async ()=>{
const restaurants = await fetchRestaurants()
expect(restaurants).toEqual(fixtures.dummyRestaurants)
})
it("should return null on fetch error", async ()=>{
global.fetch.mockRejectedValue("some error occured")
const restaurants = await fetchRestaurants()
expect(restaurants).toEqual([])
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment