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

Merge pull request #267 from hindessm/flows-tests

Flows tests
This commit is contained in:
Nick O'Leary 2014-07-15 17:03:16 +01:00
commit cfd590a397
2 changed files with 122 additions and 7 deletions

View File

@ -80,7 +80,7 @@ var parseConfig = function() {
} }
} }
// console.log(nn); // console.log(nn);
if (nn == null) { if (nn === null) {
util.log("[red] unknown type: "+activeConfig[i].type); util.log("[red] unknown type: "+activeConfig[i].type);
} }
} }
@ -88,7 +88,7 @@ var parseConfig = function() {
// Clean up any orphaned credentials // Clean up any orphaned credentials
credentials.clean(flowNodes.get); credentials.clean(flowNodes.get);
events.emit("nodes-started"); events.emit("nodes-started");
} };
function stopFlows() { function stopFlows() {
@ -151,9 +151,6 @@ var flowNodes = module.exports = {
} }
} }
}, },
addLogHandler: function(handler) {
logHandlers.push(handler);
},
getFlows: function() { getFlows: function() {
return activeConfig; return activeConfig;
@ -164,7 +161,7 @@ var flowNodes = module.exports = {
activeConfig = conf; activeConfig = conf;
parseConfig(); parseConfig();
}); });
}) });
}, },
stopFlows: stopFlows stopFlows: stopFlows
} };

118
test/node_flows_spec.js Normal file
View File

@ -0,0 +1,118 @@
/**
* 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(); });
});
});
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(); });
});
});
});