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

65 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-10-05 00:17:12 +03:00
import { screen } from '@testing-library/react';
import { fromPartial } from '@total-typescript/shoehorn';
import { MemoryRouter } from 'react-router-dom';
import type { ShortUrl } from '../../../src/short-urls/data';
import { ExportShortUrlsBtn as createExportShortUrlsBtn } from '../../../src/short-urls/helpers/ExportShortUrlsBtn';
import type { ReportExporter } from '../../../src/utils/services/ReportExporter';
2022-07-10 20:44:49 +03:00
import { renderWithEvents } from '../../__helpers__/setUpTest';
2022-03-17 22:28:47 +03:00
describe('<ExportShortUrlsBtn />', () => {
2023-05-27 12:57:26 +03:00
const listShortUrls = vi.fn();
const buildShlinkApiClient = vi.fn().mockReturnValue({ listShortUrls });
const exportShortUrls = vi.fn();
const reportExporter = fromPartial<ReportExporter>({ exportShortUrls });
2022-03-17 22:28:47 +03:00
const ExportShortUrlsBtn = createExportShortUrlsBtn(buildShlinkApiClient, reportExporter);
2023-08-04 12:16:01 +03:00
const setUp = (amount?: number) => renderWithEvents(
<MemoryRouter>
2023-08-04 12:16:01 +03:00
<ExportShortUrlsBtn amount={amount} />
</MemoryRouter>,
);
2022-03-17 22:28:47 +03:00
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
[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: [] });
2023-08-04 12:16:01 +03:00
const { user } = setUp(amount);
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
});
it('maps short URLs for exporting', async () => {
listShortUrls.mockResolvedValue({
data: [fromPartial<ShortUrl>({
shortUrl: 'https://s.test/short-code',
tags: [],
})],
});
2023-08-04 12:16:01 +03:00
const { user } = setUp();
await user.click(screen.getByRole('button'));
expect(exportShortUrls).toHaveBeenCalledWith([expect.objectContaining({
shortUrl: 'https://s.test/short-code',
domain: 's.test',
shortCode: 'short-code',
})]);
});
2022-03-17 22:28:47 +03:00
});