shlink-web-client/test/short-urls/helpers/ShortUrlsRow.test.tsx

101 lines
3.5 KiB
TypeScript
Raw Normal View History

import { screen } from '@testing-library/react';
import { Mock } from 'ts-mockery';
import { formatISO } from 'date-fns';
import { ShortUrlsRow as createShortUrlsRow } from '../../../src/short-urls/helpers/ShortUrlsRow';
import { TimeoutToggle } from '../../../src/utils/helpers/hooks';
import { ShortUrl } from '../../../src/short-urls/data';
import { ReachableServer } from '../../../src/servers/data';
import { parseDate } from '../../../src/utils/helpers/date';
import { renderWithEvents } from '../../__helpers__/setUpTest';
import { OptionalString } from '../../../src/utils/utils';
2022-07-16 11:52:45 +03:00
import { colorGeneratorMock } from '../../utils/services/__mocks__/ColorGenerator.mock';
2019-01-13 11:49:02 +03:00
describe('<ShortUrlsRow />', () => {
const timeoutToggle = jest.fn(() => true);
const useTimeoutToggle = jest.fn(() => [false, timeoutToggle]) as TimeoutToggle;
2021-03-05 17:23:38 +03:00
const server = Mock.of<ReachableServer>({ url: 'https://doma.in' });
const shortUrl: ShortUrl = {
2019-01-13 11:49:02 +03:00
shortCode: 'abc123',
shortUrl: 'https://doma.in/abc123',
longUrl: 'https://foo.com/bar',
dateCreated: formatISO(parseDate('2018-05-23 18:30:41', 'yyyy-MM-dd HH:mm:ss')),
tags: [],
2019-01-13 11:49:02 +03:00
visitsCount: 45,
domain: null,
meta: {
validSince: null,
validUntil: null,
maxVisits: null,
},
2019-01-13 11:49:02 +03:00
};
2022-07-16 11:52:45 +03:00
const ShortUrlsRow = createShortUrlsRow(() => <span>ShortUrlsRowMenu</span>, colorGeneratorMock, useTimeoutToggle);
const setUp = (title?: OptionalString, tags: string[] = []) => renderWithEvents(
<table>
<tbody>
<ShortUrlsRow selectedServer={server} shortUrl={{ ...shortUrl, title, tags }} onTagClick={() => null} />
</tbody>
</table>,
);
2019-01-13 11:49:02 +03:00
2021-03-05 17:23:38 +03:00
it.each([
2022-03-26 14:17:42 +03:00
[null, 6],
[undefined, 6],
['The title', 7],
2021-03-05 17:23:38 +03:00
])('renders expected amount of columns', (title, expectedAmount) => {
setUp(title);
expect(screen.getAllByRole('cell')).toHaveLength(expectedAmount);
2021-03-05 17:23:38 +03:00
});
2019-01-13 11:49:02 +03:00
it('renders date in first column', () => {
setUp();
expect(screen.getAllByRole('cell')[0]).toHaveTextContent('2018-05-23 18:30');
2019-01-13 11:49:02 +03:00
});
it.each([
[1, shortUrl.shortUrl],
[2, shortUrl.longUrl],
])('renders expected links on corresponding columns', (colIndex, expectedLink) => {
setUp();
2019-01-13 11:49:02 +03:00
const col = screen.getAllByRole('cell')[colIndex];
const link = col.querySelector('a');
2021-03-05 17:23:38 +03:00
expect(link).toHaveAttribute('href', expectedLink);
2021-03-05 17:23:38 +03:00
});
it.each([
['My super cool title', 'My super cool title'],
[undefined, shortUrl.longUrl],
])('renders title when short URL has it', (title, expectedContent) => {
setUp(title);
2019-01-13 11:49:02 +03:00
const titleSharedCol = screen.getAllByRole('cell')[2];
2019-01-13 11:49:02 +03:00
expect(titleSharedCol.querySelector('a')).toHaveAttribute('href', shortUrl.longUrl);
expect(titleSharedCol).toHaveTextContent(expectedContent);
});
2019-01-13 11:49:02 +03:00
it.each([
[[], ['No tags']],
[['nodejs', 'reactjs'], ['nodejs', 'reactjs']],
])('renders list of tags in fourth row', (tags, expectedContents) => {
setUp(undefined, tags);
const cell = screen.getAllByRole('cell')[3];
2019-01-13 11:49:02 +03:00
expectedContents.forEach((content) => expect(cell).toHaveTextContent(content));
2019-01-13 11:49:02 +03:00
});
it('renders visits count in fifth row', () => {
setUp();
expect(screen.getAllByRole('cell')[4]).toHaveTextContent(`${shortUrl.visitsCount}`);
2019-01-13 11:49:02 +03:00
});
it('updates state when copied to clipboard', async () => {
const { user } = setUp();
2019-01-13 11:49:02 +03:00
expect(timeoutToggle).not.toHaveBeenCalled();
await user.click(screen.getByRole('img', { hidden: true }));
expect(timeoutToggle).toHaveBeenCalledTimes(1);
2019-01-13 11:49:02 +03:00
});
});