2022-07-10 00:03:21 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2022-06-10 22:31:13 +03:00
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2018-09-08 14:28:40 +03:00
|
|
|
import { identity } from 'ramda';
|
2020-09-04 20:33:16 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-06-10 22:31:13 +03:00
|
|
|
import { formatISO } from 'date-fns';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { ShortUrlVisits as createShortUrlVisits, ShortUrlVisitsProps } from '../../src/visits/ShortUrlVisits';
|
2020-09-04 20:33:16 +03:00
|
|
|
import { ShortUrlVisits as ShortUrlVisitsState } from '../../src/visits/reducers/shortUrlVisits';
|
2021-03-05 18:04:02 +03:00
|
|
|
import { ShortUrlDetail } from '../../src/short-urls/reducers/shortUrlDetail';
|
2020-09-06 20:41:15 +03:00
|
|
|
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
|
2022-03-13 20:56:42 +03:00
|
|
|
import { ReportExporter } from '../../src/common/services/ReportExporter';
|
2022-06-10 22:31:13 +03:00
|
|
|
import { Visit } from '../../src/visits/types';
|
|
|
|
import { Settings } from '../../src/settings/reducers/settings';
|
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 />', () => {
|
2019-04-19 13:41:59 +03:00
|
|
|
const getShortUrlVisitsMock = jest.fn();
|
2022-06-10 22:31:13 +03:00
|
|
|
const exportVisits = jest.fn();
|
|
|
|
const shortUrlVisits = Mock.of<ShortUrlVisitsState>({ visits: [Mock.of<Visit>({ date: formatISO(new Date()) })] });
|
|
|
|
const ShortUrlVisits = createShortUrlVisits(Mock.of<ReportExporter>({ exportVisits }));
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = () => renderWithEvents(
|
|
|
|
<MemoryRouter>
|
|
|
|
<ShortUrlVisits
|
|
|
|
{...Mock.all<ShortUrlVisitsProps>()}
|
|
|
|
{...Mock.of<MercureBoundProps>({ mercureInfo: {} })}
|
|
|
|
getShortUrlDetail={identity}
|
|
|
|
getShortUrlVisits={getShortUrlVisitsMock}
|
|
|
|
shortUrlVisits={shortUrlVisits}
|
|
|
|
shortUrlDetail={Mock.all<ShortUrlDetail>()}
|
|
|
|
settings={Mock.all<Settings>()}
|
|
|
|
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
|
|
|
});
|