mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-09 09:47:28 +03:00
Created ExportShortUrlsBtn test
This commit is contained in:
parent
ea7345b872
commit
0a57390c46
2 changed files with 83 additions and 18 deletions
|
@ -15,7 +15,7 @@ interface ExportShortUrlsBtnConnectProps extends ExportShortUrlsBtnProps {
|
||||||
selectedServer: SelectedServer;
|
selectedServer: SelectedServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
const itemsPerPage = 10;
|
const itemsPerPage = 20;
|
||||||
|
|
||||||
export const ExportShortUrlsBtn = (
|
export const ExportShortUrlsBtn = (
|
||||||
buildShlinkApiClient: ShlinkApiClientBuilder,
|
buildShlinkApiClient: ShlinkApiClientBuilder,
|
||||||
|
@ -23,7 +23,7 @@ export const ExportShortUrlsBtn = (
|
||||||
): FC<ExportShortUrlsBtnConnectProps> => ({ amount = 0, selectedServer }) => {
|
): FC<ExportShortUrlsBtnConnectProps> => ({ amount = 0, selectedServer }) => {
|
||||||
const [{ tags, search, startDate, endDate, orderBy, tagsMode }] = useShortUrlsQuery();
|
const [{ tags, search, startDate, endDate, orderBy, tagsMode }] = useShortUrlsQuery();
|
||||||
const [ loading,, startLoading, stopLoading ] = useToggle();
|
const [ loading,, startLoading, stopLoading ] = useToggle();
|
||||||
const exportAllUrls = () => {
|
const exportAllUrls = async () => {
|
||||||
if (!isServerWithId(selectedServer)) {
|
if (!isServerWithId(selectedServer)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -44,22 +44,17 @@ export const ExportShortUrlsBtn = (
|
||||||
};
|
};
|
||||||
|
|
||||||
startLoading();
|
startLoading();
|
||||||
loadAllUrls()
|
const shortUrls = await loadAllUrls();
|
||||||
.then((shortUrls) => {
|
|
||||||
exportShortUrls(shortUrls.map((shortUrl) => ({
|
exportShortUrls(shortUrls.map((shortUrl) => ({
|
||||||
createdAt: shortUrl.dateCreated,
|
createdAt: shortUrl.dateCreated,
|
||||||
shortUrl: shortUrl.shortUrl,
|
shortUrl: shortUrl.shortUrl,
|
||||||
longUrl: shortUrl.longUrl,
|
longUrl: shortUrl.longUrl,
|
||||||
title: shortUrl.title ?? '',
|
title: shortUrl.title ?? '',
|
||||||
tags: shortUrl.tags.join(','),
|
tags: shortUrl.tags.join(','),
|
||||||
visits: shortUrl.visitsCount,
|
visits: shortUrl.visitsCount,
|
||||||
})));
|
})));
|
||||||
stopLoading();
|
stopLoading();
|
||||||
})
|
|
||||||
.catch((e) => {
|
|
||||||
// TODO Handle error properly
|
|
||||||
console.error('An error occurred while exporting short URLs', e); // eslint-disable-line no-console
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return <ExportBtn loading={loading} className="btn-md-block" amount={amount} onClick={exportAllUrls} />;
|
return <ExportBtn loading={loading} className="btn-md-block" amount={amount} onClick={exportAllUrls} />;
|
||||||
|
|
70
test/short-urls/helpers/ExportShortUrlsBtn.test.tsx
Normal file
70
test/short-urls/helpers/ExportShortUrlsBtn.test.tsx
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
import { Mock } from 'ts-mockery';
|
||||||
|
import { shallow, ShallowWrapper } from 'enzyme';
|
||||||
|
import { ReportExporter } from '../../../src/common/services/ReportExporter';
|
||||||
|
import { ExportShortUrlsBtn as createExportShortUrlsBtn } from '../../../src/short-urls/helpers/ExportShortUrlsBtn';
|
||||||
|
import { NotFoundServer, ReachableServer, SelectedServer } from '../../../src/servers/data';
|
||||||
|
|
||||||
|
jest.mock('react-router-dom', () => ({
|
||||||
|
...jest.requireActual('react-router-dom'),
|
||||||
|
useNavigate: jest.fn().mockReturnValue(jest.fn()),
|
||||||
|
useParams: jest.fn().mockReturnValue({}),
|
||||||
|
useLocation: jest.fn().mockReturnValue({}),
|
||||||
|
}));
|
||||||
|
|
||||||
|
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);
|
||||||
|
let wrapper: ShallowWrapper;
|
||||||
|
const createWrapper = (amount?: number, selectedServer?: SelectedServer) => {
|
||||||
|
wrapper = shallow(
|
||||||
|
<ExportShortUrlsBtn selectedServer={selectedServer ?? Mock.all<SelectedServer>()} amount={amount} />,
|
||||||
|
);
|
||||||
|
|
||||||
|
return wrapper;
|
||||||
|
};
|
||||||
|
|
||||||
|
afterEach(jest.clearAllMocks);
|
||||||
|
afterEach(() => wrapper?.unmount());
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
[ undefined, 0 ],
|
||||||
|
[ 1, 1 ],
|
||||||
|
[ 4578, 4578 ],
|
||||||
|
])('renders expected amount', (amount, expectedAmount) => {
|
||||||
|
const wrapper = createWrapper(amount);
|
||||||
|
|
||||||
|
expect(wrapper.prop('amount')).toEqual(expectedAmount);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
[ null ],
|
||||||
|
[ Mock.of<NotFoundServer>() ],
|
||||||
|
])('does nothing on click if selected server is not reachable', (selectedServer) => {
|
||||||
|
const wrapper = createWrapper(0, selectedServer);
|
||||||
|
|
||||||
|
wrapper.simulate('click');
|
||||||
|
expect(listShortUrls).not.toHaveBeenCalled();
|
||||||
|
expect(exportShortUrls).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
[ 10, 1 ],
|
||||||
|
[ 30, 2 ],
|
||||||
|
[ 39, 2 ],
|
||||||
|
[ 40, 2 ],
|
||||||
|
[ 41, 3 ],
|
||||||
|
[ 385, 20 ],
|
||||||
|
])('loads proper amount of pages based on the amount of results', async (amount, expectedPageLoads) => {
|
||||||
|
const wrapper = createWrapper(amount, Mock.of<ReachableServer>({ id: '123' }));
|
||||||
|
|
||||||
|
listShortUrls.mockResolvedValue({ data: [] });
|
||||||
|
|
||||||
|
await (wrapper.prop('onClick') as Function)();
|
||||||
|
|
||||||
|
expect(listShortUrls).toHaveBeenCalledTimes(expectedPageLoads);
|
||||||
|
expect(exportShortUrls).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue