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

Increase registry test coverage

This commit is contained in:
Nick O'Leary 2015-03-22 22:54:52 +00:00
parent e19b8d35a9
commit 20bdea7ae0
2 changed files with 112 additions and 23 deletions

View File

@ -66,6 +66,7 @@ var registry = (function() {
var moduleList = {};
for (var module in moduleConfigs) {
/* istanbul ignore else */
if (moduleConfigs.hasOwnProperty(module)) {
if (Object.keys(moduleConfigs[module].nodes).length > 0) {
if (!moduleList[module]) {
@ -77,6 +78,7 @@ var registry = (function() {
}
var nodes = moduleConfigs[module].nodes;
for(var node in nodes) {
/* istanbul ignore else */
if (nodes.hasOwnProperty(node)) {
var config = nodes[node];
var n = filterNodeInfo(config);
@ -109,6 +111,7 @@ var registry = (function() {
// Migrate from the 0.9.1 format of settings
var newConfigs = {};
for (var id in configs) {
/* istanbul ignore else */
if (configs.hasOwnProperty(id)) {
var nodeConfig = configs[id];
var moduleName;
@ -220,6 +223,8 @@ var registry = (function() {
if (nodeTypeToId[typeOrId]) {
id = nodeTypeToId[typeOrId];
}
/* istanbul ignore else */
if (id) {
var module = moduleConfigs[getModule(id)];
if (module) {
@ -234,9 +239,11 @@ var registry = (function() {
getNodeList: function() {
var list = [];
for (var module in moduleConfigs) {
/* istanbul ignore else */
if (moduleConfigs.hasOwnProperty(module)) {
var nodes = moduleConfigs[module].nodes;
for (var node in nodes) {
/* istanbul ignore else */
if (nodes.hasOwnProperty(node)) {
list.push(filterNodeInfo(nodes[node]));
}
@ -248,6 +255,7 @@ var registry = (function() {
getModuleList: function() {
var list = [];
for (var module in moduleNodes) {
/* istanbul ignore else */
if (moduleNodes.hasOwnProperty(module)) {
var nodes = moduleNodes[module];
var m = {
@ -307,14 +315,14 @@ var registry = (function() {
var config = moduleConfigs[getModule(id)].nodes[getNode(id)];
if (config.enabled && !config.err) {
result += config.config;
script += config.script;
//script += config.script;
}
}
if (script.length > 0) {
result += '<script type="text/javascript">';
result += UglifyJS.minify(script, {fromString: true}).code;
result += '</script>';
}
//if (script.length > 0) {
// result += '<script type="text/javascript">';
// result += UglifyJS.minify(script, {fromString: true}).code;
// result += '</script>';
//}
nodeConfigCache = result;
}
return nodeConfigCache;
@ -328,9 +336,9 @@ var registry = (function() {
config = config.nodes[getNode(id)];
if (config) {
var result = config.config;
if (config.script) {
result += '<script type="text/javascript">'+config.script+'</script>';
}
//if (config.script) {
// result += '<script type="text/javascript">'+config.script+'</script>';
//}
return result;
} else {
return null;
@ -422,12 +430,14 @@ var registry = (function() {
cleanModuleList: function() {
var removed = false;
for (var mod in moduleConfigs) {
/* istanbul ignore else */
if (moduleConfigs.hasOwnProperty(mod)) {
var nodes = moduleConfigs[mod].nodes;
var node;
if (mod == "node-red") {
// For core nodes, look for nodes that are enabled, !loaded and !errored
for (node in nodes) {
/* istanbul ignore else */
if (nodes.hasOwnProperty(node)) {
var n = nodes[node];
if (n.enabled && !n.err && !n.loaded) {
@ -439,6 +449,7 @@ var registry = (function() {
} else if (moduleConfigs[mod] && !moduleNodes[mod]) {
// For node modules, look for missing ones
for (node in nodes) {
/* istanbul ignore else */
if (nodes.hasOwnProperty(node)) {
registry.removeNode(mod+"/"+node);
removed = true;
@ -577,6 +588,7 @@ function loadNodesFromModule(moduleDir,pkg) {
var results = [];
var iconDirs = [];
for (var n in nodes) {
/* istanbul ignore else */
if (nodes.hasOwnProperty(n)) {
var file = path.join(moduleDir,nodes[n]);
try {
@ -649,8 +661,7 @@ function loadNodeConfig(file,module,name,version) {
node.config = content;
// TODO: parse out the javascript portion of the template
node.script = "";
//node.script = "";
for (var i=0;i<node.types.length;i++) {
if (registry.getTypeId(node.types[i])) {
node.err = node.types[i]+" already registered";

View File

@ -28,19 +28,28 @@ afterEach(function() {
typeRegistry.clear();
});
describe('NodeRegistry', function() {
describe('red/nodes/registry', function() {
var resourcesDir = __dirname+ path.sep + "resources" + path.sep;
function stubSettings(s,available) {
function stubSettings(s,available,initialConfig) {
s.available = function() {return available;};
s.set = function(s,v) { return when.resolve();};
s.get = function(s) { return null;};
s.get = function(s) { return initialConfig;};
return s;
}
var settings = stubSettings({},false);
var settingsWithStorage = stubSettings({},true);
var settings = stubSettings({},false,null);
var settingsWithStorage = stubSettings({},true,null);
var settingsWithStorageAndInitialConfig = stubSettings({},true,{"node-red":{module:"testModule",name:"testName",version:"testVersion"}});
it('loads initial config', function(done) {
typeRegistry.init(settingsWithStorageAndInitialConfig);
var version = typeRegistry.getModuleVersion("node-red");
should.exist(version);
version.should.eql("testVersion");
done();
});
it('handles nodes that export a function', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
@ -178,8 +187,7 @@ describe('NodeRegistry', function() {
});
});
it('rejects a duplicate node type registration', function(done) {
it('rejects a duplicate node type registration during load', function(done) {
typeRegistry.init(stubSettings({
nodesDir:[resourcesDir + "TestNode1",resourcesDir + "DuplicateTestNode"]
},false));
@ -199,6 +207,27 @@ describe('NodeRegistry', function() {
});
});
it('rejects a duplicate node type registration', function(done) {
typeRegistry.init(stubSettings({
nodesDir:[resourcesDir + "TestNode1"]
},false));
typeRegistry.load("wontexist",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
/*jshint immed: false */
(function(){
typeRegistry.registerType("test-node-1",{});
}).should.throw();
done();
}).catch(function(e) {
done(e);
});
});
it('handles nodesDir as a string', function(done) {
typeRegistry.init(stubSettings({
@ -374,16 +403,17 @@ describe('NodeRegistry', function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
var id = list[0].id;
var type = list[0].types[0];
list[0].should.have.property("id","node-red/TestNode1");
list[0].should.have.property("name","TestNode1");
list[0].should.have.property("module","node-red");
list[0].should.have.property("types",["test-node-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
var id = list[0].id;
var type = list[0].types[0];
var info = typeRegistry.getNodeInfo(id);
list[0].should.eql(info);
@ -396,6 +426,21 @@ describe('NodeRegistry', function() {
});
});
it('returns null node info for unrecognised id', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
should.not.exist(typeRegistry.getNodeInfo("does-not-exist"));
done();
}).catch(function(e) {
done(e);
});
});
it('returns modules list', function(done) {
var fs = require("fs");
@ -487,6 +532,9 @@ describe('NodeRegistry', function() {
var module = typeRegistry.getModuleInfo(list[0].name);
module.should.have.property("name", list[0].name);
module.should.have.property("nodes", nodes);
should.not.exist(typeRegistry.getModuleInfo("does-not-exist"));
done();
}).catch(function(e) {
done(e);
@ -746,6 +794,9 @@ describe('NodeRegistry', function() {
modules[0].should.have.property("name","TestNodeModule");
modules[0].should.have.property("version","0.0.1");
var version = typeRegistry.getModuleVersion("TestNodeModule");
version.should.eql("0.0.1");
done();
}).catch(function(e) {
done(e);
@ -879,7 +930,7 @@ describe('NodeRegistry', function() {
});
it('fails to remove non-existent module name', function(done) {
typeRegistry.init(settings);
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
@ -887,7 +938,7 @@ describe('NodeRegistry', function() {
/*jshint immed: false */
(function() {
typeRegistry.removeModule("DoesNotExistModule");
}).should.throw();
}).should.throw("Unrecognised module: DoesNotExistModule");
done();
@ -1002,4 +1053,31 @@ describe('NodeRegistry', function() {
done(e);
});
});
it("handles unavailable settings", function(done) {
typeRegistry.init(settings);
/*jshint immed: false */
(function() {
typeRegistry.enableNode("123");
}).should.throw("Settings unavailable");
/*jshint immed: false */
(function() {
typeRegistry.disableNode("123");
}).should.throw("Settings unavailable");
/*jshint immed: false */
(function() {
typeRegistry.addModule("123");
}).should.throw("Settings unavailable");
/*jshint immed: false */
(function() {
typeRegistry.removeModule("123");
}).should.throw("Settings unavailable");
/*jshint immed: false */
(function() {
typeRegistry.addNode("123");
}).should.throw("Settings unavailable");
done();
});
});