|
| 1 | +/* |
| 2 | + We currently don't support pubsub when run in the browser, |
| 3 | + and we test it with separate set of tests to make sure |
| 4 | + if it's being used in the browser, pubsub errors. |
| 5 | +
|
| 6 | + More info: https://github.com/ipfs/js-ipfs-api/issues/518 |
| 7 | +
|
| 8 | + This means: |
| 9 | + - You can use pubsub from js-ipfs-api in Node.js |
| 10 | + - You can use pubsub from js-ipfs-api in Electron |
| 11 | + (when js-ipfs-api is ran in the main process of Electron) |
| 12 | +
|
| 13 | + - You can't use pubsub from js-ipfs-api in the browser |
| 14 | + - You can't use pubsub from js-ipfs-api in Electron's |
| 15 | + renderer process |
| 16 | +
|
| 17 | + - You can use pubsub from js-ipfs in the browsers |
| 18 | + - You can use pubsub from js-ipfs in Node.js |
| 19 | + - You can use pubsub from js-ipfs in Electron |
| 20 | + (in both the main process and the renderer process) |
| 21 | + - See https://github.com/ipfs/js-ipfs for details on |
| 22 | + pubsub in js-ipfs |
| 23 | +*/ |
| 24 | + |
| 25 | +/* eslint-env mocha */ |
| 26 | +/* eslint max-nested-callbacks: ['error', 8] */ |
| 27 | +'use strict' |
| 28 | + |
| 29 | +const expect = require('chai').expect |
| 30 | +const series = require('async/series') |
| 31 | +const waterfall = require('async/waterfall') |
| 32 | +const isNode = require('detect-node') |
| 33 | +const FactoryClient = require('./ipfs-factory/client') |
| 34 | + |
| 35 | +const expectedError = 'pubsub is currently not supported when run in the browser' |
| 36 | + |
| 37 | +function spawnWithId (factory, callback) { |
| 38 | + waterfall([ |
| 39 | + (cb) => factory.spawnNode(cb), |
| 40 | + (node, cb) => node.id((err, res) => { |
| 41 | + if (err) { |
| 42 | + return cb(err) |
| 43 | + } |
| 44 | + node.peerId = res |
| 45 | + cb(null, node) |
| 46 | + }) |
| 47 | + ], callback) |
| 48 | +} |
| 49 | + |
| 50 | +if (!isNode) { |
| 51 | + describe('.pubsub-browser (pubsub not supported in the browsers currently)', () => { |
| 52 | + const topic = 'pubsub-tests' |
| 53 | + |
| 54 | + let factory |
| 55 | + let ipfs1 |
| 56 | + |
| 57 | + before((done) => { |
| 58 | + factory = new FactoryClient() |
| 59 | + |
| 60 | + series([ |
| 61 | + (cb) => spawnWithId(factory, cb) |
| 62 | + ], (err, nodes) => { |
| 63 | + if (err) { |
| 64 | + return done(err) |
| 65 | + } |
| 66 | + |
| 67 | + ipfs1 = nodes[0] |
| 68 | + done() |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + after((done) => { |
| 73 | + factory.dismantle(done) |
| 74 | + }) |
| 75 | + |
| 76 | + describe('everything errors', () => { |
| 77 | + describe('Callback API', () => { |
| 78 | + describe('.publish', () => { |
| 79 | + it('throws an error', (done) => { |
| 80 | + ipfs1.pubsub.publish(topic, 'hello friend', (err, topics) => { |
| 81 | + expect(err).to.exist |
| 82 | + expect(err.message).to.equal(expectedError) |
| 83 | + done() |
| 84 | + }) |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + describe('.subscribe', () => { |
| 89 | + const handler = () => {} |
| 90 | + it('throws an error', (done) => { |
| 91 | + ipfs1.pubsub.subscribe(topic, {}, handler, (err, topics) => { |
| 92 | + expect(err).to.exist |
| 93 | + expect(err.message).to.equal(expectedError) |
| 94 | + done() |
| 95 | + }) |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + describe('.peers', () => { |
| 100 | + it('throws an error', (done) => { |
| 101 | + ipfs1.pubsub.peers(topic, (err, topics) => { |
| 102 | + expect(err).to.exist |
| 103 | + expect(err.message).to.equal(expectedError) |
| 104 | + done() |
| 105 | + }) |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + describe('.ls', () => { |
| 110 | + it('throws an error', (done) => { |
| 111 | + ipfs1.pubsub.ls((err, topics) => { |
| 112 | + expect(err).to.exist |
| 113 | + expect(err.message).to.equal(expectedError) |
| 114 | + done() |
| 115 | + }) |
| 116 | + }) |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + describe('Promise API', () => { |
| 121 | + describe('.publish', () => { |
| 122 | + it('throws an error', () => { |
| 123 | + return ipfs1.pubsub.publish(topic, 'hello friend') |
| 124 | + .catch((e) => { |
| 125 | + expect(e).to.exist |
| 126 | + expect(e.message).to.equal(expectedError) |
| 127 | + }) |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + describe('.subscribe', () => { |
| 132 | + const handler = () => {} |
| 133 | + it('throws an error', (done) => { |
| 134 | + ipfs1.pubsub.subscribe(topic, {}, handler) |
| 135 | + .catch((e) => { |
| 136 | + expect(e).to.exist |
| 137 | + expect(e.message).to.equal(expectedError) |
| 138 | + done() |
| 139 | + }) |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + describe('.peers', () => { |
| 144 | + it('throws an error', (done) => { |
| 145 | + ipfs1.pubsub.peers(topic) |
| 146 | + .catch((e) => { |
| 147 | + expect(e).to.exist |
| 148 | + expect(e.message).to.equal(expectedError) |
| 149 | + done() |
| 150 | + }) |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + describe('.ls', () => { |
| 155 | + it('throws an error', () => { |
| 156 | + return ipfs1.pubsub.ls() |
| 157 | + .catch((e) => { |
| 158 | + expect(e).to.exist |
| 159 | + expect(e.message).to.equal(expectedError) |
| 160 | + }) |
| 161 | + }) |
| 162 | + }) |
| 163 | + }) |
| 164 | + |
| 165 | + describe('.unsubscribe', () => { |
| 166 | + it('throws an error', (done) => { |
| 167 | + try { |
| 168 | + ipfs1.pubsub.unsubscribe(topic) |
| 169 | + done('unsubscribe() didn\'t throw an error') |
| 170 | + } catch (e) { |
| 171 | + expect(e).to.exist |
| 172 | + expect(e.message).to.equal(expectedError) |
| 173 | + done() |
| 174 | + } |
| 175 | + }) |
| 176 | + }) |
| 177 | + }) |
| 178 | + }) |
| 179 | +} |
0 commit comments