2020-08-30 20:45:17 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2021-11-09 00:13:37 +03:00
|
|
|
import { PaginationItem, PaginationLink } from 'reactstrap';
|
|
|
|
import { Mock } from 'ts-mockery';
|
2018-09-16 13:18:02 +03:00
|
|
|
import Paginator from '../../src/short-urls/Paginator';
|
2021-11-09 00:13:37 +03:00
|
|
|
import { ShlinkPaginator } from '../../src/api/types';
|
|
|
|
import { ELLIPSIS } from '../../src/utils/helpers/pagination';
|
2018-09-16 13:18:02 +03:00
|
|
|
|
|
|
|
describe('<Paginator />', () => {
|
2020-08-30 20:45:17 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2021-11-09 00:13:37 +03:00
|
|
|
const buildPaginator = (pagesCount?: number) => Mock.of<ShlinkPaginator>({ pagesCount, currentPage: 1 });
|
2018-09-16 13:18:02 +03:00
|
|
|
|
2020-08-30 20:45:17 +03:00
|
|
|
afterEach(() => wrapper?.unmount());
|
2018-09-16 13:18:02 +03:00
|
|
|
|
2021-11-09 00:13:37 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[undefined],
|
|
|
|
[buildPaginator()],
|
|
|
|
[buildPaginator(0)],
|
|
|
|
[buildPaginator(1)],
|
2021-11-09 00:13:37 +03:00
|
|
|
])('renders nothing if the number of pages is below 2', (paginator) => {
|
|
|
|
wrapper = shallow(<Paginator serverId="abc123" paginator={paginator} />);
|
2018-09-16 13:18:02 +03:00
|
|
|
expect(wrapper.text()).toEqual('');
|
|
|
|
});
|
|
|
|
|
2021-11-09 00:13:37 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[buildPaginator(2), 4, 0],
|
|
|
|
[buildPaginator(3), 5, 0],
|
|
|
|
[buildPaginator(4), 6, 0],
|
|
|
|
[buildPaginator(5), 7, 1],
|
|
|
|
[buildPaginator(6), 7, 1],
|
|
|
|
[buildPaginator(23), 7, 1],
|
2021-11-09 00:13:37 +03:00
|
|
|
])('renders previous, next and the list of pages, with ellipses when expected', (
|
|
|
|
paginator,
|
|
|
|
expectedPages,
|
|
|
|
expectedEllipsis,
|
|
|
|
) => {
|
2018-09-16 13:18:02 +03:00
|
|
|
wrapper = shallow(<Paginator serverId="abc123" paginator={paginator} />);
|
2021-11-09 00:13:37 +03:00
|
|
|
const items = wrapper.find(PaginationItem);
|
|
|
|
const ellipsis = items.filterWhere((item) => item.find(PaginationLink).prop('children') === ELLIPSIS);
|
|
|
|
|
|
|
|
expect(items).toHaveLength(expectedPages);
|
|
|
|
expect(ellipsis).toHaveLength(expectedEllipsis);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('appends query string to all pages', () => {
|
|
|
|
const paginator = buildPaginator(3);
|
|
|
|
const currentQueryString = '?foo=bar';
|
|
|
|
|
|
|
|
wrapper = shallow(<Paginator serverId="abc123" paginator={paginator} currentQueryString={currentQueryString} />);
|
|
|
|
const links = wrapper.find(PaginationLink);
|
2018-09-16 13:18:02 +03:00
|
|
|
|
2021-11-09 00:13:37 +03:00
|
|
|
expect(links).toHaveLength(5);
|
|
|
|
links.forEach((link) => expect(link.prop('to')).toContain(currentQueryString));
|
2018-09-16 13:18:02 +03:00
|
|
|
});
|
|
|
|
});
|