Skip to content

Instantly share code, notes, and snippets.

@itaditya
Last active October 5, 2020 13:27
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 itaditya/fbd966299226d1032656c7514a38e3f9 to your computer and use it in GitHub Desktop.
Save itaditya/fbd966299226d1032656c7514a38e3f9 to your computer and use it in GitHub Desktop.
(Blog) Testing a Redux hooked app
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