shlink-web-client/test/visits/ShortUrlVisitsHeader.test.tsx

56 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-09-05 09:49:18 +03:00
import { shallow, ShallowWrapper } from 'enzyme';
import Moment from 'react-moment';
import { ExternalLink } from 'react-external-link';
2020-09-05 09:49:18 +03:00
import { Mock } from 'ts-mockery';
import ShortUrlVisitsHeader from '../../src/visits/ShortUrlVisitsHeader';
2020-09-05 09:49:18 +03:00
import { ShortUrlDetail } from '../../src/visits/reducers/shortUrlDetail';
import { ShortUrlVisits } from '../../src/visits/reducers/shortUrlVisits';
describe('<ShortUrlVisitsHeader />', () => {
2020-09-05 09:49:18 +03:00
let wrapper: ShallowWrapper;
2021-03-05 17:44:15 +03:00
const dateCreated = '2018-01-01T10:00:00+01:00';
const longUrl = 'https://foo.bar/bar/foo';
2020-09-05 09:49:18 +03:00
const shortUrlVisits = Mock.of<ShortUrlVisits>({
visits: [{}, {}, {}],
2020-09-05 09:49:18 +03:00
});
const goBack = jest.fn();
2021-03-05 17:44:15 +03:00
const createWrapper = (title?: string | null) => {
const shortUrlDetail = Mock.of<ShortUrlDetail>({
shortUrl: {
shortUrl: 'https://doma.in/abc123',
longUrl,
dateCreated,
title,
},
loading: false,
});
wrapper = shallow(
2020-08-22 09:10:31 +03:00
<ShortUrlVisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} goBack={goBack} />,
);
2021-03-05 17:44:15 +03:00
return wrapper;
};
beforeEach(() => createWrapper());
afterEach(() => wrapper.unmount());
it('shows when the URL was created', () => {
const moment = wrapper.find(Moment).first();
2021-03-05 17:44:15 +03:00
expect(moment.prop('children')).toEqual(dateCreated);
});
2021-03-05 17:44:15 +03:00
it.each([
[ null, longUrl ],
[ undefined, longUrl ],
[ 'My cool title', 'My cool title' ],
])('shows the long URL and title', (title, expectedContent) => {
const wrapper = createWrapper(title);
const longUrlLink = wrapper.find(ExternalLink).last();
2021-03-05 17:44:15 +03:00
expect(longUrlLink.prop('href')).toEqual(longUrl);
expect(longUrlLink.prop('children')).toEqual(expectedContent);
});
});