Migrated AsideMenu test to react testing library

This commit is contained in:
Alejandro Celaya 2022-05-03 18:03:14 +02:00
parent f72251c125
commit e128b847be

View file

@ -1,32 +1,41 @@
import { shallow, ShallowWrapper } from 'enzyme';
import { render, screen } from '@testing-library/react';
import { Mock } from 'ts-mockery';
import { MemoryRouter } from 'react-router-dom';
import asideMenuCreator from '../../src/common/AsideMenu';
import { ReachableServer } from '../../src/servers/data';
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn().mockReturnValue({ pathname: '' }),
}));
import { SemVer } from '../../src/utils/helpers/version';
describe('<AsideMenu />', () => {
let wrapped: ShallowWrapper;
const DeleteServerButton = () => null;
const AsideMenu = asideMenuCreator(() => <>DeleteServerButton</>);
const setUp = (version: SemVer, id: string | false = 'abc123') => render(
<MemoryRouter>
<AsideMenu selectedServer={Mock.of<ReachableServer>({ id: id || undefined, version })} />
</MemoryRouter>,
);
beforeEach(() => {
const AsideMenu = asideMenuCreator(DeleteServerButton);
it.each([
['2.7.0' as SemVer, 5],
['2.8.0' as SemVer, 6],
])('contains links to different sections', (version, expectedAmountOfLinks) => {
setUp(version);
wrapped = shallow(<AsideMenu selectedServer={Mock.of<ReachableServer>({ id: 'abc123' })} />);
});
afterEach(() => wrapped.unmount());
const links = screen.getAllByRole('link');
it('contains links to different sections', () => {
const links = wrapped.find('[to]');
expect(links).toHaveLength(5);
links.forEach((link) => expect(link.prop('to')).toContain('abc123'));
expect.assertions(links.length + 1);
expect(links).toHaveLength(expectedAmountOfLinks);
links.forEach((link) => expect(link.getAttribute('href')).toContain('abc123'));
});
it('contains a button to delete server', () => {
expect(wrapped.find(DeleteServerButton)).toHaveLength(1);
it.each([
['abc', true],
[false, false],
])('contains a button to delete server if appropriate', (id, shouldHaveBtn) => {
setUp('2.8.0', id as string | false);
if (shouldHaveBtn) {
expect(screen.getByText('DeleteServerButton')).toBeInTheDocument();
} else {
expect(screen.queryByText('DeleteServerButton')).not.toBeInTheDocument();
}
});
});