Skip to content

Commit

Permalink
start adding tests for creation reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
bengladwell committed Nov 1, 2016
1 parent 88d3d0b commit f8400ef
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 50 deletions.
3 changes: 2 additions & 1 deletion app/app.js
Expand Up @@ -6,7 +6,8 @@ import { createStore, combineReducers } from 'redux';
import Firebase from 'firebase';

import config from '../config';
import { words, creation, user as userReducer, activePhraseIndex, people } from './lib/reducers';
import { words, user as userReducer, activePhraseIndex, people } from './lib/reducers';
import { creation } from './lib/reducers/creation';
import { isPlaying } from './lib/reducers/isPlaying';
import { phrases } from './lib/reducers/phrases';
import Routes from './lib/Routes';
Expand Down
49 changes: 0 additions & 49 deletions app/lib/reducers.js
@@ -1,5 +1,3 @@
import update from 'react-addons-update';

/* state tree: {
* words: [ {id: backend_id, text: word}, ... ],
*
Expand Down Expand Up @@ -83,50 +81,3 @@ export function words(state = [], action) {
return state;
}
}

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;
}
}
48 changes: 48 additions & 0 deletions app/lib/reducers/creation.js
@@ -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;
}
}
15 changes: 15 additions & 0 deletions app/lib/reducers/creation.test.js
@@ -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);
});

});

0 comments on commit f8400ef

Please sign in to comment.