2022-05-09 19:38:14 +03:00
|
|
|
import { render, screen } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-08-04 12:16:01 +03:00
|
|
|
import type { ShlinkDomainRedirects } from '../../src/api-contract';
|
2023-08-02 10:01:44 +03:00
|
|
|
import type { Domain } from '../../src/domains/data';
|
|
|
|
import { DomainRow } from '../../src/domains/DomainRow';
|
2021-08-24 20:53:28 +03:00
|
|
|
|
|
|
|
describe('<DomainRow />', () => {
|
2022-05-09 19:38:14 +03:00
|
|
|
const redirectsCombinations = [
|
2023-04-13 22:48:29 +03:00
|
|
|
[fromPartial<ShlinkDomainRedirects>({ baseUrlRedirect: 'foo' })],
|
|
|
|
[fromPartial<ShlinkDomainRedirects>({ invalidShortUrlRedirect: 'bar' })],
|
|
|
|
[fromPartial<ShlinkDomainRedirects>({ baseUrlRedirect: 'baz', regular404Redirect: 'foo' })],
|
2021-08-24 20:53:28 +03:00
|
|
|
[
|
2023-04-13 22:48:29 +03:00
|
|
|
fromPartial<ShlinkDomainRedirects>(
|
2022-05-09 19:38:14 +03:00
|
|
|
{ baseUrlRedirect: 'baz', regular404Redirect: 'bar', invalidShortUrlRedirect: 'foo' },
|
2021-08-24 20:53:28 +03:00
|
|
|
),
|
|
|
|
],
|
2022-05-09 19:38:14 +03:00
|
|
|
];
|
|
|
|
const setUp = (domain: Domain, defaultRedirects?: ShlinkDomainRedirects) => render(
|
2022-05-13 21:21:20 +03:00
|
|
|
<table>
|
|
|
|
<tbody>
|
|
|
|
<DomainRow
|
|
|
|
domain={domain}
|
|
|
|
defaultRedirects={defaultRedirects}
|
2023-05-27 12:57:26 +03:00
|
|
|
editDomainRedirects={vi.fn()}
|
|
|
|
checkDomainHealth={vi.fn()}
|
2022-05-13 21:21:20 +03:00
|
|
|
/>
|
|
|
|
</tbody>
|
|
|
|
</table>,
|
2022-05-09 19:38:14 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
it.each(redirectsCombinations)('shows expected redirects', (redirects) => {
|
2023-04-13 22:48:29 +03:00
|
|
|
setUp(fromPartial({ domain: '', isDefault: true, redirects }));
|
2022-05-09 19:38:14 +03:00
|
|
|
const cells = screen.getAllByRole('cell');
|
|
|
|
|
|
|
|
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([
|
|
|
|
[undefined],
|
2023-04-13 22:48:29 +03:00
|
|
|
[fromPartial<ShlinkDomainRedirects>({})],
|
2022-05-09 19:38:14 +03:00
|
|
|
])('shows expected "no redirects"', (redirects) => {
|
2023-04-13 22:48:29 +03:00
|
|
|
setUp(fromPartial({ domain: '', isDefault: true, redirects }));
|
2022-05-09 19:38:14 +03:00
|
|
|
const cells = screen.getAllByRole('cell');
|
|
|
|
|
|
|
|
expect(cells[1]).toHaveTextContent('No redirect');
|
|
|
|
expect(cells[2]).toHaveTextContent('No redirect');
|
|
|
|
expect(cells[3]).toHaveTextContent('No redirect');
|
|
|
|
expect(screen.queryByText('(as fallback)')).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each(redirectsCombinations)('shows expected fallback redirects', (fallbackRedirects) => {
|
2023-04-13 22:48:29 +03:00
|
|
|
setUp(fromPartial({ domain: '', isDefault: true }), fallbackRedirects);
|
2022-05-09 19:38:14 +03:00
|
|
|
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) => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const { container } = setUp(fromPartial({ domain: '', isDefault }));
|
2022-05-09 19:38:14 +03:00
|
|
|
|
|
|
|
if (isDefault) {
|
|
|
|
expect(container.querySelector('#defaultDomainIcon')).toBeInTheDocument();
|
|
|
|
} else {
|
|
|
|
expect(container.querySelector('#defaultDomainIcon')).not.toBeInTheDocument();
|
|
|
|
}
|
2021-08-24 20:53:28 +03:00
|
|
|
});
|
|
|
|
});
|