2020-08-30 10:59:14 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2019-01-13 11:49:02 +03:00
|
|
|
import moment from 'moment';
|
|
|
|
import Moment from 'react-moment';
|
|
|
|
import { assoc, toString } from 'ramda';
|
2020-08-30 10:59:14 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2020-01-15 20:16:12 +03:00
|
|
|
import { ExternalLink } from 'react-external-link';
|
2019-01-13 11:49:02 +03:00
|
|
|
import createShortUrlsRow from '../../../src/short-urls/helpers/ShortUrlsRow';
|
|
|
|
import Tag from '../../../src/tags/helpers/Tag';
|
2020-08-30 10:59:14 +03:00
|
|
|
import ColorGenerator from '../../../src/utils/services/ColorGenerator';
|
|
|
|
import { StateFlagTimeout } from '../../../src/utils/helpers/hooks';
|
|
|
|
import { ShortUrl } from '../../../src/short-urls/data';
|
|
|
|
import { ReachableServer } from '../../../src/servers/data';
|
2021-01-24 19:37:31 +03:00
|
|
|
import { CopyToClipboardIcon } from '../../../src/utils/CopyToClipboardIcon';
|
2019-01-13 11:49:02 +03:00
|
|
|
|
|
|
|
describe('<ShortUrlsRow />', () => {
|
2020-08-30 10:59:14 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
|
|
|
const mockFunction = () => null;
|
2019-01-13 11:49:02 +03:00
|
|
|
const ShortUrlsRowMenu = mockFunction;
|
2020-03-06 23:44:03 +03:00
|
|
|
const stateFlagTimeout = jest.fn(() => true);
|
2020-08-30 10:59:14 +03:00
|
|
|
const useStateFlagTimeout = jest.fn(() => [ false, stateFlagTimeout ]) as StateFlagTimeout;
|
|
|
|
const colorGenerator = Mock.of<ColorGenerator>({
|
|
|
|
getColorForKey: jest.fn(),
|
|
|
|
setColorForKey: jest.fn(),
|
|
|
|
});
|
|
|
|
const server = Mock.of<ReachableServer>({
|
2019-01-13 11:49:02 +03:00
|
|
|
url: 'https://doma.in',
|
2020-08-30 10:59:14 +03:00
|
|
|
});
|
|
|
|
const shortUrl: ShortUrl = {
|
2019-01-13 11:49:02 +03:00
|
|
|
shortCode: 'abc123',
|
|
|
|
shortUrl: 'http://doma.in/abc123',
|
|
|
|
longUrl: 'http://foo.com/bar',
|
|
|
|
dateCreated: moment('2018-05-23 18:30:41').format(),
|
|
|
|
tags: [ 'nodejs', 'reactjs' ],
|
|
|
|
visitsCount: 45,
|
2020-08-30 10:59:14 +03:00
|
|
|
domain: null,
|
|
|
|
meta: {
|
|
|
|
validSince: null,
|
|
|
|
validUntil: null,
|
|
|
|
maxVisits: null,
|
|
|
|
},
|
2019-01-13 11:49:02 +03:00
|
|
|
};
|
2020-01-11 21:40:16 +03:00
|
|
|
|
2020-01-11 21:58:04 +03:00
|
|
|
beforeEach(() => {
|
2020-03-06 23:44:03 +03:00
|
|
|
const ShortUrlsRow = createShortUrlsRow(ShortUrlsRowMenu, colorGenerator, useStateFlagTimeout);
|
2020-01-11 21:40:16 +03:00
|
|
|
|
2020-12-20 11:09:22 +03:00
|
|
|
wrapper = shallow(<ShortUrlsRow selectedServer={server} shortUrl={shortUrl} onTagClick={mockFunction} />);
|
2020-01-11 21:58:04 +03:00
|
|
|
});
|
|
|
|
afterEach(() => wrapper.unmount());
|
2019-01-13 11:49:02 +03:00
|
|
|
|
|
|
|
it('renders date in first column', () => {
|
|
|
|
const col = wrapper.find('td').first();
|
|
|
|
const moment = col.find(Moment);
|
|
|
|
|
|
|
|
expect(moment.html()).toContain('>2018-05-23 18:30</time>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders short URL in second row', () => {
|
|
|
|
const col = wrapper.find('td').at(1);
|
|
|
|
const link = col.find(ExternalLink);
|
|
|
|
|
|
|
|
expect(link.prop('href')).toEqual(shortUrl.shortUrl);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders long URL in third row', () => {
|
2019-03-10 10:28:14 +03:00
|
|
|
const col = wrapper.find('td').at(2);
|
2019-01-13 11:49:02 +03:00
|
|
|
const link = col.find(ExternalLink);
|
|
|
|
|
|
|
|
expect(link.prop('href')).toEqual(shortUrl.longUrl);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('renders list of tags in fourth row', () => {
|
|
|
|
it('with tags', () => {
|
2019-03-10 10:28:14 +03:00
|
|
|
const col = wrapper.find('td').at(3);
|
2019-01-13 11:49:02 +03:00
|
|
|
const tags = col.find(Tag);
|
|
|
|
|
|
|
|
expect(tags).toHaveLength(shortUrl.tags.length);
|
|
|
|
shortUrl.tags.forEach((tagValue, index) => {
|
|
|
|
const tag = tags.at(index);
|
|
|
|
|
|
|
|
expect(tag.prop('text')).toEqual(tagValue);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('without tags', () => {
|
|
|
|
wrapper.setProps({ shortUrl: assoc('tags', [], shortUrl) });
|
|
|
|
|
2019-03-10 10:28:14 +03:00
|
|
|
const col = wrapper.find('td').at(3);
|
2019-01-13 11:49:02 +03:00
|
|
|
|
|
|
|
expect(col.text()).toContain('No tags');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders visits count in fifth row', () => {
|
2019-03-10 10:28:14 +03:00
|
|
|
const col = wrapper.find('td').at(4);
|
2020-01-11 21:40:16 +03:00
|
|
|
|
2020-01-11 21:58:04 +03:00
|
|
|
expect(col.html()).toContain(toString(shortUrl.visitsCount));
|
2019-01-13 11:49:02 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('updates state when copied to clipboard', () => {
|
2020-03-06 23:22:07 +03:00
|
|
|
const col = wrapper.find('td').at(1);
|
2021-01-24 19:37:31 +03:00
|
|
|
const menu = col.find(CopyToClipboardIcon);
|
2019-01-13 11:49:02 +03:00
|
|
|
|
|
|
|
expect(menu).toHaveLength(1);
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(stateFlagTimeout).not.toHaveBeenCalled();
|
2020-03-06 23:22:07 +03:00
|
|
|
menu.simulate('copy');
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(stateFlagTimeout).toHaveBeenCalledTimes(1);
|
2019-01-13 11:49:02 +03:00
|
|
|
});
|
|
|
|
});
|