2020-12-07 13:17:19 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { ShortUrlsTable as shortUrlsTableCreator } from '../../src/short-urls/ShortUrlsTable';
|
|
|
|
import { ShortUrlsList } from '../../src/short-urls/reducers/shortUrlsList';
|
2021-03-05 16:20:49 +03:00
|
|
|
import { ReachableServer, SelectedServer } from '../../src/servers/data';
|
2021-03-06 11:38:48 +03:00
|
|
|
import { SemVer } from '../../src/utils/helpers/version';
|
2021-12-25 12:24:37 +03:00
|
|
|
import { ShortUrlsOrderableFields, SHORT_URLS_ORDERABLE_FIELDS } from '../../src/short-urls/data';
|
2020-12-07 13:17:19 +03:00
|
|
|
|
|
|
|
describe('<ShortUrlsTable />', () => {
|
|
|
|
let wrapper: ShallowWrapper;
|
|
|
|
const shortUrlsList = Mock.all<ShortUrlsList>();
|
|
|
|
const orderByColumn = jest.fn();
|
|
|
|
const ShortUrlsRow = () => null;
|
|
|
|
const ShortUrlsTable = shortUrlsTableCreator(ShortUrlsRow);
|
|
|
|
|
2021-03-05 16:20:49 +03:00
|
|
|
const createWrapper = (server: SelectedServer = null) => {
|
2020-12-07 13:17:19 +03:00
|
|
|
wrapper = shallow(
|
2021-03-05 16:20:49 +03:00
|
|
|
<ShortUrlsTable shortUrlsList={shortUrlsList} selectedServer={server} orderByColumn={() => orderByColumn} />,
|
2020-12-07 13:17:19 +03:00
|
|
|
);
|
|
|
|
|
2021-03-05 16:20:49 +03:00
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => createWrapper());
|
2020-12-07 13:17:19 +03:00
|
|
|
afterEach(jest.resetAllMocks);
|
|
|
|
afterEach(() => wrapper?.unmount());
|
|
|
|
|
|
|
|
it('should render inner table by default', () => {
|
|
|
|
expect(wrapper.find('table')).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render table header by default', () => {
|
|
|
|
expect(wrapper.find('table').find('thead')).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render 6 table header cells by default', () => {
|
|
|
|
expect(wrapper.find('table').find('thead').find('tr').find('th')).toHaveLength(6);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render 6 table header cells without order by icon by default', () => {
|
|
|
|
const thElements = wrapper.find('table').find('thead').find('tr').find('th');
|
|
|
|
|
|
|
|
thElements.forEach((thElement) => {
|
|
|
|
expect(thElement.find(FontAwesomeIcon)).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-05 16:20:49 +03:00
|
|
|
it('should render table header cells with conditional order by icon', () => {
|
|
|
|
const getThElementForSortableField = (orderableField: string) => wrapper.find('table')
|
2020-12-07 13:17:19 +03:00
|
|
|
.find('thead')
|
|
|
|
.find('tr')
|
|
|
|
.find('th')
|
2021-12-25 12:24:37 +03:00
|
|
|
.filterWhere((e) => e.text().includes(SHORT_URLS_ORDERABLE_FIELDS[orderableField as ShortUrlsOrderableFields]));
|
|
|
|
const sortableFields = Object.keys(SHORT_URLS_ORDERABLE_FIELDS).filter((sortableField) => sortableField !== 'title');
|
2020-12-07 13:17:19 +03:00
|
|
|
|
|
|
|
expect.assertions(sortableFields.length);
|
|
|
|
sortableFields.forEach((sortableField) => {
|
|
|
|
getThElementForSortableField(sortableField).simulate('click');
|
|
|
|
expect(orderByColumn).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2021-03-05 16:20:49 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
['2.6.0' as SemVer],
|
|
|
|
['2.6.1' as SemVer],
|
|
|
|
['2.7.0' as SemVer],
|
|
|
|
['3.0.0' as SemVer],
|
2021-03-05 16:20:49 +03:00
|
|
|
])('should render composed column when server supports title', (version) => {
|
|
|
|
const wrapper = createWrapper(Mock.of<ReachableServer>({ version }));
|
|
|
|
const composedColumn = wrapper.find('table').find('th').at(2);
|
|
|
|
const text = composedColumn.text();
|
|
|
|
|
|
|
|
expect(text).toContain('Title');
|
|
|
|
expect(text).toContain('Long URL');
|
|
|
|
});
|
2020-12-07 13:17:19 +03:00
|
|
|
});
|