Migrated DomainRow test to react testing library

This commit is contained in:
Alejandro Celaya 2022-05-09 18:38:14 +02:00
parent 43302ef5a8
commit 0b16300a70
2 changed files with 63 additions and 33 deletions

View file

@ -4,3 +4,4 @@ import ResizeObserver from 'resize-observer-polyfill';
(global as any).ResizeObserver = ResizeObserver; (global as any).ResizeObserver = ResizeObserver;
(global as any).scrollTo = () => {}; (global as any).scrollTo = () => {};
(global as any).matchMedia = (media: string) => ({ matches: false, media });

View file

@ -1,4 +1,4 @@
import { shallow, ShallowWrapper } from 'enzyme'; import { render, screen } from '@testing-library/react';
import { Mock } from 'ts-mockery'; import { Mock } from 'ts-mockery';
import { ShlinkDomainRedirects } from '../../src/api/types'; import { ShlinkDomainRedirects } from '../../src/api/types';
import { DomainRow } from '../../src/domains/DomainRow'; import { DomainRow } from '../../src/domains/DomainRow';
@ -6,42 +6,71 @@ import { SelectedServer } from '../../src/servers/data';
import { Domain } from '../../src/domains/data'; import { Domain } from '../../src/domains/data';
describe('<DomainRow />', () => { describe('<DomainRow />', () => {
let wrapper: ShallowWrapper; const redirectsCombinations = [
const createWrapper = (domain: Domain, selectedServer = Mock.all<SelectedServer>()) => { [Mock.of<ShlinkDomainRedirects>({ baseUrlRedirect: 'foo' })],
wrapper = shallow( [Mock.of<ShlinkDomainRedirects>({ invalidShortUrlRedirect: 'bar' })],
[Mock.of<ShlinkDomainRedirects>({ baseUrlRedirect: 'baz', regular404Redirect: 'foo' })],
[
Mock.of<ShlinkDomainRedirects>(
{ baseUrlRedirect: 'baz', regular404Redirect: 'bar', invalidShortUrlRedirect: 'foo' },
),
],
];
const setUp = (domain: Domain, defaultRedirects?: ShlinkDomainRedirects) => render(
<DomainRow <DomainRow
domain={domain} domain={domain}
selectedServer={selectedServer} defaultRedirects={defaultRedirects}
selectedServer={Mock.all<SelectedServer>()}
editDomainRedirects={jest.fn()} editDomainRedirects={jest.fn()}
checkDomainHealth={jest.fn()} checkDomainHealth={jest.fn()}
/>, />,
); );
return wrapper; it.each(redirectsCombinations)('shows expected redirects', (redirects) => {
}; setUp(Mock.of<Domain>({ domain: '', isDefault: true, redirects }));
const cells = screen.getAllByRole('cell');
afterEach(() => wrapper?.unmount()); redirects?.baseUrlRedirect && expect(cells[1]).toHaveTextContent(redirects.baseUrlRedirect);
redirects?.regular404Redirect && expect(cells[2]).toHaveTextContent(redirects.regular404Redirect);
redirects?.invalidShortUrlRedirect && expect(cells[3]).toHaveTextContent(redirects.invalidShortUrlRedirect);
expect(screen.queryByText('(as fallback)')).not.toBeInTheDocument();
});
it.each([ it.each([
[undefined, 3], [undefined],
[Mock.of<ShlinkDomainRedirects>(), 3], [Mock.of<ShlinkDomainRedirects>()],
[Mock.of<ShlinkDomainRedirects>({ baseUrlRedirect: 'foo' }), 2], ])('shows expected "no redirects"', (redirects) => {
[Mock.of<ShlinkDomainRedirects>({ invalidShortUrlRedirect: 'foo' }), 2], setUp(Mock.of<Domain>({ domain: '', isDefault: true, redirects }));
[Mock.of<ShlinkDomainRedirects>({ baseUrlRedirect: 'foo', regular404Redirect: 'foo' }), 1], const cells = screen.getAllByRole('cell');
[
Mock.of<ShlinkDomainRedirects>(
{ baseUrlRedirect: 'foo', regular404Redirect: 'foo', invalidShortUrlRedirect: 'foo' },
),
0,
],
])('shows expected redirects', (redirects, expectedNoRedirects) => {
const wrapper = createWrapper(Mock.of<Domain>({ domain: '', isDefault: true, redirects }));
const noRedirects = wrapper.find('Nr');
const cells = wrapper.find('td');
expect(noRedirects).toHaveLength(expectedNoRedirects); expect(cells[1]).toHaveTextContent('No redirect');
redirects?.baseUrlRedirect && expect(cells.at(1).html()).toContain(redirects.baseUrlRedirect); expect(cells[2]).toHaveTextContent('No redirect');
redirects?.regular404Redirect && expect(cells.at(2).html()).toContain(redirects.regular404Redirect); expect(cells[3]).toHaveTextContent('No redirect');
redirects?.invalidShortUrlRedirect && expect(cells.at(3).html()).toContain(redirects.invalidShortUrlRedirect); expect(screen.queryByText('(as fallback)')).not.toBeInTheDocument();
});
it.each(redirectsCombinations)('shows expected fallback redirects', (fallbackRedirects) => {
setUp(Mock.of<Domain>({ domain: '', isDefault: true }), fallbackRedirects);
const cells = screen.getAllByRole('cell');
fallbackRedirects?.baseUrlRedirect && expect(cells[1]).toHaveTextContent(
`${fallbackRedirects.baseUrlRedirect} (as fallback)`,
);
fallbackRedirects?.regular404Redirect && expect(cells[2]).toHaveTextContent(
`${fallbackRedirects.regular404Redirect} (as fallback)`,
);
fallbackRedirects?.invalidShortUrlRedirect && expect(cells[3]).toHaveTextContent(
`${fallbackRedirects.invalidShortUrlRedirect} (as fallback)`,
);
});
it.each([[true], [false]])('shows icon on default domain only', (isDefault) => {
const { container } = setUp(Mock.of<Domain>({ domain: '', isDefault }));
if (isDefault) {
expect(container.querySelector('#defaultDomainIcon')).toBeInTheDocument();
} else {
expect(container.querySelector('#defaultDomainIcon')).not.toBeInTheDocument();
}
}); });
}); });