2021-08-24 20:53:28 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import { Button, UncontrolledTooltip } from 'reactstrap';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faBan as forbiddenIcon, faEdit as editIcon } from '@fortawesome/free-solid-svg-icons';
|
2021-12-26 15:53:17 +03:00
|
|
|
import { ShlinkDomainRedirects } from '../../src/api/types';
|
2021-08-24 20:53:28 +03:00
|
|
|
import { DomainRow } from '../../src/domains/DomainRow';
|
2021-12-09 15:44:29 +03:00
|
|
|
import { ReachableServer, SelectedServer } from '../../src/servers/data';
|
2021-12-26 15:53:17 +03:00
|
|
|
import { Domain } from '../../src/domains/data';
|
2021-08-24 20:53:28 +03:00
|
|
|
|
|
|
|
describe('<DomainRow />', () => {
|
|
|
|
let wrapper: ShallowWrapper;
|
2021-12-26 15:53:17 +03:00
|
|
|
const createWrapper = (domain: Domain, selectedServer = Mock.all<SelectedServer>()) => {
|
|
|
|
wrapper = shallow(
|
|
|
|
<DomainRow
|
|
|
|
domain={domain}
|
|
|
|
selectedServer={selectedServer}
|
|
|
|
editDomainRedirects={jest.fn()}
|
|
|
|
checkDomainHealth={jest.fn()}
|
|
|
|
/>,
|
|
|
|
);
|
2021-08-24 20:53:28 +03:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => wrapper?.unmount());
|
|
|
|
|
|
|
|
it.each([
|
2021-12-26 15:53:17 +03:00
|
|
|
[ Mock.of<Domain>({ domain: '', isDefault: true }), undefined, 1, 1, 'defaultDomainBtn' ],
|
|
|
|
[ Mock.of<Domain>({ domain: '', isDefault: false }), undefined, 0, 0, undefined ],
|
|
|
|
[ Mock.of<Domain>({ domain: 'foo.com', isDefault: true }), undefined, 1, 1, 'defaultDomainBtn' ],
|
|
|
|
[ Mock.of<Domain>({ domain: 'foo.bar.com', isDefault: true }), undefined, 1, 1, 'defaultDomainBtn' ],
|
|
|
|
[ Mock.of<Domain>({ domain: 'foo.baz', isDefault: false }), undefined, 0, 0, undefined ],
|
2021-12-09 15:44:29 +03:00
|
|
|
[
|
2021-12-26 15:53:17 +03:00
|
|
|
Mock.of<Domain>({ domain: 'foo.baz', isDefault: true }),
|
2021-12-09 15:44:29 +03:00
|
|
|
Mock.of<ReachableServer>({ version: '2.10.0' }),
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
undefined,
|
|
|
|
],
|
|
|
|
[
|
2021-12-26 15:53:17 +03:00
|
|
|
Mock.of<Domain>({ domain: 'foo.baz', isDefault: true }),
|
2021-12-09 15:44:29 +03:00
|
|
|
Mock.of<ReachableServer>({ version: '2.9.0' }),
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
'defaultDomainBtn',
|
|
|
|
],
|
|
|
|
[
|
2021-12-26 15:53:17 +03:00
|
|
|
Mock.of<Domain>({ domain: 'foo.baz', isDefault: false }),
|
2021-12-09 15:44:29 +03:00
|
|
|
Mock.of<ReachableServer>({ version: '2.9.0' }),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
undefined,
|
|
|
|
],
|
|
|
|
[
|
2021-12-26 15:53:17 +03:00
|
|
|
Mock.of<Domain>({ domain: 'foo.baz', isDefault: false }),
|
2021-12-09 15:44:29 +03:00
|
|
|
Mock.of<ReachableServer>({ version: '2.10.0' }),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
undefined,
|
|
|
|
],
|
|
|
|
])('shows proper components based on provided domain and selectedServer', (
|
2021-09-27 23:50:12 +03:00
|
|
|
domain,
|
2021-12-09 15:44:29 +03:00
|
|
|
selectedServer,
|
|
|
|
expectedDefaultDomainIcons,
|
|
|
|
expectedDisabledComps,
|
2021-09-27 23:50:12 +03:00
|
|
|
expectedDomainId,
|
|
|
|
) => {
|
2021-12-09 15:44:29 +03:00
|
|
|
const wrapper = createWrapper(domain, selectedServer);
|
2021-08-24 20:53:28 +03:00
|
|
|
const defaultDomainComp = wrapper.find('td').first().find('DefaultDomain');
|
2021-12-09 15:44:29 +03:00
|
|
|
const disabledBtn = wrapper.find(Button).findWhere((btn) => !!btn.prop('disabled'));
|
2021-08-24 20:53:28 +03:00
|
|
|
const tooltip = wrapper.find(UncontrolledTooltip);
|
|
|
|
const button = wrapper.find(Button);
|
|
|
|
const icon = wrapper.find(FontAwesomeIcon);
|
|
|
|
|
2021-12-09 15:44:29 +03:00
|
|
|
expect(defaultDomainComp).toHaveLength(expectedDefaultDomainIcons);
|
|
|
|
expect(disabledBtn).toHaveLength(expectedDisabledComps);
|
|
|
|
expect(button.prop('disabled')).toEqual(expectedDisabledComps > 0);
|
|
|
|
expect(icon.prop('icon')).toEqual(expectedDisabledComps > 0 ? forbiddenIcon : editIcon);
|
|
|
|
expect(tooltip).toHaveLength(expectedDisabledComps);
|
2021-09-27 23:50:12 +03:00
|
|
|
|
2021-12-09 15:44:29 +03:00
|
|
|
if (expectedDisabledComps > 0) {
|
2021-09-27 23:50:12 +03:00
|
|
|
expect(tooltip.prop('target')).toEqual(expectedDomainId);
|
|
|
|
}
|
2021-08-24 20:53:28 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[ undefined, 3 ],
|
|
|
|
[ Mock.of<ShlinkDomainRedirects>(), 3 ],
|
|
|
|
[ Mock.of<ShlinkDomainRedirects>({ baseUrlRedirect: 'foo' }), 2 ],
|
|
|
|
[ Mock.of<ShlinkDomainRedirects>({ invalidShortUrlRedirect: 'foo' }), 2 ],
|
|
|
|
[ Mock.of<ShlinkDomainRedirects>({ baseUrlRedirect: 'foo', regular404Redirect: 'foo' }), 1 ],
|
|
|
|
[
|
|
|
|
Mock.of<ShlinkDomainRedirects>(
|
|
|
|
{ baseUrlRedirect: 'foo', regular404Redirect: 'foo', invalidShortUrlRedirect: 'foo' },
|
|
|
|
),
|
|
|
|
0,
|
|
|
|
],
|
|
|
|
])('shows expected redirects', (redirects, expectedNoRedirects) => {
|
2021-12-26 15:53:17 +03:00
|
|
|
const wrapper = createWrapper(Mock.of<Domain>({ domain: '', isDefault: true, redirects }));
|
2021-08-24 20:53:28 +03:00
|
|
|
const noRedirects = wrapper.find('Nr');
|
|
|
|
const cells = wrapper.find('td');
|
|
|
|
|
|
|
|
expect(noRedirects).toHaveLength(expectedNoRedirects);
|
|
|
|
redirects?.baseUrlRedirect && expect(cells.at(1).html()).toContain(redirects.baseUrlRedirect);
|
|
|
|
redirects?.regular404Redirect && expect(cells.at(2).html()).toContain(redirects.regular404Redirect);
|
|
|
|
redirects?.invalidShortUrlRedirect && expect(cells.at(3).html()).toContain(redirects.invalidShortUrlRedirect);
|
|
|
|
});
|
|
|
|
});
|