forked from dceddia/getting-started-with-tdd-in-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.spec.js
94 lines (82 loc) · 3.02 KB
/
components.spec.js
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
83
84
85
86
87
88
89
90
91
92
93
94
import React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import { shallow, mount } from 'enzyme';
import { BeerListContainer } from './components';
import { InputArea, BeerList } from './components';
describe('BeerListContainer', () => {
it('should render InputArea and BeerList', () => {
const wrapper = shallow(<BeerListContainer/>);
expect(wrapper.containsAllMatchingElements([
<InputArea/>,
<BeerList/>
])).to.equal(true);
});
it('should start with an empty list', () => {
const wrapper = shallow(<BeerListContainer/>);
expect(wrapper.state('beers')).to.eql([]);
});
it('adds items to the list', () => {
const wrapper = shallow(<BeerListContainer/>);
wrapper.instance().addItem('Sam Adams');
expect(wrapper.state('beers')).to.eql(['Sam Adams']);
});
it('passes addItem to InputArea', () => {
const wrapper = shallow(<BeerListContainer/>);
const inputArea = wrapper.find(InputArea);
const addItem = wrapper.instance().addItem;
expect(inputArea.prop('onSubmit')).to.eql(addItem);
});
it('passes a bound addItem function to InputArea', () => {
const wrapper = shallow(<BeerListContainer/>);
const inputArea = wrapper.find(InputArea);
inputArea.prop('onSubmit')('Sam Adams');
expect(wrapper.state('beers')).to.eql(['Sam Adams']);
});
it('renders the items', () => {
const wrapper = mount(<BeerListContainer/>);
wrapper.instance().addItem('Sam Adams');
wrapper.instance().addItem('Resin');
expect(wrapper.find('li').length).to.equal(2);
});
});
describe('InputArea', () => {
it('should contain an input and a button', () => {
const wrapper = shallow(<InputArea/>);
expect(wrapper.containsAllMatchingElements([
<input/>,
<button>Add</button>
])).to.equal(true);
});
it('should accept input', () => {
const wrapper = mount(<InputArea/>);
const input = wrapper.find('input');
input.simulate('change', {target: { value: 'Resin' }});
expect(wrapper.state('text')).to.equal('Resin');
expect(input.prop('value')).to.equal('Resin');
});
it('should call onSubmit when Add is clicked', () => {
const addItemSpy = spy();
const wrapper = shallow(<InputArea onSubmit={addItemSpy}/>);
wrapper.setState({text: 'Octoberfest'});
const addButton = wrapper.find('button');
addButton.simulate('click');
expect(addItemSpy.calledOnce).to.equal(true);
expect(addItemSpy.calledWith('Octoberfest')).to.equal(true);
});
});
describe('BeerList', () => {
it('should render zero items', () => {
const wrapper = shallow(<BeerList items={[]}/>);
expect(wrapper.find('li')).to.have.length(0);
});
it('should render undefined items', () => {
const wrapper = shallow(<BeerList items={undefined}/>);
expect(wrapper.find('li')).to.have.length(0);
});
it('should render the items', () => {
const items = ['Sam Adams', 'Resin', 'Octoberfest'];
const wrapper = shallow(<BeerList items={items}/>);
expect(wrapper.find('li')).to.have.length(3);
});
});