git ssb

0+

Piet / ssb-loomio



Commit 92693260135502dac86a07eef9c477fb0198568a

Pass all the tests

Piet Geursen committed on 3/9/2018, 12:28:12 AM
Parent: be17b27d1ff6966e3d7d922f5e612f4d2477dce4

Files changed

position/sync/chooseOneResults.jschanged
test/poll/async/get.test.jschanged
test/position/sync/chooseOneResults.test.jschanged
position/sync/chooseOneResults.jsView
@@ -1,5 +1,6 @@
1-const isArray = require('isarray')
1 +var getMsgContent = require('../../lib/getMsgContent')
2 +var ChooseOne = require('../../poll/sync/chooseOne')
23 const PositionChoiceError = require('../../errors/sync/positionChoiceError')
34 const PositionLateError = require('../../errors/sync/positionLateError')
45
56 // Expects `poll` and `position` objects passed in to be of shape:
@@ -13,39 +14,36 @@
1314 // }
1415 //
1516 // postions must be of the correct type ie: type checked by the caller.
1617 module.exports = function chooseOneResults ({positions, poll}) {
17- return positions.reduce(function (results, position) {
18 + var results = getMsgContent(poll)
19 + .pollDetails
20 + .choices
21 + .map(choice => {
22 + return {
23 + choice,
24 + voters: {}
25 + }
26 + })
27 +
28 + return positions.reduce(function (acc, position) {
1829 const { author, content } = position.value
1930 const { choice } = content.positionDetails
2031
2132 if (isInvalidChoice({position, poll})) {
22- results.errors.push(PositionChoiceError({position}))
23- return results
33 + acc.errors.push(PositionChoiceError({position}))
34 + return acc
2435 }
2536
2637 if (isPositionLate({position, poll})) {
27- results.errors.push(PositionLateError({position}))
28- return results
38 + acc.errors.push(PositionLateError({position}))
39 + return acc
2940 }
3041
31- // TODO convert from Array to Object
32- // {
33- // 'kea': {
34- // @piet: 'because things'
35- // },
36- // 'hermit crab': {
37- // @katie: 'scuttz..',
38- // @mix: 'what she said'
39- // } ]
40- // }
41- if (!isArray(results[choice])) {
42- results[choice] = []
43- }
44- results[choice].push(author)
42 + acc.results[choice].voters[author] = position
4543
46- return results
47- }, {errors: []})
44 + return acc
45 + }, {errors: [], results})
4846 }
4947
5048 function isInvalidChoice ({position, poll}) {
5149 const { choice } = position.value.content.positionDetails
test/poll/async/get.test.jsView
@@ -48,13 +48,10 @@
4848 t.equal(data.title, poll.value.content.title, 'has title')
4949
5050 t.equal(data.positions.length, 2, 'has positions')
5151
52- t.deepEqual(data.results, {
53- 1: [katie.id], // TODO update this data structure
54- 2: [piet.id],
55- errors: [] // TODO prune this later
56- }, 'has results!')
52 + t.ok(data.results.results[1].voters[katie.id])
53 + t.ok(data.results.results[2].voters[piet.id])
5754
5855 server.close()
5956 t.end()
6057 })
test/position/sync/chooseOneResults.test.jsView
@@ -1,9 +1,9 @@
11 const test = require('tape')
22 const ChooseOne = require('../../../position/sync/chooseOne')
33 const ChooseOnePoll = require('../../../poll/sync/chooseOne')
44 const chooseOneResults = require('../../../position/sync/chooseOneResults')
5-const {ERROR_POSITION_CHOICE, ERROR_POSITION_TYPE, ERROR_POSITION_LATE} = require('../../../types')
5 +const {ERROR_POSITION_CHOICE, ERROR_POSITION_LATE} = require('../../../types')
66
77 const pietId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/0=.ed25519'
88 const mixId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/1=.ed25519'
99 const mikeyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/2=.ed25519'
@@ -38,17 +38,17 @@
3838
3939 const expected = {
4040 results: [
4141 {
42- choice: '1',
42 + choice: 1,
4343 voters: {
4444 [pietId]: positions[0],
4545 [mixId]: positions[1],
4646 [mikeyId]: positions[2]
4747 }
4848 },
4949 {
50- choice: '2',
50 + choice: 2,
5151 voters: {
5252 [timmyId]: positions[3],
5353 [tommyId]: positions[4]
5454 }
@@ -73,9 +73,9 @@
7373 { value: { content: ChooseOne({choice: 3, poll}), author: pietId } }
7474 ]
7575
7676 const actual = chooseOneResults({positions, poll: validPoll})
77- t.false(actual[3], 'invalid vote is not counted')
77 + t.false(actual.results[3], 'invalid vote is not counted')
7878 t.end()
7979 })
8080
8181 test('ChooseOneResults - a position stated for an invalid choice index is included in the errors object', function (t) {
@@ -93,9 +93,9 @@
9393 { value: { content: ChooseOne({choice: 0, poll}), author: pietId, timestamp: now - 1 } }
9494 ]
9595
9696 const actual = chooseOneResults({positions, poll: validPoll})
97- t.true(actual[0], 'valid vote is counted')
97 + t.true(actual.results[0], 'valid vote is counted')
9898 t.end()
9999 })
100100
101101 test('ChooseOneResults - A position stated after the closing time of the poll is not counted', function (t) {
@@ -103,9 +103,9 @@
103103 { value: { content: ChooseOne({choice: 0, poll}), author: pietId, timestamp: now + 1 } }
104104 ]
105105
106106 const actual = chooseOneResults({positions, poll: validPoll})
107- t.false(actual[0], 'invalid vote is not counted')
107 + t.deepEqual(actual.results[0].voters, {}, 'invalid vote is not counted')
108108 t.end()
109109 })
110110
111111 test('ChooseOneResults - A position stated after the closing time of the poll is included in the error object', function (t) {

Built with git-ssb-web