Permalink
Browse files

restart functionality

  • Loading branch information...
teropa committed Sep 6, 2015
1 parent 3371786 commit 8dbdc663050d74b547018a8a0d2c47b26f5d45a7
Showing with 57 additions and 11 deletions.
  1. +15 −3 src/core.js
  2. +3 −1 src/reducer.js
  3. +31 −3 test/core_spec.js
  4. +6 −3 test/reducer_spec.js
  5. +2 −1 test/store_spec.js
View
@@ -3,7 +3,9 @@ import {List, Map} from 'immutable';
export const INITIAL_STATE = Map();
export function setEntries(state, entries) {
return state.set('entries', List(entries));
const list = List(entries);
return state.set('entries', list)
.set('initialEntries', list);
}
function getWinners(vote) {
@@ -16,7 +18,7 @@ function getWinners(vote) {
else return [one, two];
}
export function next(state) {
export function next(state, round = state.getIn(['vote', 'round'], 0)) {
const entries = state.get('entries')
.concat(getWinners(state.get('vote')));
if (entries.size === 1) {
@@ -26,14 +28,24 @@ export function next(state) {
} else {
return state.merge({
vote: Map({
round: state.getIn(['vote', 'round'], 0) + 1,
round: round + 1,
pair: entries.take(2)
}),
entries: entries.skip(2)
});
}
}
export function restart(state) {
const round = state.getIn(['vote', 'round'], 0);
return next(
state.set('entries', state.get('initialEntries'))
.remove('vote')
.remove('winner'),
round
);
}
function removePreviousVote(voteState, voter) {
const previousVote = voteState.getIn(['votes', voter]);
if (previousVote) {
View
@@ -1,11 +1,13 @@
import {setEntries, next, vote, INITIAL_STATE} from './core';
import {setEntries, next, restart, vote, INITIAL_STATE} from './core';
export default function reducer(state = INITIAL_STATE, action) {
switch (action.type) {
case 'SET_ENTRIES':
return setEntries(state, action.entries);
case 'NEXT':
return next(state);
case 'RESTART':
return restart(state);
case 'VOTE':
return state.update('vote',
voteState => vote(voteState, action.entry, action.clientId));
View
@@ -1,7 +1,7 @@
import {List, Map} from 'immutable';
import {expect} from 'chai';
import {setEntries, next, vote} from '../src/core';
import {setEntries, next, vote, restart} from '../src/core';
describe('application logic', () => {
@@ -12,7 +12,8 @@ describe('application logic', () => {
const entries = List.of('Trainspotting', '28 Days Later');
const nextState = setEntries(state, entries);
expect(nextState).to.equal(Map({
entries: List.of('Trainspotting', '28 Days Later')
entries: List.of('Trainspotting', '28 Days Later'),
initialEntries: List.of('Trainspotting', '28 Days Later')
}));
});
@@ -21,7 +22,8 @@ describe('application logic', () => {
const entries = ['Trainspotting', '28 Days Later'];
const nextState = setEntries(state, entries);
expect(nextState).to.equal(Map({
entries: List.of('Trainspotting', '28 Days Later')
entries: List.of('Trainspotting', '28 Days Later'),
initialEntries: List.of('Trainspotting', '28 Days Later')
}));
});
@@ -113,6 +115,32 @@ describe('application logic', () => {
});
describe('restart', () => {
it('returns to initial entries and takes the first two entries under vote', () => {
expect(
restart(Map({
vote: Map({
round: 1,
pair: List.of('Trainspotting', 'Sunshine')
}),
entries: List(),
initialEntries: List.of('Trainspotting', '28 Days Later', 'Sunshine')
}))
).to.equal(
Map({
vote: Map({
round: 2,
pair: List.of('Trainspotting', '28 Days Later')
}),
entries: List.of('Sunshine'),
initialEntries: List.of('Trainspotting', '28 Days Later', 'Sunshine')
})
);
});
});
describe('vote', () => {
it('creates a tally for the voted entry', () => {
View
@@ -11,7 +11,8 @@ describe('reducer', () => {
const nextState = reducer(initialState, action);
expect(nextState).to.equal(fromJS({
entries: ['Trainspotting']
entries: ['Trainspotting'],
initialEntries: ['Trainspotting']
}));
});
@@ -59,7 +60,8 @@ describe('reducer', () => {
const action = {type: 'SET_ENTRIES', entries: ['Trainspotting']};
const nextState = reducer(undefined, action);
expect(nextState).to.equal(fromJS({
entries: ['Trainspotting']
entries: ['Trainspotting'],
initialEntries: ['Trainspotting']
}));
});
@@ -75,7 +77,8 @@ describe('reducer', () => {
const finalState = actions.reduce(reducer, Map());
expect(finalState).to.equal(fromJS({
winner: 'Trainspotting'
winner: 'Trainspotting',
initialEntries: ['Trainspotting', '28 Days Later']
}));
});
View
@@ -14,7 +14,8 @@ describe('store', () => {
entries: ['Trainspotting', '28 Days Later']
});
expect(store.getState()).to.equal(fromJS({
entries: ['Trainspotting', '28 Days Later']
entries: ['Trainspotting', '28 Days Later'],
initialEntries: ['Trainspotting', '28 Days Later']
}));
});

0 comments on commit 8dbdc66

Please sign in to comment.