Skip to content

Commit

Permalink
add planReducer unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyubinhan committed Jun 23, 2018
1 parent e0d352f commit d82bbf4
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/actionCreators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const getPlan = () => (dispatch) => {
(response) => {
dispatch(success(reducerTypes.GET_PLAN), response);
dispatch(storePlan(normalize(response.plan, schema.plan)));
dispatch(storePlan(normalize(response.plan, schema.plan)));
},
(err) => {
dispatch(error(reducerTypes.GET_PLAN, err.message));
Expand Down
18 changes: 9 additions & 9 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// import { v4 } from 'node-uuid';

import paginatedFakeAgreements from './fakeAgreements';
import fakePlanWithAgreement from './fakePlan';
import { fakeAgreements, fakePlan } from '../tests/intergration/mockData';

const fakeDatabase = {
paginatedAgreements: paginatedFakeAgreements,
plan: fakePlanWithAgreement,
paginatedAgreements: fakeAgreements,
plan: fakePlan,
};

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

export const fetchAgreements = filter =>
export const fetchAgreements = filter => (
delay(500).then(() => {
switch (filter) {
case 'all':
Expand All @@ -20,12 +18,14 @@ export const fetchAgreements = filter =>
default:
throw new Error(`Unknown filter: ${filter}`);
}
});
})
);

export const fetchPlan = () =>
export const fetchPlan = () => (
delay(500).then(() => {
return fakeDatabase.plan;
});
})
);

// export const addTodo = (text) =>
// delay(500).then(() => {
Expand Down
6 changes: 2 additions & 4 deletions src/reducers/planReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ const storePlan = (state, action) => {

return {
plans: {
...state.plan,
[planId]: {
...plan,
},
...state.plans,
...plan,
},
planIds: handlePlanIds(state, planId),
pastures: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable quotes, comma-dangle */
const paginatedFakeAgreements = {
"perPage": 10,
"currentPage": 1,
Expand Down
1 change: 1 addition & 0 deletions src/api/fakePlan.js → src/tests/intergration/fakePlan.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable quotes, comma-dangle */
const fakePlanWithAgreement = {
"forestFileId": "RAN077965",
"agreementStartDate": "2019-01-01T08:00:00.000Z",
Expand Down
2 changes: 2 additions & 0 deletions src/tests/intergration/mockData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as fakeAgreements } from './fakeAgreements';
export { default as fakePlan } from './fakePlan';
84 changes: 84 additions & 0 deletions src/tests/reducers/planReducer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { normalize } from 'normalizr';
import planReducer from '../../reducers/planReducer';
import { storePlan } from '../../actions/storeActions';
import * as schema from '../../actionCreators/schema';

const initialState = {
plans: {},
planIds: [],
pastures: {},
grazingSchedules: {},
ministerIssues: {},
};

const mockPlanData = {
id: 'plan_id',
rangeName: 'hello',
pastures: [
{
id: 'pasture_id',
name: 'Pasture 1',
},
],
grazingSchedules: [
{
id: 'grazing_schedule_id',
year: 2018,
},
],
ministerIssues: [
{
id: 'minister_issue_id',
detail: 'detail',
},
],
};

const mockState = {
plans: {
plan_id: {
id: 'plan_id',
rangeName: 'hello',
pastures: ['pasture_id'],
grazingSchedules: ['grazing_schedule_id'],
ministerIssues: ['minister_issue_id'],
},
},
planIds: ['plan_id'],
pastures: {
pasture_id: {
id: 'pasture_id',
name: 'Pasture 1',
},
},
grazingSchedules: {
grazing_schedule_id: {
id: 'grazing_schedule_id',
year: 2018,
},
},
ministerIssues: {
minister_issue_id: {
id: 'minister_issue_id',
detail: 'detail',
},
},
};

describe('Plan reducer', () => {
it('Initialized with the initial state', () => {
expect(planReducer(undefined, {})).toEqual(initialState);
});

describe('Handles \'STORE_PLAN\'', () => {
it('Correctly stores the plans and related fields with initial state', () => {
const normalized = normalize(mockPlanData, schema.plan);
expect(planReducer(initialState, storePlan(normalized))).toEqual(mockState);
});

it('Correctly stores the plans and related fields with pre-existing state', () => {
const normalized = normalize(mockPlanData, schema.plan);
expect(planReducer(mockState, storePlan(normalized))).toEqual(mockState);
});
});
});

0 comments on commit d82bbf4

Please sign in to comment.