diff options
Diffstat (limited to 'third_party/webkit/PerformanceTests/Speedometer/resources/todomvc/architecture-examples/react-redux/src/reducers')
3 files changed, 341 insertions, 0 deletions
diff --git a/third_party/webkit/PerformanceTests/Speedometer/resources/todomvc/architecture-examples/react-redux/src/reducers/index.js b/third_party/webkit/PerformanceTests/Speedometer/resources/todomvc/architecture-examples/react-redux/src/reducers/index.js new file mode 100644 index 0000000000..a94ace36b9 --- /dev/null +++ b/third_party/webkit/PerformanceTests/Speedometer/resources/todomvc/architecture-examples/react-redux/src/reducers/index.js @@ -0,0 +1,8 @@ +import { combineReducers } from 'redux' +import todos from './todos' + +const rootReducer = combineReducers({ + todos +}) + +export default rootReducer diff --git a/third_party/webkit/PerformanceTests/Speedometer/resources/todomvc/architecture-examples/react-redux/src/reducers/todos.js b/third_party/webkit/PerformanceTests/Speedometer/resources/todomvc/architecture-examples/react-redux/src/reducers/todos.js new file mode 100644 index 0000000000..84d982664d --- /dev/null +++ b/third_party/webkit/PerformanceTests/Speedometer/resources/todomvc/architecture-examples/react-redux/src/reducers/todos.js @@ -0,0 +1,49 @@ +import { ADD_TODO, DELETE_TODO, EDIT_TODO, COMPLETE_TODO, COMPLETE_ALL, CLEAR_COMPLETED } from '../constants/ActionTypes' + +const initialState = [] + +export default function todos(state = initialState, action) { + switch (action.type) { + case ADD_TODO: + return [ + ...state, + { + id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1, + completed: false, + text: action.text + } + ] + + case DELETE_TODO: + return state.filter(todo => + todo.id !== action.id + ) + + case EDIT_TODO: + return state.map(todo => + todo.id === action.id ? + { ...todo, text: action.text } : + todo + ) + + case COMPLETE_TODO: + return state.map(todo => + todo.id === action.id ? + { ...todo, completed: !todo.completed } : + todo + ) + + case COMPLETE_ALL: + const areAllMarked = state.every(todo => todo.completed) + return state.map(todo => ({ + ...todo, + completed: !areAllMarked + })) + + case CLEAR_COMPLETED: + return state.filter(todo => todo.completed === false) + + default: + return state + } +} diff --git a/third_party/webkit/PerformanceTests/Speedometer/resources/todomvc/architecture-examples/react-redux/src/reducers/todos.spec.js b/third_party/webkit/PerformanceTests/Speedometer/resources/todomvc/architecture-examples/react-redux/src/reducers/todos.spec.js new file mode 100644 index 0000000000..1ddded36fb --- /dev/null +++ b/third_party/webkit/PerformanceTests/Speedometer/resources/todomvc/architecture-examples/react-redux/src/reducers/todos.spec.js @@ -0,0 +1,284 @@ +import todos from './todos' +import * as types from '../constants/ActionTypes' + +describe('todos reducer', () => { + it('should handle initial state', () => { + expect( + todos(undefined, {}) + ).toEqual([ + { + text: 'Use Redux', + completed: false, + id: 0 + } + ]) + }) + + it('should handle ADD_TODO', () => { + expect( + todos([], { + type: types.ADD_TODO, + text: 'Run the tests' + }) + ).toEqual([ + { + text: 'Run the tests', + completed: false, + id: 0 + } + ]) + + expect( + todos([ + { + text: 'Use Redux', + completed: false, + id: 0 + } + ], { + type: types.ADD_TODO, + text: 'Run the tests' + }) + ).toEqual([ + { + text: 'Run the tests', + completed: false, + id: 1 + }, { + text: 'Use Redux', + completed: false, + id: 0 + } + ]) + + expect( + todos([ + { + text: 'Run the tests', + completed: false, + id: 1 + }, { + text: 'Use Redux', + completed: false, + id: 0 + } + ], { + type: types.ADD_TODO, + text: 'Fix the tests' + }) + ).toEqual([ + { + text: 'Fix the tests', + completed: false, + id: 2 + }, { + text: 'Run the tests', + completed: false, + id: 1 + }, { + text: 'Use Redux', + completed: false, + id: 0 + } + ]) + }) + + it('should handle DELETE_TODO', () => { + expect( + todos([ + { + text: 'Run the tests', + completed: false, + id: 1 + }, { + text: 'Use Redux', + completed: false, + id: 0 + } + ], { + type: types.DELETE_TODO, + id: 1 + }) + ).toEqual([ + { + text: 'Use Redux', + completed: false, + id: 0 + } + ]) + }) + + it('should handle EDIT_TODO', () => { + expect( + todos([ + { + text: 'Run the tests', + completed: false, + id: 1 + }, { + text: 'Use Redux', + completed: false, + id: 0 + } + ], { + type: types.EDIT_TODO, + text: 'Fix the tests', + id: 1 + }) + ).toEqual([ + { + text: 'Fix the tests', + completed: false, + id: 1 + }, { + text: 'Use Redux', + completed: false, + id: 0 + } + ]) + }) + + it('should handle COMPLETE_TODO', () => { + expect( + todos([ + { + text: 'Run the tests', + completed: false, + id: 1 + }, { + text: 'Use Redux', + completed: false, + id: 0 + } + ], { + type: types.COMPLETE_TODO, + id: 1 + }) + ).toEqual([ + { + text: 'Run the tests', + completed: true, + id: 1 + }, { + text: 'Use Redux', + completed: false, + id: 0 + } + ]) + }) + + it('should handle COMPLETE_ALL', () => { + expect( + todos([ + { + text: 'Run the tests', + completed: true, + id: 1 + }, { + text: 'Use Redux', + completed: false, + id: 0 + } + ], { + type: types.COMPLETE_ALL + }) + ).toEqual([ + { + text: 'Run the tests', + completed: true, + id: 1 + }, { + text: 'Use Redux', + completed: true, + id: 0 + } + ]) + + // Unmark if all todos are currently completed + expect( + todos([ + { + text: 'Run the tests', + completed: true, + id: 1 + }, { + text: 'Use Redux', + completed: true, + id: 0 + } + ], { + type: types.COMPLETE_ALL + }) + ).toEqual([ + { + text: 'Run the tests', + completed: false, + id: 1 + }, { + text: 'Use Redux', + completed: false, + id: 0 + } + ]) + }) + + it('should handle CLEAR_COMPLETED', () => { + expect( + todos([ + { + text: 'Run the tests', + completed: true, + id: 1 + }, { + text: 'Use Redux', + completed: false, + id: 0 + } + ], { + type: types.CLEAR_COMPLETED + }) + ).toEqual([ + { + text: 'Use Redux', + completed: false, + id: 0 + } + ]) + }) + + it('should not generate duplicate ids after CLEAR_COMPLETED', () => { + expect( + [ + { + type: types.COMPLETE_TODO, + id: 0 + }, { + type: types.CLEAR_COMPLETED + }, { + type: types.ADD_TODO, + text: 'Write more tests' + } + ].reduce(todos, [ + { + id: 0, + completed: false, + text: 'Use Redux' + }, { + id: 1, + completed: false, + text: 'Write tests' + } + ]) + ).toEqual([ + { + text: 'Write more tests', + completed: false, + id: 2 + }, { + text: 'Write tests', + completed: false, + id: 1 + } + ]) + }) +}) |