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 { Mock } from 'ts-mockery';
import { MemoryRouter } from 'react-router-dom';
import asideMenuCreator from '../../src/common/AsideMenu'; import asideMenuCreator from '../../src/common/AsideMenu';
import { ReachableServer } from '../../src/servers/data'; import { ReachableServer } from '../../src/servers/data';
import { SemVer } from '../../src/utils/helpers/version';
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn().mockReturnValue({ pathname: '' }),
}));
describe('<AsideMenu />', () => { describe('<AsideMenu />', () => {
let wrapped: ShallowWrapper; const AsideMenu = asideMenuCreator(() => <>DeleteServerButton</>);
const DeleteServerButton = () => null; const setUp = (version: SemVer, id: string | false = 'abc123') => render(
<MemoryRouter>
<AsideMenu selectedServer={Mock.of<ReachableServer>({ id: id || undefined, version })} />
</MemoryRouter>,
);
beforeEach(() => { it.each([
const AsideMenu = asideMenuCreator(DeleteServerButton); ['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' })} />); const links = screen.getAllByRole('link');
});
afterEach(() => wrapped.unmount());
it('contains links to different sections', () => { expect.assertions(links.length + 1);
const links = wrapped.find('[to]'); expect(links).toHaveLength(expectedAmountOfLinks);
links.forEach((link) => expect(link.getAttribute('href')).toContain('abc123'));
expect(links).toHaveLength(5);
links.forEach((link) => expect(link.prop('to')).toContain('abc123'));
}); });
it('contains a button to delete server', () => { it.each([
expect(wrapped.find(DeleteServerButton)).toHaveLength(1); ['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();
}
}); });
}); });