Skip to content

Commit

Permalink
restart functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
teropa committed Sep 10, 2015
1 parent 3371786 commit 8dbdc66
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
18 changes: 15 additions & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/reducer.js
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
34 changes: 31 additions & 3 deletions test/core_spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {

Expand All @@ -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')
}));
});

Expand All @@ -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')
}));
});

Expand Down Expand Up @@ -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', () => {
Expand Down
9 changes: 6 additions & 3 deletions test/reducer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('reducer', () => {
const nextState = reducer(initialState, action);

expect(nextState).to.equal(fromJS({
entries: ['Trainspotting']
entries: ['Trainspotting'],
initialEntries: ['Trainspotting']
}));
});

Expand Down Expand Up @@ -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']
}));
});

Expand All @@ -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']
}));
});

Expand Down
3 changes: 2 additions & 1 deletion test/store_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
}));
});

Expand Down

0 comments on commit 8dbdc66

Please sign in to comment.