2020-08-31 19:38:27 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
2020-12-25 13:05:50 +03:00
|
|
|
import { DropdownItem, DropdownToggle } 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';
|
2021-12-25 12:31:48 +03:00
|
|
|
import { OrderingDropdown, OrderingDropdownProps } from '../../src/utils/OrderingDropdown';
|
2021-11-01 14:48:11 +03:00
|
|
|
import { OrderDir } from '../../src/utils/helpers/ordering';
|
2018-10-28 23:26:47 +03:00
|
|
|
|
|
|
|
describe('<SortingDropdown />', () => {
|
2020-08-31 19:38:27 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2018-10-28 23:26:47 +03:00
|
|
|
const items = {
|
|
|
|
foo: 'Foo',
|
|
|
|
bar: 'Bar',
|
|
|
|
baz: 'Hello World',
|
|
|
|
};
|
2021-12-25 12:31:48 +03:00
|
|
|
const createWrapper = (props: Partial<OrderingDropdownProps> = {}) => {
|
|
|
|
wrapper = shallow(<OrderingDropdown items={items} order={{}} onChange={identity} {...props} />);
|
2018-10-28 23:26:47 +03:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2020-08-31 19:38:27 +03:00
|
|
|
afterEach(() => wrapper?.unmount());
|
2018-10-28 23:26:47 +03:00
|
|
|
|
|
|
|
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-28 23:26:47 +03:00
|
|
|
|
2018-10-29 00:54:08 +03:00
|
|
|
expect(dropdownItems).toHaveLength(values(items).length + clearItemsCount);
|
2018-10-28 23:26:47 +03:00
|
|
|
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', () => {
|
2021-11-06 14:26:20 +03:00
|
|
|
const wrapper = createWrapper({ order: { field: 'bar', dir: 'DESC' } });
|
2018-10-28 23:26:47 +03:00
|
|
|
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();
|
2018-10-28 23:26:47 +03:00
|
|
|
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');
|
2018-10-28 23:26:47 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
2021-11-06 14:26:20 +03:00
|
|
|
const wrapper = createWrapper({ onChange, order: { field: 'baz', dir: 'ASC' } });
|
2018-10-28 23:26:47 +03:00
|
|
|
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');
|
2018-10-28 23:26:47 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('updates order dir when already selected item is clicked', () => {
|
2019-04-19 13:41:59 +03:00
|
|
|
const onChange = jest.fn();
|
2021-11-06 14:26:20 +03:00
|
|
|
const wrapper = createWrapper({ onChange, order: { field: 'foo', dir: 'ASC' } });
|
2018-10-28 23:26:47 +03:00
|
|
|
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');
|
2018-10-28 23:26:47 +03:00
|
|
|
});
|
2020-12-25 13:05:50 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[{ isButton: false }, <>Order by</>],
|
|
|
|
[{ isButton: true }, <>Order by...</>],
|
2020-12-25 13:05:50 +03:00
|
|
|
[
|
2021-11-06 14:26:20 +03:00
|
|
|
{ isButton: true, order: { field: 'foo', dir: 'ASC' as OrderDir } },
|
2021-01-24 20:06:37 +03:00
|
|
|
'Order by: "Foo" - "ASC"',
|
2020-12-25 13:05:50 +03:00
|
|
|
],
|
|
|
|
[
|
2021-11-06 14:26:20 +03:00
|
|
|
{ isButton: true, order: { field: 'baz', dir: 'DESC' as OrderDir } },
|
2021-01-24 20:06:37 +03:00
|
|
|
'Order by: "Hello World" - "DESC"',
|
2020-12-25 13:05:50 +03:00
|
|
|
],
|
2022-03-26 14:17:42 +03:00
|
|
|
[{ isButton: true, order: { field: 'baz' } }, 'Order by: "Hello World" - "DESC"'],
|
2020-12-25 13:05:50 +03:00
|
|
|
])('displays expected text in toggle', (props, expectedText) => {
|
|
|
|
const wrapper = createWrapper(props);
|
|
|
|
const toggle = wrapper.find(DropdownToggle);
|
2022-03-26 14:17:42 +03:00
|
|
|
const [children] = (toggle.prop('children') as any[]).filter(Boolean);
|
2020-12-25 13:05:50 +03:00
|
|
|
|
2021-01-24 20:06:37 +03:00
|
|
|
expect(children).toEqual(expectedText);
|
2020-12-25 13:05:50 +03:00
|
|
|
});
|
2018-10-28 23:26:47 +03:00
|
|
|
});
|