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

45 lines
1.8 KiB
TypeScript
Raw Normal View History

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';
import { MemoryRouter } from 'react-router-dom';
2023-02-18 12:40:37 +03:00
import type { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
import { NonOrphanVisits as createNonOrphanVisits } from '../../src/shlink-web-component/visits/NonOrphanVisits';
import type { VisitsInfo } from '../../src/shlink-web-component/visits/reducers/types';
2023-02-18 13:11:01 +03:00
import { renderWithEvents } from '../__helpers__/setUpTest';
describe('<NonOrphanVisits />', () => {
2023-05-27 12:57:26 +03:00
const exportVisits = vi.fn();
const getNonOrphanVisits = vi.fn();
const cancelGetNonOrphanVisits = vi.fn();
2023-04-13 23:47:13 +03:00
const nonOrphanVisits = fromPartial<VisitsInfo>({ visits: [{ date: formatISO(new Date()) }] });
const NonOrphanVisits = createNonOrphanVisits(fromPartial({ exportVisits }));
const setUp = () => renderWithEvents(
<MemoryRouter>
<NonOrphanVisits
2023-04-13 23:47:13 +03:00
{...fromPartial<MercureBoundProps>({ mercureInfo: {} })}
getNonOrphanVisits={getNonOrphanVisits}
cancelGetNonOrphanVisits={cancelGetNonOrphanVisits}
nonOrphanVisits={nonOrphanVisits}
2023-04-13 23:47:13 +03:00
settings={fromPartial({})}
/>
</MemoryRouter>,
);
it('wraps visits stats and header', () => {
setUp();
expect(screen.getByRole('heading', { name: 'Non-orphan visits' })).toBeInTheDocument();
expect(getNonOrphanVisits).toHaveBeenCalled();
});
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('non_orphan_visits.csv', expect.anything());
});
});