mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-14 05:14:28 -08:00
redux example init
This commit is contained in:
8
docs/examples/todos/src/reducers/index.js
Normal file
8
docs/examples/todos/src/reducers/index.js
Normal file
@@ -0,0 +1,8 @@
|
||||
import { combineReducers } from 'redux'
|
||||
import todos from './todos'
|
||||
import visibilityFilter from './visibilityFilter'
|
||||
|
||||
export default combineReducers({
|
||||
todos,
|
||||
visibilityFilter
|
||||
})
|
||||
23
docs/examples/todos/src/reducers/todos.js
Normal file
23
docs/examples/todos/src/reducers/todos.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const todos = (state = [], action) => {
|
||||
switch (action.type) {
|
||||
case 'ADD_TODO':
|
||||
return [
|
||||
...state,
|
||||
{
|
||||
id: action.id,
|
||||
text: action.text,
|
||||
completed: false
|
||||
}
|
||||
]
|
||||
case 'TOGGLE_TODO':
|
||||
return state.map(todo =>
|
||||
(todo.id === action.id)
|
||||
? {...todo, completed: !todo.completed}
|
||||
: todo
|
||||
)
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
|
||||
export default todos
|
||||
111
docs/examples/todos/src/reducers/todos.spec.js
Normal file
111
docs/examples/todos/src/reducers/todos.spec.js
Normal file
@@ -0,0 +1,111 @@
|
||||
import todos from './todos'
|
||||
|
||||
describe('todos reducer', () => {
|
||||
it('should handle initial state', () => {
|
||||
expect(
|
||||
todos(undefined, {})
|
||||
).toEqual([])
|
||||
})
|
||||
|
||||
it('should handle ADD_TODO', () => {
|
||||
expect(
|
||||
todos([], {
|
||||
type: 'ADD_TODO',
|
||||
text: 'Run the tests',
|
||||
id: 0
|
||||
})
|
||||
).toEqual([
|
||||
{
|
||||
text: 'Run the tests',
|
||||
completed: false,
|
||||
id: 0
|
||||
}
|
||||
])
|
||||
|
||||
expect(
|
||||
todos([
|
||||
{
|
||||
text: 'Run the tests',
|
||||
completed: false,
|
||||
id: 0
|
||||
}
|
||||
], {
|
||||
type: 'ADD_TODO',
|
||||
text: 'Use Redux',
|
||||
id: 1
|
||||
})
|
||||
).toEqual([
|
||||
{
|
||||
text: 'Run the tests',
|
||||
completed: false,
|
||||
id: 0
|
||||
}, {
|
||||
text: 'Use Redux',
|
||||
completed: false,
|
||||
id: 1
|
||||
}
|
||||
])
|
||||
|
||||
expect(
|
||||
todos([
|
||||
{
|
||||
text: 'Run the tests',
|
||||
completed: false,
|
||||
id: 0
|
||||
}, {
|
||||
text: 'Use Redux',
|
||||
completed: false,
|
||||
id: 1
|
||||
}
|
||||
], {
|
||||
type: 'ADD_TODO',
|
||||
text: 'Fix the tests',
|
||||
id: 2
|
||||
})
|
||||
).toEqual([
|
||||
{
|
||||
text: 'Run the tests',
|
||||
completed: false,
|
||||
id: 0
|
||||
}, {
|
||||
text: 'Use Redux',
|
||||
completed: false,
|
||||
id: 1
|
||||
}, {
|
||||
text: 'Fix the tests',
|
||||
completed: false,
|
||||
id: 2
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
it('should handle TOGGLE_TODO', () => {
|
||||
expect(
|
||||
todos([
|
||||
{
|
||||
text: 'Run the tests',
|
||||
completed: false,
|
||||
id: 1
|
||||
}, {
|
||||
text: 'Use Redux',
|
||||
completed: false,
|
||||
id: 0
|
||||
}
|
||||
], {
|
||||
type: 'TOGGLE_TODO',
|
||||
id: 1
|
||||
})
|
||||
).toEqual([
|
||||
{
|
||||
text: 'Run the tests',
|
||||
completed: true,
|
||||
id: 1
|
||||
}, {
|
||||
text: 'Use Redux',
|
||||
completed: false,
|
||||
id: 0
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
})
|
||||
12
docs/examples/todos/src/reducers/visibilityFilter.js
Normal file
12
docs/examples/todos/src/reducers/visibilityFilter.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import { VisibilityFilters } from '../actions'
|
||||
|
||||
const visibilityFilter = (state = VisibilityFilters.SHOW_ALL, action) => {
|
||||
switch (action.type) {
|
||||
case 'SET_VISIBILITY_FILTER':
|
||||
return action.filter
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
|
||||
export default visibilityFilter
|
||||
Reference in New Issue
Block a user