shlink-web-client/test/utils/SortingDropdown.test.js

78 lines
2.7 KiB
JavaScript
Raw Normal View History

import React from 'react';
import { shallow } from 'enzyme';
import { DropdownItem } from 'reactstrap';
2018-10-29 00:54:08 +03:00
import { identity, values } from 'ramda';
2019-01-06 01:16:13 +03:00
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
2019-01-07 11:06:29 +03:00
import { faSortAmountDown as caretDownIcon } from '@fortawesome/free-solid-svg-icons';
import SortingDropdown from '../../src/utils/SortingDropdown';
describe('<SortingDropdown />', () => {
let wrapper;
const items = {
foo: 'Foo',
bar: 'Bar',
baz: 'Hello World',
};
const createWrapper = (props) => {
2018-10-29 00:54:08 +03:00
wrapper = shallow(<SortingDropdown items={items} onChange={identity} {...props} />);
return wrapper;
};
afterEach(() => wrapper && wrapper.unmount());
it('properly renders provided list of items', () => {
const wrapper = createWrapper();
const dropdownItems = wrapper.find(DropdownItem);
const secondIndex = 2;
2018-10-29 00:54:08 +03:00
const clearItemsCount = 2;
2018-10-29 00:54:08 +03:00
expect(dropdownItems).toHaveLength(values(items).length + clearItemsCount);
expect(dropdownItems.at(0).html()).toContain('Foo');
expect(dropdownItems.at(1).html()).toContain('Bar');
expect(dropdownItems.at(secondIndex).html()).toContain('Hello World');
});
it('properly marks selected field as active with proper icon', () => {
const wrapper = createWrapper({ orderField: 'bar', orderDir: 'DESC' });
const activeItem = wrapper.find('DropdownItem[active=true]');
const activeItemIcon = activeItem.first().find(FontAwesomeIcon);
expect(activeItem).toHaveLength(1);
expect(activeItemIcon.prop('icon')).toEqual(caretDownIcon);
});
it('triggers change function when item is clicked and no order field was provided', () => {
2019-04-19 13:41:59 +03:00
const onChange = jest.fn();
const wrapper = createWrapper({ onChange });
const firstItem = wrapper.find(DropdownItem).first();
firstItem.simulate('click');
2019-04-19 13:41:59 +03:00
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith('foo', 'ASC');
});
it('triggers change function when item is clicked and an order field was provided', () => {
2019-04-19 13:41:59 +03:00
const onChange = jest.fn();
const wrapper = createWrapper({ onChange, orderField: 'baz', orderDir: 'ASC' });
const firstItem = wrapper.find(DropdownItem).first();
firstItem.simulate('click');
2019-04-19 13:41:59 +03:00
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith('foo', 'ASC');
});
it('updates order dir when already selected item is clicked', () => {
2019-04-19 13:41:59 +03:00
const onChange = jest.fn();
const wrapper = createWrapper({ onChange, orderField: 'foo', orderDir: 'ASC' });
const firstItem = wrapper.find(DropdownItem).first();
firstItem.simulate('click');
2019-04-19 13:41:59 +03:00
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith('foo', 'DESC');
});
});