1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/test/red/nodes/flows_spec.js

165 lines
5.9 KiB
JavaScript
Raw Normal View History

2014-07-14 22:44:34 +02:00
/**
* 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 sinon = require("sinon");
2014-07-14 22:44:34 +02:00
var when = require("when");
2014-07-17 09:06:30 +02:00
var flows = require("../../../red/nodes/flows");
var RedNode = require("../../../red/nodes/Node");
var RED = require("../../../red/nodes");
var events = require("../../../red/events");
var typeRegistry = require("../../../red/nodes/registry");
var settings = {
available: function() { return false; }
}
2014-07-14 22:44:34 +02:00
function loadFlows(testFlows, cb) {
var storage = {
getFlows: function() {
return when.resolve(testFlows);
2014-07-14 22:44:34 +02:00
},
getCredentials: function() {
return when.resolve({});
}
2014-07-14 22:44:34 +02:00
};
RED.init(settings, storage);
2014-07-14 22:44:34 +02:00
flows.load().then(function() {
should.deepEqual(testFlows, flows.getFlows());
cb();
});
}
describe('flows', function() {
2014-10-29 22:38:05 +01:00
afterEach(function(done) {
2015-01-10 23:09:37 +01:00
flows.stopFlows().then(function() {
2014-10-29 22:38:05 +01:00
loadFlows([],done);
});
});
2014-07-14 22:44:34 +02:00
describe('#load',function() {
2015-01-10 23:09:37 +01:00
2014-07-14 22:44:34 +02:00
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() {});
2014-07-14 22:44:34 +02:00
events.once('nodes-started', function() { done(); });
});
it('should load and start a registered node type', function(done) {
RED.registerType('debug', function() {});
var typeRegistryGet = sinon.stub(typeRegistry,"get",function(nt) {
2015-01-10 23:09:37 +01:00
return RedNode;
});
2014-07-14 22:44:34 +02:00
loadFlows([{"id":"n1","type":"debug"}], function() { });
events.once('nodes-started', function() {
typeRegistryGet.restore();
done();
});
2014-07-14 22:44:34 +02:00
});
it('should load and start when node type is registered', function(done) {
var typeRegistryGet = sinon.stub(typeRegistry,"get");
typeRegistryGet.onCall(0).returns(null);
2015-01-10 23:09:37 +01:00
typeRegistryGet.returns(RedNode);
loadFlows([{"id":"n2","type":"inject"}], function() {
events.emit('type-registered','inject');
});
events.once('nodes-started', function() {
typeRegistryGet.restore();
done();
});
2014-07-14 22:44:34 +02:00
});
2014-10-29 22:38:05 +01:00
it('should not instantiate nodes of an unused subflow', function(done) {
RED.registerType('abc', function() {});
var typeRegistryGet = sinon.stub(typeRegistry,"get",function(nt) {
2015-01-10 23:09:37 +01:00
return RedNode;
2014-10-29 22:38:05 +01:00
});
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
2015-01-10 23:09:37 +01:00
flows.eachNode(function(n) {
2014-10-29 22:38:05 +01:00
ncount++;
});
ncount.should.equal(0);
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 = [];
2015-01-10 23:09:37 +01:00
flows.eachNode(function(n) {
2014-10-29 22:38:05 +01:00
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();
});
});
2014-07-14 22:44:34 +02:00
});
2014-07-14 23:00:09 +02:00
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;
},
saveCredentials: function (creds) {
return when(true);
2014-07-14 23:00:09 +02:00
}
};
RED.init(settings, storage);
2014-07-14 23:00:09 +02:00
flows.setFlows(testFlows);
events.once('nodes-started', function() { done(); });
});
});
2014-07-14 22:44:34 +02:00
});