Skip to content

Commit da67694

Browse files
committed
Add todos reducer and tests
1 parent 378c089 commit da67694

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

app/reducers/reducers.jsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const uuid = require('node-uuid');
2+
const moment = require('moment');
3+
14
export var searchTextReducer = (state = '', action) => {
25
switch(action.type){
36
case 'SET_SEARCH_TEXT':
@@ -14,4 +17,34 @@ export var showCompletedReducer = (state = false, action) => {
1417
default:
1518
return state;
1619
};
17-
};
20+
};
21+
22+
export var todoReducer = (state = [], action) => {
23+
switch(action.type){
24+
case 'ADD_TODO':
25+
return [
26+
...state,
27+
{
28+
id: uuid(),
29+
text: action.text,
30+
completed: false,
31+
createdAt: moment().unix(),
32+
completedAt: undefined
33+
}
34+
];
35+
case 'TOGGLE_TODO':
36+
return state.map((todo) => {
37+
if(todo.id === action.id)
38+
{
39+
var nextCompleted = !todo.completed;
40+
return {
41+
...todo,
42+
completed: nextCompleted,
43+
completedAt: nextCompleted ? moment().unix() : undefined
44+
};
45+
};
46+
});
47+
default:
48+
return state;
49+
};
50+
};

app/tests/reducers/reducers.test.jsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,39 @@ describe('Reducers',() => {
2828
expect(res).toEqual(true);
2929
});
3030
});
31+
32+
describe('todosReducer', () => {
33+
it('should add new todo', () => {
34+
var action = {
35+
type:'ADD_TODO',
36+
text:'Walk'
37+
}
38+
39+
var res = reducers.todoReducer(df([]), df(action));
40+
41+
expect(res.length).toBe(1);
42+
expect(res[0].text).toEqual(action.text);
43+
});
44+
45+
it('should toggle a todo', () => {
46+
var action = {
47+
type:'TOGGLE_TODO',
48+
id:1
49+
}
50+
51+
var todo = [{
52+
id: 1,
53+
text: 'Text',
54+
completed: false,
55+
createdAt: 1,
56+
completedAt: undefined
57+
}];
58+
59+
var res = reducers.todoReducer(df(todo), df(action));
60+
61+
expect(res.length).toBe(1);
62+
expect(res[0].completed).toBe(true);
63+
expect(res[0].completedAt).toBeA('number');
64+
});
65+
});
3166
});

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = {
3838
{
3939
loader: 'babel-loader',
4040
query: {
41-
presets: ['react', 'es2015']
41+
presets: ['react', 'es2015', 'stage-0']
4242
},
4343
test: /\.jsx?$/,
4444
exclude: /(node_modules|bower_components)/

0 commit comments

Comments
 (0)