git ssb

0+

Piet / ssb-loomio



Commit 7e7895afa684f692c8d3b741dfc512571ef88c23

Use error types. Pass tests

Piet Geursen committed on 3/7/2018, 2:03:34 AM
Parent: 7610206161f8ec304e4fb592fc4c9861f6990e80

Files changed

position/sync/chooseOneResults.jschanged
test/position/sync/chooseOneResults.test.jschanged
position/sync/chooseOneResults.jsView
@@ -1,6 +1,7 @@
11 const isArray = require('isarray')
22 const Position = require('../../position/sync/position')
3+const {ERROR_POSITION_CHOICE, ERROR_POSITION_TYPE, ERROR_POSITION_LATE} = require('../../types')
34
45 // Expects `poll` and `position` objects passed in to be of shape:
56 // {
67 // value: {
@@ -15,26 +16,31 @@
1516 return positions.reduce(function (results, position) {
1617 const { value: {author} } = position
1718 const { positionDetails: {choice} } = Position(position)
1819
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})
2122 return results
2223 }
2324
25+ if (isPositionLate({position, poll})) {
26+ results.errors.push({type: ERROR_POSITION_LATE, position})
27+ return results
28+ }
29+
2430 if (!isArray(results[choice])) {
2531 results[choice] = []
2632 }
2733 results[choice].push(author)
2834
2935 return results
30- }, {errors: {invalidPositions: []}})
36+ }, {errors: []})
3137 }
3238
3339 function isInvalidChoice ({position, poll}) {
3440 const { positionDetails: {choice} } = Position(position)
3541 return choice >= poll.pollDetails.choices.length
3642 }
3743
38-function isPositionAfterClose ({position, poll}) {
44+function isPositionLate ({position, poll}) {
3945 return position.value.timestamp > poll.closesAt
4046 }
test/position/sync/chooseOneResults.test.jsView
@@ -2,8 +2,9 @@
22 const ChooseOne = require('../../../position/sync/chooseOne')
33 const ChooseOnePoll = require('../../../poll/sync/chooseOne')
44 const Position = require('../../../position/sync/position')
55 const chooseOneResults = require('../../../position/sync/chooseOneResults')
6+const {ERROR_POSITION_CHOICE, ERROR_POSITION_TYPE, ERROR_POSITION_LATE} = require('../../../types')
67
78 const pietId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/0=.ed25519'
89 const mixId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/1=.ed25519'
910 const mikeyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/2=.ed25519'
@@ -53,9 +54,9 @@
5354 { value: { content: Position(ChooseOne({choice: 3, poll})), author: pietId } }
5455 ]
5556
5657 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')
5859 t.end()
5960 })
6061
6162 test('ChooseOneResults - A position stated before the closing time of the poll is counted', function (t) {
@@ -83,7 +84,7 @@
8384 { value: { content: Position(ChooseOne({choice: 0, poll})), author: pietId, timestamp: now + 1} }
8485 ]
8586
8687 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')
8889 t.end()
8990 })

Built with git-ssb-web