-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathFuelSavingsForm.spec.js
169 lines (145 loc) · 5.43 KB
/
FuelSavingsForm.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import React from 'react';
import { shallow } from 'enzyme';
import FuelSavingsForm from './FuelSavingsForm';
import FuelSavingsTextInput from './FuelSavingsTextInput';
import FuelSavingsResults from './FuelSavingsResults';
/* Object builder. Could use test data builder pattern too.
More info: http://blog.codeleak.pl/2014/06/test-data-builders-and-object-mother.html
Returns fuel savings object. Overrides default values
for any properties sent in on args object.
Example: To get a fuel savings object like the
default below, but with newMpg set to 10, call with
getFuelSavings({ newMpg: 10});
*/
function getFuelSavings(args) {
const defaultFuelSavings = {
newMpg: 20,
tradeMpg: 10,
newPpg: 1.50,
tradePpg: 1.50,
milesDriven: 100,
milesDrivenTimeframe: 'week',
displayResults: false,
dateModified: null,
necessaryDataIsProvidedToCalculateSavings: false,
savings: {
monthly: 0,
annual: 0,
threeYear: 0
}
};
return {
...defaultFuelSavings,
...args
};
}
describe('<FuelSavingsForm />', () => {
it('should contain <FuelSavingsTextInput /> components', () => {
const fuelSavings = getFuelSavings();
const wrapper = shallow(<FuelSavingsForm
onSaveClick={jest.fn()}
onChange={jest.fn()}
fuelSavings={fuelSavings}
/>);
const allInputs = wrapper.find(FuelSavingsTextInput);
expect(allInputs.length).toEqual(5);
expect(allInputs.at(0).props().name).toEqual('newMpg');
expect(allInputs.at(0).props().value).toEqual(fuelSavings.newMpg);
expect(allInputs.at(1).props().name).toEqual('tradeMpg');
expect(allInputs.at(1).props().value).toEqual(fuelSavings.tradeMpg);
expect(allInputs.at(2).props().name).toEqual('newPpg');
expect(allInputs.at(2).props().value).toEqual(fuelSavings.newPpg);
expect(allInputs.at(3).props().name).toEqual('tradePpg');
expect(allInputs.at(3).props().value).toEqual(fuelSavings.tradePpg);
expect(allInputs.at(4).props().name).toEqual('milesDriven');
expect(allInputs.at(4).props().value).toEqual(fuelSavings.milesDriven);
});
it('should contain options to change miles driven timeframe', () => {
const wrapper = shallow(<FuelSavingsForm
onSaveClick={jest.fn()}
onChange={jest.fn()}
fuelSavings={getFuelSavings()}
/>);
const expectedOption1 = '<option value="week">Week</option>';
const expectedOption2 = '<option value="month">Month</option>';
const expectedOption3 = '<option value="year">Year</option>';
expect(wrapper.find('select').childAt(0).html()).toEqual(expectedOption1);
expect(wrapper.find('select').childAt(1).html()).toEqual(expectedOption2);
expect(wrapper.find('select').childAt(2).html()).toEqual(expectedOption3);
});
it('should contain <FuelSavingsResults /> when necessary conditions are met', () => {
const fuelSavings = getFuelSavings({
necessaryDataIsProvidedToCalculateSavings: true,
savings: {
monthly: 10,
annual: 120,
threeYear: 360
}
});
const wrapper = shallow(<FuelSavingsForm
onSaveClick={jest.fn()}
onChange={jest.fn()}
fuelSavings={fuelSavings}
/>);
const expected = <FuelSavingsResults savings={fuelSavings.savings} />;
expect(wrapper.contains(expected)).toBeTruthy();
});
it('should not contain <FuelSavingsResults /> when necessary conditions are not met', () => {
const fuelSavings = getFuelSavings();
const wrapper = shallow(<FuelSavingsForm
onSaveClick={jest.fn()}
onChange={jest.fn()}
fuelSavings={fuelSavings}
/>);
const expected = <FuelSavingsResults savings={fuelSavings.savings} />;
expect(wrapper.contains(expected)).toBeFalsy();
});
it('should handle save button click', () => {
const onSaveClick = jest.fn();
const wrapper = shallow(<FuelSavingsForm
onSaveClick={onSaveClick}
onChange={jest.fn()}
fuelSavings={getFuelSavings()}
/>);
expect(onSaveClick).not.toBeCalled();
wrapper.find('input[type="submit"]').simulate('click');
expect(onSaveClick).toBeCalled();
});
it('should submit appState', () => {
const fuelSavings = getFuelSavings();
const onSaveClick = jest.fn();
const wrapper = shallow(<FuelSavingsForm
onSaveClick={onSaveClick}
onChange={jest.fn()}
fuelSavings={fuelSavings}
/>);
expect(onSaveClick).not.toBeCalled();
wrapper.find('input[type="submit"]').simulate('click');
expect(onSaveClick).toBeCalled();
});
it('should call onChange when text input changes', () => {
const onChange = jest.fn();
const wrapper = shallow(<FuelSavingsForm
onSaveClick={jest.fn()}
onChange={onChange}
fuelSavings={getFuelSavings()}
/>);
const changeEvent = { target: { name: 'newMpg', value: '20' } };
expect(onChange).not.toBeCalled();
wrapper.find(FuelSavingsTextInput).first().simulate('change', changeEvent);
expect(onChange).toBeCalledWith(changeEvent);
});
it('should call onChange when timeframe changes', () => {
const onChange = jest.fn();
const fuelSavings = getFuelSavings();
const wrapper = shallow(<FuelSavingsForm
onSaveClick={jest.fn()}
onChange={onChange}
fuelSavings={fuelSavings}
/>);
const changeEvent = { target: { name: 'timeframe', value: 'year' } };
expect(onChange).not.toBeCalled();
wrapper.find('select').simulate('change', changeEvent);
expect(onChange).toBeCalledWith(changeEvent);
});
});