Commit 6730668ae7223a39f319ed33a69019c0ef1d8c38
Passes test
Piet Geursen committed on 3/9/2018, 4:20:29 AMParent: 5caf2c17fe0bf657ce2cbd15e624b51c8176b2e4
Files changed
position/sync/chooseOneResults.js | changed |
test/position/sync/chooseOneResults.test.js | changed |
position/sync/chooseOneResults.js | ||
---|---|---|
@@ -38,14 +38,25 @@ | ||
38 | 38 … | acc.errors.push(PositionLateError({position})) |
39 | 39 … | return acc |
40 | 40 … | } |
41 | 41 … | |
42 … | + deleteExistingVotesByAuthor({results: acc.results, author}) | |
42 | 43 … | acc.results[choice].voters[author] = position |
43 | 44 … | |
44 | 45 … | return acc |
45 | 46 … | }, {errors: [], results}) |
46 | 47 … | } |
47 | 48 … | |
49 … | +// !!! assumes these are already sorted by time. | |
50 … | +// modifies results passed in | |
51 … | +function deleteExistingVotesByAuthor ({author, results}) { | |
52 … | + results.forEach(result => { | |
53 … | + if (result.voters[author]) { | |
54 … | + delete result.voters[author] | |
55 … | + } | |
56 … | + }) | |
57 … | +} | |
58 … | + | |
48 | 59 … | function isInvalidChoice ({position, poll}) { |
49 | 60 … | const { choice } = position.value.content.details |
50 | 61 … | return choice >= poll.value.content.details.choices.length |
51 | 62 … | } |
test/position/sync/chooseOneResults.test.js | ||
---|---|---|
@@ -116,4 +116,16 @@ | ||
116 | 116 … | const actual = chooseOneResults({positions, poll: validPoll}) |
117 | 117 … | t.deepEqual(actual.errors[0].type, ERROR_POSITION_LATE, 'invalid vote is on error object') |
118 | 118 … | t.end() |
119 | 119 … | }) |
120 … | + | |
121 … | +test('ChooseOneResults - ChooseOneResults only counts latest vote by an author', function (t) { | |
122 … | + const positions = [ | |
123 … | + { value: { content: ChooseOne({choice: 2, poll}), author: pietId } }, | |
124 … | + { value: { content: ChooseOne({choice: 0, poll}), author: pietId } } | |
125 … | + ] | |
126 … | + | |
127 … | + const actual = chooseOneResults({positions, poll: validPoll}) | |
128 … | + t.false(actual.results[2].voters[pietId], 'old vote is deleted') | |
129 … | + t.true(actual.results[0].voters[pietId], 'new vote is counted') | |
130 … | + t.end() | |
131 … | +}) |
Built with git-ssb-web