Skip to content

Commit

Permalink
implement and test value.reset, value.clear
Browse files Browse the repository at this point in the history
  • Loading branch information
thk2b committed Apr 16, 2018
1 parent c753a33 commit 353f5aa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Value/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ A standard redux reducer to be passed to `redux.combineReducers`.

sets the state to `nextValue`.

### `reset()`

sets the state to its initial state.

### `clear()`

sets the state to `null`.

## Example

`name.js`
Expand Down
18 changes: 17 additions & 1 deletion src/Value/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,30 @@ export default function Value(name, initialState=null){
type: createActionType('set'),
value: nextValue
})
},
clear(){
return ({
type: createActionType('clear')
})
},
reset(){
return ({
type: createActionType('reset')
})
}
}
const reducer = (state=initialState, action) => {
if(!matchInstance(action)) return state
if(action.type.endsWith('reset')){
return initialState
}
if(action.type.endsWith('set')){
return action.value
}
else return state
if(action.type.endsWith('clear')){
return null
}
return state
}

return {
Expand Down
15 changes: 13 additions & 2 deletions src/Value/test/value.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ test('Value', main => {
t.equal( reducer(initialState, actions.set(nextState)), nextState)
t.end()
})
main.test('`reset` action', t => {
const initialState = 9
const { actions, reducer } = Value('test-name-3', initialState)
t.equal( reducer(initialState, actions.reset()), initialState)
t.end()
})
main.test('`clear` action', t => {
const { actions, reducer } = Value('test-name-4')
t.equal( reducer([1, 2, 3], actions.clear()), null)
t.end()
})
main.test('multiple instances', t => {
const n0 = 'name-0'
const n1 = 'name-1'
Expand All @@ -50,8 +61,8 @@ test('Value', main => {
})
main.test('invariants', t => {
t.test('duplicate name', t => {
const v0 = Value('test-name-3')
t.throws(() => Value('test-name-3'), 'should throw when creating an instance with an already existing name')
const v0 = Value('name-3')
t.throws(() => Value('name-3'), 'should throw when creating an instance with an already existing name')
t.end()
})
t.test('undefined name', t => {
Expand Down

0 comments on commit 353f5aa

Please sign in to comment.