about summary refs log tree commit diff
path: root/app/javascript/mastodon/components/__tests__/button-test.js
blob: 924ba39dc61af1b00ec1d79ad858dee1fd5b6df1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { shallow } from 'enzyme';
import React from 'react';
import renderer from 'react-test-renderer';
import Button from '../button';

describe('<Button />', () => {
  it('renders a button element', () => {
    const component = renderer.create(<Button />);
    const tree      = component.toJSON();

    expect(tree).toMatchSnapshot();
  });

  it('renders the given text', () => {
    const text      = 'foo';
    const component = renderer.create(<Button text={text} />);
    const tree      = component.toJSON();

    expect(tree).toMatchSnapshot();
  });

  it('handles click events using the given handler', () => {
    const handler = jest.fn();
    const button  = shallow(<Button onClick={handler} />);
    button.find('button').simulate('click');

    expect(handler.mock.calls.length).toEqual(1);
  });

  it('does not handle click events if props.disabled given', () => {
    const handler = jest.fn();
    const button  = shallow(<Button onClick={handler} disabled />);
    button.find('button').simulate('click');

    expect(handler.mock.calls.length).toEqual(0);
  });

  it('renders a disabled attribute if props.disabled given', () => {
    const component = renderer.create(<Button disabled />);
    const tree      = component.toJSON();

    expect(tree).toMatchSnapshot();
  });

  it('renders the children', () => {
    const children  = <p>children</p>;
    const component = renderer.create(<Button>{children}</Button>);
    const tree      = component.toJSON();

    expect(tree).toMatchSnapshot();
  });

  it('renders the props.text instead of children', () => {
    const text      = 'foo';
    const children  = <p>children</p>;
    const component = renderer.create(<Button text={text}>{children}</Button>);
    const tree      = component.toJSON();

    expect(tree).toMatchSnapshot();
  });

  it('renders class="button--block" if props.block given', () => {
    const component = renderer.create(<Button block />);
    const tree      = component.toJSON();

    expect(tree).toMatchSnapshot();
  });

  it('adds class "button-secondary" if props.secondary given', () => {
    const component = renderer.create(<Button secondary />);
    const tree      = component.toJSON();

    expect(tree).toMatchSnapshot();
  });

  it('renders title if props.title is given', () => {
    const component = renderer.create(<Button title='foo' />);
    const tree      = component.toJSON();

    expect(tree).toMatchSnapshot();
  });
});