Migrated NotFound test to react testing library

This commit is contained in:
Alejandro Celaya 2022-05-06 19:55:25 +02:00
parent fcbb9cda12
commit 37adcb52cf

View file

@ -1,47 +1,32 @@
import { shallow, ShallowWrapper } from 'enzyme'; import { render, screen } from '@testing-library/react';
import { Link } from 'react-router-dom'; import { MemoryRouter } from 'react-router-dom';
import { NotFound } from '../../src/common/NotFound'; import { NotFound } from '../../src/common/NotFound';
import { SimpleCard } from '../../src/utils/SimpleCard';
describe('<NotFound />', () => { describe('<NotFound />', () => {
let wrapper: ShallowWrapper; const setUp = (props = {}) => render(<MemoryRouter><NotFound {...props} /></MemoryRouter>);
const createWrapper = (props = {}) => {
wrapper = shallow(<NotFound {...props} />).find(SimpleCard);
return wrapper;
};
afterEach(() => wrapper?.unmount());
it('shows expected error title', () => { it('shows expected error title', () => {
const wrapper = createWrapper(); setUp();
expect(screen.getByText('Oops! We could not find requested route.')).toBeInTheDocument();
expect(wrapper.contains('Oops! We could not find requested route.')).toEqual(true);
}); });
it('shows expected error message', () => { it('shows expected error message', () => {
const wrapper = createWrapper(); setUp();
expect(screen.getByText(
expect(wrapper.contains(
'Use your browser\'s back button to navigate to the page you have previously come from, or just press this button.', 'Use your browser\'s back button to navigate to the page you have previously come from, or just press this button.',
)).toEqual(true); )).toBeInTheDocument();
}); });
it('shows a link to the home', () => { it.each([
const wrapper = createWrapper(); [{}, '/', 'Home'],
const link = wrapper.find(Link); [{ to: '/foo/bar', children: 'Hello' }, '/foo/bar', 'Hello'],
[{ to: '/baz-bar', children: <>Foo</> }, '/baz-bar', 'Foo'],
])('shows expected link and text', (props, expectedLink, expectedText) => {
setUp(props);
const link = screen.getByRole('link');
expect(link.prop('to')).toEqual('/'); expect(link).toHaveAttribute('href', expectedLink);
expect(link.prop('className')).toEqual('btn btn-outline-primary btn-lg'); expect(link).toHaveTextContent(expectedText);
expect(link.prop('children')).toEqual('Home'); expect(link).toHaveAttribute('class', 'btn btn-outline-primary btn-lg');
});
it('shows a link with provided props', () => {
const wrapper = createWrapper({ to: '/foo/bar', children: 'Hello' });
const link = wrapper.find(Link);
expect(link.prop('to')).toEqual('/foo/bar');
expect(link.prop('className')).toEqual('btn btn-outline-primary btn-lg');
expect(link.prop('children')).toEqual('Hello');
}); });
}); });