From c9b97519006a7f1399414672517c03f66c3187b7 Mon Sep 17 00:00:00 2001 From: haad Date: Mon, 5 Dec 2016 13:26:09 +0100 Subject: [PATCH] Fix pubsub-message tests. --- src/index.js | 1 + src/pubsub-message.js | 136 ++++++++++++++++++++++++------------------ 2 files changed, 78 insertions(+), 59 deletions(-) diff --git a/src/index.js b/src/index.js index 24b2e4b3..50b2efff 100644 --- a/src/index.js +++ b/src/index.js @@ -9,3 +9,4 @@ exports.swarm = require('./swarm') exports.block = require('./block') exports.dht = require('./dht') exports.pubsub = require('./pubsub') +exports.pubsubMessage = require('./pubsub-message') diff --git a/src/pubsub-message.js b/src/pubsub-message.js index 004fa0e2..d48f4a3f 100644 --- a/src/pubsub-message.js +++ b/src/pubsub-message.js @@ -3,80 +3,98 @@ const expect = require('chai').expect const isNode = require('detect-node') -const PubsubMessage = require('../../src/pubsub-message') // eslint-disable-line no-unused-vars -const PubsubMessageUtils = require('../../src/pubsub-message-utils') - -const topicName = 'js-ipfs-api-tests' // NOTE! -// These tests are skipped for now until we figure out the +// (Most of) these tests are skipped for now until we figure out the // final data types for the messages coming over the wire -describe('.pubsub-message', () => { - if (!isNode) { - return - } +const topicName = 'js-ipfs-api-tests' - it.skip('create message', () => { - // TODO - }) +module.exports = (common, deps) => { + // Make sure the needed dependencies are injected + expect(deps.PubsubMessage).to.exist + expect(deps.PubsubMessageUtils).to.exist - it.skip('deserialize message from JSON object', () => { - const obj = { - from: 'BI:ۛv�m�uyѱ����tU�+��#���V', - data: 'aGk=', - seqno: 'FIlj2BpyEgI=', - topicIDs: [ topicName ] - } - try { - const message = PubsubMessageUtils.deserialize(obj) - expect(message.from).to.equal('AAA') - expect(message.data).to.equal('hi') - expect(message.seqno).to.equal('\u0014�c�\u001ar\u0012\u0002') - expect(message.topicIDs.length).to.equal(1) - expect(message.topicIDs[0]).to.equal(topicName) - } catch (e) { - expect(e).to.not.exist - } - }) + const PubsubMessage = deps.PubsubMessage // eslint-disable-line no-unused-vars + const PubsubMessageUtils = deps.PubsubMessageUtils // eslint-disable-line no-unused-vars - describe('immutable properties', () => { - const message = PubsubMessageUtils.create('A', 'hello', '123', ['hello world']) + // TESTS + describe('.pubsub-message', () => { + if (!isNode) { + return + } - it('from', () => { - try { - message.from = 'not allowed' - } catch (e) { - expect(e).to.be.an('error') - expect(e.toString()).to.equal(`TypeError: Cannot set property from of # which has only a getter`) - } + it.skip('create message', () => { + // TODO }) - it('data', () => { - try { - message.data = 'not allowed' - } catch (e) { - expect(e).to.be.an('error') - expect(e.toString()).to.equal(`TypeError: Cannot set property data of # which has only a getter`) + it.skip('deserialize message from JSON object', () => { + const obj = { + from: 'BI:ۛv�m�uyѱ����tU�+��#���V', + data: 'aGk=', + seqno: 'FIlj2BpyEgI=', + topicIDs: [ topicName ] } - }) - - it('seqno', () => { try { - message.seqno = 'not allowed' + const message = PubsubMessageUtils.deserialize(obj) + expect(message.from).to.equal('AAA') + expect(message.data).to.equal('hi') + expect(message.seqno).to.equal('\u0014�c�\u001ar\u0012\u0002') + expect(message.topicIDs.length).to.equal(1) + expect(message.topicIDs[0]).to.equal(topicName) } catch (e) { - expect(e).to.be.an('error') - expect(e.toString()).to.equal(`TypeError: Cannot set property seqno of # which has only a getter`) + expect(e).to.not.exist } }) - it('topicIDs', () => { - try { - message.topicIDs = ['not allowed'] - } catch (e) { - expect(e).to.be.an('error') - expect(e.toString()).to.equal(`TypeError: Cannot set property topicIDs of # which has only a getter`) - } + describe('immutable properties', () => { + const sender = 'A' + const data = 'hello' + const seqno = '123' + const topicIDs = ['hello world'] + + const message = PubsubMessageUtils.create(sender, data, seqno, topicIDs) + + it('from', () => { + try { + message.from = 'not allowed' + } catch (e) { + expect(e).to.be.an('error') + expect(e.toString()).to.equal(`TypeError: Cannot set property from of # which has only a getter`) + } + expect(message.from).to.equal(sender) + }) + + it('data', () => { + try { + message.data = 'not allowed' + } catch (e) { + expect(e).to.be.an('error') + expect(e.toString()).to.equal(`TypeError: Cannot set property data of # which has only a getter`) + } + expect(message.data).to.equal(data) + }) + + it('seqno', () => { + try { + message.seqno = 'not allowed' + } catch (e) { + expect(e).to.be.an('error') + expect(e.toString()).to.equal(`TypeError: Cannot set property seqno of # which has only a getter`) + } + expect(message.seqno).to.equal(seqno) + }) + + it('topicIDs', () => { + try { + message.topicIDs = ['not allowed'] + } catch (e) { + expect(e).to.be.an('error') + expect(e.toString()).to.equal(`TypeError: Cannot set property topicIDs of # which has only a getter`) + } + expect(message.topicIDs[0]).to.equal(topicIDs[0]) + expect(message.topicIDs.length).to.equal(topicIDs.length) + }) }) }) -}) +}