Merge pull request #101 from kschaper/master

Add specs for Button, DisplayName, DropdownMenu components
pull/125/head
Eugen 2016-10-20 18:39:30 +02:00 committed by GitHub
commit 17122df80d
3 changed files with 144 additions and 13 deletions

View File

@ -2,13 +2,72 @@ import { expect } from 'chai';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import sinon from 'sinon'; import sinon from 'sinon';
import Button from '../../../app/assets/javascripts/components/components/button' import Button from '../../../app/assets/javascripts/components/components/button';
describe('<Button />', () => { describe('<Button />', () => {
it('simulates click events', () => { it('renders a button element', () => {
const onClick = sinon.spy(); const wrapper = shallow(<Button />);
const wrapper = shallow(<Button onClick={onClick} />); 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'); 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');
}); });
}); });

View File

@ -5,15 +5,23 @@ import Immutable from 'immutable';
import DisplayName from '../../../app/assets/javascripts/components/components/display_name' import DisplayName from '../../../app/assets/javascripts/components/components/display_name'
describe('<DisplayName />', () => { describe('<DisplayName />', () => {
it('renders display name + account name', () => {
const account = Immutable.fromJS({ const account = Immutable.fromJS({
username: 'bar', username: 'bar',
acct: 'bar@baz', acct: 'bar@baz',
display_name: 'Foo' display_name: 'Foo'
}); });
const wrapper = render(<DisplayName account={account} />); const wrapper = render(<DisplayName account={account} />);
expect(wrapper).to.have.text('Foo @bar@baz');
});
it('renders display name', () => { it('renders the username + account name if display name is empty', () => {
expect(wrapper.text()).to.match(/Foo @bar@baz/); 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');
}); });
}); });

View File

@ -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);
});
});