2019-09-14 19:23:26 +03:00
|
|
|
import React from 'react';
|
2020-08-30 20:45:17 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2019-09-14 19:23:26 +03:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faCaretDown as caretDownIcon, faCaretUp as caretUpIcon } from '@fortawesome/free-solid-svg-icons';
|
2020-08-30 20:45:17 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2020-09-13 10:47:29 +03:00
|
|
|
import shortUrlsListCreator, { ShortUrlsListProps } from '../../src/short-urls/ShortUrlsList';
|
2020-08-30 20:45:17 +03:00
|
|
|
import { ShortUrl } from '../../src/short-urls/data';
|
2020-09-06 20:41:15 +03:00
|
|
|
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
|
2020-09-12 18:59:58 +03:00
|
|
|
import { SORTABLE_FIELDS } from '../../src/short-urls/reducers/shortUrlsListParams';
|
2019-09-14 19:23:26 +03:00
|
|
|
|
|
|
|
describe('<ShortUrlsList />', () => {
|
2020-08-30 20:45:17 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
|
|
|
const ShortUrlsRow = () => null;
|
2019-09-14 19:23:26 +03:00
|
|
|
const listShortUrlsMock = jest.fn();
|
|
|
|
const resetShortUrlParamsMock = jest.fn();
|
|
|
|
|
|
|
|
const ShortUrlsList = shortUrlsListCreator(ShortUrlsRow);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = shallow(
|
|
|
|
<ShortUrlsList
|
2020-08-30 20:45:17 +03:00
|
|
|
{...Mock.all<ShortUrlsListProps>()}
|
2020-09-06 20:41:15 +03:00
|
|
|
{...Mock.of<MercureBoundProps>({ mercureInfo: { loading: true } })}
|
2019-09-14 19:23:26 +03:00
|
|
|
listShortUrls={listShortUrlsMock}
|
|
|
|
resetShortUrlParams={resetShortUrlParamsMock}
|
|
|
|
shortUrlsListParams={{
|
|
|
|
page: '1',
|
|
|
|
tags: [ 'test tag' ],
|
|
|
|
searchTerm: 'example.com',
|
|
|
|
}}
|
2020-08-30 20:45:17 +03:00
|
|
|
match={{ params: {} } as any}
|
|
|
|
location={{} as any}
|
2019-09-14 19:23:26 +03:00
|
|
|
loading={false}
|
|
|
|
error={false}
|
|
|
|
shortUrlsList={
|
|
|
|
[
|
2020-08-30 20:45:17 +03:00
|
|
|
Mock.of<ShortUrl>({
|
2019-09-14 19:23:26 +03:00
|
|
|
shortCode: 'testShortCode',
|
|
|
|
shortUrl: 'https://www.example.com/testShortUrl',
|
|
|
|
longUrl: 'https://www.example.com/testLongUrl',
|
|
|
|
tags: [ 'test tag' ],
|
2020-08-30 20:45:17 +03:00
|
|
|
}),
|
2019-09-14 19:23:26 +03:00
|
|
|
]
|
|
|
|
}
|
2020-08-22 09:10:31 +03:00
|
|
|
/>,
|
2020-09-06 20:41:15 +03:00
|
|
|
).dive(); // Dive is needed as this component is wrapped in a HOC
|
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
|
|
|
|
|
|
|
it('wraps a ShortUrlsList with 1 ShortUrlsRow', () => {
|
|
|
|
expect(wrapper.find(ShortUrlsRow)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render inner table by default', () => {
|
|
|
|
expect(wrapper.find('table')).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render table header by default', () => {
|
2020-04-17 18:11:52 +03:00
|
|
|
expect(wrapper.find('table').find('thead')).toHaveLength(1);
|
2019-09-14 19:23:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render 6 table header cells by default', () => {
|
2020-04-17 18:11:52 +03:00
|
|
|
expect(wrapper.find('table').find('thead').find('tr').find('th')).toHaveLength(6);
|
2019-09-14 19:23:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render 6 table header cells without order by icon by default', () => {
|
2020-04-17 18:11:52 +03:00
|
|
|
const thElements = wrapper.find('table').find('thead').find('tr').find('th');
|
2019-09-14 19:23:26 +03:00
|
|
|
|
2020-04-17 18:11:52 +03:00
|
|
|
thElements.forEach((thElement) => {
|
2019-09-14 19:23:26 +03:00
|
|
|
expect(thElement.find(FontAwesomeIcon)).toHaveLength(0);
|
2020-04-17 18:11:52 +03:00
|
|
|
});
|
2019-09-14 19:23:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render 6 table header cells with conditional order by icon', () => {
|
2020-08-30 20:45:17 +03:00
|
|
|
const getThElementForSortableField = (sortableField: string) => wrapper.find('table')
|
2020-04-17 18:11:52 +03:00
|
|
|
.find('thead')
|
|
|
|
.find('tr')
|
|
|
|
.find('th')
|
2020-08-30 20:45:17 +03:00
|
|
|
.filterWhere((e) => e.text().includes(SORTABLE_FIELDS[sortableField as keyof typeof SORTABLE_FIELDS]));
|
2019-09-14 19:23:26 +03:00
|
|
|
|
2020-04-17 18:11:52 +03:00
|
|
|
Object.keys(SORTABLE_FIELDS).forEach((sortableField) => {
|
2020-04-17 18:39:30 +03:00
|
|
|
expect(getThElementForSortableField(sortableField).find(FontAwesomeIcon)).toHaveLength(0);
|
2019-09-14 19:23:26 +03:00
|
|
|
|
2020-04-17 18:39:30 +03:00
|
|
|
getThElementForSortableField(sortableField).simulate('click');
|
2020-04-17 18:11:52 +03:00
|
|
|
expect(getThElementForSortableField(sortableField).find(FontAwesomeIcon)).toHaveLength(1);
|
2020-04-17 18:39:30 +03:00
|
|
|
expect(getThElementForSortableField(sortableField).find(FontAwesomeIcon).prop('icon')).toEqual(caretUpIcon);
|
2020-04-17 18:11:52 +03:00
|
|
|
|
2020-04-17 18:39:30 +03:00
|
|
|
getThElementForSortableField(sortableField).simulate('click');
|
2020-04-17 18:11:52 +03:00
|
|
|
expect(getThElementForSortableField(sortableField).find(FontAwesomeIcon)).toHaveLength(1);
|
2020-04-17 18:39:30 +03:00
|
|
|
expect(getThElementForSortableField(sortableField).find(FontAwesomeIcon).prop('icon')).toEqual(caretDownIcon);
|
2020-04-17 18:11:52 +03:00
|
|
|
|
2020-04-17 18:39:30 +03:00
|
|
|
getThElementForSortableField(sortableField).simulate('click');
|
|
|
|
expect(getThElementForSortableField(sortableField).find(FontAwesomeIcon)).toHaveLength(0);
|
2020-04-17 18:11:52 +03:00
|
|
|
});
|
2019-09-14 19:23:26 +03:00
|
|
|
});
|
|
|
|
});
|