2022-12-19 22:18:50 +03:00
|
|
|
import { render, screen, waitFor } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-08-02 10:01:44 +03:00
|
|
|
import type { ShortUrl } from '../../../src/short-urls/data';
|
|
|
|
import { ShortUrlVisitsCount } from '../../../src/short-urls/helpers/ShortUrlVisitsCount';
|
2020-01-11 21:58:04 +03:00
|
|
|
|
|
|
|
describe('<ShortUrlVisitsCount />', () => {
|
2022-12-19 22:18:50 +03:00
|
|
|
const setUp = (visitsCount: number, shortUrl: ShortUrl) => ({
|
|
|
|
user: userEvent.setup(),
|
|
|
|
...render(
|
|
|
|
<ShortUrlVisitsCount visitsCount={visitsCount} shortUrl={shortUrl} />,
|
|
|
|
),
|
|
|
|
});
|
2020-01-11 21:58:04 +03:00
|
|
|
|
2022-12-19 22:18:50 +03:00
|
|
|
it.each([undefined, {}])('just returns visits when no limits are provided', (meta) => {
|
2020-01-11 21:58:04 +03:00
|
|
|
const visitsCount = 45;
|
2023-04-13 22:48:29 +03:00
|
|
|
const { container } = setUp(visitsCount, fromPartial({ meta }));
|
2020-01-11 21:58:04 +03:00
|
|
|
|
2022-07-07 19:44:00 +03:00
|
|
|
expect(container.firstChild).toHaveTextContent(`${visitsCount}`);
|
|
|
|
expect(container.querySelector('.short-urls-visits-count__max-visits-control')).not.toBeInTheDocument();
|
2020-01-11 21:58:04 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('displays the maximum amount of visits when present', () => {
|
|
|
|
const visitsCount = 45;
|
|
|
|
const maxVisits = 500;
|
|
|
|
const meta = { maxVisits };
|
2023-04-13 22:48:29 +03:00
|
|
|
const { container } = setUp(visitsCount, fromPartial({ meta }));
|
2020-01-11 21:58:04 +03:00
|
|
|
|
2022-07-07 19:44:00 +03:00
|
|
|
expect(container.firstChild).toHaveTextContent(`/ ${maxVisits}`);
|
2022-12-19 22:18:50 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[['This short URL will not accept more than 50 visits'], { maxVisits: 50 }],
|
|
|
|
[['This short URL will not accept more than 1 visit'], { maxVisits: 1 }],
|
|
|
|
[['This short URL will not accept visits before 2022-01-01 10:00'], { validSince: '2022-01-01T10:00:00' }],
|
|
|
|
[['This short URL will not accept visits after 2022-05-05 15:30'], { validUntil: '2022-05-05T15:30:30' }],
|
|
|
|
[[
|
|
|
|
'This short URL will not accept more than 100 visits',
|
|
|
|
'This short URL will not accept visits after 2022-05-05 15:30',
|
|
|
|
], { validUntil: '2022-05-05T15:30:30', maxVisits: 100 }],
|
|
|
|
[[
|
|
|
|
'This short URL will not accept more than 100 visits',
|
|
|
|
'This short URL will not accept visits before 2023-01-01 10:00',
|
|
|
|
'This short URL will not accept visits after 2023-05-05 15:30',
|
|
|
|
], { validSince: '2023-01-01T10:00:00', validUntil: '2023-05-05T15:30:30', maxVisits: 100 }],
|
|
|
|
])('displays proper amount of tooltip list items', async (expectedListItems, meta) => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const { user } = setUp(100, fromPartial({ meta }));
|
2022-12-19 22:18:50 +03:00
|
|
|
|
|
|
|
await user.hover(screen.getByRole('img', { hidden: true }));
|
|
|
|
await waitFor(() => expect(screen.getByRole('list')));
|
|
|
|
|
|
|
|
const items = screen.getAllByRole('listitem');
|
|
|
|
expect(items).toHaveLength(expectedListItems.length);
|
|
|
|
expectedListItems.forEach((text, index) => expect(items[index]).toHaveTextContent(text));
|
2020-01-11 21:58:04 +03:00
|
|
|
});
|
|
|
|
});
|