2022-10-05 00:17:12 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2022-07-06 19:59:30 +03:00
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ReportExporter } from '../../../src/common/services/ReportExporter';
|
2023-04-13 22:48:29 +03:00
|
|
|
import type { NotFoundServer, SelectedServer } from '../../../src/servers/data';
|
2023-04-21 10:36:51 +03:00
|
|
|
import type { ShortUrl } from '../../../src/short-urls/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();
|
2023-04-13 22:48:29 +03:00
|
|
|
const reportExporter = fromPartial<ReportExporter>({ exportShortUrls });
|
2022-03-17 22:28:47 +03:00
|
|
|
const ExportShortUrlsBtn = createExportShortUrlsBtn(buildShlinkApiClient, reportExporter);
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (amount?: number, selectedServer?: SelectedServer) => renderWithEvents(
|
|
|
|
<MemoryRouter>
|
2023-04-13 22:48:29 +03:00
|
|
|
<ExportShortUrlsBtn selectedServer={selectedServer ?? fromPartial({})} amount={amount} />
|
2022-07-10 00:03:21 +03:00
|
|
|
</MemoryRouter>,
|
|
|
|
);
|
2022-03-17 22:28:47 +03:00
|
|
|
|
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
|
|
|
|
it.each([
|
2022-07-06 19:59:30 +03:00
|
|
|
[undefined, '0'],
|
|
|
|
[1, '1'],
|
|
|
|
[4578, '4,578'],
|
2022-03-17 22:28:47 +03:00
|
|
|
])('renders expected amount', (amount, expectedAmount) => {
|
2022-07-06 19:59:30 +03:00
|
|
|
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],
|
2023-04-13 22:48:29 +03:00
|
|
|
[fromPartial<NotFoundServer>({})],
|
2022-07-06 19:59:30 +03:00
|
|
|
])('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
|
|
|
|
2022-07-06 19:59:30 +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: [] });
|
2023-04-13 22:48:29 +03:00
|
|
|
const { user } = setUp(amount, fromPartial({ id: '123' }));
|
2022-03-17 22:28:47 +03:00
|
|
|
|
2022-07-06 19:59:30 +03:00
|
|
|
await user.click(screen.getByRole('button'));
|
2022-03-17 22:28:47 +03:00
|
|
|
|
|
|
|
expect(listShortUrls).toHaveBeenCalledTimes(expectedPageLoads);
|
2022-07-06 19:59:30 +03:00
|
|
|
expect(exportShortUrls).toHaveBeenCalled();
|
2022-03-17 22:28:47 +03:00
|
|
|
});
|
2023-04-21 10:36:51 +03:00
|
|
|
|
|
|
|
it('maps short URLs for exporting', async () => {
|
|
|
|
listShortUrls.mockResolvedValue({
|
|
|
|
data: [fromPartial<ShortUrl>({
|
|
|
|
shortUrl: 'https://s.test/short-code',
|
|
|
|
tags: [],
|
|
|
|
})],
|
|
|
|
});
|
|
|
|
const { user } = setUp(undefined, fromPartial({ id: '123' }));
|
|
|
|
|
|
|
|
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
|
|
|
});
|