From d4548deeb3d730ed8c10e3ab1c48a337a5a4f3a0 Mon Sep 17 00:00:00 2001 From: Mark Hindess Date: Mon, 14 Jul 2014 21:44:34 +0100 Subject: [PATCH 1/4] Add node flows tests. --- test/node_flows_spec.js | 100 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 test/node_flows_spec.js diff --git a/test/node_flows_spec.js b/test/node_flows_spec.js new file mode 100644 index 000000000..634b7ac54 --- /dev/null +++ b/test/node_flows_spec.js @@ -0,0 +1,100 @@ +/** + * Copyright 2014 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +var should = require("should"); +var when = require("when"); +var flows = require("../red/nodes/flows"); +var RedNode = require("../red/nodes/Node"); +var RED = require("../red/nodes"); +var events = require("../red/events"); + +function loadFlows(testFlows, cb) { + var storage = { + getFlows: function() { + var defer = when.defer(); + defer.resolve(testFlows); + return defer.promise; + }, + getCredentials: function() { + var defer = when.defer(); + defer.resolve({}); + return defer.promise; + }, + }; + RED.init({}, storage); + flows.load().then(function() { + should.deepEqual(testFlows, flows.getFlows()); + cb(); + }); +} + +describe('flows', function() { + + describe('#add',function() { + it('should be called by node constructor',function(done) { + var n = new RedNode({id:'123',type:'abc'}); + should.deepEqual(n, flows.get("123")); + flows.clear().then(function() { + done(); + }); + }); + }); + + describe('#each',function() { + it('should "visit" all nodes',function(done) { + var nodes = [ + new RedNode({id:'n0'}), + new RedNode({id:'n1'}) + ]; + var count = 0; + flows.each(function(node) { + should.deepEqual(nodes[count], node); + count += 1; + if (count == 2) { + done(); + } + }); + }); + }); + + describe('#load',function() { + it('should load nothing when storage is empty',function(done) { + loadFlows([], done); + }); + + it('should load and start an empty tab flow',function(done) { + loadFlows([{"type":"tab","id":"tab1","label":"Sheet 1"}], + function() {}); + events.once('nodes-started', function() { done(); }); + }); + + it('should load and start a registered node type', function(done) { + RED.registerType('debug', function() {}); + loadFlows([{"id":"n1","type":"debug"}], function() { }); + events.once('nodes-started', function() { done(); }); + }); + + it('should load and start when node type is registered', + function(done) { + loadFlows([{"id":"n2","type":"inject"}], + function() { + RED.registerType('inject', function() { }); + }); + events.once('nodes-started', function() { done(); }); + }); + }); + +}); From 7281d273a138ded70ed87827e57d179b97e29a61 Mon Sep 17 00:00:00 2001 From: Mark Hindess Date: Mon, 14 Jul 2014 21:45:03 +0100 Subject: [PATCH 2/4] Remove unused code. --- red/nodes/flows.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/red/nodes/flows.js b/red/nodes/flows.js index 5f9114757..6db72efa3 100644 --- a/red/nodes/flows.js +++ b/red/nodes/flows.js @@ -151,9 +151,6 @@ var flowNodes = module.exports = { } } }, - addLogHandler: function(handler) { - logHandlers.push(handler); - }, getFlows: function() { return activeConfig; From fe9ff0a297bbb0abf4640bd77b035a5182a12bff Mon Sep 17 00:00:00 2001 From: Mark Hindess Date: Mon, 14 Jul 2014 21:46:36 +0100 Subject: [PATCH 3/4] Fix jshint complaints. --- red/nodes/flows.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/red/nodes/flows.js b/red/nodes/flows.js index 6db72efa3..747e1b461 100644 --- a/red/nodes/flows.js +++ b/red/nodes/flows.js @@ -80,7 +80,7 @@ var parseConfig = function() { } } // console.log(nn); - if (nn == null) { + if (nn === null) { util.log("[red] unknown type: "+activeConfig[i].type); } } @@ -88,7 +88,7 @@ var parseConfig = function() { // Clean up any orphaned credentials credentials.clean(flowNodes.get); events.emit("nodes-started"); -} +}; function stopFlows() { @@ -161,7 +161,7 @@ var flowNodes = module.exports = { activeConfig = conf; parseConfig(); }); - }) + }); }, stopFlows: stopFlows -} +}; From 8e32427109be31b823e89b072971301326c1be23 Mon Sep 17 00:00:00 2001 From: Mark Hindess Date: Mon, 14 Jul 2014 22:00:09 +0100 Subject: [PATCH 4/4] Add setFlows test. --- test/node_flows_spec.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/node_flows_spec.js b/test/node_flows_spec.js index 634b7ac54..c0283e5e5 100644 --- a/test/node_flows_spec.js +++ b/test/node_flows_spec.js @@ -97,4 +97,22 @@ describe('flows', function() { }); }); + describe('#setFlows',function() { + it('should save and start an empty tab flow',function(done) { + var saved = 0; + var testFlows = [{"type":"tab","id":"tab1","label":"Sheet 1"}]; + var storage = { + saveFlows: function(conf) { + var defer = when.defer(); + defer.resolve(); + should.deepEqual(testFlows, conf); + return defer.promise; + } + }; + RED.init({}, storage); + flows.setFlows(testFlows); + events.once('nodes-started', function() { done(); }); + }); + }); + });