Commit 7e7895afa684f692c8d3b741dfc512571ef88c23
Use error types. Pass tests
Piet Geursen committed on 3/7/2018, 2:03:34 AMParent: 7610206161f8ec304e4fb592fc4c9861f6990e80
Files changed
position/sync/chooseOneResults.js | changed |
test/position/sync/chooseOneResults.test.js | changed |
position/sync/chooseOneResults.js | ||
---|---|---|
@@ -1,6 +1,7 @@ | ||
1 | 1 | const isArray = require('isarray') |
2 | 2 | const Position = require('../../position/sync/position') |
3 | +const {ERROR_POSITION_CHOICE, ERROR_POSITION_TYPE, ERROR_POSITION_LATE} = require('../../types') | |
3 | 4 | |
4 | 5 | // Expects `poll` and `position` objects passed in to be of shape: |
5 | 6 | // { |
6 | 7 | // value: { |
@@ -15,26 +16,31 @@ | ||
15 | 16 | return positions.reduce(function (results, position) { |
16 | 17 | const { value: {author} } = position |
17 | 18 | const { positionDetails: {choice} } = Position(position) |
18 | 19 | |
19 | - if (isInvalidChoice({position, poll}) || isPositionAfterClose({position, poll})) { | |
20 | - results.errors.invalidPositions.push(position) | |
20 | + if (isInvalidChoice({position, poll})) { | |
21 | + results.errors.push({type: ERROR_POSITION_CHOICE, position}) | |
21 | 22 | return results |
22 | 23 | } |
23 | 24 | |
25 | + if (isPositionLate({position, poll})) { | |
26 | + results.errors.push({type: ERROR_POSITION_LATE, position}) | |
27 | + return results | |
28 | + } | |
29 | + | |
24 | 30 | if (!isArray(results[choice])) { |
25 | 31 | results[choice] = [] |
26 | 32 | } |
27 | 33 | results[choice].push(author) |
28 | 34 | |
29 | 35 | return results |
30 | - }, {errors: {invalidPositions: []}}) | |
36 | + }, {errors: []}) | |
31 | 37 | } |
32 | 38 | |
33 | 39 | function isInvalidChoice ({position, poll}) { |
34 | 40 | const { positionDetails: {choice} } = Position(position) |
35 | 41 | return choice >= poll.pollDetails.choices.length |
36 | 42 | } |
37 | 43 | |
38 | -function isPositionAfterClose ({position, poll}) { | |
44 | +function isPositionLate ({position, poll}) { | |
39 | 45 | return position.value.timestamp > poll.closesAt |
40 | 46 | } |
test/position/sync/chooseOneResults.test.js | ||
---|---|---|
@@ -2,8 +2,9 @@ | ||
2 | 2 | const ChooseOne = require('../../../position/sync/chooseOne') |
3 | 3 | const ChooseOnePoll = require('../../../poll/sync/chooseOne') |
4 | 4 | const Position = require('../../../position/sync/position') |
5 | 5 | const chooseOneResults = require('../../../position/sync/chooseOneResults') |
6 | +const {ERROR_POSITION_CHOICE, ERROR_POSITION_TYPE, ERROR_POSITION_LATE} = require('../../../types') | |
6 | 7 | |
7 | 8 | const pietId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/0=.ed25519' |
8 | 9 | const mixId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/1=.ed25519' |
9 | 10 | const mikeyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/2=.ed25519' |
@@ -53,9 +54,9 @@ | ||
53 | 54 | { value: { content: Position(ChooseOne({choice: 3, poll})), author: pietId } } |
54 | 55 | ] |
55 | 56 | |
56 | 57 | const actual = chooseOneResults({positions, poll: validPoll}) |
57 | - t.deepEqual(actual.errors.invalidPositions[0], positions[0], 'invalid vote is on error object') | |
58 | + t.deepEqual(actual.errors[0].type, ERROR_POSITION_CHOICE, 'invalid vote is on error object') | |
58 | 59 | t.end() |
59 | 60 | }) |
60 | 61 | |
61 | 62 | test('ChooseOneResults - A position stated before the closing time of the poll is counted', function (t) { |
@@ -83,7 +84,7 @@ | ||
83 | 84 | { value: { content: Position(ChooseOne({choice: 0, poll})), author: pietId, timestamp: now + 1} } |
84 | 85 | ] |
85 | 86 | |
86 | 87 | const actual = chooseOneResults({positions, poll: validPoll}) |
87 | - t.deepEqual(actual.errors.invalidPositions[0], positions[0], 'invalid vote is on error object') | |
88 | + t.deepEqual(actual.errors[0].type, ERROR_POSITION_LATE, 'invalid vote is on error object') | |
88 | 89 | t.end() |
89 | 90 | }) |
Built with git-ssb-web