Files: 15faa252573d24fb834a5b13c7db4411cf2edb21 / test / poll / async / get.test.js
2884 bytesRaw
1 | const test = require('tape') |
2 | const Server = require('../../../lib/testServer') |
3 | const pull = require('pull-stream') |
4 | |
5 | const ChooseOnePoll = require('../../../poll/sync/buildChooseOne') |
6 | const ChooseOnePosition = require('../../../position/async/buildChooseOne') |
7 | const UpdatedClosingTime = require('../../../poll/async/buildUpdatedClosingTime') |
8 | const getPoll = require('../../../poll/async/get') |
9 | |
10 | const server = Server() |
11 | |
12 | const katie = server.createFeed() |
13 | const piet = server.createFeed() |
14 | |
15 | const pollContent = ChooseOnePoll({ |
16 | title: "what's our mascott team?", |
17 | choices: ['prairie dog', 'kea', 'hermit crab'], |
18 | closesAt: nDaysTime(2) |
19 | }) |
20 | |
21 | const agesAgoDate = new Date() |
22 | |
23 | agesAgoDate.setYear(1989) |
24 | const agesAgo = agesAgoDate.toISOString() |
25 | |
26 | test('pull.async.get', t => { |
27 | piet.publish(pollContent, (err, poll) => { |
28 | if (err) throw err |
29 | |
30 | pull( |
31 | pull.values([ |
32 | { author: katie, opts: { poll, choice: 1, reason: 'they are sick!' } }, |
33 | { author: piet, opts: { poll, choice: 2, reason: 'scuttles 4life' } } |
34 | ]), |
35 | pull.asyncMap((t, cb) => { |
36 | // NOTE: piet.get does not exist, so have to build using the master server |
37 | ChooseOnePosition(server)(t.opts, (err, position) => { |
38 | if (err) return cb(err) |
39 | t.position = position |
40 | cb(null, t) |
41 | }) |
42 | }), |
43 | pull.asyncMap((t, cb) => t.author.publish(t.position, cb)), |
44 | pull.asyncMap((m, cb) => UpdatedClosingTime(server)({poll, closesAt: agesAgo}, cb)), |
45 | pull.asyncMap((t, cb) => piet.publish(t, cb)), |
46 | pull.drain( |
47 | m => {}, // console.log(m.value.content.type), |
48 | onDone |
49 | ) |
50 | ) |
51 | |
52 | function onDone () { |
53 | getPoll(server)(poll.key, (err, data) => { |
54 | if (err) throw err |
55 | |
56 | // print(data) |
57 | t.equal(data.key, poll.key, 'has key') |
58 | t.deepEqual(data.value, poll.value, 'has value') |
59 | |
60 | t.equal(data.author, poll.value.author, 'has author') |
61 | t.equal(data.title, poll.value.content.title, 'has title') |
62 | |
63 | t.equal(data.positions.length, 2, 'has positions') |
64 | |
65 | t.equal(data.closesAt, agesAgo, 'gets the most recently published updated closing time') |
66 | |
67 | const positions = data.positions |
68 | t.deepEqual(positions[0].value.content.branch, [], 'first published position has no branch') |
69 | t.deepEqual(positions[1].value.content.branch, [positions[0].key], 'second published branch has first position as branch') |
70 | |
71 | t.equal(positions[0].choice, pollContent.details.choices[1], 'choice is the value from the poll, not the index.') |
72 | |
73 | t.ok(data.results[1].voters[katie.id]) |
74 | t.ok(data.results[2].voters[piet.id]) |
75 | |
76 | server.close() |
77 | t.end() |
78 | }) |
79 | } |
80 | }) |
81 | }) |
82 | |
83 | function print (obj) { |
84 | console.log(JSON.stringify(obj, null, 2)) |
85 | } |
86 | |
87 | function nDaysTime (n) { |
88 | const d = new Date() |
89 | d.setDate(d.getDate() + n) |
90 | |
91 | return d.toISOString() |
92 | } |
93 |
Built with git-ssb-web