Last active
April 11, 2020 13:29
-
-
Save devbkhadka/b7e7abfe35125f61068eb74f1dea5b77 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 React from 'react'; | |
import { render } from '@testing-library/react'; | |
import App from '../App'; | |
import Restaurants from '../Restaurants' | |
import {fetchRestaurants} from '../utils' | |
import * as fixtures from '../fixtures' | |
import { act } from 'react-dom/test-utils'; | |
// First mock whole '../Restaurants' and '../utils' | |
// By default it will mock all the functions in module to return undefined | |
jest.mock('../Restaurants') | |
jest.mock('../utils') | |
// Provide fake return values for the functions | |
Restaurants.mockReturnValue(null) | |
// we want fetchRestaurants to return promise that resolves to fixtures.dummyRestaurants | |
fetchRestaurants.mockResolvedValue(fixtures.dummyRestaurants) | |
describe("App Component", ()=>{ | |
// function passed to before each is called before running each test | |
// It is used to setup pre-condition for each test | |
beforeEach(()=>{ | |
// mockClear clears call history of the mock function | |
Restaurants.mockClear() | |
fetchRestaurants.mockClear() | |
}) | |
it('Should call "fetchRestaurants" function to get restaurants', async ()=>{ | |
await act(async () => { | |
render(<App />) | |
}) | |
expect(fetchRestaurants).toBeCalled() | |
}) | |
it('Should render "Restaurants" component with result from "fetchRestaurants"', async ()=>{ | |
await act(async () => { | |
render(<App />) | |
}) | |
expect(Restaurants.mock.calls[1][0]).toEqual({list: fixtures.dummyRestaurants}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment