闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.1 KiB

  1. import { expect } from 'chai';
  2. import { shallow, mount } from 'enzyme';
  3. import sinon from 'sinon';
  4. import DropdownMenu from '../../../app/javascript/mastodon/components/dropdown_menu';
  5. import Dropdown, { DropdownTrigger, DropdownContent } from 'react-simple-dropdown';
  6. describe('<DropdownMenu />', () => {
  7. const icon = 'my-icon';
  8. const size = 123;
  9. const action = sinon.spy();
  10. const items = [
  11. { text: 'first item', action: action, href: '/some/url' },
  12. { text: 'second item', action: 'noop' }
  13. ];
  14. const wrapper = shallow(<DropdownMenu icon={icon} items={items} size={size} />);
  15. it('contains one <Dropdown />', () => {
  16. expect(wrapper).to.have.exactly(1).descendants(Dropdown);
  17. });
  18. it('contains one <DropdownTrigger />', () => {
  19. expect(wrapper.find(Dropdown)).to.have.exactly(1).descendants(DropdownTrigger);
  20. });
  21. it('contains one <DropdownContent />', () => {
  22. expect(wrapper.find(Dropdown)).to.have.exactly(1).descendants(DropdownContent);
  23. });
  24. it('uses props.size for <DropdownTrigger /> style values', () => {
  25. ['font-size', 'width', 'line-height'].map((property) => {
  26. expect(wrapper.find(DropdownTrigger)).to.have.style(property, `${size}px`);
  27. });
  28. });
  29. it('uses props.icon as icon class name', () => {
  30. expect(wrapper.find(DropdownTrigger).find('i')).to.have.className(`fa-${icon}`)
  31. });
  32. it('renders list elements for each props.items', () => {
  33. const lis = wrapper.find(DropdownContent).find('li');
  34. expect(lis.length).to.be.equal(items.length);
  35. });
  36. it('uses the href passed in via props.items', () => {
  37. wrapper
  38. .find(DropdownContent).find('li a')
  39. .forEach((a, i) => expect(a).to.have.attr('href', items[i].href));
  40. });
  41. it('uses the text passed in via props.items', () => {
  42. wrapper
  43. .find(DropdownContent).find('li a')
  44. .forEach((a, i) => expect(a).to.have.text(items[i].text));
  45. });
  46. it('uses the action passed in via props.items as click handler', () => {
  47. const wrapper = mount(<DropdownMenu icon={icon} items={items} size={size} />);
  48. wrapper.find(DropdownContent).find('li a').first().simulate('click');
  49. expect(action.calledOnce).to.equal(true);
  50. });
  51. });