shlink-web-client/test/visits/ShortUrlVisits.test.tsx

53 lines
2.2 KiB
TypeScript
Raw Normal View History

import { screen } from '@testing-library/react';
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';
import { Mock } from 'ts-mockery';
2023-02-18 13:11:01 +03:00
import type { ReportExporter } from '../../src/common/services/ReportExporter';
import type { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
import type { Settings } from '../../src/settings/reducers/settings';
import type { ShortUrlDetail } from '../../src/short-urls/reducers/shortUrlDetail';
import type { ShortUrlVisits as ShortUrlVisitsState } from '../../src/visits/reducers/shortUrlVisits';
2023-02-18 12:40:37 +03:00
import type { ShortUrlVisitsProps } from '../../src/visits/ShortUrlVisits';
import { ShortUrlVisits as createShortUrlVisits } from '../../src/visits/ShortUrlVisits';
import type { Visit } from '../../src/visits/types';
2022-07-10 20:44:49 +03:00
import { renderWithEvents } from '../__helpers__/setUpTest';
2018-09-08 14:28:40 +03:00
describe('<ShortUrlVisits />', () => {
2019-04-19 13:41:59 +03:00
const getShortUrlVisitsMock = jest.fn();
const exportVisits = jest.fn();
const shortUrlVisits = Mock.of<ShortUrlVisitsState>({ visits: [Mock.of<Visit>({ date: formatISO(new Date()) })] });
const ShortUrlVisits = createShortUrlVisits(Mock.of<ReportExporter>({ exportVisits }));
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
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
});
it('exports visits when clicking the button', async () => {
const { user } = setUp();
const btn = screen.getByRole('button', { name: 'Export (1)' });
expect(exportVisits).not.toHaveBeenCalled();
expect(btn).toBeInTheDocument();
await user.click(btn);
expect(exportVisits).toHaveBeenCalledWith('short-url_undefined_visits.csv', expect.anything());
});
2018-09-08 14:28:40 +03:00
});