Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start adding tests for creation reducer
- Loading branch information
1 parent
88d3d0b
commit f8400ef
Showing
4 changed files
with
65 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import update from 'react-addons-update'; | ||
|
||
export function creation(state = {available: [], phrase: []}, action) { | ||
switch (action.type) { | ||
|
||
case 'CREATION_INIT': | ||
return Object.assign({}, state, { | ||
available: action.words.map(function (word) { | ||
return word.id; | ||
}) | ||
}); | ||
|
||
case 'CREATION_MOVE_WORD': | ||
if (action.space === 'AVAILABLE' && state.available[action.index] === action.word || | ||
action.space === 'PHRASE' && state.phrase[action.index] === action.word) { | ||
return state; | ||
} | ||
|
||
// could improve performance by not filtering both spaces if word is found in first one | ||
|
||
if (action.space === 'AVAILABLE') { | ||
return Object.assign({}, state, { | ||
available: update(state.available.filter(function (id) { | ||
return id !== action.word; | ||
}), {$splice: [[action.index, 0, action.word]]}), | ||
phrase: state.phrase.filter(function (id) { | ||
return id !== action.word; | ||
}) | ||
}); | ||
} | ||
|
||
if (action.space === 'PHRASE') { | ||
return Object.assign({}, state, { | ||
available: state.available.filter(function (id) { | ||
return id !== action.word; | ||
}), | ||
phrase: update(state.phrase.filter(function (id) { | ||
return id !== action.word; | ||
}), {$splice: [[action.index, 0, action.word]]}) | ||
}); | ||
} | ||
|
||
throw new TypeError('Unknown space for CREATION_MOVE_WORD action'); | ||
|
||
default: | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/*eslint no-unused-expressions:0 dot-notation:0*/ | ||
import {describe, it} from 'mocha'; | ||
import {expect} from 'chai'; | ||
|
||
import {creation} from './creation'; | ||
|
||
describe('creation', () => { | ||
|
||
it('throws a TypeError for unrecognized space', () => { | ||
expect(() => { | ||
creation(undefined, {type: 'CREATION_MOVE_WORD', space: 'INNER_SPACE'}); | ||
}).to.throw(TypeError); | ||
}); | ||
|
||
}); |