shlink-web-client/test/servers/Overview.test.tsx

88 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-04-24 12:28:32 +03:00
import { FC, PropsWithChildren } from 'react';
import { mount, ReactWrapper } from 'enzyme';
2020-12-08 19:51:49 +03:00
import { Mock } from 'ts-mockery';
import { Link, MemoryRouter } from 'react-router-dom';
2020-12-08 19:51:49 +03:00
import { ShortUrlsList as ShortUrlsListState } from '../../src/short-urls/reducers/shortUrlsList';
import { Overview as overviewCreator } from '../../src/servers/Overview';
import { TagsList } from '../../src/tags/reducers/tagsList';
import { VisitsOverview } from '../../src/visits/reducers/visitsOverview';
import { MercureInfo } from '../../src/mercure/reducers/mercureInfo';
import { ReachableServer } from '../../src/servers/data';
import { prettify } from '../../src/utils/helpers/numbers';
import { HighlightCard } from '../../src/servers/helpers/HighlightCard';
2020-12-08 19:51:49 +03:00
describe('<Overview />', () => {
let wrapper: ReactWrapper;
2020-12-08 19:51:49 +03:00
const ShortUrlsTable = () => null;
const CreateShortUrl = () => null;
2022-04-24 12:28:32 +03:00
const ForServerVersion: FC<PropsWithChildren<unknown>> = ({ children }) => <>{children}</>;
2020-12-08 19:51:49 +03:00
const listShortUrls = jest.fn();
const listTags = jest.fn();
const loadVisitsOverview = jest.fn();
const Overview = overviewCreator(ShortUrlsTable, CreateShortUrl, ForServerVersion);
2020-12-08 19:51:49 +03:00
const shortUrls = {
pagination: { totalItems: 83710 },
};
const serverId = '123';
const createWrapper = (loading = false) => {
wrapper = mount(
<MemoryRouter>
<Overview
listShortUrls={listShortUrls}
listTags={listTags}
loadVisitsOverview={loadVisitsOverview}
shortUrlsList={Mock.of<ShortUrlsListState>({ loading, shortUrls })}
2022-03-26 14:17:42 +03:00
tagsList={Mock.of<TagsList>({ loading, tags: ['foo', 'bar', 'baz'] })}
visitsOverview={Mock.of<VisitsOverview>({ loading, visitsCount: 3456, orphanVisitsCount: 28 })}
selectedServer={Mock.of<ReachableServer>({ id: serverId })}
createNewVisits={jest.fn()}
loadMercureInfo={jest.fn()}
mercureInfo={Mock.all<MercureInfo>()}
/>
</MemoryRouter>,
);
2020-12-08 19:51:49 +03:00
return wrapper;
};
afterEach(() => wrapper?.unmount());
2021-03-06 19:25:09 +03:00
it('displays loading messages when still loading', () => {
2020-12-08 19:51:49 +03:00
const wrapper = createWrapper(true);
const cards = wrapper.find(HighlightCard);
2020-12-08 19:51:49 +03:00
expect(cards).toHaveLength(4);
2020-12-08 19:51:49 +03:00
cards.forEach((card) => expect(card.html()).toContain('Loading...'));
});
2021-03-06 19:25:09 +03:00
it('displays amounts in cards after finishing loading', () => {
2020-12-08 19:51:49 +03:00
const wrapper = createWrapper();
const cards = wrapper.find(HighlightCard);
2020-12-08 19:51:49 +03:00
expect(cards).toHaveLength(4);
2020-12-08 19:51:49 +03:00
expect(cards.at(0).html()).toContain(prettify(3456));
expect(cards.at(1).html()).toContain(prettify(28));
expect(cards.at(2).html()).toContain(prettify(83710));
expect(cards.at(3).html()).toContain(prettify(3));
2020-12-08 19:51:49 +03:00
});
2021-03-06 19:25:09 +03:00
it('nests complex components', () => {
2020-12-08 19:51:49 +03:00
const wrapper = createWrapper();
expect(wrapper.find(CreateShortUrl)).toHaveLength(1);
expect(wrapper.find(ShortUrlsTable)).toHaveLength(1);
});
2021-03-06 19:25:09 +03:00
it('displays links to other sections', () => {
2020-12-08 19:51:49 +03:00
const wrapper = createWrapper();
const links = wrapper.find(Link);
expect(links).toHaveLength(5);
expect(links.at(0).prop('to')).toEqual(`/server/${serverId}/orphan-visits`);
expect(links.at(1).prop('to')).toEqual(`/server/${serverId}/list-short-urls/1`);
expect(links.at(2).prop('to')).toEqual(`/server/${serverId}/manage-tags`);
expect(links.at(3).prop('to')).toEqual(`/server/${serverId}/create-short-url`);
expect(links.at(4).prop('to')).toEqual(`/server/${serverId}/list-short-urls/1`);
2020-12-08 19:51:49 +03:00
});
});