From f0afbf0a33b6d177581fe6c88fb51ab457022e5a Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Wed, 11 Dec 2019 17:34:12 +0100 Subject: [PATCH] Complete test coverage for lib/link/link.js Add tests for the remaining uncovered lines in lib/link/link.js. --- lib/link/link.js | 4 +-- test/lib/link/link.js | 57 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/lib/link/link.js b/lib/link/link.js index c2ba460a..9d174833 100644 --- a/lib/link/link.js +++ b/lib/link/link.js @@ -34,7 +34,7 @@ class Link { if (err instanceof errors.InvalidMessage) { this.connector.close(`Invalid message: ${err.message}`); } else { - throw err; + this.connector.emit('error', err); } } }); @@ -57,7 +57,7 @@ class Link { */ processMessage(message) { if (!schema.message(message)) { - throw new errors.InvalidMessage("Malformed message"); + throw new errors.InvalidMessage("Malformed"); } let validator = this._validators.get(message.type); diff --git a/test/lib/link/link.js b/test/lib/link/link.js index f93f578a..213e2e19 100644 --- a/test/lib/link/link.js +++ b/test/lib/link/link.js @@ -9,17 +9,49 @@ const mock = require("../../mock"); describe("lib/link/link", function() { describe("class Link", function() { - let testLink = new link.Link('source', 'target', new mock.MockConnector()); - testLink.setValidator('simple', schema.compile({ - properties: { - "data": { - type: 'object', - properties: { - "string": { type: 'string' }, + let testConnector; + let testLink; + + before(function() { + testConnector = new mock.MockConnector(); + testLink = new link.Link('source', 'target', testConnector); + testLink.setValidator('simple', schema.compile({ + properties: { + "data": { + type: 'object', + properties: { + "string": { type: 'string' }, + }, }, }, - }, - })); + })); + }); + + it("should close connector on invalid messages", function() { + testConnector.sentMessages = []; + testConnector.emit('message', { invalid: true }); + assert.deepEqual( + testConnector.sentMessages, + [{ + seq: testConnector._seq - 1, + type: 'close', + data: { reason: "Invalid message: Malformed" }, + }] + ); + }); + + it("should disconnect on close", function() { + testConnector.disconnectCalled = false; + testConnector.emit('message', { seq: 1, type: 'close', data: { reason: "test" }}); + assert(testConnector.disconnectCalled, "disconnect was not called"); + }); + + it("should emitt error processing message to connector", async function() { + testLink.setHandler('throws', () => { throw new Error("Handler error"); }, () => true); + let result = events.once(testConnector, 'message'); + testConnector.emit('message', { seq: 1, type: 'throws', data: {}}); + await assert.rejects(result, new Error("Handler error")); + }); describe(".setValidator()", function() { it("should set the validator", function() { @@ -58,7 +90,7 @@ describe("lib/link/link", function() { it("should throw on invalid message", function() { assert.throws( () => testLink.processMessage({ data: "invalid message" }), - new errors.InvalidMessage("Malformed message") + new errors.InvalidMessage("Malformed") ); }); it("should throw on message without validator", function() { @@ -130,7 +162,10 @@ describe("lib/link/link", function() { }); describe(".waitFor()", function() { - testLink.setValidator('waiter', (message) => true); + before(function() { + testLink.setValidator('waiter', (message) => true); + }); + it("should throw on missing validator", async function() { await assert.rejects( testLink.waitFor('no_validator', {}),