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

Commit 42a6ac0

Browse files
richardschneiderdaviddias
authored andcommitted
test(pubsub): subscribe with a Promise (#181)
* test(pubsub): subscribe with a Promise * test(pubsub): subscribe with options and a Promise * test(pubsub): catch the promise errors
1 parent 9f80211 commit 42a6ac0

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/pubsub.js

+52
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,58 @@ module.exports = (common) => {
142142
})
143143
})
144144

145+
it('to one topic with Promise', (done) => {
146+
const check = makeCheck(2, done)
147+
const topic = getTopic()
148+
149+
const handler = (msg) => {
150+
expect(msg.data.toString()).to.equal('hi')
151+
expect(msg).to.have.property('seqno')
152+
expect(Buffer.isBuffer(msg.seqno)).to.eql(true)
153+
expect(msg).to.have.property('topicIDs').eql([topic])
154+
expect(msg).to.have.property('from', ipfs1.peerId.id)
155+
156+
ipfs1.pubsub.unsubscribe(topic, handler)
157+
158+
ipfs1.pubsub.ls((err, topics) => {
159+
expect(err).to.not.exist()
160+
expect(topics).to.be.empty()
161+
check()
162+
})
163+
}
164+
165+
ipfs1.pubsub
166+
.subscribe(topic, handler)
167+
.then(() => ipfs1.pubsub.publish(topic, Buffer.from('hi'), check))
168+
.catch((err) => expect(err).to.not.exist())
169+
})
170+
171+
it('to one topic with options and Promise', (done) => {
172+
const check = makeCheck(2, done)
173+
const topic = getTopic()
174+
175+
const handler = (msg) => {
176+
expect(msg.data.toString()).to.equal('hi')
177+
expect(msg).to.have.property('seqno')
178+
expect(Buffer.isBuffer(msg.seqno)).to.eql(true)
179+
expect(msg).to.have.property('topicIDs').eql([topic])
180+
expect(msg).to.have.property('from', ipfs1.peerId.id)
181+
182+
ipfs1.pubsub.unsubscribe(topic, handler)
183+
184+
ipfs1.pubsub.ls((err, topics) => {
185+
expect(err).to.not.exist()
186+
expect(topics).to.be.empty()
187+
check()
188+
})
189+
}
190+
191+
ipfs1.pubsub
192+
.subscribe(topic, {}, handler)
193+
.then(() => ipfs1.pubsub.publish(topic, Buffer.from('hi'), check))
194+
.catch((err) => expect(err).to.not.exist())
195+
})
196+
145197
it('attaches multiple event listeners', (done) => {
146198
const topic = getTopic()
147199

0 commit comments

Comments
 (0)