2020-08-30 20:45:17 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2021-10-17 14:43:37 +03:00
|
|
|
import { ReactElement } from 'react';
|
2020-08-30 20:45:17 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2021-11-09 01:41:17 +03:00
|
|
|
import { History, Location } from 'history';
|
|
|
|
import { match } from 'react-router';
|
|
|
|
import shortUrlsListCreator from '../../src/short-urls/ShortUrlsList';
|
2021-12-25 12:24:37 +03:00
|
|
|
import { ShortUrlsOrderableFields, ShortUrl, ShortUrlsOrder } from '../../src/short-urls/data';
|
2020-09-06 20:41:15 +03:00
|
|
|
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
|
2020-12-07 13:17:19 +03:00
|
|
|
import { ShortUrlsList as ShortUrlsListModel } from '../../src/short-urls/reducers/shortUrlsList';
|
2021-12-25 12:31:48 +03:00
|
|
|
import { OrderingDropdown } from '../../src/utils/OrderingDropdown';
|
2021-10-17 14:43:37 +03:00
|
|
|
import Paginator from '../../src/short-urls/Paginator';
|
2021-11-09 01:41:17 +03:00
|
|
|
import { ReachableServer } from '../../src/servers/data';
|
|
|
|
import { ShortUrlListRouteParams } from '../../src/short-urls/helpers/hooks';
|
2021-12-24 15:14:13 +03:00
|
|
|
import { Settings } from '../../src/settings/reducers/settings';
|
2019-09-14 19:23:26 +03:00
|
|
|
|
|
|
|
describe('<ShortUrlsList />', () => {
|
2020-08-30 20:45:17 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2020-12-07 13:17:19 +03:00
|
|
|
const ShortUrlsTable = () => null;
|
2021-11-09 01:41:17 +03:00
|
|
|
const SearchBar = () => null;
|
2019-09-14 19:23:26 +03:00
|
|
|
const listShortUrlsMock = jest.fn();
|
2021-11-09 01:41:17 +03:00
|
|
|
const push = jest.fn();
|
2020-12-07 13:17:19 +03:00
|
|
|
const shortUrlsList = Mock.of<ShortUrlsListModel>({
|
|
|
|
shortUrls: {
|
|
|
|
data: [
|
|
|
|
Mock.of<ShortUrl>({
|
|
|
|
shortCode: 'testShortCode',
|
|
|
|
shortUrl: 'https://www.example.com/testShortUrl',
|
|
|
|
longUrl: 'https://www.example.com/testLongUrl',
|
|
|
|
tags: [ 'test tag' ],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
2021-11-09 01:41:17 +03:00
|
|
|
const ShortUrlsList = shortUrlsListCreator(ShortUrlsTable, SearchBar);
|
2021-12-24 15:39:51 +03:00
|
|
|
const createWrapper = (defaultOrdering: ShortUrlsOrder = {}) => shallow(
|
2021-10-17 14:43:37 +03:00
|
|
|
<ShortUrlsList
|
|
|
|
{...Mock.of<MercureBoundProps>({ mercureInfo: { loading: true } })}
|
|
|
|
listShortUrls={listShortUrlsMock}
|
2021-11-09 01:41:17 +03:00
|
|
|
match={Mock.of<match<ShortUrlListRouteParams>>({ params: {} })}
|
|
|
|
location={Mock.of<Location>({ search: '?tags=test%20tag&search=example.com' })}
|
2021-10-17 14:43:37 +03:00
|
|
|
shortUrlsList={shortUrlsList}
|
2021-11-09 01:41:17 +03:00
|
|
|
history={Mock.of<History>({ push })}
|
|
|
|
selectedServer={Mock.of<ReachableServer>({ id: '1' })}
|
2021-12-24 16:15:28 +03:00
|
|
|
settings={Mock.of<Settings>({ shortUrlsList: { defaultOrdering } })}
|
2021-10-17 14:43:37 +03:00
|
|
|
/>,
|
|
|
|
).dive(); // Dive is needed as this component is wrapped in a HOC
|
2019-09-14 19:23:26 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-10-17 14:43:37 +03:00
|
|
|
wrapper = createWrapper();
|
2019-09-14 19:23:26 +03:00
|
|
|
});
|
|
|
|
|
2020-08-30 20:45:17 +03:00
|
|
|
afterEach(jest.resetAllMocks);
|
|
|
|
afterEach(() => wrapper?.unmount());
|
2019-09-14 19:23:26 +03:00
|
|
|
|
2021-10-17 14:43:37 +03:00
|
|
|
it('wraps expected components', () => {
|
2020-12-07 13:17:19 +03:00
|
|
|
expect(wrapper.find(ShortUrlsTable)).toHaveLength(1);
|
2021-12-25 12:31:48 +03:00
|
|
|
expect(wrapper.find(OrderingDropdown)).toHaveLength(1);
|
2021-10-17 14:43:37 +03:00
|
|
|
expect(wrapper.find(Paginator)).toHaveLength(1);
|
2021-11-09 01:41:17 +03:00
|
|
|
expect(wrapper.find(SearchBar)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes current query to paginator', () => {
|
|
|
|
expect(wrapper.find(Paginator).prop('currentQueryString')).toEqual('?tags=test%20tag&search=example.com');
|
2019-09-14 19:23:26 +03:00
|
|
|
});
|
|
|
|
|
2021-10-17 14:43:37 +03:00
|
|
|
it('gets list refreshed every time a tag is clicked', () => {
|
|
|
|
wrapper.find(ShortUrlsTable).simulate('tagClick', 'foo');
|
|
|
|
wrapper.find(ShortUrlsTable).simulate('tagClick', 'bar');
|
|
|
|
wrapper.find(ShortUrlsTable).simulate('tagClick', 'baz');
|
|
|
|
|
2021-11-09 01:41:17 +03:00
|
|
|
expect(push).toHaveBeenCalledTimes(3);
|
|
|
|
expect(push).toHaveBeenNthCalledWith(1, expect.stringContaining(`tags=${encodeURIComponent('test tag,foo')}`));
|
|
|
|
expect(push).toHaveBeenNthCalledWith(2, expect.stringContaining(`tags=${encodeURIComponent('test tag,bar')}`));
|
|
|
|
expect(push).toHaveBeenNthCalledWith(3, expect.stringContaining(`tags=${encodeURIComponent('test tag,baz')}`));
|
2021-10-17 14:43:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('invokes order icon rendering', () => {
|
2021-12-25 12:24:37 +03:00
|
|
|
const renderIcon = (field: ShortUrlsOrderableFields) =>
|
|
|
|
(wrapper.find(ShortUrlsTable).prop('renderOrderIcon') as (field: ShortUrlsOrderableFields) => ReactElement)(field);
|
2021-10-17 14:43:37 +03:00
|
|
|
|
2021-11-07 00:34:29 +03:00
|
|
|
expect(renderIcon('visits').props.currentOrder).toEqual({});
|
2021-10-17 14:43:37 +03:00
|
|
|
|
2021-12-25 12:31:48 +03:00
|
|
|
wrapper.find(OrderingDropdown).simulate('change', 'visits');
|
2021-11-07 00:34:29 +03:00
|
|
|
expect(renderIcon('visits').props.currentOrder).toEqual({ field: 'visits' });
|
2021-10-17 14:43:37 +03:00
|
|
|
|
2021-12-25 12:31:48 +03:00
|
|
|
wrapper.find(OrderingDropdown).simulate('change', 'visits', 'ASC');
|
2021-11-07 00:34:29 +03:00
|
|
|
expect(renderIcon('visits').props.currentOrder).toEqual({ field: 'visits', dir: 'ASC' });
|
2021-10-17 14:43:37 +03:00
|
|
|
});
|
|
|
|
|
2021-11-09 01:41:17 +03:00
|
|
|
it('handles order through table', () => {
|
2021-12-25 12:24:37 +03:00
|
|
|
const orderByColumn: (field: ShortUrlsOrderableFields) => Function = wrapper.find(ShortUrlsTable).prop('orderByColumn');
|
2021-10-17 14:43:37 +03:00
|
|
|
|
2021-12-25 12:31:48 +03:00
|
|
|
expect(wrapper.find(OrderingDropdown).prop('order')).toEqual({});
|
2021-12-24 15:14:13 +03:00
|
|
|
|
2021-10-17 14:43:37 +03:00
|
|
|
orderByColumn('visits')();
|
2021-12-25 12:31:48 +03:00
|
|
|
expect(wrapper.find(OrderingDropdown).prop('order')).toEqual({ field: 'visits', dir: 'ASC' });
|
2021-12-24 15:14:13 +03:00
|
|
|
|
2021-10-17 14:43:37 +03:00
|
|
|
orderByColumn('title')();
|
2021-12-25 12:31:48 +03:00
|
|
|
expect(wrapper.find(OrderingDropdown).prop('order')).toEqual({ field: 'title', dir: 'ASC' });
|
2021-10-17 14:43:37 +03:00
|
|
|
|
2021-12-24 15:14:13 +03:00
|
|
|
orderByColumn('shortCode')();
|
2021-12-25 12:31:48 +03:00
|
|
|
expect(wrapper.find(OrderingDropdown).prop('order')).toEqual({ field: 'shortCode', dir: 'ASC' });
|
2021-10-17 14:43:37 +03:00
|
|
|
});
|
|
|
|
|
2021-11-09 01:41:17 +03:00
|
|
|
it('handles order through dropdown', () => {
|
2021-12-25 12:31:48 +03:00
|
|
|
expect(wrapper.find(OrderingDropdown).prop('order')).toEqual({});
|
2021-10-17 14:43:37 +03:00
|
|
|
|
2021-12-25 12:31:48 +03:00
|
|
|
wrapper.find(OrderingDropdown).simulate('change', 'visits', 'ASC');
|
|
|
|
expect(wrapper.find(OrderingDropdown).prop('order')).toEqual({ field: 'visits', dir: 'ASC' });
|
2021-10-17 14:43:37 +03:00
|
|
|
|
2021-12-25 12:31:48 +03:00
|
|
|
wrapper.find(OrderingDropdown).simulate('change', 'shortCode', 'DESC');
|
|
|
|
expect(wrapper.find(OrderingDropdown).prop('order')).toEqual({ field: 'shortCode', dir: 'DESC' });
|
2021-10-17 14:43:37 +03:00
|
|
|
|
2021-12-25 12:31:48 +03:00
|
|
|
wrapper.find(OrderingDropdown).simulate('change', undefined, undefined);
|
|
|
|
expect(wrapper.find(OrderingDropdown).prop('order')).toEqual({});
|
2021-10-17 14:43:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2021-12-24 15:14:13 +03:00
|
|
|
[ Mock.of<ShortUrlsOrder>({ field: 'visits', dir: 'ASC' }), 'visits', 'ASC' ],
|
|
|
|
[ Mock.of<ShortUrlsOrder>({ field: 'title', dir: 'DESC' }), 'title', 'DESC' ],
|
|
|
|
[ Mock.of<ShortUrlsOrder>(), undefined, undefined ],
|
2021-11-06 14:26:20 +03:00
|
|
|
])('has expected initial ordering', (initialOrderBy, field, dir) => {
|
2021-10-17 14:43:37 +03:00
|
|
|
const wrapper = createWrapper(initialOrderBy);
|
|
|
|
|
2021-12-25 12:31:48 +03:00
|
|
|
expect(wrapper.find(OrderingDropdown).prop('order')).toEqual({ field, dir });
|
2019-09-14 19:23:26 +03:00
|
|
|
});
|
|
|
|
});
|