Migrated ShlinkVersions test to react testing library

This commit is contained in:
Alejandro Celaya 2022-05-06 20:25:48 +02:00
parent d327142d00
commit 4ea826ed2c

View file

@ -1,17 +1,10 @@
import { shallow, ShallowWrapper } from 'enzyme'; import { render, screen } from '@testing-library/react';
import { Mock } from 'ts-mockery'; import { Mock } from 'ts-mockery';
import ShlinkVersions, { ShlinkVersionsProps } from '../../src/common/ShlinkVersions'; import ShlinkVersions, { ShlinkVersionsProps } from '../../src/common/ShlinkVersions';
import { NonReachableServer, NotFoundServer, ReachableServer } from '../../src/servers/data'; import { NonReachableServer, NotFoundServer, ReachableServer } from '../../src/servers/data';
describe('<ShlinkVersions />', () => { describe('<ShlinkVersions />', () => {
let wrapper: ShallowWrapper; const setUp = (props: ShlinkVersionsProps) => render(<ShlinkVersions {...props} />);
const createWrapper = (props: ShlinkVersionsProps) => {
wrapper = shallow(<ShlinkVersions {...props} />);
return wrapper;
};
afterEach(() => wrapper?.unmount());
it.each([ it.each([
['1.2.3', Mock.of<ReachableServer>({ version: '1.0.0', printableVersion: 'foo' }), 'v1.2.3', 'foo'], ['1.2.3', Mock.of<ReachableServer>({ version: '1.0.0', printableVersion: 'foo' }), 'v1.2.3', 'foo'],
@ -22,15 +15,19 @@ describe('<ShlinkVersions />', () => {
])( ])(
'displays expected versions when selected server is reachable', 'displays expected versions when selected server is reachable',
(clientVersion, selectedServer, expectedClientVersion, expectedServerVersion) => { (clientVersion, selectedServer, expectedClientVersion, expectedServerVersion) => {
const wrapper = createWrapper({ clientVersion, selectedServer }); setUp({ clientVersion, selectedServer });
const links = wrapper.find('VersionLink'); const [serverLink, clientLink] = screen.getAllByRole('link');
const serverLink = links.at(0);
const clientLink = links.at(1);
expect(serverLink.prop('project')).toEqual('shlink'); expect(serverLink).toHaveAttribute(
expect(serverLink.prop('version')).toEqual(expectedServerVersion); 'href',
expect(clientLink.prop('project')).toEqual('shlink-web-client'); `https://github.com/shlinkio/shlink/releases/${expectedServerVersion}`,
expect(clientLink.prop('version')).toEqual(expectedClientVersion); );
expect(serverLink).toHaveTextContent(expectedServerVersion);
expect(clientLink).toHaveAttribute(
'href',
`https://github.com/shlinkio/shlink-web-client/releases/${expectedClientVersion}`,
);
expect(clientLink).toHaveTextContent(expectedClientVersion);
}, },
); );
@ -39,10 +36,10 @@ describe('<ShlinkVersions />', () => {
['1.2.3', Mock.of<NotFoundServer>({ serverNotFound: true })], ['1.2.3', Mock.of<NotFoundServer>({ serverNotFound: true })],
['1.2.3', Mock.of<NonReachableServer>({ serverNotReachable: true })], ['1.2.3', Mock.of<NonReachableServer>({ serverNotReachable: true })],
])('displays only client version when selected server is not reachable', (clientVersion, selectedServer) => { ])('displays only client version when selected server is not reachable', (clientVersion, selectedServer) => {
const wrapper = createWrapper({ clientVersion, selectedServer }); setUp({ clientVersion, selectedServer });
const links = wrapper.find('VersionLink'); const links = screen.getAllByRole('link');
expect(links).toHaveLength(1); expect(links).toHaveLength(1);
expect(links.at(0).prop('project')).toEqual('shlink-web-client'); expect(links[0]).toHaveAttribute('href', 'https://github.com/shlinkio/shlink-web-client/releases/v1.2.3');
}); });
}); });