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

47 lines
1.8 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';
import { MemoryRouter } from 'react-router-dom';
2021-02-28 12:36:56 +03:00
import { Mock } from 'ts-mockery';
2023-02-18 13:11:01 +03:00
import type { ReportExporter } from '../../src/common/services/ReportExporter';
2023-02-18 12:40:37 +03:00
import type { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
import type { Settings } from '../../src/settings/reducers/settings';
2023-02-18 13:11:01 +03:00
import { OrphanVisits as createOrphanVisits } from '../../src/visits/OrphanVisits';
2023-02-18 12:40:37 +03:00
import type { VisitsInfo } from '../../src/visits/reducers/types';
2023-02-18 13:11:01 +03:00
import type { Visit } from '../../src/visits/types';
import { renderWithEvents } from '../__helpers__/setUpTest';
2021-02-28 12:36:56 +03:00
describe('<OrphanVisits />', () => {
const getOrphanVisits = jest.fn();
const exportVisits = jest.fn();
const orphanVisits = Mock.of<VisitsInfo>({ visits: [Mock.of<Visit>({ date: formatISO(new Date()) })] });
const OrphanVisits = createOrphanVisits(Mock.of<ReportExporter>({ exportVisits }));
const setUp = () => renderWithEvents(
<MemoryRouter>
<OrphanVisits
{...Mock.of<MercureBoundProps>({ mercureInfo: {} })}
getOrphanVisits={getOrphanVisits}
orphanVisits={orphanVisits}
cancelGetOrphanVisits={jest.fn()}
settings={Mock.all<Settings>()}
/>
</MemoryRouter>,
);
2021-02-28 12:36:56 +03:00
it('wraps visits stats and header', () => {
setUp();
expect(screen.getByRole('heading', { name: 'Orphan visits' })).toBeInTheDocument();
expect(getOrphanVisits).toHaveBeenCalled();
});
it('exports visits when clicking the button', async () => {
const { user } = setUp();
const btn = screen.getByRole('button', { name: 'Export (1)' });
2021-02-28 12:36:56 +03:00
expect(exportVisits).not.toHaveBeenCalled();
expect(btn).toBeInTheDocument();
2021-02-28 12:36:56 +03:00
await user.click(btn);
expect(exportVisits).toHaveBeenCalledWith('orphan_visits.csv', expect.anything());
2021-02-28 12:36:56 +03:00
});
});