闭社主体 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.

132 lines
5.2 KiB

  1. import { expect } from 'chai';
  2. import { shallow, mount } from 'enzyme';
  3. import sinon from 'sinon';
  4. import React from 'react';
  5. import DropdownMenu from '../../../app/javascript/mastodon/components/dropdown_menu';
  6. import Dropdown, { DropdownTrigger, DropdownContent } from 'react-simple-dropdown';
  7. const isTrue = () => true;
  8. describe('<DropdownMenu />', () => {
  9. const icon = 'my-icon';
  10. const size = 123;
  11. let items;
  12. let wrapper;
  13. let action;
  14. beforeEach(() => {
  15. action = sinon.spy();
  16. items = [
  17. { text: 'first item', action: action, href: '/some/url' },
  18. { text: 'second item', action: 'noop' },
  19. ];
  20. wrapper = shallow(<DropdownMenu icon={icon} items={items} size={size} />);
  21. });
  22. it('contains one <Dropdown />', () => {
  23. expect(wrapper).to.have.exactly(1).descendants(Dropdown);
  24. });
  25. it('contains one <DropdownTrigger />', () => {
  26. expect(wrapper.find(Dropdown)).to.have.exactly(1).descendants(DropdownTrigger);
  27. });
  28. it('contains one <DropdownContent />', () => {
  29. expect(wrapper.find(Dropdown)).to.have.exactly(1).descendants(DropdownContent);
  30. });
  31. it('does not contain a <DropdownContent /> if isUserTouching', () => {
  32. const touchingWrapper = shallow(<DropdownMenu icon={icon} items={items} size={size} isUserTouching={isTrue} />);
  33. expect(touchingWrapper.find(Dropdown)).to.have.exactly(0).descendants(DropdownContent);
  34. });
  35. it('does not contain a <DropdownContent /> if isUserTouching', () => {
  36. const touchingWrapper = shallow(<DropdownMenu icon={icon} items={items} size={size} isUserTouching={isTrue} />);
  37. expect(touchingWrapper.find(Dropdown)).to.have.exactly(0).descendants(DropdownContent);
  38. });
  39. it('uses props.size for <DropdownTrigger /> style values', () => {
  40. ['font-size', 'width', 'line-height'].map((property) => {
  41. expect(wrapper.find(DropdownTrigger)).to.have.style(property, `${size}px`);
  42. });
  43. });
  44. it('uses props.icon as icon class name', () => {
  45. expect(wrapper.find(DropdownTrigger).find('i')).to.have.className(`fa-${icon}`);
  46. });
  47. it('is not expanded by default', () => {
  48. expect(wrapper.state('expanded')).to.be.equal(false);
  49. });
  50. it('does not render the list elements if not expanded', () => {
  51. const lis = wrapper.find(DropdownContent).find('li');
  52. expect(lis.length).to.be.equal(0);
  53. });
  54. it('sets expanded to true when clicking the trigger', () => {
  55. const wrapper = mount(<DropdownMenu icon={icon} items={items} size={size} />);
  56. wrapper.find(DropdownTrigger).first().simulate('click');
  57. expect(wrapper.state('expanded')).to.be.equal(true);
  58. });
  59. it('calls onModalOpen when clicking the trigger if isUserTouching', () => {
  60. const onModalOpen = sinon.spy();
  61. const touchingWrapper = mount(<DropdownMenu icon={icon} items={items} status={3.14} size={size} onModalOpen={onModalOpen} isUserTouching={isTrue} />);
  62. touchingWrapper.find(DropdownTrigger).first().simulate('click');
  63. expect(onModalOpen.calledOnce).to.be.equal(true);
  64. expect(onModalOpen.args[0][0]).to.be.deep.equal({ status: 3.14, actions: items, onClick: touchingWrapper.node.handleClick });
  65. });
  66. it('calls onModalClose when clicking an action if isUserTouching and isModalOpen', () => {
  67. const onModalOpen = sinon.spy();
  68. const onModalClose = sinon.spy();
  69. const touchingWrapper = mount(<DropdownMenu icon={icon} items={items} status={3.14} size={size} isModalOpen onModalOpen={onModalOpen} onModalClose={onModalClose} isUserTouching={isTrue} />);
  70. touchingWrapper.find(DropdownTrigger).first().simulate('click');
  71. touchingWrapper.node.handleClick({ currentTarget: { getAttribute: () => '0' }, preventDefault: () => null });
  72. expect(onModalClose.calledOnce).to.be.equal(true);
  73. });
  74. // Error: ReactWrapper::state() can only be called on the root
  75. /*it('sets expanded to false when clicking outside', () => {
  76. const wrapper = mount((
  77. <div>
  78. <DropdownMenu icon={icon} items={items} size={size} />
  79. <span />
  80. </div>
  81. ));
  82. wrapper.find(DropdownTrigger).first().simulate('click');
  83. expect(wrapper.find(DropdownMenu).first().state('expanded')).to.be.equal(true);
  84. wrapper.find('span').first().simulate('click');
  85. expect(wrapper.find(DropdownMenu).first().state('expanded')).to.be.equal(false);
  86. })*/
  87. it('renders list elements for each props.items if expanded', () => {
  88. const wrapper = mount(<DropdownMenu icon={icon} items={items} size={size} />);
  89. wrapper.find(DropdownTrigger).first().simulate('click');
  90. const lis = wrapper.find(DropdownContent).find('li');
  91. expect(lis.length).to.be.equal(items.length);
  92. });
  93. it('uses the href passed in via props.items', () => {
  94. wrapper
  95. .find(DropdownContent).find('li a')
  96. .forEach((a, i) => expect(a).to.have.attr('href', items[i].href));
  97. });
  98. it('uses the text passed in via props.items', () => {
  99. wrapper
  100. .find(DropdownContent).find('li a')
  101. .forEach((a, i) => expect(a).to.have.text(items[i].text));
  102. });
  103. it('uses the action passed in via props.items as click handler', () => {
  104. const wrapper = mount(<DropdownMenu icon={icon} items={items} size={size} />);
  105. wrapper.find(DropdownTrigger).first().simulate('click');
  106. wrapper.find(DropdownContent).find('li a').first().simulate('click');
  107. expect(action.calledOnce).to.equal(true);
  108. });
  109. });