about summary refs log tree commit diff
path: root/spec/javascript/components
diff options
context:
space:
mode:
authorKai Schaper <303@posteo.de>2016-10-20 17:34:44 +0200
committerKai Schaper <303@posteo.de>2016-10-20 17:34:44 +0200
commit19cfe64455d805e7c21a4176256cc3e72d062d0b (patch)
treedf4f0f6950ce75dbee707cbe5ed6137c37f24d04 /spec/javascript/components
parent8698cd3281ac1d699c723a151b14f1e2f2e8b07e (diff)
add/update specs for Button, DisplayName, DropdownMenu
Diffstat (limited to 'spec/javascript/components')
-rw-r--r--spec/javascript/components/button.test.jsx69
-rw-r--r--spec/javascript/components/display_name.test.jsx24
-rw-r--r--spec/javascript/components/dropdown_menu.test.jsx64
3 files changed, 144 insertions, 13 deletions
diff --git a/spec/javascript/components/button.test.jsx b/spec/javascript/components/button.test.jsx
index 0f16ebe8e..13bbe95a2 100644
--- a/spec/javascript/components/button.test.jsx
+++ b/spec/javascript/components/button.test.jsx
@@ -2,13 +2,72 @@ import { expect } from 'chai';
 import { shallow } from 'enzyme';
 import sinon from 'sinon';
 
-import Button from '../../../app/assets/javascripts/components/components/button'
+import Button from '../../../app/assets/javascripts/components/components/button';
 
 describe('<Button />', () => {
-  it('simulates click events', () => {
-    const onClick = sinon.spy();
-    const wrapper = shallow(<Button onClick={onClick} />);
+  it('renders a button element', () => {
+    const wrapper = shallow(<Button />);
+    expect(wrapper).to.match('button');
+  });
+
+  it('renders the given text', () => {
+    const text = 'foo';
+    const wrapper = shallow(<Button text={text} />);
+    expect(wrapper.find('button')).to.have.text(text);
+  });
+
+  it('handles click events using the given handler', () => {
+    const handler = sinon.spy();
+    const wrapper = shallow(<Button onClick={handler} />);
     wrapper.find('button').simulate('click');
-    expect(onClick.calledOnce).to.equal(true);
+    expect(handler.calledOnce).to.equal(true);
+  });
+
+  it('does not handle click events if props.disabled given', () => {
+    const handler = sinon.spy();
+    const wrapper = shallow(<Button onClick={handler} disabled />);
+    wrapper.find('button').simulate('click');
+    expect(handler.called).to.equal(false);
+  });
+
+  it('renders a disabled attribute if props.disabled given', () => {
+    const wrapper = shallow(<Button disabled />);
+    expect(wrapper.find('button')).to.be.disabled();
+  });
+
+  it('renders the children', () => {
+    const children = <p>children</p>;
+    const wrapper = shallow(<Button>{children}</Button>);
+    expect(wrapper.find('button')).to.contain(children);
+  });
+
+  it('renders the props.text instead of children', () => {
+    const text = 'foo';
+    const children = <p>children</p>;
+    const wrapper = shallow(<Button text={text}>{children}</Button>);
+    expect(wrapper.find('button')).to.have.text(text);
+    expect(wrapper.find('button')).to.not.contain(children);
+  });
+
+  it('renders style="display: block; width: 100%;" if props.block given', () => {
+    const wrapper = shallow(<Button block />);
+    expect(wrapper.find('button')).to.have.style('display', 'block');
+    expect(wrapper.find('button')).to.have.style('width', '100%');
+  });
+
+  it('renders style="display: inline-block; width: auto;" by default', () => {
+    const wrapper = shallow(<Button />);
+    expect(wrapper.find('button')).to.have.style('display', 'inline-block');
+    expect(wrapper.find('button')).to.have.style('width', 'auto');
+  });
+
+  it('adds class "button-secondary" if props.secondary given', () => {
+    const wrapper = shallow(<Button secondary />);
+    expect(wrapper.find('button')).to.have.className('button-secondary');
+  });
+
+  it('does not add class "button-secondary" by default', () => {
+    const wrapper = shallow(<Button />);
+    expect(wrapper.find('button')).to.not.have.className('button-secondary');
   });
 });
diff --git a/spec/javascript/components/display_name.test.jsx b/spec/javascript/components/display_name.test.jsx
index c25eb8530..e5a932f4b 100644
--- a/spec/javascript/components/display_name.test.jsx
+++ b/spec/javascript/components/display_name.test.jsx
@@ -5,15 +5,23 @@ import Immutable  from 'immutable';
 import DisplayName from '../../../app/assets/javascripts/components/components/display_name'
 
 describe('<DisplayName />', () => {
-  const account = Immutable.fromJS({
-    username: 'bar',
-    acct: 'bar@baz',
-    display_name: 'Foo'
+  it('renders display name + account name', () => {
+    const account = Immutable.fromJS({
+      username: 'bar',
+      acct: 'bar@baz',
+      display_name: 'Foo'
+    });
+    const wrapper = render(<DisplayName account={account} />);
+    expect(wrapper).to.have.text('Foo @bar@baz');
   });
 
-  const wrapper = render(<DisplayName account={account} />);
-
-  it('renders display name', () => {
-    expect(wrapper.text()).to.match(/Foo @bar@baz/);
+  it('renders the username + account name if display name is empty', () => {
+    const account = Immutable.fromJS({
+      username: 'bar',
+      acct: 'bar@baz',
+      display_name: ''
+    });
+    const wrapper = render(<DisplayName account={account} />);
+    expect(wrapper).to.have.text('bar @bar@baz');
   });
 });
diff --git a/spec/javascript/components/dropdown_menu.test.jsx b/spec/javascript/components/dropdown_menu.test.jsx
new file mode 100644
index 000000000..ef3b25a60
--- /dev/null
+++ b/spec/javascript/components/dropdown_menu.test.jsx
@@ -0,0 +1,64 @@
+import { expect } from 'chai';
+import { shallow, mount } from 'enzyme';
+import sinon from 'sinon';
+
+import DropdownMenu from '../../../app/assets/javascripts/components/components/dropdown_menu';
+import Dropdown, { DropdownTrigger, DropdownContent } from 'react-simple-dropdown';
+
+describe('<DropdownMenu />', () => {
+  const icon = 'my-icon';
+  const size = 123;
+  const action = sinon.spy();
+
+  const items = [
+    { text: 'first item',  action: action, href: '/some/url' },
+    { text: 'second item', action: 'noop' }
+  ];
+  const wrapper = shallow(<DropdownMenu icon={icon} items={items} size={size} />);
+
+  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);
+  });
+
+  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', () => {
+    expect(wrapper.find(DropdownTrigger).find('i')).to.have.className(`fa-${icon}`)
+  });
+
+  it('renders list elements for each props.items', () => {
+    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} />);
+
+    wrapper.find(DropdownContent).find('li a').first().simulate('click');
+    expect(action.calledOnce).to.equal(true);
+  });
+});