Skip to content

Commit 8dbdc66

Browse files
committed
restart functionality
1 parent 3371786 commit 8dbdc66

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

src/core.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {List, Map} from 'immutable';
33
export const INITIAL_STATE = Map();
44

55
export function setEntries(state, entries) {
6-
return state.set('entries', List(entries));
6+
const list = List(entries);
7+
return state.set('entries', list)
8+
.set('initialEntries', list);
79
}
810

911
function getWinners(vote) {
@@ -16,7 +18,7 @@ function getWinners(vote) {
1618
else return [one, two];
1719
}
1820

19-
export function next(state) {
21+
export function next(state, round = state.getIn(['vote', 'round'], 0)) {
2022
const entries = state.get('entries')
2123
.concat(getWinners(state.get('vote')));
2224
if (entries.size === 1) {
@@ -26,14 +28,24 @@ export function next(state) {
2628
} else {
2729
return state.merge({
2830
vote: Map({
29-
round: state.getIn(['vote', 'round'], 0) + 1,
31+
round: round + 1,
3032
pair: entries.take(2)
3133
}),
3234
entries: entries.skip(2)
3335
});
3436
}
3537
}
3638

39+
export function restart(state) {
40+
const round = state.getIn(['vote', 'round'], 0);
41+
return next(
42+
state.set('entries', state.get('initialEntries'))
43+
.remove('vote')
44+
.remove('winner'),
45+
round
46+
);
47+
}
48+
3749
function removePreviousVote(voteState, voter) {
3850
const previousVote = voteState.getIn(['votes', voter]);
3951
if (previousVote) {

src/reducer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import {setEntries, next, vote, INITIAL_STATE} from './core';
1+
import {setEntries, next, restart, vote, INITIAL_STATE} from './core';
22

33
export default function reducer(state = INITIAL_STATE, action) {
44
switch (action.type) {
55
case 'SET_ENTRIES':
66
return setEntries(state, action.entries);
77
case 'NEXT':
88
return next(state);
9+
case 'RESTART':
10+
return restart(state);
911
case 'VOTE':
1012
return state.update('vote',
1113
voteState => vote(voteState, action.entry, action.clientId));

test/core_spec.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {List, Map} from 'immutable';
22
import {expect} from 'chai';
33

4-
import {setEntries, next, vote} from '../src/core';
4+
import {setEntries, next, vote, restart} from '../src/core';
55

66
describe('application logic', () => {
77

@@ -12,7 +12,8 @@ describe('application logic', () => {
1212
const entries = List.of('Trainspotting', '28 Days Later');
1313
const nextState = setEntries(state, entries);
1414
expect(nextState).to.equal(Map({
15-
entries: List.of('Trainspotting', '28 Days Later')
15+
entries: List.of('Trainspotting', '28 Days Later'),
16+
initialEntries: List.of('Trainspotting', '28 Days Later')
1617
}));
1718
});
1819

@@ -21,7 +22,8 @@ describe('application logic', () => {
2122
const entries = ['Trainspotting', '28 Days Later'];
2223
const nextState = setEntries(state, entries);
2324
expect(nextState).to.equal(Map({
24-
entries: List.of('Trainspotting', '28 Days Later')
25+
entries: List.of('Trainspotting', '28 Days Later'),
26+
initialEntries: List.of('Trainspotting', '28 Days Later')
2527
}));
2628
});
2729

@@ -113,6 +115,32 @@ describe('application logic', () => {
113115

114116
});
115117

118+
describe('restart', () => {
119+
120+
it('returns to initial entries and takes the first two entries under vote', () => {
121+
expect(
122+
restart(Map({
123+
vote: Map({
124+
round: 1,
125+
pair: List.of('Trainspotting', 'Sunshine')
126+
}),
127+
entries: List(),
128+
initialEntries: List.of('Trainspotting', '28 Days Later', 'Sunshine')
129+
}))
130+
).to.equal(
131+
Map({
132+
vote: Map({
133+
round: 2,
134+
pair: List.of('Trainspotting', '28 Days Later')
135+
}),
136+
entries: List.of('Sunshine'),
137+
initialEntries: List.of('Trainspotting', '28 Days Later', 'Sunshine')
138+
})
139+
);
140+
});
141+
142+
});
143+
116144
describe('vote', () => {
117145

118146
it('creates a tally for the voted entry', () => {

test/reducer_spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('reducer', () => {
1111
const nextState = reducer(initialState, action);
1212

1313
expect(nextState).to.equal(fromJS({
14-
entries: ['Trainspotting']
14+
entries: ['Trainspotting'],
15+
initialEntries: ['Trainspotting']
1516
}));
1617
});
1718

@@ -59,7 +60,8 @@ describe('reducer', () => {
5960
const action = {type: 'SET_ENTRIES', entries: ['Trainspotting']};
6061
const nextState = reducer(undefined, action);
6162
expect(nextState).to.equal(fromJS({
62-
entries: ['Trainspotting']
63+
entries: ['Trainspotting'],
64+
initialEntries: ['Trainspotting']
6365
}));
6466
});
6567

@@ -75,7 +77,8 @@ describe('reducer', () => {
7577
const finalState = actions.reduce(reducer, Map());
7678

7779
expect(finalState).to.equal(fromJS({
78-
winner: 'Trainspotting'
80+
winner: 'Trainspotting',
81+
initialEntries: ['Trainspotting', '28 Days Later']
7982
}));
8083
});
8184

test/store_spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ describe('store', () => {
1414
entries: ['Trainspotting', '28 Days Later']
1515
});
1616
expect(store.getState()).to.equal(fromJS({
17-
entries: ['Trainspotting', '28 Days Later']
17+
entries: ['Trainspotting', '28 Days Later'],
18+
initialEntries: ['Trainspotting', '28 Days Later']
1819
}));
1920
});
2021

0 commit comments

Comments
 (0)