Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

test(pubsub): subscribe with a Promise #181

Merged
merged 3 commits into from
Dec 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,58 @@ module.exports = (common) => {
})
})

it('to one topic with Promise', (done) => {
const check = makeCheck(2, done)
const topic = getTopic()

const handler = (msg) => {
expect(msg.data.toString()).to.equal('hi')
expect(msg).to.have.property('seqno')
expect(Buffer.isBuffer(msg.seqno)).to.eql(true)
expect(msg).to.have.property('topicIDs').eql([topic])
expect(msg).to.have.property('from', ipfs1.peerId.id)

ipfs1.pubsub.unsubscribe(topic, handler)

ipfs1.pubsub.ls((err, topics) => {
expect(err).to.not.exist()
expect(topics).to.be.empty()
check()
})
}

ipfs1.pubsub
.subscribe(topic, handler)
.then(() => ipfs1.pubsub.publish(topic, Buffer.from('hi'), check))
.catch((err) => expect(err).to.not.exist())
})

it('to one topic with options and Promise', (done) => {
const check = makeCheck(2, done)
const topic = getTopic()

const handler = (msg) => {
expect(msg.data.toString()).to.equal('hi')
expect(msg).to.have.property('seqno')
expect(Buffer.isBuffer(msg.seqno)).to.eql(true)
expect(msg).to.have.property('topicIDs').eql([topic])
expect(msg).to.have.property('from', ipfs1.peerId.id)

ipfs1.pubsub.unsubscribe(topic, handler)

ipfs1.pubsub.ls((err, topics) => {
expect(err).to.not.exist()
expect(topics).to.be.empty()
check()
})
}

ipfs1.pubsub
.subscribe(topic, {}, handler)
.then(() => ipfs1.pubsub.publish(topic, Buffer.from('hi'), check))
.catch((err) => expect(err).to.not.exist())
})

it('attaches multiple event listeners', (done) => {
const topic = getTopic()

Expand Down