2022-07-10 00:03:21 +03:00
|
|
|
import { screen, waitFor } from '@testing-library/react';
|
2023-04-13 23:47:13 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2022-06-11 09:53:48 +03:00
|
|
|
import { formatDistance, parseISO } from 'date-fns';
|
2023-08-02 09:23:48 +03:00
|
|
|
import type { ShortUrlDetail } from '../../shlink-web-component/src/short-urls/reducers/shortUrlDetail';
|
|
|
|
import type { ShortUrlVisits } from '../../shlink-web-component/src/visits/reducers/shortUrlVisits';
|
|
|
|
import { ShortUrlVisitsHeader } from '../../shlink-web-component/src/visits/ShortUrlVisitsHeader';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2020-05-10 20:37:00 +03:00
|
|
|
|
|
|
|
describe('<ShortUrlVisitsHeader />', () => {
|
2022-06-11 09:53:48 +03:00
|
|
|
const dateCreated = '2018-01-01T10:00:00+00:00';
|
2021-03-05 17:44:15 +03:00
|
|
|
const longUrl = 'https://foo.bar/bar/foo';
|
2023-04-13 23:47:13 +03:00
|
|
|
const shortUrlVisits = fromPartial<ShortUrlVisits>({
|
2020-05-10 20:37:00 +03:00
|
|
|
visits: [{}, {}, {}],
|
2020-09-05 09:49:18 +03:00
|
|
|
});
|
2023-05-27 12:57:26 +03:00
|
|
|
const goBack = vi.fn();
|
2022-06-11 09:53:48 +03:00
|
|
|
const setUp = (title?: string | null) => {
|
2023-04-13 23:47:13 +03:00
|
|
|
const shortUrlDetail = fromPartial<ShortUrlDetail>({
|
2021-03-05 17:44:15 +03:00
|
|
|
shortUrl: {
|
2023-01-18 00:53:49 +03:00
|
|
|
shortUrl: 'https://s.test/abc123',
|
2021-03-05 17:44:15 +03:00
|
|
|
longUrl,
|
|
|
|
dateCreated,
|
|
|
|
title,
|
|
|
|
},
|
|
|
|
loading: false,
|
|
|
|
});
|
2022-07-10 00:03:21 +03:00
|
|
|
return renderWithEvents(
|
|
|
|
<ShortUrlVisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} goBack={goBack} />,
|
|
|
|
);
|
2021-03-05 17:44:15 +03:00
|
|
|
};
|
|
|
|
|
2022-06-11 09:53:48 +03:00
|
|
|
it('shows when the URL was created', async () => {
|
|
|
|
const { user } = setUp();
|
|
|
|
const dateElement = screen.getByText(`${formatDistance(new Date(), parseISO(dateCreated))} ago`);
|
2020-05-10 20:37:00 +03:00
|
|
|
|
2022-06-11 09:53:48 +03:00
|
|
|
expect(dateElement).toBeInTheDocument();
|
|
|
|
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
|
|
|
|
await user.hover(dateElement);
|
|
|
|
await waitFor(() => expect(screen.getByRole('tooltip')).toHaveTextContent('2018-01-01 10:00'));
|
2020-05-10 20:37:00 +03:00
|
|
|
});
|
|
|
|
|
2021-03-05 17:44:15 +03:00
|
|
|
it.each([
|
2022-06-11 09:53:48 +03:00
|
|
|
[null, `Long URL: ${longUrl}`],
|
|
|
|
[undefined, `Long URL: ${longUrl}`],
|
|
|
|
['My cool title', 'Title: My cool title'],
|
2021-03-05 17:44:15 +03:00
|
|
|
])('shows the long URL and title', (title, expectedContent) => {
|
2022-06-11 09:53:48 +03:00
|
|
|
const { container } = setUp(title);
|
2020-05-10 20:37:00 +03:00
|
|
|
|
2022-06-11 09:53:48 +03:00
|
|
|
expect(container.querySelector('.long-url-container')).toHaveTextContent(expectedContent);
|
|
|
|
expect(screen.getByRole('link', { name: title ?? longUrl })).toHaveAttribute('href', longUrl);
|
2020-05-10 20:37:00 +03:00
|
|
|
});
|
|
|
|
});
|