From 6169e4299af0cc487235c782c4edf3e7013c0b0b Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Wed, 29 Oct 2014 21:38:05 +0000 Subject: [PATCH] Add initial subflow tests --- red/nodes/flows.js | 1 + test/red/nodes/flows_spec.js | 58 ++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/red/nodes/flows.js b/red/nodes/flows.js index 90b10164b..45923f556 100644 --- a/red/nodes/flows.js +++ b/red/nodes/flows.js @@ -79,6 +79,7 @@ function createSubflow(sf,sfn) { var Node = require("./Node"); var subflowInstance = { id: sfn.id, + type: sfn.type, name: sfn.name, wires: [] } diff --git a/test/red/nodes/flows_spec.js b/test/red/nodes/flows_spec.js index 091bf409d..c6556c7e9 100644 --- a/test/red/nodes/flows_spec.js +++ b/test/red/nodes/flows_spec.js @@ -46,6 +46,12 @@ function loadFlows(testFlows, cb) { describe('flows', function() { + afterEach(function(done) { + flows.clear().then(function() { + loadFlows([],done); + }); + }); + describe('#add',function() { it('should be called by node constructor',function(done) { var n = new RedNode({id:'123',type:'abc'}); @@ -108,6 +114,58 @@ describe('flows', function() { done(); }); }); + + it('should not instantiate nodes of an unused subflow', function(done) { + RED.registerType('abc', function() {}); + var typeRegistryGet = sinon.stub(typeRegistry,"get",function(nt) { + return function() {}; + }); + loadFlows([{"id":"n1","type":"subflow",inputs:[],outputs:[],wires:[]}, + {"id":"n2","type":"abc","z":"n1",wires:[]} + ],function() { }); + events.once('nodes-started', function() { + (flows.get("n2") == null).should.be.true; + var ncount = 0 + flows.each(function(n) { + ncount++; + }); + ncount.should.equal(0); + console.log(ncount); + typeRegistryGet.restore(); + done(); + }); + }); + it('should instantiate nodes of an used subflow with new IDs', function(done) { + RED.registerType('abc', function() {}); + var typeRegistryGet = sinon.stub(typeRegistry,"get",function(nt) { + return RedNode; + }); + loadFlows([{"id":"n1","type":"subflow",inputs:[],outputs:[]}, + {"id":"n2","type":"abc","z":"n1","name":"def",wires:[]}, + {"id":"n3","type":"subflow:n1"} + ], function() { }); + events.once('nodes-started', function() { + // n2 should not get instantiated with that id + (flows.get("n2") == null).should.be.true; + var ncount = 0 + var nodes = []; + flows.each(function(n) { + nodes.push(n); + }); + nodes.should.have.lengthOf(2); + + // Assume the nodes are instantiated in this order - not + // a requirement, but makes the test easier to write. + nodes[0].should.have.property("id","n3"); + nodes[0].should.have.property("type","subflow:n1"); + nodes[1].should.not.have.property("id","n2"); + nodes[1].should.have.property("name","def"); + + // TODO: verify instance wiring is correct + typeRegistryGet.restore(); + done(); + }); + }); }); describe('#setFlows',function() {