Migrated ServerError test to react testing library

This commit is contained in:
Alejandro Celaya 2022-06-04 19:13:00 +02:00
parent cd1a926292
commit 7f35fb0ada

View file

@ -1,49 +1,43 @@
import { shallow, ShallowWrapper } from 'enzyme'; import { render, screen } from '@testing-library/react';
import { BrowserRouter } from 'react-router-dom'; import { MemoryRouter } from 'react-router-dom';
import { Mock } from 'ts-mockery'; import { Mock } from 'ts-mockery';
import { ServerError as createServerError } from '../../../src/servers/helpers/ServerError'; import { ServerError as createServerError } from '../../../src/servers/helpers/ServerError';
import { NonReachableServer, NotFoundServer } from '../../../src/servers/data'; import { NonReachableServer, NotFoundServer } from '../../../src/servers/data';
describe('<ServerError />', () => { describe('<ServerError />', () => {
let wrapper: ShallowWrapper;
const ServerError = createServerError(() => null); const ServerError = createServerError(() => null);
afterEach(() => wrapper?.unmount());
it.each([ it.each([
[ [
Mock.all<NotFoundServer>(), Mock.all<NotFoundServer>(),
{ {
'Could not find this Shlink server.': true, found: ['Could not find this Shlink server.'],
'Oops! Could not connect to this Shlink server.': false, notFound: [
'Make sure you have internet connection, and the server is properly configured and on-line.': false, 'Oops! Could not connect to this Shlink server.',
'Alternatively, if you think you may have miss-configured this server': false, 'Make sure you have internet connection, and the server is properly configured and on-line.',
/^Alternatively, if you think you may have miss-configured this server/,
],
}, },
], ],
[ [
Mock.of<NonReachableServer>({ id: 'abc123' }), Mock.of<NonReachableServer>({ id: 'abc123' }),
{ {
'Could not find this Shlink server.': false, found: [
'Oops! Could not connect to this Shlink server.': true, 'Oops! Could not connect to this Shlink server.',
'Make sure you have internet connection, and the server is properly configured and on-line.': true, 'Make sure you have internet connection, and the server is properly configured and on-line.',
'Alternatively, if you think you may have miss-configured this server': true, /^Alternatively, if you think you may have miss-configured this server/,
],
notFound: ['Could not find this Shlink server.'],
}, },
], ],
])('renders expected information based on provided server type', (selectedServer, textsToFind) => { ])('renders expected information based on provided server type', (selectedServer, { found, notFound }) => {
wrapper = shallow( render(
<BrowserRouter> <MemoryRouter>
<ServerError servers={{}} selectedServer={selectedServer} /> <ServerError servers={{}} selectedServer={selectedServer} />
</BrowserRouter>, </MemoryRouter>,
); );
const wrapperText = wrapper.html();
const textPairs = Object.entries(textsToFind);
textPairs.forEach(([text, shouldBeFound]) => { found.forEach((text) => expect(screen.getByText(text)).toBeInTheDocument());
if (shouldBeFound) { notFound.forEach((text) => expect(screen.queryByText(text)).not.toBeInTheDocument());
expect(wrapperText).toContain(text);
} else {
expect(wrapperText).not.toContain(text);
}
});
}); });
}); });