Skip to content

Commit

Permalink
Add tests for tree-view example
Browse files Browse the repository at this point in the history
  • Loading branch information
CrocoDillon committed Jan 30, 2016
1 parent c738ac5 commit e6cbc32
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 1 deletion.
5 changes: 4 additions & 1 deletion examples/tree-view/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"version": "0.0.0",
"description": "Redux tree-view example",
"scripts": {
"start": "node server.js"
"start": "node server.js",
"test": "NODE_ENV=test mocha --recursive --compilers js:babel-core/register",
"test:watch": "npm test -- --watch"
},
"repository": {
"type": "git",
Expand All @@ -24,6 +26,7 @@
"babel-core": "^5.6.18",
"babel-loader": "^5.1.4",
"babel-plugin-react-transform": "^1.1.0",
"deep-freeze": "0.0.1",
"expect": "^1.6.0",
"express": "^4.13.3",
"jsdom": "^5.6.1",
Expand Down
5 changes: 5 additions & 0 deletions examples/tree-view/test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"mocha": true
}
}
164 changes: 164 additions & 0 deletions examples/tree-view/test/reducer.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import expect from 'expect'
import deepFreeze from 'deep-freeze'
import reducer from '../reducers'
import { increment, createNode, deleteNode, addChild, removeChild } from '../actions'

describe('reducer', () => {
it('should provide the initial state', () => {
expect(reducer(undefined, {})).toEqual({})
})

it('should handle INCREMENT action', () => {
const stateBefore = {
'node_0': {
id: 'node_0',
counter: 0,
childIds: []
}
}
const action = increment('node_0')
const stateAfter = {
'node_0': {
id: 'node_0',
counter: 1,
childIds: []
}
}

deepFreeze(stateBefore)
deepFreeze(action)

expect(reducer(stateBefore, action)).toEqual(stateAfter)
})

it('should handle CREATE_NODE action', () => {
const stateBefore = {}
const action = createNode()
const stateAfter = {
[action.nodeId]: {
id: action.nodeId,
counter: 0,
childIds: []
}
}

deepFreeze(stateBefore)
deepFreeze(action)

expect(reducer(stateBefore, action)).toEqual(stateAfter)
})

it('should handle DELETE_NODE action', () => {
const stateBefore = {
'node_0': {
id: 'node_0',
counter: 0,
childIds: [ 'node_1' ]
},
'node_1': {
id: 'node_1',
counter: 0,
childIds: []
},
'node_2': {
id: 'node_2',
counter: 0,
childIds: [ 'node_3', 'node_4' ]
},
'node_3': {
id: 'node_3',
counter: 0,
childIds: []
},
'node_4': {
id: 'node_4',
counter: 0,
childIds: []
}
}
const action = deleteNode('node_2')
const stateAfter = {
'node_0': {
id: 'node_0',
counter: 0,
childIds: [ 'node_1' ]
},
'node_1': {
id: 'node_1',
counter: 0,
childIds: []
}
}

deepFreeze(stateBefore)
deepFreeze(action)

expect(reducer(stateBefore, action)).toEqual(stateAfter)
})

it('should handle ADD_CHILD action', () => {
const stateBefore = {
'node_0': {
id: 'node_0',
counter: 0,
childIds: []
},
'node_1': {
id: 'node_1',
counter: 0,
childIds: []
}
}
const action = addChild('node_0', 'node_1')
const stateAfter = {
'node_0': {
id: 'node_0',
counter: 0,
childIds: [ 'node_1' ]
},
'node_1': {
id: 'node_1',
counter: 0,
childIds: []
}
}

deepFreeze(stateBefore)
deepFreeze(action)

expect(reducer(stateBefore, action)).toEqual(stateAfter)
})

it('should handle REMOVE_CHILD action', () => {
const stateBefore = {
'node_0': {
id: 'node_0',
counter: 0,
childIds: [ 'node_1' ]
},
'node_1': {
id: 'node_1',
counter: 0,
childIds: []
}
}
const action = removeChild('node_0', 'node_1')
const stateAfter = {
'node_0': {
id: 'node_0',
counter: 0,
childIds: []
},
'node_1': {
id: 'node_1',
counter: 0,
childIds: []
}
}

deepFreeze(stateBefore)
deepFreeze(action)

expect(reducer(stateBefore, action)).toEqual(stateAfter)
})
})

0 comments on commit e6cbc32

Please sign in to comment.