Last active
October 5, 2020 13:27
-
-
Save itaditya/fbd966299226d1032656c7514a38e3f9 to your computer and use it in GitHub Desktop.
(Blog) Testing a Redux hooked app
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 { render, fireEvent, screen, waitForElementToBeRemoved } from '@testing-library/react'; | |
import * as utils from './utils'; | |
jest.mock('./utils'); | |
const foodData = [ | |
{ | |
id: 'SM', | |
label: 'Sausage McMuffin', | |
description: 'Description of McMuffin', | |
price: 12, | |
}, | |
{ | |
id: 'MP', | |
label: 'Mushroom Pizza', | |
diet: 'veg', | |
description: 'Description of Pizza', | |
price: 20, | |
}, | |
]; | |
describe('Test App', () => { | |
beforeEach(() => { | |
utils.loadFoodData.mockImplementation(() => Promise.resolve(foodData)); | |
}); | |
afterEach(() => { | |
utils.loadFoodData.mockRestore(); | |
}); | |
test('only show veg food when veg filter is applied', async () => { | |
renderApp(); | |
await waitForElementToBeRemoved(() => screen.getByText(/Loading/i)); | |
// enable Veg Only filter | |
fireEvent.click(screen.getByRole('checkbox', {name: /Veg Only/i})); | |
expect(screen.queryByText(/Sausage McMuffin/i)).toBe(null) | |
expect(screen.getByText(/Mushroom Pizza/i)).toBeInTheDocument(); | |
// disable Veg Only filter | |
fireEvent.click(screen.getByRole('checkbox', {name: /Veg Only/i})); | |
expect(screen.getByText(/Sausage McMuffin/i)).toBeInTheDocument(); | |
expect(screen.getByText(/Mushroom Pizza/i)).toBeInTheDocument(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment