Commit 92693260135502dac86a07eef9c477fb0198568a
Pass all the tests
Piet Geursen committed on 3/9/2018, 12:28:12 AMParent: be17b27d1ff6966e3d7d922f5e612f4d2477dce4
Files changed
position/sync/chooseOneResults.js | changed |
test/poll/async/get.test.js | changed |
test/position/sync/chooseOneResults.test.js | changed |
position/sync/chooseOneResults.js | ||
---|---|---|
@@ -1,5 +1,6 @@ | ||
1 | -const isArray = require('isarray') | |
1 … | +var getMsgContent = require('../../lib/getMsgContent') | |
2 … | +var ChooseOne = require('../../poll/sync/chooseOne') | |
2 | 3 … | const PositionChoiceError = require('../../errors/sync/positionChoiceError') |
3 | 4 … | const PositionLateError = require('../../errors/sync/positionLateError') |
4 | 5 … | |
5 | 6 … | // Expects `poll` and `position` objects passed in to be of shape: |
@@ -13,39 +14,36 @@ | ||
13 | 14 … | // } |
14 | 15 … | // |
15 | 16 … | // postions must be of the correct type ie: type checked by the caller. |
16 | 17 … | 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) { | |
18 | 29 … | const { author, content } = position.value |
19 | 30 … | const { choice } = content.positionDetails |
20 | 31 … | |
21 | 32 … | if (isInvalidChoice({position, poll})) { |
22 | - results.errors.push(PositionChoiceError({position})) | |
23 | - return results | |
33 … | + acc.errors.push(PositionChoiceError({position})) | |
34 … | + return acc | |
24 | 35 … | } |
25 | 36 … | |
26 | 37 … | if (isPositionLate({position, poll})) { |
27 | - results.errors.push(PositionLateError({position})) | |
28 | - return results | |
38 … | + acc.errors.push(PositionLateError({position})) | |
39 … | + return acc | |
29 | 40 … | } |
30 | 41 … | |
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 | |
45 | 43 … | |
46 | - return results | |
47 | - }, {errors: []}) | |
44 … | + return acc | |
45 … | + }, {errors: [], results}) | |
48 | 46 … | } |
49 | 47 … | |
50 | 48 … | function isInvalidChoice ({position, poll}) { |
51 | 49 … | const { choice } = position.value.content.positionDetails |
test/poll/async/get.test.js | ||
---|---|---|
@@ -48,13 +48,10 @@ | ||
48 | 48 … | t.equal(data.title, poll.value.content.title, 'has title') |
49 | 49 … | |
50 | 50 … | t.equal(data.positions.length, 2, 'has positions') |
51 | 51 … | |
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]) | |
57 | 54 … | |
58 | 55 … | server.close() |
59 | 56 … | t.end() |
60 | 57 … | }) |
test/position/sync/chooseOneResults.test.js | |||
---|---|---|---|
@@ -1,9 +1,9 @@ | |||
1 | 1 … | const test = require('tape') | |
2 | 2 … | const ChooseOne = require('../../../position/sync/chooseOne') | |
3 | 3 … | const ChooseOnePoll = require('../../../poll/sync/chooseOne') | |
4 | 4 … | 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') | ||
6 | 6 … | ||
7 | 7 … | const pietId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/0=.ed25519' | |
8 | 8 … | const mixId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/1=.ed25519' | |
9 | 9 … | const mikeyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/2=.ed25519' | |
@@ -38,17 +38,17 @@ | |||
38 | 38 … | ||
39 | 39 … | const expected = { | |
40 | 40 … | results: [ | |
41 | 41 … | { | |
42 | - choice: '1', | ||
42 … | + choice: 1, | ||
43 | 43 … | voters: { | |
44 | 44 … | [pietId]: positions[0], | |
45 | 45 … | [mixId]: positions[1], | |
46 | 46 … | [mikeyId]: positions[2] | |
47 | 47 … | } | |
48 | 48 … | }, | |
49 | 49 … | { | |
50 | - choice: '2', | ||
50 … | + choice: 2, | ||
51 | 51 … | voters: { | |
52 | 52 … | [timmyId]: positions[3], | |
53 | 53 … | [tommyId]: positions[4] | |
54 | 54 … | } | |
@@ -73,9 +73,9 @@ | |||
73 | 73 … | { value: { content: ChooseOne({choice: 3, poll}), author: pietId } } | |
74 | 74 … | ] | |
75 | 75 … | ||
76 | 76 … | 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') | ||
78 | 78 … | t.end() | |
79 | 79 … | }) | |
80 | 80 … | ||
81 | 81 … | test('ChooseOneResults - a position stated for an invalid choice index is included in the errors object', function (t) { | |
@@ -93,9 +93,9 @@ | |||
93 | 93 … | { value: { content: ChooseOne({choice: 0, poll}), author: pietId, timestamp: now - 1 } } | |
94 | 94 … | ] | |
95 | 95 … | ||
96 | 96 … | 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') | ||
98 | 98 … | t.end() | |
99 | 99 … | }) | |
100 | 100 … | ||
101 | 101 … | test('ChooseOneResults - A position stated after the closing time of the poll is not counted', function (t) { | |
@@ -103,9 +103,9 @@ | |||
103 | 103 … | { value: { content: ChooseOne({choice: 0, poll}), author: pietId, timestamp: now + 1 } } | |
104 | 104 … | ] | |
105 | 105 … | ||
106 | 106 … | 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') | ||
108 | 108 … | t.end() | |
109 | 109 … | }) | |
110 | 110 … | ||
111 | 111 … | 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