Skip to content

Commit f9b93d1

Browse files
committed
Refactored example app's reducer structure for clarity. Updated initialState to place example app's state under an object to help convey that it's just a potential slice of state.
1 parent adbd56b commit f9b93d1

9 files changed

Lines changed: 57 additions & 55 deletions

File tree

src/components/FuelSavingsForm.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ class FuelSavingsForm extends React.Component {
1212
}
1313

1414
onTimeframeChange(e) {
15-
this.props.calculateFuelSavings(this.props.appState, 'milesDrivenTimeframe', e.target.value);
15+
this.props.calculateFuelSavings(this.props.fuelSavings, 'milesDrivenTimeframe', e.target.value);
1616
}
1717

1818
fuelSavingsKeypress(name, value) {
19-
this.props.calculateFuelSavings(this.props.appState, name, value);
19+
this.props.calculateFuelSavings(this.props.fuelSavings, name, value);
2020
}
2121

2222
save() {
23-
this.props.saveFuelSavings(this.props.appState);
23+
this.props.saveFuelSavings(this.props.fuelSavings);
2424
}
2525

2626
render() {
27-
const {appState} = this.props;
27+
const {fuelSavings} = this.props;
2828

2929
return (
3030
<div>
@@ -33,20 +33,22 @@ class FuelSavingsForm extends React.Component {
3333
<tbody>
3434
<tr>
3535
<td><label htmlFor="newMpg">New Vehicle MPG</label></td>
36-
<td><FuelSavingsTextInput onChange={this.fuelSavingsKeypress} name="newMpg" value={appState.newMpg}/></td>
36+
<td><FuelSavingsTextInput onChange={this.fuelSavingsKeypress} name="newMpg" value={fuelSavings.newMpg}/>
37+
</td>
3738
</tr>
3839
<tr>
3940
<td><label htmlFor="tradeMpg">Trade-in MPG</label></td>
40-
<td><FuelSavingsTextInput onChange={this.fuelSavingsKeypress} name="tradeMpg" value={appState.tradeMpg}/>
41+
<td><FuelSavingsTextInput onChange={this.fuelSavingsKeypress} name="tradeMpg" value={fuelSavings.tradeMpg}/>
4142
</td>
4243
</tr>
4344
<tr>
4445
<td><label htmlFor="newPpg">New Vehicle price per gallon</label></td>
45-
<td><FuelSavingsTextInput onChange={this.fuelSavingsKeypress} name="newPpg" value={appState.newPpg}/></td>
46+
<td><FuelSavingsTextInput onChange={this.fuelSavingsKeypress} name="newPpg" value={fuelSavings.newPpg}/>
47+
</td>
4648
</tr>
4749
<tr>
4850
<td><label htmlFor="tradePpg">Trade-in price per gallon</label></td>
49-
<td><FuelSavingsTextInput onChange={this.fuelSavingsKeypress} name="tradePpg" value={appState.tradePpg}/>
51+
<td><FuelSavingsTextInput onChange={this.fuelSavingsKeypress} name="tradePpg" value={fuelSavings.tradePpg}/>
5052
</td>
5153
</tr>
5254
<tr>
@@ -55,12 +57,12 @@ class FuelSavingsForm extends React.Component {
5557
<FuelSavingsTextInput
5658
onChange={this.fuelSavingsKeypress}
5759
name="milesDriven"
58-
value={appState.milesDriven}/>
60+
value={fuelSavings.milesDriven}/>
5961
miles per
6062
<select
6163
name="milesDrivenTimeframe"
6264
onChange={this.onTimeframeChange}
63-
value={appState.milesDrivenTimeframe}>
65+
value={fuelSavings.milesDrivenTimeframe}>
6466
<option value="week">Week</option>
6567
<option value="month">Month</option>
6668
<option value="year">Year</option>
@@ -69,14 +71,14 @@ class FuelSavingsForm extends React.Component {
6971
</tr>
7072
<tr>
7173
<td><label>Date Modified</label></td>
72-
<td>{appState.dateModified}</td>
74+
<td>{fuelSavings.dateModified}</td>
7375
</tr>
7476
</tbody>
7577
</table>
7678

7779
<hr/>
7880

79-
{appState.necessaryDataIsProvidedToCalculateSavings && <FuelSavingsResults savings={appState.savings}/>}
81+
{fuelSavings.necessaryDataIsProvidedToCalculateSavings && <FuelSavingsResults savings={fuelSavings.savings}/>}
8082
<input type="submit" value="Save" onClick={this.save}/>
8183
</div>
8284
);
@@ -86,7 +88,7 @@ class FuelSavingsForm extends React.Component {
8688
FuelSavingsForm.propTypes = {
8789
saveFuelSavings: PropTypes.func.isRequired,
8890
calculateFuelSavings: PropTypes.func.isRequired,
89-
appState: PropTypes.object.isRequired
91+
fuelSavings: PropTypes.object.isRequired
9092
};
9193

9294
export default FuelSavingsForm;

src/components/FuelSavingsForm.spec.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('<FuelSavingsForm />', () => {
1010
it('should contain <FuelSavingsTextInput /> components', () => {
1111
const saveFuelSavings = () => {};
1212
const calculateFuelSavings = () => {};
13-
const appState = {
13+
const fuelSavings = {
1414
newMpg: 20,
1515
tradeMpg: 10,
1616
newPpg: 1.50,
@@ -30,27 +30,27 @@ describe('<FuelSavingsForm />', () => {
3030
const wrapper = shallow(<FuelSavingsForm
3131
saveFuelSavings={saveFuelSavings}
3232
calculateFuelSavings={calculateFuelSavings}
33-
appState={appState}
33+
fuelSavings={fuelSavings}
3434
/>);
3535
const allInputs = wrapper.find(FuelSavingsTextInput);
3636

3737
expect(allInputs).to.be.length(5);
3838
expect(allInputs.at(0).props().name).to.equal('newMpg');
39-
expect(allInputs.at(0).props().value).to.equal(appState.newMpg);
39+
expect(allInputs.at(0).props().value).to.equal(fuelSavings.newMpg);
4040
expect(allInputs.at(1).props().name).to.equal('tradeMpg');
41-
expect(allInputs.at(1).props().value).to.equal(appState.tradeMpg);
41+
expect(allInputs.at(1).props().value).to.equal(fuelSavings.tradeMpg);
4242
expect(allInputs.at(2).props().name).to.equal('newPpg');
43-
expect(allInputs.at(2).props().value).to.equal(appState.newPpg);
43+
expect(allInputs.at(2).props().value).to.equal(fuelSavings.newPpg);
4444
expect(allInputs.at(3).props().name).to.equal('tradePpg');
45-
expect(allInputs.at(3).props().value).to.equal(appState.tradePpg);
45+
expect(allInputs.at(3).props().value).to.equal(fuelSavings.tradePpg);
4646
expect(allInputs.at(4).props().name).to.equal('milesDriven');
47-
expect(allInputs.at(4).props().value).to.equal(appState.milesDriven);
47+
expect(allInputs.at(4).props().value).to.equal(fuelSavings.milesDriven);
4848
});
4949

5050
it('should contain options to change miles driven timeframe', () => {
5151
const saveFuelSavings = () => {};
5252
const calculateFuelSavings = () => {};
53-
const appState = {
53+
const fuelSavings = {
5454
newMpg: 20,
5555
tradeMpg: 10,
5656
newPpg: 1.50,
@@ -70,7 +70,7 @@ describe('<FuelSavingsForm />', () => {
7070
const wrapper = shallow(<FuelSavingsForm
7171
saveFuelSavings={saveFuelSavings}
7272
calculateFuelSavings={calculateFuelSavings}
73-
appState={appState}
73+
fuelSavings={fuelSavings}
7474
/>);
7575
const expectedOption1 = '<option value="week">Week</option>';
7676
const expectedOption2 = '<option value="month">Month</option>';
@@ -84,7 +84,7 @@ describe('<FuelSavingsForm />', () => {
8484
it('should contain <FuelSavingsResults /> when necessary conditions are met', () => {
8585
const saveFuelSavings = () => {};
8686
const calculateFuelSavings = () => {};
87-
const appState = {
87+
const fuelSavings = {
8888
newMpg: 20,
8989
tradeMpg: 10,
9090
newPpg: 1.50,
@@ -104,17 +104,17 @@ describe('<FuelSavingsForm />', () => {
104104
const wrapper = shallow(<FuelSavingsForm
105105
saveFuelSavings={saveFuelSavings}
106106
calculateFuelSavings={calculateFuelSavings}
107-
appState={appState}
107+
fuelSavings={fuelSavings}
108108
/>);
109-
const expected = <FuelSavingsResults savings={appState.savings}/>;
109+
const expected = <FuelSavingsResults savings={fuelSavings.savings}/>;
110110

111111
expect(wrapper.contains(expected)).to.be.true;
112112
});
113113

114114
it('should not contain <FuelSavingsResults /> when necessary conditions are not met', () => {
115115
const saveFuelSavings = () => {};
116116
const calculateFuelSavings = () => {};
117-
const appState = {
117+
const fuelSavings = {
118118
newMpg: 20,
119119
tradeMpg: 10,
120120
newPpg: 1.50,
@@ -134,17 +134,17 @@ describe('<FuelSavingsForm />', () => {
134134
const wrapper = shallow(<FuelSavingsForm
135135
saveFuelSavings={saveFuelSavings}
136136
calculateFuelSavings={calculateFuelSavings}
137-
appState={appState}
137+
fuelSavings={fuelSavings}
138138
/>);
139-
const expected = <FuelSavingsResults savings={appState.savings}/>;
139+
const expected = <FuelSavingsResults savings={fuelSavings.savings}/>;
140140

141141
expect(wrapper.contains(expected)).to.be.false;
142142
});
143143

144144
it('should handle form submit', () => {
145145
const saveFuelSavings = sinon.spy();
146146
const calculateFuelSavings = () => {};
147-
const appState = {
147+
const fuelSavings = {
148148
newMpg: 20,
149149
tradeMpg: 10,
150150
newPpg: 1.50,
@@ -164,7 +164,7 @@ describe('<FuelSavingsForm />', () => {
164164
const wrapper = shallow(<FuelSavingsForm
165165
saveFuelSavings={saveFuelSavings}
166166
calculateFuelSavings={calculateFuelSavings}
167-
appState={appState}
167+
fuelSavings={fuelSavings}
168168
/>);
169169

170170
expect(saveFuelSavings.calledOnce).to.be.false;
@@ -175,7 +175,7 @@ describe('<FuelSavingsForm />', () => {
175175
it('should submit appState', () => {
176176
const saveFuelSavings = sinon.spy();
177177
const calculateFuelSavings = () => {};
178-
const appState = {
178+
const fuelSavings = {
179179
newMpg: 20,
180180
tradeMpg: 10,
181181
newPpg: 1.50,
@@ -195,18 +195,18 @@ describe('<FuelSavingsForm />', () => {
195195
const wrapper = shallow(<FuelSavingsForm
196196
saveFuelSavings={saveFuelSavings}
197197
calculateFuelSavings={calculateFuelSavings}
198-
appState={appState}
198+
fuelSavings={fuelSavings}
199199
/>);
200200

201201
wrapper.find('input[type="submit"]').simulate('click');
202-
expect(saveFuelSavings.args[0][0]).to.equal(appState);
202+
expect(saveFuelSavings.args[0][0]).to.equal(fuelSavings);
203203
});
204204

205205

206206
it('should calculate fuel savings on text input change', () => {
207207
const saveFuelSavings = () => {};
208208
const calculateFuelSavings = sinon.spy();
209-
const appState = {
209+
const fuelSavings = {
210210
newMpg: 20,
211211
tradeMpg: 10,
212212
newPpg: 1.50,
@@ -226,7 +226,7 @@ describe('<FuelSavingsForm />', () => {
226226
const wrapper = shallow(<FuelSavingsForm
227227
saveFuelSavings={saveFuelSavings}
228228
calculateFuelSavings={calculateFuelSavings}
229-
appState={appState}
229+
fuelSavings={fuelSavings}
230230
/>);
231231

232232
expect(calculateFuelSavings.calledOnce).to.be.false;
@@ -237,7 +237,7 @@ describe('<FuelSavingsForm />', () => {
237237
it('should calculate fuel savings on miles driven timeframe change', () => {
238238
const saveFuelSavings = () => {};
239239
const calculateFuelSavings = sinon.spy();
240-
const appState = {
240+
const fuelSavings = {
241241
newMpg: 20,
242242
tradeMpg: 10,
243243
newPpg: 1.50,
@@ -257,7 +257,7 @@ describe('<FuelSavingsForm />', () => {
257257
const wrapper = shallow(<FuelSavingsForm
258258
saveFuelSavings={saveFuelSavings}
259259
calculateFuelSavings={calculateFuelSavings}
260-
appState={appState}
260+
fuelSavings={fuelSavings}
261261
/>);
262262

263263
expect(calculateFuelSavings.calledOnce).to.be.false;

src/containers/FuelSavingsPage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ export const FuelSavingsPage = (props) => {
99
<FuelSavingsForm
1010
saveFuelSavings={props.actions.saveFuelSavings}
1111
calculateFuelSavings={props.actions.calculateFuelSavings}
12-
appState={props.appState}
12+
fuelSavings={props.fuelSavings}
1313
/>
1414
);
1515
};
1616

1717
FuelSavingsPage.propTypes = {
1818
actions: PropTypes.object.isRequired,
19-
appState: PropTypes.object.isRequired
19+
fuelSavings: PropTypes.object.isRequired
2020
};
2121

2222
function mapStateToProps(state) {
2323
return {
24-
appState: state.fuelSavingsAppState
24+
fuelSavings: state.fuelSavings
2525
};
2626
}
2727

src/containers/FuelSavingsPage.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ describe('<FuelSavingsPage />', () => {
1010
saveFuelSavings: () => { },
1111
calculateFuelSavings: () => { }
1212
};
13-
const appState = {};
14-
const wrapper = shallow(<FuelSavingsPage actions={actions} appState={appState}/>);
13+
const fuelSavings = {};
14+
const wrapper = shallow(<FuelSavingsPage actions={actions} fuelSavings={fuelSavings}/>);
1515

1616
expect(wrapper.find(FuelSavingsForm)).to.be.length(1);
1717
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import initialState from './initialState';
99
// create a copy of the state passed and set new values on the copy.
1010
// Note that I'm using Object.assign to create a copy of current state
1111
// and update values on the copy.
12-
export default function fuelSavingsAppState(state = initialState.fuelSavingsAppState, action) {
12+
export default function fuelSavingsReducer(state = initialState.fuelSavings, action) {
1313
let newState;
1414

1515
switch (action.type) {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { expect } from 'chai';
22
import * as ActionTypes from '../constants/actionTypes';
3-
import reducer from './fuelSavings';
3+
import reducer from './fuelSavingsReducer';
44
import dateHelper from '../businessLogic/dateHelper';
55

66
describe('Reducers::FuelSavings', () => {
7-
const getInitialSate = () => {
7+
const getInitialState = () => {
88
return {
99
newMpg: '',
1010
tradeMpg: '',
@@ -44,9 +44,9 @@ describe('Reducers::FuelSavings', () => {
4444

4545
it('should set initial state by default', () => {
4646
const action = { type: 'unknown' };
47-
const expected = getInitialSate();
47+
const expected = getInitialState();
4848

49-
expect(reducer(undefined, action)).to.deep.equal(expected); // Notice use of deep because it's a nested object
49+
expect(reducer(getInitialState(), action)).to.deep.equal(expected); // Notice use of deep because it's a nested object
5050
// expect(reducer(undefined, action)).to.equal(expected); // Fails. Not deeply equal
5151
});
5252

src/reducers/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { combineReducers } from 'redux';
2-
import fuelSavingsAppState from './fuelSavings';
2+
import fuelSavings from './fuelSavingsReducer';
33

44
const rootReducer = combineReducers({
5-
fuelSavingsAppState
5+
fuelSavings
66
});
77

88
export default rootReducer;

src/reducers/initialState.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default {
2-
fuelSavingsAppState: {
2+
fuelSavings: {
33
newMpg: '',
44
tradeMpg: '',
55
newPpg: '',

src/store/store.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ describe('Store', () => {
3131
displayResults: false,
3232
dateModified: dateHelper.getFormattedDateTime(new Date()),
3333
necessaryDataIsProvidedToCalculateSavings: true,
34-
savings: calculator().calculateSavings(store.getState().fuelSavingsAppState)
34+
savings: calculator().calculateSavings(store.getState().fuelSavings)
3535
};
3636

37-
expect(actual.fuelSavingsAppState).to.deep.equal(expected);
37+
expect(actual.fuelSavings).to.deep.equal(expected);
3838
});
3939

4040
it('should not display results when necessary data is not provided', () => {
@@ -67,7 +67,7 @@ describe('Store', () => {
6767
};
6868

6969

70-
expect(actual.fuelSavingsAppState).to.deep.equal(expected);
70+
expect(actual.fuelSavings).to.deep.equal(expected);
7171
});
7272

7373

@@ -94,7 +94,7 @@ describe('Store', () => {
9494
];
9595
actions.forEach(action => store.dispatch(action));
9696

97-
const lastGoodSavings = calculator().calculateSavings(store.getState().fuelSavingsAppState);
97+
const lastGoodSavings = calculator().calculateSavings(store.getState().fuelSavings);
9898

9999
const moreActions = [
100100
{ type: ActionTypes.CALCULATE_FUEL_SAVINGS, settings: store.getState(), fieldName: 'tradePpg', value: 0 },
@@ -119,6 +119,6 @@ describe('Store', () => {
119119
savings: lastGoodSavings
120120
};
121121

122-
expect(actual.fuelSavingsAppState).to.deep.equal(expected);
122+
expect(actual.fuelSavings).to.deep.equal(expected);
123123
});
124124
});

0 commit comments

Comments
 (0)