2016-10-20 16:34:44 +01:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import { shallow, mount } from 'enzyme';
|
|
|
|
import sinon from 'sinon';
|
2017-06-01 16:27:15 +01:00
|
|
|
import React from 'react';
|
2017-05-03 01:04:16 +01:00
|
|
|
import DropdownMenu from '../../../app/javascript/mastodon/components/dropdown_menu';
|
2016-10-20 16:34:44 +01:00
|
|
|
import Dropdown, { DropdownTrigger, DropdownContent } from 'react-simple-dropdown';
|
|
|
|
|
2017-07-27 21:31:59 +01:00
|
|
|
const isTrue = () => true;
|
|
|
|
|
2016-10-20 16:34:44 +01:00
|
|
|
describe('<DropdownMenu />', () => {
|
|
|
|
const icon = 'my-icon';
|
|
|
|
const size = 123;
|
2017-07-27 21:31:59 +01:00
|
|
|
let items;
|
|
|
|
let wrapper;
|
|
|
|
let action;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
action = sinon.spy();
|
2016-10-20 16:34:44 +01:00
|
|
|
|
2017-07-27 21:31:59 +01:00
|
|
|
items = [
|
|
|
|
{ text: 'first item', action: action, href: '/some/url' },
|
|
|
|
{ text: 'second item', action: 'noop' },
|
|
|
|
];
|
|
|
|
wrapper = shallow(<DropdownMenu icon={icon} items={items} size={size} />);
|
|
|
|
});
|
2016-10-20 16:34:44 +01:00
|
|
|
|
|
|
|
it('contains one <Dropdown />', () => {
|
|
|
|
expect(wrapper).to.have.exactly(1).descendants(Dropdown);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('contains one <DropdownTrigger />', () => {
|
|
|
|
expect(wrapper.find(Dropdown)).to.have.exactly(1).descendants(DropdownTrigger);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('contains one <DropdownContent />', () => {
|
|
|
|
expect(wrapper.find(Dropdown)).to.have.exactly(1).descendants(DropdownContent);
|
|
|
|
});
|
|
|
|
|
2017-07-27 21:31:59 +01:00
|
|
|
it('does not contain a <DropdownContent /> if isUserTouching', () => {
|
|
|
|
const touchingWrapper = shallow(<DropdownMenu icon={icon} items={items} size={size} isUserTouching={isTrue} />);
|
|
|
|
expect(touchingWrapper.find(Dropdown)).to.have.exactly(0).descendants(DropdownContent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not contain a <DropdownContent /> if isUserTouching', () => {
|
|
|
|
const touchingWrapper = shallow(<DropdownMenu icon={icon} items={items} size={size} isUserTouching={isTrue} />);
|
|
|
|
expect(touchingWrapper.find(Dropdown)).to.have.exactly(0).descendants(DropdownContent);
|
|
|
|
});
|
|
|
|
|
2016-10-20 16:34:44 +01:00
|
|
|
it('uses props.size for <DropdownTrigger /> style values', () => {
|
|
|
|
['font-size', 'width', 'line-height'].map((property) => {
|
|
|
|
expect(wrapper.find(DropdownTrigger)).to.have.style(property, `${size}px`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses props.icon as icon class name', () => {
|
2017-06-01 16:27:15 +01:00
|
|
|
expect(wrapper.find(DropdownTrigger).find('i')).to.have.className(`fa-${icon}`);
|
2016-10-20 16:34:44 +01:00
|
|
|
});
|
|
|
|
|
2017-05-20 13:58:13 +01:00
|
|
|
it('is not expanded by default', () => {
|
|
|
|
expect(wrapper.state('expanded')).to.be.equal(false);
|
2017-06-01 16:27:15 +01:00
|
|
|
});
|
2017-05-20 13:58:13 +01:00
|
|
|
|
|
|
|
it('does not render the list elements if not expanded', () => {
|
|
|
|
const lis = wrapper.find(DropdownContent).find('li');
|
|
|
|
expect(lis.length).to.be.equal(0);
|
2017-06-01 16:27:15 +01:00
|
|
|
});
|
2017-05-20 13:58:13 +01:00
|
|
|
|
|
|
|
it('sets expanded to true when clicking the trigger', () => {
|
|
|
|
const wrapper = mount(<DropdownMenu icon={icon} items={items} size={size} />);
|
|
|
|
wrapper.find(DropdownTrigger).first().simulate('click');
|
|
|
|
expect(wrapper.state('expanded')).to.be.equal(true);
|
2017-06-01 16:27:15 +01:00
|
|
|
});
|
2017-05-20 13:58:13 +01:00
|
|
|
|
2017-07-27 21:31:59 +01:00
|
|
|
it('calls onModalOpen when clicking the trigger if isUserTouching', () => {
|
|
|
|
const onModalOpen = sinon.spy();
|
|
|
|
const touchingWrapper = mount(<DropdownMenu icon={icon} items={items} status={3.14} size={size} onModalOpen={onModalOpen} isUserTouching={isTrue} />);
|
|
|
|
touchingWrapper.find(DropdownTrigger).first().simulate('click');
|
|
|
|
expect(onModalOpen.calledOnce).to.be.equal(true);
|
|
|
|
expect(onModalOpen.args[0][0]).to.be.deep.equal({ status: 3.14, actions: items, onClick: touchingWrapper.node.handleClick });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls onModalClose when clicking an action if isUserTouching and isModalOpen', () => {
|
|
|
|
const onModalOpen = sinon.spy();
|
|
|
|
const onModalClose = sinon.spy();
|
|
|
|
const touchingWrapper = mount(<DropdownMenu icon={icon} items={items} status={3.14} size={size} isModalOpen onModalOpen={onModalOpen} onModalClose={onModalClose} isUserTouching={isTrue} />);
|
|
|
|
touchingWrapper.find(DropdownTrigger).first().simulate('click');
|
|
|
|
touchingWrapper.node.handleClick({ currentTarget: { getAttribute: () => '0' }, preventDefault: () => null });
|
|
|
|
expect(onModalClose.calledOnce).to.be.equal(true);
|
|
|
|
});
|
|
|
|
|
2017-05-20 13:58:13 +01:00
|
|
|
// Error: ReactWrapper::state() can only be called on the root
|
|
|
|
/*it('sets expanded to false when clicking outside', () => {
|
|
|
|
const wrapper = mount((
|
|
|
|
<div>
|
|
|
|
<DropdownMenu icon={icon} items={items} size={size} />
|
|
|
|
<span />
|
|
|
|
</div>
|
|
|
|
));
|
|
|
|
|
|
|
|
wrapper.find(DropdownTrigger).first().simulate('click');
|
|
|
|
expect(wrapper.find(DropdownMenu).first().state('expanded')).to.be.equal(true);
|
|
|
|
|
|
|
|
wrapper.find('span').first().simulate('click');
|
|
|
|
expect(wrapper.find(DropdownMenu).first().state('expanded')).to.be.equal(false);
|
|
|
|
})*/
|
|
|
|
|
|
|
|
it('renders list elements for each props.items if expanded', () => {
|
|
|
|
const wrapper = mount(<DropdownMenu icon={icon} items={items} size={size} />);
|
|
|
|
wrapper.find(DropdownTrigger).first().simulate('click');
|
2016-10-20 16:34:44 +01:00
|
|
|
const lis = wrapper.find(DropdownContent).find('li');
|
|
|
|
expect(lis.length).to.be.equal(items.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses the href passed in via props.items', () => {
|
|
|
|
wrapper
|
|
|
|
.find(DropdownContent).find('li a')
|
|
|
|
.forEach((a, i) => expect(a).to.have.attr('href', items[i].href));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses the text passed in via props.items', () => {
|
|
|
|
wrapper
|
|
|
|
.find(DropdownContent).find('li a')
|
|
|
|
.forEach((a, i) => expect(a).to.have.text(items[i].text));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses the action passed in via props.items as click handler', () => {
|
|
|
|
const wrapper = mount(<DropdownMenu icon={icon} items={items} size={size} />);
|
2017-05-20 13:58:13 +01:00
|
|
|
wrapper.find(DropdownTrigger).first().simulate('click');
|
2016-10-20 16:34:44 +01:00
|
|
|
wrapper.find(DropdownContent).find('li a').first().simulate('click');
|
|
|
|
expect(action.calledOnce).to.equal(true);
|
|
|
|
});
|
|
|
|
});
|