2020-08-30 10:59:14 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2020-02-08 11:20:19 +03:00
|
|
|
import { Link } from 'react-router-dom';
|
2020-08-30 10:59:14 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2021-03-20 18:32:12 +03:00
|
|
|
import ShortUrlDetailLink, { LinkSuffix } from '../../../src/short-urls/helpers/ShortUrlDetailLink';
|
2020-08-30 10:59:14 +03:00
|
|
|
import { NotFoundServer, ReachableServer } from '../../../src/servers/data';
|
|
|
|
import { ShortUrl } from '../../../src/short-urls/data';
|
2020-02-08 11:20:19 +03:00
|
|
|
|
2021-03-20 18:32:12 +03:00
|
|
|
describe('<ShortUrlDetailLink />', () => {
|
2020-08-30 10:59:14 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2020-02-08 11:20:19 +03:00
|
|
|
|
2020-08-30 10:59:14 +03:00
|
|
|
afterEach(() => wrapper?.unmount());
|
2020-02-08 11:20:19 +03:00
|
|
|
|
2020-02-17 20:21:52 +03:00
|
|
|
it.each([
|
2020-02-08 11:20:19 +03:00
|
|
|
[ undefined, undefined ],
|
|
|
|
[ null, null ],
|
2020-08-30 10:59:14 +03:00
|
|
|
[ Mock.of<ReachableServer>({ id: '1' }), null ],
|
|
|
|
[ Mock.of<ReachableServer>({ id: '1' }), undefined ],
|
|
|
|
[ Mock.of<NotFoundServer>(), Mock.all<ShortUrl>() ],
|
|
|
|
[ null, Mock.all<ShortUrl>() ],
|
|
|
|
[ undefined, Mock.all<ShortUrl>() ],
|
|
|
|
])('only renders a plain span when either server or short URL are not set', (selectedServer, shortUrl) => {
|
2021-03-20 18:32:12 +03:00
|
|
|
wrapper = shallow(
|
|
|
|
<ShortUrlDetailLink selectedServer={selectedServer} shortUrl={shortUrl} suffix="visits">
|
|
|
|
Something
|
|
|
|
</ShortUrlDetailLink>,
|
|
|
|
);
|
2020-02-08 11:20:19 +03:00
|
|
|
const link = wrapper.find(Link);
|
|
|
|
|
|
|
|
expect(link).toHaveLength(0);
|
|
|
|
expect(wrapper.html()).toEqual('<span>Something</span>');
|
|
|
|
});
|
|
|
|
|
2020-02-17 20:21:52 +03:00
|
|
|
it.each([
|
2020-02-08 11:20:19 +03:00
|
|
|
[
|
2020-08-30 10:59:14 +03:00
|
|
|
Mock.of<ReachableServer>({ id: '1' }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode: 'abc123' }),
|
2021-03-20 18:32:12 +03:00
|
|
|
'visits' as LinkSuffix,
|
2020-08-30 10:59:14 +03:00
|
|
|
'/server/1/short-code/abc123/visits',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
Mock.of<ReachableServer>({ id: '3' }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode: 'def456', domain: 'example.com' }),
|
2021-03-20 18:32:12 +03:00
|
|
|
'visits' as LinkSuffix,
|
2020-02-08 11:20:19 +03:00
|
|
|
'/server/3/short-code/def456/visits?domain=example.com',
|
|
|
|
],
|
2021-03-20 18:32:12 +03:00
|
|
|
[
|
|
|
|
Mock.of<ReachableServer>({ id: '1' }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode: 'abc123' }),
|
|
|
|
'edit' as LinkSuffix,
|
|
|
|
'/server/1/short-code/abc123/edit',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
Mock.of<ReachableServer>({ id: '3' }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode: 'def456', domain: 'example.com' }),
|
|
|
|
'edit' as LinkSuffix,
|
|
|
|
'/server/3/short-code/def456/edit?domain=example.com',
|
|
|
|
],
|
|
|
|
])('renders link with expected query when', (selectedServer, shortUrl, suffix, expectedLink) => {
|
|
|
|
wrapper = shallow(
|
|
|
|
<ShortUrlDetailLink selectedServer={selectedServer} shortUrl={shortUrl} suffix={suffix}>
|
|
|
|
Something
|
|
|
|
</ShortUrlDetailLink>,
|
|
|
|
);
|
2020-02-08 11:20:19 +03:00
|
|
|
const link = wrapper.find(Link);
|
|
|
|
const to = link.prop('to');
|
|
|
|
|
|
|
|
expect(link).toHaveLength(1);
|
|
|
|
expect(to).toEqual(expectedLink);
|
|
|
|
});
|
|
|
|
});
|