shlink-web-client/test/short-urls/helpers/ExportShortUrlsBtn.test.tsx

60 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-10-05 00:17:12 +03:00
import { screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
2023-02-18 13:11:01 +03:00
import { Mock } from 'ts-mockery';
2023-02-18 12:40:37 +03:00
import type { ReportExporter } from '../../../src/common/services/ReportExporter';
import type { NotFoundServer, ReachableServer, SelectedServer } from '../../../src/servers/data';
2023-02-18 13:11:01 +03:00
import { ExportShortUrlsBtn as createExportShortUrlsBtn } from '../../../src/short-urls/helpers/ExportShortUrlsBtn';
2022-07-10 20:44:49 +03:00
import { renderWithEvents } from '../../__helpers__/setUpTest';
2022-03-17 22:28:47 +03:00
describe('<ExportShortUrlsBtn />', () => {
const listShortUrls = jest.fn();
const buildShlinkApiClient = jest.fn().mockReturnValue({ listShortUrls });
const exportShortUrls = jest.fn();
const reportExporter = Mock.of<ReportExporter>({ exportShortUrls });
const ExportShortUrlsBtn = createExportShortUrlsBtn(buildShlinkApiClient, reportExporter);
const setUp = (amount?: number, selectedServer?: SelectedServer) => renderWithEvents(
<MemoryRouter>
<ExportShortUrlsBtn selectedServer={selectedServer ?? Mock.all<SelectedServer>()} amount={amount} />
</MemoryRouter>,
);
2022-03-17 22:28:47 +03:00
afterEach(jest.clearAllMocks);
it.each([
[undefined, '0'],
[1, '1'],
[4578, '4,578'],
2022-03-17 22:28:47 +03:00
])('renders expected amount', (amount, expectedAmount) => {
setUp(amount);
expect(screen.getByText(/Export/)).toHaveTextContent(`Export (${expectedAmount})`);
2022-03-17 22:28:47 +03:00
});
it.each([
2022-03-26 14:17:42 +03:00
[null],
[Mock.of<NotFoundServer>()],
])('does nothing on click if selected server is not reachable', async (selectedServer) => {
const { user } = setUp(0, selectedServer);
2022-03-17 22:28:47 +03:00
await user.click(screen.getByRole('button'));
2022-03-17 22:28:47 +03:00
expect(listShortUrls).not.toHaveBeenCalled();
expect(exportShortUrls).not.toHaveBeenCalled();
});
it.each([
2022-03-26 14:17:42 +03:00
[10, 1],
[30, 2],
[39, 2],
[40, 2],
[41, 3],
[385, 20],
2022-03-17 22:28:47 +03:00
])('loads proper amount of pages based on the amount of results', async (amount, expectedPageLoads) => {
listShortUrls.mockResolvedValue({ data: [] });
const { user } = setUp(amount, Mock.of<ReachableServer>({ id: '123' }));
2022-03-17 22:28:47 +03:00
await user.click(screen.getByRole('button'));
2022-03-17 22:28:47 +03:00
expect(listShortUrls).toHaveBeenCalledTimes(expectedPageLoads);
expect(exportShortUrls).toHaveBeenCalled();
2022-03-17 22:28:47 +03:00
});
});