Migrated Result test to react testing library

This commit is contained in:
Alejandro Celaya 2022-07-08 11:24:19 +02:00
parent 65f739499f
commit 21101d4da8
2 changed files with 10 additions and 23 deletions

View file

@ -15,6 +15,7 @@ export const Result: FC<ResultProps> = ({ children, type, className, small = fal
<Row className={className}>
<div className={classNames({ 'col-md-10 offset-md-1': !small, 'col-12': small })}>
<SimpleCard
role="document"
className={classNames('text-center', {
'bg-main': type === 'success',
'bg-danger': type === 'error',

View file

@ -1,46 +1,32 @@
import { shallow, ShallowWrapper } from 'enzyme';
import { render, screen } from '@testing-library/react';
import { Result, ResultProps, ResultType } from '../../src/utils/Result';
import { SimpleCard } from '../../src/utils/SimpleCard';
describe('<Result />', () => {
let wrapper: ShallowWrapper;
const createWrapper = (props: ResultProps) => {
wrapper = shallow(<Result {...props} />);
return wrapper;
};
afterEach(() => wrapper?.unmount());
const setUp = (props: ResultProps) => render(<Result {...props} />);
it.each([
['success' as ResultType, 'bg-main text-white'],
['error' as ResultType, 'bg-danger text-white'],
['warning' as ResultType, 'bg-warning'],
])('renders expected classes based on type', (type, expectedClasses) => {
const wrapper = createWrapper({ type });
const innerCard = wrapper.find(SimpleCard);
expect(innerCard.prop('className')).toEqual(`text-center ${expectedClasses}`);
setUp({ type });
expect(screen.getByRole('document')).toHaveClass(expectedClasses);
});
it.each([
[undefined],
['foo'],
['bar'],
])('renders provided classes in root element', (className) => {
const wrapper = createWrapper({ type: 'success', className });
expect(wrapper.prop('className')).toEqual(className);
const { container } = setUp({ type: 'success', className });
expect(container.firstChild).toHaveClass(className);
});
it.each([{ small: true }, { small: false }])('renders small results properly', ({ small }) => {
const wrapper = createWrapper({ type: 'success', small });
const bigElement = wrapper.find('.col-md-10');
const smallElement = wrapper.find('.col-12');
const innerCard = wrapper.find(SimpleCard);
const { container } = setUp({ type: 'success', small });
const bigElement = container.querySelectorAll('.col-md-10');
const smallElement = container.querySelectorAll('.col-12');
expect(bigElement).toHaveLength(small ? 0 : 1);
expect(smallElement).toHaveLength(small ? 1 : 0);
expect(innerCard.prop('bodyClassName')).toEqual(small ? 'p-2' : '');
});
});