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.

73 lines
2.6 KiB

  1. import { expect } from 'chai';
  2. import { shallow } from 'enzyme';
  3. import sinon from 'sinon';
  4. import Button from '../../../app/assets/javascripts/components/components/button';
  5. describe('<Button />', () => {
  6. it('renders a button element', () => {
  7. const wrapper = shallow(<Button />);
  8. expect(wrapper).to.match('button');
  9. });
  10. it('renders the given text', () => {
  11. const text = 'foo';
  12. const wrapper = shallow(<Button text={text} />);
  13. expect(wrapper.find('button')).to.have.text(text);
  14. });
  15. it('handles click events using the given handler', () => {
  16. const handler = sinon.spy();
  17. const wrapper = shallow(<Button onClick={handler} />);
  18. wrapper.find('button').simulate('click');
  19. expect(handler.calledOnce).to.equal(true);
  20. });
  21. it('does not handle click events if props.disabled given', () => {
  22. const handler = sinon.spy();
  23. const wrapper = shallow(<Button onClick={handler} disabled />);
  24. wrapper.find('button').simulate('click');
  25. expect(handler.called).to.equal(false);
  26. });
  27. it('renders a disabled attribute if props.disabled given', () => {
  28. const wrapper = shallow(<Button disabled />);
  29. expect(wrapper.find('button')).to.be.disabled();
  30. });
  31. it('renders the children', () => {
  32. const children = <p>children</p>;
  33. const wrapper = shallow(<Button>{children}</Button>);
  34. expect(wrapper.find('button')).to.contain(children);
  35. });
  36. it('renders the props.text instead of children', () => {
  37. const text = 'foo';
  38. const children = <p>children</p>;
  39. const wrapper = shallow(<Button text={text}>{children}</Button>);
  40. expect(wrapper.find('button')).to.have.text(text);
  41. expect(wrapper.find('button')).to.not.contain(children);
  42. });
  43. it('renders style="display: block; width: 100%;" if props.block given', () => {
  44. const wrapper = shallow(<Button block />);
  45. expect(wrapper.find('button')).to.have.style('display', 'block');
  46. expect(wrapper.find('button')).to.have.style('width', '100%');
  47. });
  48. it('renders style="display: inline-block; width: auto;" by default', () => {
  49. const wrapper = shallow(<Button />);
  50. expect(wrapper.find('button')).to.have.style('display', 'inline-block');
  51. expect(wrapper.find('button')).to.have.style('width', 'auto');
  52. });
  53. it('adds class "button-secondary" if props.secondary given', () => {
  54. const wrapper = shallow(<Button secondary />);
  55. expect(wrapper.find('button')).to.have.className('button-secondary');
  56. });
  57. it('does not add class "button-secondary" by default', () => {
  58. const wrapper = shallow(<Button />);
  59. expect(wrapper.find('button')).to.not.have.className('button-secondary');
  60. });
  61. });