Created
April 12, 2020 09:03
-
-
Save devbkhadka/5525106a6bb10197b8598351aa880a7a to your computer and use it in GitHub Desktop.
jest tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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