Permalink
Browse files

duplicate vote prevention

  • Loading branch information...
teropa committed Sep 6, 2015
1 parent 80b1cd6 commit 33717866792084ec222c214829fbfa315649b7a2
Showing with 68 additions and 15 deletions.
  1. +21 −6 src/core.js
  2. +1 −1 src/reducer.js
  3. +38 −3 test/core_spec.js
  4. +8 −5 test/reducer_spec.js
@@ -34,14 +34,29 @@ export function next(state) {
}
}
export function vote(voteState, entry) {
function removePreviousVote(voteState, voter) {
const previousVote = voteState.getIn(['votes', voter]);
if (previousVote) {
return voteState.updateIn(['tally', previousVote], t => t - 1)
.removeIn(['votes', voter]);
} else {
return voteState;
}
}
function addVote(voteState, entry, voter) {
if (voteState.get('pair').includes(entry)) {
return voteState.updateIn(
['tally', entry],
0,
tally => tally + 1
);
return voteState.updateIn(['tally', entry], 0, t => t + 1)
.setIn(['votes', voter], entry);
} else {
return voteState;
}
}
export function vote(voteState, entry, voter) {
return addVote(
removePreviousVote(voteState, voter),
entry,
voter
);
}
@@ -8,7 +8,7 @@ export default function reducer(state = INITIAL_STATE, action) {
return next(state);
case 'VOTE':
return state.update('vote',
voteState => vote(voteState, action.entry));
voteState => vote(voteState, action.entry, action.clientId));
}
return state;
}
@@ -120,13 +120,16 @@ describe('application logic', () => {
vote(Map({
round: 1,
pair: List.of('Trainspotting', '28 Days Later')
}), 'Trainspotting')
}), 'Trainspotting', 'voter1')
).to.equal(
Map({
round: 1,
pair: List.of('Trainspotting', '28 Days Later'),
tally: Map({
'Trainspotting': 1
}),
votes: Map({
voter1: 'Trainspotting'
})
})
);
@@ -140,15 +143,47 @@ describe('application logic', () => {
tally: Map({
'Trainspotting': 3,
'28 Days Later': 2
})
}), 'Trainspotting')
}),
votes: Map()
}), 'Trainspotting', 'voter1')
).to.equal(
Map({
round: 1,
pair: List.of('Trainspotting', '28 Days Later'),
tally: Map({
'Trainspotting': 4,
'28 Days Later': 2
}),
votes: Map({
voter1: 'Trainspotting'
})
})
);
});
it('nullifies previous vote for the same voter', () => {
expect(
vote(Map({
round: 1,
pair: List.of('Trainspotting', '28 Days Later'),
tally: Map({
'Trainspotting': 3,
'28 Days Later': 2
}),
votes: Map({
voter1: '28 Days Later'
})
}), 'Trainspotting', 'voter1')
).to.equal(
Map({
round: 1,
pair: List.of('Trainspotting', '28 Days Later'),
tally: Map({
'Trainspotting': 4,
'28 Days Later': 1
}),
votes: Map({
voter1: 'Trainspotting'
})
})
);
@@ -39,14 +39,17 @@ describe('reducer', () => {
},
entries: []
});
const action = {type: 'VOTE', entry: 'Trainspotting'};
const action = {type: 'VOTE', entry: 'Trainspotting', clientId: 'voter1'};
const nextState = reducer(initialState, action);
expect(nextState).to.equal(fromJS({
vote: {
round: 1,
pair: ['Trainspotting', '28 Days Later'],
tally: {Trainspotting: 1}
tally: {Trainspotting: 1},
votes: {
voter1: 'Trainspotting'
}
},
entries: []
}));
@@ -64,9 +67,9 @@ describe('reducer', () => {
const actions = [
{type: 'SET_ENTRIES', entries: ['Trainspotting', '28 Days Later']},
{type: 'NEXT'},
{type: 'VOTE', entry: 'Trainspotting'},
{type: 'VOTE', entry: '28 Days Later'},
{type: 'VOTE', entry: 'Trainspotting'},
{type: 'VOTE', entry: 'Trainspotting', clientId: 'voter1'},
{type: 'VOTE', entry: '28 Days Later', clientId: 'voter2'},
{type: 'VOTE', entry: 'Trainspotting', clientId: 'voter3'},
{type: 'NEXT'}
];
const finalState = actions.reduce(reducer, Map());

1 comment on commit 3371786

@mark-stephenson-

This comment has been minimized.

mark-stephenson- commented on 3371786 May 15, 2018

I'm missing something here, but I cant see what. Can you point me in the right direction please?
If I copy and paste your test for the same assertion I get the same error.

  1. application logic
    vote
    ignores the vote if for an invalid entry:

    AssertionError: expected 'Map { "round": 1, "pair": List [ "Trainspotting", "28 Days Later" ], "tally": Map { "Sunshine": 1 }, "votes": Map { undefined: "Sunshine" } }' to equal 'Map { "round": 1, "pair": List [ "Trainspotting", "28 Days Later" ] }'

    • expected - actual

    -Map { "round": 1, "pair": List [ "Trainspotting", "28 Days Later" ], "tally": Map { "Sunshine": 1 }, "votes": Map { undefined: "Sunshine" } }
    +Map { "round": 1, "pair": List [ "Trainspotting", "28 Days Later" ] }

Please sign in to comment.