Migrated TableOrderIcon test to react testing library

This commit is contained in:
Alejandro Celaya 2022-06-11 17:28:47 +02:00
parent f9909713d9
commit ec7c7d521f
4 changed files with 53 additions and 26 deletions

View file

@ -9,7 +9,7 @@ describe('<SimplePaginator />', () => {
it.each([-3, -2, 0, 1])('renders empty when the amount of pages is smaller than 2', (pagesCount) => {
const { container } = setUp(pagesCount);
expect(container.firstChild).toEqual(null);
expect(container.firstChild).toBeNull();
});
describe('ELLIPSIS are rendered where expected', () => {

View file

@ -14,7 +14,7 @@ import { NonReachableServer, NotFoundServer, RegularServer } from '../../../src/
describe('selectedServerReducer', () => {
describe('reducer', () => {
it('returns default when action is RESET_SELECTED_SERVER', () =>
expect(reducer(null, { type: RESET_SELECTED_SERVER, selectedServer: null })).toEqual(null));
expect(reducer(null, { type: RESET_SELECTED_SERVER, selectedServer: null })).toBeNull());
it('returns selected server when action is SELECT_SERVER', () => {
const selectedServer = Mock.of<RegularServer>({ id: 'abc123' });

View file

@ -1,38 +1,29 @@
import { shallow, ShallowWrapper } from 'enzyme';
import { faCaretDown as caretDownIcon, faCaretUp as caretUpIcon } from '@fortawesome/free-solid-svg-icons';
import { render } from '@testing-library/react';
import { TableOrderIcon } from '../../../src/utils/table/TableOrderIcon';
import { OrderDir } from '../../../src/utils/helpers/ordering';
describe('<TableOrderIcon />', () => {
let wrapper: ShallowWrapper;
const createWrapper = (field: string, currentDir?: OrderDir, className?: string) => {
wrapper = shallow(
<TableOrderIcon currentOrder={{ dir: currentDir, field: 'foo' }} field={field} className={className} />,
);
return wrapper;
};
afterEach(() => wrapper?.unmount());
const setUp = (field: string, currentDir?: OrderDir, className?: string) => render(
<TableOrderIcon currentOrder={{ dir: currentDir, field: 'foo' }} field={field} className={className} />,
);
it.each([
['foo', undefined],
['bar', 'DESC' as OrderDir],
['bar', 'ASC' as OrderDir],
])('renders empty when not all conditions are met', (field, dir) => {
const wrapper = createWrapper(field, dir);
expect(wrapper.html()).toEqual(null);
const { container } = setUp(field, dir);
expect(container.firstChild).toBeNull();
});
it.each([
['DESC' as OrderDir, caretDownIcon],
['ASC' as OrderDir, caretUpIcon],
])('renders an icon when all conditions are met', (dir, expectedIcon) => {
const wrapper = createWrapper('foo', dir);
['DESC' as OrderDir],
['ASC' as OrderDir],
])('renders an icon when all conditions are met', (dir) => {
const { container } = setUp('foo', dir);
expect(wrapper.html()).not.toEqual(null);
expect(wrapper.prop('icon')).toEqual(expectedIcon);
expect(container.firstChild).not.toBeNull();
expect(container.firstChild).toMatchSnapshot();
});
it.each([
@ -40,8 +31,7 @@ describe('<TableOrderIcon />', () => {
['foo', 'foo'],
['bar', 'bar'],
])('renders expected classname', (className, expectedClassName) => {
const wrapper = createWrapper('foo', 'ASC', className);
expect(wrapper.prop('className')).toEqual(expectedClassName);
const { container } = setUp('foo', 'ASC', className);
expect(container.firstChild).toHaveClass(expectedClassName);
});
});

View file

@ -0,0 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`<TableOrderIcon /> renders an icon when all conditions are met 1`] = `
<svg
aria-hidden="true"
class="svg-inline--fa fa-caret-down ms-1"
data-icon="caret-down"
data-prefix="fas"
focusable="false"
role="img"
viewBox="0 0 320 512"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M310.6 246.6l-127.1 128C176.4 380.9 168.2 384 160 384s-16.38-3.125-22.63-9.375l-127.1-128C.2244 237.5-2.516 223.7 2.438 211.8S19.07 192 32 192h255.1c12.94 0 24.62 7.781 29.58 19.75S319.8 237.5 310.6 246.6z"
fill="currentColor"
/>
</svg>
`;
exports[`<TableOrderIcon /> renders an icon when all conditions are met 2`] = `
<svg
aria-hidden="true"
class="svg-inline--fa fa-caret-up ms-1"
data-icon="caret-up"
data-prefix="fas"
focusable="false"
role="img"
viewBox="0 0 320 512"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M9.39 265.4l127.1-128C143.6 131.1 151.8 128 160 128s16.38 3.125 22.63 9.375l127.1 128c9.156 9.156 11.9 22.91 6.943 34.88S300.9 320 287.1 320H32.01c-12.94 0-24.62-7.781-29.58-19.75S.2333 274.5 9.39 265.4z"
fill="currentColor"
/>
</svg>
`;