Skip to content

Commit 92483e0

Browse files
author
Olajide
committed
Refactoring WIP
1 parent af066a2 commit 92483e0

File tree

6 files changed

+110
-102
lines changed

6 files changed

+110
-102
lines changed

src/js/actions/proverbActions.js

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,40 @@ import webAPI from '../utils/webAPI';
33
import mockProverbApi from '../api/mockProverbsApi';
44

55
/**
6-
* @param {Object} proverbs: object
6+
* @param array of proverbs
77
* @return {Object} containing the action type and data
88
*/
99
export function loadProverbsSuccess(proverbs) {
1010
return { type: types.LOAD_PROVERBS_SUCCESS, proverbs };
1111
}
1212

1313
/**
14-
* @param {Object} proverbs: object
15-
* @return {Object} containing the action type and data
16-
*/
17-
export function loadProverbSuccess(proverb) {
18-
return { type: types.LOAD_PROVERB_SUCCESS, proverb };
19-
}
20-
21-
/**
22-
* @param {Object} proverbs: key proverbs and value is an array of questions
14+
* @param {Object} proverb
2315
* @return {Object} containing the action type and proverb
2416
*/
2517
export function updateProverbSuccess(proverb) {
2618
return { type: types.UPDATE_PROVERB_SUCCESS, proverb };
2719
}
2820

29-
export function createProverbSuccess(proverb) {
30-
return {
31-
type: types.CREATE_PROVERB_SUCCESS,
32-
proverb
33-
};
34-
}
35-
36-
export function loadProverbs() {
21+
export function loadProverbs(locale) {
22+
const defaultLocale = locale ? locale : 'en';
3723
return dispatch => {
38-
return webAPI(`/proverbs`, 'GET', '')
24+
return webAPI(`${'/' + defaultLocale + '/proverbs'}`, 'GET', '')
3925
.then(res => {
40-
dispatch(loadProverbsSuccess(res.proverbs));
41-
});
42-
};
43-
}
44-
45-
export function loadProverb(proverbId) {
46-
return dispatch => {
47-
return mockProverbApi.getProverb(proverbId)
48-
.then(proverb => {
49-
dispatch(loadProverbSuccess(proverb));
26+
dispatch(loadProverbsSuccess(res));
5027
});
5128
};
5229
}
5330

5431
export function saveProverb(proverb) {
32+
const type = proverb.id ? 'PUT' : 'POST';
33+
const rootUrl = `/proverbs`;
34+
const url = proverb.id ? `${rootUrl + '/' + proverb.id}` : rootUrl;
35+
5536
return (dispatch) => {
56-
return mockProverbApi.saveProverb(proverb).then(proverb => {
57-
proverb.id ? dispatch(updateProverbSuccess(proverb))
58-
: dispatch(createProverbSuccess(proverb));
59-
}).catch(error => {
60-
throw error;
61-
});
37+
return webAPI(url, type, proverb)
38+
.then(res => {
39+
dispatch(updateProverbSuccess(res))
40+
})
6241
};
6342
}
Lines changed: 85 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,97 @@
11
import expect from 'expect';
2-
import * as proverbActions from './proverbActions';
3-
import * as types from './actionTypes';
42
import thunk from 'redux-thunk';
53
import nock from 'nock';
64
import configureMockStore from 'redux-mock-store';
7-
5+
import { omit } from 'underscore';
86
import Config from '../config/environment';
7+
import * as proverbActions from './proverbActions';
8+
import * as types from './actionTypes';
99

10-
describe('loadProverbsSuccess', () => {
11-
describe('Create load Proverbs Action', () => {
12-
it('should create a LOAD_PROVERBS_SUCCESS action', () => {
13-
// setup
14-
const proverbs = [
15-
{id: 'A1023'}
16-
];
17-
const expectedAction = {
18-
type: types.default.LOAD_PROVERBS_SUCCESS,
19-
proverbs
20-
};
21-
22-
// actions
23-
const action = proverbActions.loadProverbsSuccess(proverbs);
24-
25-
// assertions
26-
expect(action).toEqual(expectedAction);
27-
});
28-
});
29-
});
10+
const proverb = { id: 'A1023', body: 'Testing proverb' };
11+
const proverbs = [ proverb ];
3012

31-
const middleware = [thunk];
32-
const mockStore = configureMockStore(middleware);
13+
describe('Proverb actions', () => {
3314

34-
describe('loadProverbs', function() {
35-
afterEach(() => {
36-
nock.cleanAll();
37-
});
38-
// this.timeout(15000);
39-
it('should dispatch a success action on successful API response', done => {
15+
it('should create a LOAD_PROVERBS_SUCCESS action', () => {
16+
17+
const expectedAction = {
18+
type: types.default.LOAD_PROVERBS_SUCCESS,
19+
proverbs
20+
};
21+
22+
// actions
23+
const action = proverbActions.loadProverbsSuccess(proverbs);
24+
25+
// assertions
26+
expect(action).toEqual(expectedAction);
27+
});
28+
29+
it('should create a UPDATE_PROVERB_SUCCESS action', () => {
4030
// setup
41-
const proverbs = [ {id: 'A1023', body: 'first proverb'} ];
42-
43-
nock(Config.host)
44-
.get('/proverbs')
45-
.reply(200, { proverbs });
46-
47-
const expectedActions = [
48-
{type: types.default.LOAD_PROVERBS_SUCCESS, proverbs}
49-
];
50-
51-
// action
52-
const initialAppState = { proverbs: [] };
53-
const store = mockStore(initialAppState, expectedActions);
54-
55-
store.dispatch(
56-
proverbActions.loadProverbs())
57-
.then(() => {
58-
const [ actions ] = store.getActions();
59-
expect(actions.type).toEqual(types.default.LOAD_PROVERBS_SUCCESS);
60-
expect(actions.proverbs).toEqual(proverbs);
61-
});
31+
const expectedUpdateAction = {
32+
type: types.default.UPDATE_PROVERB_SUCCESS,
33+
proverb
34+
};
6235

63-
done();
36+
// actions
37+
const action = proverbActions.updateProverbSuccess(proverb);
38+
39+
// assertions
40+
expect(action).toEqual(expectedUpdateAction);
6441
});
42+
43+
describe('Proverb action thunks', function() {
44+
45+
const middleware = [thunk];
46+
const mockStore = configureMockStore(middleware);
47+
const initialAppState = { proverbs: [], proverb: {} };
48+
49+
afterEach(() => {
50+
nock.cleanAll();
51+
});
52+
53+
// this.timeout(15000);
54+
describe('loadProverbs', () => {
55+
it('should dispatch a success action on successful API response', done => {
56+
57+
nock(Config.host)
58+
.get('/proverbs')
59+
.reply(200, proverbs);
60+
61+
const expectedActions = [
62+
{type: types.default.LOAD_PROVERBS_SUCCESS, proverbs}
63+
];
64+
65+
// action
66+
const store = mockStore(initialAppState, expectedActions);
67+
store.dispatch(proverbActions.loadProverbs())
68+
.then(() => {
69+
const [actions] = store.getActions();
70+
expect(actions.type).toEqual(types.default.LOAD_PROVERBS_SUCCESS);
71+
});
72+
73+
done();
74+
});
75+
})
76+
77+
describe('saveProverbs', () => {
78+
it('should dispatch a success action on successful API response', done => {
79+
80+
const expectedActions = [
81+
{type: types.default.UPDATE_PROVERB_SUCCESS, proverb}
82+
];
83+
84+
// action
85+
const store = mockStore(initialAppState, expectedActions);
86+
const [actions] = store.getActions();
87+
store.dispatch(proverbActions.saveProverb(proverb))
88+
.then(() => {
89+
expect(actions.type).toEqual(types.default.UPDATE_PROVERB_SUCCESS);
90+
});
91+
92+
done();
93+
});
94+
})
95+
});
96+
6597
});

src/js/components/proverbs/proverb/Index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class Proverb extends Component {
1212
const { proverb } = this.props;
1313
this.state = {
1414
proverb,
15-
errors : {},
1615
loading : false
1716
};
1817

@@ -41,14 +40,13 @@ class Proverb extends Component {
4140
}
4241

4342
render() {
44-
const { proverb, errors, loading } = this.state;
43+
const { proverb, loading } = this.state;
4544
return (
4645
<div className="panel-container about-page">
4746
<h3>{`${this.props.params.proverbId ? "Edit" : "Create"} A Proverb`}</h3>
4847
<ProverbForm
4948
proverb={proverb}
5049
loading={loading}
51-
errors={errors}
5250
handleSubmit={this.handleSubmit}
5351
/>
5452
</div>
@@ -68,7 +66,11 @@ Proverb.propTypes = {
6866
};
6967

7068
const getProverbValues = (proverbId, proverbs) => {
71-
const proverb = Object.values(proverbs).filter(proverb => proverb.id.toString() === proverbId);
69+
const proverb = Object.values(proverbs)
70+
.filter(
71+
proverb => proverb.id.toString() === proverbId
72+
);
73+
7274
return !isEmpty(proverb) ? proverb[0] : null;
7375
}
7476

src/js/components/proverbs/proverb/ProverbForm.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ class ProverbForm extends Component {
151151
ProverbForm.propTypes = {
152152
loading : PropTypes.bool,
153153
proverb : PropTypes.object.isRequired,
154-
errors : PropTypes.object.isRequired,
155154
handleSubmit: PropTypes.func
156155
};
157156

src/js/reducers/proverbs/proverbReducer.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,8 @@ export default function proverbReducer(state = initialState.proverbs, action) {
1010
case types.LOAD_PROVERB_SUCCESS:
1111
return storeHelpers.updateProverb(state, action.proverb);
1212

13-
case types.CREATE_PROVERB_SUCCESS:
14-
return Object.assign({}, state, {[action.proverb.id]: action.proverb });
15-
1613
case types.UPDATE_PROVERB_SUCCESS:
17-
return storeHelpers.updateProverb(state, action.proverb);
18-
19-
case types.SAVE_PROVERB_ERROR:
20-
return Object.assign({}, state, action.error);
14+
return storeHelpers.updateProverb(state, action.proverb);
2115

2216
default:
2317
return state;

src/js/reducers/proverbs/proverbReducer.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ describe('Proverb Reducer Test', () => {
2222
expect(newState).toEqual(expectedState);
2323
});
2424

25-
it("should add a new proverb when passed UPDATE_PROVERB_SUCCESS" +
26-
" if proverb doesn't already exist in store", () => {
25+
it('should add a new proverb when passed UPDATE_PROVERB_SUCCESS' +
26+
' if proverb doesn\'t already exist in store', () => {
2727
const initialState = {
2828
1: {id: 1},
2929
2: {id: 2}
@@ -34,6 +34,7 @@ describe('Proverb Reducer Test', () => {
3434

3535
// action
3636
const newState = proverbReducer(initialState, action);
37+
3738
expect(Object.keys(newState).length).toEqual(3);
3839
expect(newState[1].id).toEqual(1);
3940
expect(newState[2].id).toEqual(2);
@@ -54,4 +55,5 @@ describe('Proverb Reducer Test', () => {
5455
expect(Object.keys(updatedState).length).toEqual(2);
5556
expect(updatedState[proverb.id].labor_cost).toEqual(34);
5657
});
58+
5759
});

0 commit comments

Comments
 (0)