git ssb

0+

Piet / ssb-loomio



Commit 870e2cb73a3e01c6b5810eaa4f9e264b5b974d03

Passes test. TDD is neat.

Piet Geursen committed on 3/6/2018, 2:06:34 AM
Parent: 7a98d36437dd937d8d7daa45f726732c78c2ea33

Files changed

position/sync/chooseOneResults.jschanged
test/position/sync/chooseOneResults.test.jschanged
position/sync/chooseOneResults.jsView
@@ -5,8 +5,13 @@
55 module.exports = function ({positions, poll}) { //postions must be of the correct type ie checked by the caller.
66 return positions.reduce(function (results, position) {
77 var { choice } = Position(position).positionDetails
88
9+ if (choice >= poll.pollDetails.choices.length) {
10+ results.errors.invalidPositions.push(position)
11+ return results
12+ }
13+
914 if (!isArray(results[choice])) {
1015 results[choice] = []
1116 }
1217 results[choice].push(position.value.author)
test/position/sync/chooseOneResults.test.jsView
@@ -1,6 +1,7 @@
11 const test = require('tape')
22 const ChooseOne = require('../../../position/sync/chooseOne')
3+const ChooseOnePoll = require('../../../poll/sync/chooseOne')
34 const Position = require('../../../position/sync/position')
45 const chooseOneResults = require('../../../position/sync/chooseOneResults')
56
67 const pietId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/0=.ed25519'
@@ -11,8 +12,14 @@
1112 const sallyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/5=.ed25519'
1213
1314 const poll = '%t+PhrNxxXkw/jMo6mnwUWfFjJapoPWxzsQoe0Np+nYw=.sha256'
1415
16+const validPoll = ChooseOnePoll({
17+ choices: [1, 2, 'three'],
18+ title: 'how many food',
19+ closesAt: Date.now()
20+})
21+
1522 test('Position - ChooseOneResults', function (t) {
1623 const positions = [
1724 { value: { content: Position(ChooseOne({choice: 0, poll})), author: pietId } },
1825 { value: { content: Position(ChooseOne({choice: 0, poll})), author: mixId } },
@@ -21,21 +28,16 @@
2128 { value: { content: Position(ChooseOne({choice: 1, poll})), author: tommyId } },
2229 { value: { content: Position(ChooseOne({choice: 2, poll})), author: sallyId } }
2330 ]
2431
25- const actual = chooseOneResults({positions})
32+ const actual = chooseOneResults({positions, poll: validPoll})
2633 t.deepEqual(actual[0], [pietId, mixId, mikeyId], 'correct voters for choice 0')
2734 t.deepEqual(actual[1], [timmyId, tommyId], 'correct voters for choice 1')
2835 t.deepEqual(actual[2], [sallyId], 'correct voters for choice 2')
2936 t.end()
3037 })
3138
3239 test('Position - a position stated for an invalid choice index is not counted', function(t) {
33- var validPoll = ChooseOne({
34- choices: [1, 2, 'three'],
35- title: 'how many food',
36- closesAt: Date.now()
37- })
3840
3941 const positions = [
4042 { value: { content: Position(ChooseOne({choice: 3, poll})), author: pietId } }
4143 ]
@@ -44,10 +46,15 @@
4446 t.false(actual[3], 'invalid vote is not counted')
4547 t.end()
4648 })
4749
48-test.skip('Position - a position stated for an invalid choice index is included in the errors object', function(t) {
50+test('Position - a position stated for an invalid choice index is included in the errors object', function(t) {
51+ const positions = [
52+ { value: { content: Position(ChooseOne({choice: 3, poll})), author: pietId } }
53+ ]
4954
55+ const actual = chooseOneResults({positions, poll: validPoll})
56+ t.deepEqual(actual.errors.invalidPositions[0], positions[0], 'invalid vote is on error object')
5057 t.end()
5158 })
5259
5360 test.skip('Position - A position stated after the closing time of the poll is not counted', function(t) {

Built with git-ssb-web