2022-07-10 00:03:21 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2023-04-13 23:47:13 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { formatISO } from 'date-fns';
|
2018-09-08 14:28:40 +03:00
|
|
|
import { identity } from 'ramda';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2023-07-24 21:14:59 +03:00
|
|
|
import type { MercureBoundProps } from '../../shlink-web-component/mercure/helpers/boundToMercureHub';
|
|
|
|
import type { ShortUrlVisits as ShortUrlVisitsState } from '../../shlink-web-component/visits/reducers/shortUrlVisits';
|
|
|
|
import type { ShortUrlVisitsProps } from '../../shlink-web-component/visits/ShortUrlVisits';
|
|
|
|
import { ShortUrlVisits as createShortUrlVisits } from '../../shlink-web-component/visits/ShortUrlVisits';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2022-02-08 00:17:57 +03:00
|
|
|
|
2018-09-08 14:28:40 +03:00
|
|
|
describe('<ShortUrlVisits />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const getShortUrlVisitsMock = vi.fn();
|
|
|
|
const exportVisits = vi.fn();
|
2023-04-13 23:47:13 +03:00
|
|
|
const shortUrlVisits = fromPartial<ShortUrlVisitsState>({ visits: [{ date: formatISO(new Date()) }] });
|
|
|
|
const ShortUrlVisits = createShortUrlVisits(fromPartial({ exportVisits }));
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = () => renderWithEvents(
|
|
|
|
<MemoryRouter>
|
|
|
|
<ShortUrlVisits
|
2023-04-13 23:47:13 +03:00
|
|
|
{...fromPartial<ShortUrlVisitsProps>({})}
|
|
|
|
{...fromPartial<MercureBoundProps>({ mercureInfo: {} })}
|
2022-07-10 00:03:21 +03:00
|
|
|
getShortUrlDetail={identity}
|
|
|
|
getShortUrlVisits={getShortUrlVisitsMock}
|
|
|
|
shortUrlVisits={shortUrlVisits}
|
2023-04-13 23:47:13 +03:00
|
|
|
shortUrlDetail={fromPartial({})}
|
|
|
|
settings={fromPartial({})}
|
2022-07-10 00:03:21 +03:00
|
|
|
cancelGetShortUrlVisits={() => {}}
|
|
|
|
/>
|
|
|
|
</MemoryRouter>,
|
|
|
|
);
|
2018-09-08 14:28:40 +03:00
|
|
|
|
2022-06-10 22:31:13 +03:00
|
|
|
it('wraps visits stats and header', () => {
|
|
|
|
setUp();
|
|
|
|
expect(screen.getAllByRole('heading')[0]).toHaveTextContent('Visits for');
|
|
|
|
expect(getShortUrlVisitsMock).toHaveBeenCalled();
|
2018-09-08 14:28:40 +03:00
|
|
|
});
|
|
|
|
|
2022-06-10 22:31:13 +03:00
|
|
|
it('exports visits when clicking the button', async () => {
|
|
|
|
const { user } = setUp();
|
|
|
|
const btn = screen.getByRole('button', { name: 'Export (1)' });
|
2019-01-09 22:11:22 +03:00
|
|
|
|
2022-06-10 22:31:13 +03:00
|
|
|
expect(exportVisits).not.toHaveBeenCalled();
|
|
|
|
expect(btn).toBeInTheDocument();
|
2019-01-09 22:11:22 +03:00
|
|
|
|
2022-06-10 22:31:13 +03:00
|
|
|
await user.click(btn);
|
|
|
|
expect(exportVisits).toHaveBeenCalledWith('short-url_undefined_visits.csv', expect.anything());
|
2019-01-09 22:11:22 +03:00
|
|
|
});
|
2018-09-08 14:28:40 +03:00
|
|
|
});
|