mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Big rework of registry.js
Part of #322 Loads node.html files before node.js files Adds helper.unload which must be called by node tests to clear the registery of nodes
This commit is contained in:
@@ -23,6 +23,10 @@ describe('inject node', function() {
|
||||
before(function(done) {
|
||||
helper.startServer(done);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
helper.unload();
|
||||
});
|
||||
|
||||
it('should inject once', function(done) {
|
||||
|
||||
|
@@ -24,6 +24,11 @@ describe('debug node', function() {
|
||||
before(function(done) {
|
||||
helper.startServer(done);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
helper.unload();
|
||||
});
|
||||
|
||||
|
||||
it('should be loaded', function(done) {
|
||||
var flow = [{id:"n1", type:"debug", name: "Debug" }];
|
||||
|
@@ -23,6 +23,10 @@ describe('function node', function() {
|
||||
before(function(done) {
|
||||
helper.startServer(done);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
helper.unload();
|
||||
});
|
||||
|
||||
it('should be loaded', function(done) {
|
||||
var flow = [{id:"n1", type:"function", name: "function" }];
|
||||
|
@@ -23,6 +23,11 @@ describe('template node', function() {
|
||||
before(function(done) {
|
||||
helper.startServer(done);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
helper.unload();
|
||||
});
|
||||
|
||||
|
||||
it('should modify payload', function(done) {
|
||||
var flow = [{id:"n1", type:"template", field: "payload", template: "payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
|
||||
|
@@ -20,6 +20,10 @@ var helper = require("../../helper.js");
|
||||
|
||||
describe('comment node', function() {
|
||||
|
||||
afterEach(function() {
|
||||
helper.unload();
|
||||
});
|
||||
|
||||
it('should be loaded', function(done) {
|
||||
var flow = [{id:"n1", type:"comment", name: "comment" }];
|
||||
helper.load(commentNode, flow, function() {
|
||||
|
@@ -23,6 +23,10 @@ describe('JSON node', function() {
|
||||
before(function(done) {
|
||||
helper.startServer(done);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
helper.unload();
|
||||
});
|
||||
|
||||
it('should be loaded', function(done) {
|
||||
var flow = [{id:"jsonNode1", type:"json", name: "jsonNode" }];
|
||||
|
@@ -42,7 +42,7 @@ describe('TailNode', function() {
|
||||
});
|
||||
|
||||
afterEach(function(done) {
|
||||
|
||||
helper.unload();
|
||||
fs.exists(fileToTail, function(exists) {
|
||||
if(exists) {
|
||||
fs.unlinkSync(fileToTail);
|
||||
|
@@ -59,6 +59,10 @@ module.exports = {
|
||||
cb();
|
||||
});
|
||||
},
|
||||
unload: function() {
|
||||
// TODO: any other state to remove between tests?
|
||||
redNodes.clearRegistry();
|
||||
},
|
||||
|
||||
getNode: function(id) {
|
||||
return flows.get(id);
|
||||
|
@@ -23,7 +23,11 @@ var index = require("../../../red/nodes/index");
|
||||
var credentials = require("../../../red/nodes/credentials");
|
||||
|
||||
describe('Credentials', function() {
|
||||
|
||||
|
||||
afterEach(function() {
|
||||
index.clearRegistry();
|
||||
});
|
||||
|
||||
it('loads from storage',function(done) {
|
||||
|
||||
var storage = {
|
||||
|
@@ -23,7 +23,11 @@ var defer = when.defer();
|
||||
var index = require("../../../red/nodes/index");
|
||||
|
||||
describe("red/nodes/index", function() {
|
||||
|
||||
|
||||
afterEach(function() {
|
||||
index.clearRegistry();
|
||||
});
|
||||
|
||||
var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
|
||||
var storage = {
|
||||
getFlows: function() {
|
||||
|
@@ -15,9 +15,16 @@
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var RedNodes = require("../../../red/nodes");
|
||||
var sinon = require("sinon");
|
||||
|
||||
var RedNodes = require("../../../red/nodes");
|
||||
var RedNode = require("../../../red/nodes/Node");
|
||||
var typeRegistry = require("../../../red/nodes/registry");
|
||||
var events = require("../../../red/events");
|
||||
|
||||
afterEach(function() {
|
||||
typeRegistry.clear();
|
||||
});
|
||||
|
||||
describe('NodeRegistry', function() {
|
||||
it('automatically registers new nodes',function() {
|
||||
@@ -29,170 +36,245 @@ describe('NodeRegistry', function() {
|
||||
|
||||
should.strictEqual(n,newNode);
|
||||
});
|
||||
});
|
||||
|
||||
describe('NodeRegistry', function() {
|
||||
it('does not accept incorrect nodesDir',function(done) {
|
||||
var typeRegistry = require("../../../red/nodes/registry");
|
||||
var settings = {
|
||||
nodesDir : "wontexist"
|
||||
}
|
||||
|
||||
typeRegistry.init(null);
|
||||
typeRegistry.load().then(function(){
|
||||
try {
|
||||
should.fail(null, null, "Loading of non-existing nodesDir should never succeed");
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
}).catch(function(e) { // successful test, failed promise
|
||||
|
||||
it('handles nodes that export a function', function(done) {
|
||||
typeRegistry.init({});
|
||||
typeRegistry.load(__dirname+"/resources/TestNode1").then(function() {
|
||||
var list = typeRegistry.getNodeList();
|
||||
list.should.be.an.Array.and.have.lengthOf(1);
|
||||
list[0].should.have.property("id");
|
||||
list[0].should.have.property("name","TestNode1.js");
|
||||
list[0].should.have.property("types",["test-node-1"]);
|
||||
list[0].should.have.property("enabled",true);
|
||||
list[0].should.not.have.property("err");
|
||||
|
||||
var nodeConstructor = typeRegistry.get("test-node-1");
|
||||
(typeof nodeConstructor).should.be.equal("function");
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('fails to load additional node files from invalid nodesDir',function(done) {
|
||||
var typeRegistry = require("../../../red/nodes/registry");
|
||||
var settings = {
|
||||
nodesDir : "wontexist"
|
||||
}
|
||||
|
||||
typeRegistry.init(settings);
|
||||
typeRegistry.load().then(function(){
|
||||
try {
|
||||
should.fail(null, null, "Loading of non-existing nodesDir should never succeed");
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
}).catch(function(e) { // successful test, failed promise
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/*
|
||||
* This test does the following:
|
||||
* 1) injects settings that tell the registry to load its default nodes from
|
||||
* tempNoNodesContainedDir that contains no valid nodes => this means that no default nodes are loaded
|
||||
* 2) We only load a single node we pre-deploy into tempDir
|
||||
* 3) This node (fakeNodeJS = reads "fake Node JavaScript"), when exported automatically creates a known
|
||||
* file
|
||||
* 4) We can assert that this file exists and make the loading test pass/fail
|
||||
*/
|
||||
describe("getNodeFiles", function() {
|
||||
var fs = require('fs-extra');
|
||||
var path = require('path');
|
||||
|
||||
var tempDir = path.join(__dirname,".tmp/");
|
||||
var fakeNodeJS = tempDir + "testNode.js"; // when exported, this fake node creates a file we can assert on
|
||||
var fakeNodeHTML = tempDir + "testNode.html"; // we assume it's going to be loaded by cheerio
|
||||
|
||||
var nodeInjectedFileName = "testInjected";
|
||||
var nodeInjectedFilePath = path.join(tempDir, nodeInjectedFileName);
|
||||
|
||||
var tempNoNodesContainedDir = path.join(__dirname,".noNodes/");
|
||||
|
||||
beforeEach(function(done) {
|
||||
fs.remove(tempDir,function(err) {
|
||||
fs.mkdirSync(tempDir);
|
||||
var fileContents = "var fs = require('fs');\n" +
|
||||
"var path = require('path');\n" +
|
||||
"var tempFile = path.join(__dirname, \"" + nodeInjectedFileName + "\");\n" +
|
||||
"\n" +
|
||||
"module.exports = function(RED) {\n" +
|
||||
" fs.writeFileSync(tempFile, \"Test passes if this file has been written.\");\n" +
|
||||
"}\n";
|
||||
|
||||
var htmlContents = "<script type=\"text/javascript\">\n" +
|
||||
" RED.nodes.registerType('testFileInjector',{\n" +
|
||||
" category: 'storage-input',\n" +
|
||||
" inputs:0,\n" +
|
||||
" outputs:1,\n" +
|
||||
" icon: \"file.png\"\n" +
|
||||
" });\n" +
|
||||
"</script>\n" +
|
||||
"<script type=\"text/x-red\" data-template-name=\"testFileInjector\">\n" +
|
||||
" <div class=\"form-row\">\n" +
|
||||
" <label for=\"node-input-name\"><i class=\"icon-tag\"></i> Name</label>\n" +
|
||||
" <input type=\"text\" id=\"node-input-name\" placeholder=\"Name\">\n" +
|
||||
" </div>\n" +
|
||||
"</script>\n" +
|
||||
"<script type=\"text/x-red\" data-help-name=\"node-type\">\n" +
|
||||
" <p>This node is pretty useless</p>\n" +
|
||||
"</script>";
|
||||
fs.writeFileSync(fakeNodeJS, fileContents);
|
||||
fs.writeFileSync(fakeNodeHTML, htmlContents);
|
||||
fs.remove(tempNoNodesContainedDir,function(err) {
|
||||
fs.mkdirSync(tempNoNodesContainedDir);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
afterEach(function(done) {
|
||||
fs.exists(tempNoNodesContainedDir, function(exists) {
|
||||
if(exists) {
|
||||
fs.removeSync(tempNoNodesContainedDir);
|
||||
}
|
||||
});
|
||||
fs.exists(nodeInjectedFilePath, function(exists) {
|
||||
if(exists) {
|
||||
fs.unlinkSync(nodeInjectedFilePath);
|
||||
}
|
||||
});
|
||||
fs.exists(fakeNodeJS, function(exists) {
|
||||
if(exists) {
|
||||
fs.unlinkSync(fakeNodeJS);
|
||||
}
|
||||
fs.remove(tempDir);
|
||||
});
|
||||
fs.exists(fakeNodeHTML, function(exists) {
|
||||
if(exists) {
|
||||
fs.unlinkSync(fakeNodeHTML);
|
||||
}
|
||||
fs.remove(tempDir, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('loads additional node files from specified external nodesDir',function(done) {
|
||||
var typeRegistry = require("../../../red/nodes/registry");
|
||||
var settings = {
|
||||
nodesDir : tempDir
|
||||
}
|
||||
|
||||
typeRegistry.init(settings);
|
||||
|
||||
typeRegistry.load(tempNoNodesContainedDir).then(function(){
|
||||
var testConfig = typeRegistry.getNodeConfigs();
|
||||
|
||||
try {
|
||||
testConfig.should.equal( "<script type=\"text/x-red\" data-template-name=\"testFileInjector\">\n" +
|
||||
" <div class=\"form-row\">\n" +
|
||||
" <label for=\"node-input-name\"><i class=\"icon-tag\"></i> Name</label>\n" +
|
||||
" <input type=\"text\" id=\"node-input-name\" placeholder=\"Name\">\n" +
|
||||
" </div>\n" +
|
||||
"</script><script type=\"text/x-red\" data-help-name=\"node-type\">\n" +
|
||||
" <p>This node is pretty useless</p>\n" +
|
||||
"</script><script type=\"text/javascript\">RED.nodes.registerType(\"testFileInjector\",{category:\"storage-input\",inputs:0,outputs:1,icon:\"file.png\"});</script>");
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
|
||||
fs.exists(nodeInjectedFilePath, function(exists) {
|
||||
if(exists) {
|
||||
done();
|
||||
} else {
|
||||
try {
|
||||
should.fail(null, null, nodeInjectedFilePath + " should be created by registered test node.");
|
||||
} catch(err) {
|
||||
done(err)
|
||||
}
|
||||
}
|
||||
});
|
||||
}).catch(function(e) {
|
||||
try {
|
||||
should.fail(null, null, "Loading of nodesDir should succeed");
|
||||
} catch (err) {
|
||||
done(err);
|
||||
}
|
||||
done(e);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
it('handles nodes that export a function returning a resolving promise', function(done) {
|
||||
typeRegistry.init({});
|
||||
typeRegistry.load(__dirname+"/resources/TestNode2").then(function() {
|
||||
var list = typeRegistry.getNodeList();
|
||||
list.should.be.an.Array.and.have.lengthOf(1);
|
||||
list[0].should.have.property("id");
|
||||
list[0].should.have.property("name","TestNode2.js");
|
||||
list[0].should.have.property("types",["test-node-2"]);
|
||||
list[0].should.have.property("enabled",true);
|
||||
list[0].should.not.have.property("err");
|
||||
var nodeConstructor = typeRegistry.get("test-node-2");
|
||||
(typeof nodeConstructor).should.be.equal("function");
|
||||
|
||||
done();
|
||||
}).catch(function(e) {
|
||||
done(e);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('handles nodes that export a function returning a rejecting promise', function(done) {
|
||||
typeRegistry.init({});
|
||||
typeRegistry.load(__dirname+"/resources/TestNode3").then(function() {
|
||||
var list = typeRegistry.getNodeList();
|
||||
list.should.be.an.Array.and.have.lengthOf(1);
|
||||
list[0].should.have.property("id");
|
||||
list[0].should.have.property("name","TestNode3.js");
|
||||
list[0].should.have.property("types",["test-node-3"]);
|
||||
list[0].should.have.property("enabled",false);
|
||||
|
||||
list[0].should.have.property("err","fail");
|
||||
|
||||
var nodeConstructor = typeRegistry.get("test-node-3");
|
||||
(typeof nodeConstructor).should.be.equal("undefined");
|
||||
|
||||
done();
|
||||
}).catch(function(e) {
|
||||
done(e);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('handles files containing multiple nodes', function(done) {
|
||||
typeRegistry.init({});
|
||||
typeRegistry.load(__dirname+"/resources/MultipleNodes1").then(function() {
|
||||
var list = typeRegistry.getNodeList();
|
||||
list.should.be.an.Array.and.have.lengthOf(1);
|
||||
list[0].should.have.property("id");
|
||||
list[0].should.have.property("name","MultipleNodes1.js");
|
||||
list[0].should.have.property("types",["test-node-multiple-1a","test-node-multiple-1b"]);
|
||||
list[0].should.have.property("enabled",true);
|
||||
list[0].should.not.have.property("err");
|
||||
|
||||
var nodeConstructor = typeRegistry.get("test-node-multiple-1a");
|
||||
(typeof nodeConstructor).should.be.equal("function");
|
||||
|
||||
nodeConstructor = typeRegistry.get("test-node-multiple-1b");
|
||||
(typeof nodeConstructor).should.be.equal("function");
|
||||
|
||||
done();
|
||||
}).catch(function(e) {
|
||||
done(e);
|
||||
});
|
||||
});
|
||||
|
||||
it('handles nested directories', function(done) {
|
||||
typeRegistry.init({});
|
||||
typeRegistry.load(__dirname+"/resources/NestedDirectoryNode").then(function() {
|
||||
var list = typeRegistry.getNodeList();
|
||||
list.should.be.an.Array.and.have.lengthOf(1);
|
||||
list[0].should.have.property("id");
|
||||
list[0].should.have.property("name","NestedNode.js");
|
||||
list[0].should.have.property("types",["nested-node-1"]);
|
||||
list[0].should.have.property("enabled",true);
|
||||
list[0].should.not.have.property("err");
|
||||
done();
|
||||
}).catch(function(e) {
|
||||
done(e);
|
||||
});
|
||||
});
|
||||
|
||||
it('emits type-registered and node-icon-dir events', function(done) {
|
||||
var eventEmitSpy = sinon.spy(events,"emit");
|
||||
typeRegistry.init({});
|
||||
typeRegistry.load(__dirname+"/resources/NestedDirectoryNode").then(function() {
|
||||
var list = typeRegistry.getNodeList();
|
||||
list.should.be.an.Array.and.have.lengthOf(1);
|
||||
list[0].should.have.property("name","NestedNode.js");
|
||||
list[0].should.have.property("types",["nested-node-1"]);
|
||||
list[0].should.have.property("enabled",true);
|
||||
list[0].should.not.have.property("err");
|
||||
|
||||
eventEmitSpy.calledTwice.should.be.true;
|
||||
|
||||
eventEmitSpy.firstCall.args[0].should.be.equal("node-icon-dir");
|
||||
eventEmitSpy.firstCall.args[1].should.be.equal(__dirname+"/resources/NestedDirectoryNode/NestedNode/icons");
|
||||
|
||||
eventEmitSpy.secondCall.args[0].should.be.equal("type-registered");
|
||||
eventEmitSpy.secondCall.args[1].should.be.equal("nested-node-1");
|
||||
|
||||
done();
|
||||
}).catch(function(e) {
|
||||
done(e);
|
||||
}).finally(function() {
|
||||
eventEmitSpy.restore();
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects a duplicate node type registration', function(done) {
|
||||
typeRegistry.init({
|
||||
nodesDir:[__dirname+"/resources/TestNode1",__dirname+"/resources/DuplicateTestNode"]
|
||||
});
|
||||
typeRegistry.load("wontexist").then(function() {
|
||||
var list = typeRegistry.getNodeList();
|
||||
|
||||
list.should.be.an.Array.and.have.lengthOf(2);
|
||||
list[0].should.have.property("id");
|
||||
list[0].should.have.property("name","TestNode1.js");
|
||||
list[0].should.have.property("types",["test-node-1"]);
|
||||
list[0].should.have.property("enabled",true);
|
||||
list[0].should.not.have.property("err");
|
||||
|
||||
list[1].should.have.property("id");
|
||||
list[1].id.should.not.equal(list[0].id);
|
||||
|
||||
list[1].should.have.property("name","TestNode1.js");
|
||||
list[1].should.have.property("types",["test-node-1"]);
|
||||
list[1].should.have.property("enabled",false);
|
||||
list[1].should.have.property("err");
|
||||
/already registered/.test(list[1].err).should.be.true;
|
||||
|
||||
var nodeConstructor = typeRegistry.get("test-node-1");
|
||||
// Verify the duplicate node hasn't replaced the original one
|
||||
nodeConstructor.name.should.be.equal("TestNode");
|
||||
|
||||
done();
|
||||
}).catch(function(e) {
|
||||
done(e);
|
||||
});
|
||||
});
|
||||
|
||||
it('handles nodesDir as a string', function(done) {
|
||||
var settings = {
|
||||
nodesDir : __dirname+"/resources/TestNode1"
|
||||
}
|
||||
|
||||
typeRegistry.init(settings);
|
||||
typeRegistry.load("wontexist").then(function(){
|
||||
var list = typeRegistry.getNodeList();
|
||||
list.should.be.an.Array.and.have.lengthOf(1);
|
||||
list[0].should.have.property("types",["test-node-1"]);
|
||||
done();
|
||||
}).catch(function(e) {
|
||||
done("Loading of non-existing nodesDir should succeed");
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('handles invalid nodesDir',function(done) {
|
||||
var settings = {
|
||||
nodesDir : "wontexist"
|
||||
}
|
||||
|
||||
typeRegistry.init(settings);
|
||||
typeRegistry.load("wontexist").then(function(){
|
||||
var list = typeRegistry.getNodeList();
|
||||
list.should.be.an.Array.and.be.empty;
|
||||
done();
|
||||
}).catch(function(e) {
|
||||
done("Loading of non-existing nodesDir should succeed");
|
||||
});
|
||||
});
|
||||
|
||||
it('returns nothing for an unregistered type config', function() {
|
||||
typeRegistry.init({});
|
||||
typeRegistry.load("wontexist").then(function(){
|
||||
var config = typeRegistry.getNodeConfig("imaginary-shark");
|
||||
(config === null).should.be.true;
|
||||
}).catch(function(e) {
|
||||
done(e);
|
||||
});
|
||||
});
|
||||
|
||||
it('excludes node files listed in nodesExcludes',function(done) {
|
||||
typeRegistry.init({
|
||||
nodesExcludes: [ "TestNode1.js" ],
|
||||
nodesDir:[__dirname+"/resources/TestNode1",__dirname+"/resources/TestNode2"]
|
||||
});
|
||||
typeRegistry.load("wontexist").then(function() {
|
||||
var list = typeRegistry.getNodeList();
|
||||
list.should.be.an.Array.and.have.lengthOf(1);
|
||||
list[0].should.have.property("types",["test-node-2"]);
|
||||
done();
|
||||
}).catch(function(e) {
|
||||
done(e);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns the node configurations', function(done) {
|
||||
typeRegistry.init({
|
||||
nodesDir:[__dirname+"/resources/TestNode1",__dirname+"/resources/TestNode2"]
|
||||
});
|
||||
typeRegistry.load("wontexist").then(function() {
|
||||
var list = typeRegistry.getNodeList();
|
||||
|
||||
var nodeConfigs = typeRegistry.getNodeConfigs();
|
||||
|
||||
// TODO: this is brittle...
|
||||
nodeConfigs.should.equal("<script type=\"text/x-red\" data-template-name=\"test-node-1\"></script><script type=\"text/x-red\" data-help-name=\"test-node-1\"></script><style></style><script type=\"text/x-red\" data-template-name=\"test-node-2\"></script><script type=\"text/x-red\" data-help-name=\"test-node-2\"></script><style></style><script type=\"text/javascript\">RED.nodes.registerType(\"test-node-1\",{}),RED.nodes.registerType(\"test-node-2\",{});</script>");
|
||||
|
||||
var nodeId = list[0].id;
|
||||
var nodeConfig = typeRegistry.getNodeConfig(nodeId);
|
||||
nodeConfig.should.equal("<script type=\"text/x-red\" data-template-name=\"test-node-1\"></script><script type=\"text/x-red\" data-help-name=\"test-node-1\"></script><style></style><script type=\"text/javascript\">RED.nodes.registerType('test-node-1',{});</script>");
|
||||
done();
|
||||
}).catch(function(e) {
|
||||
done(e);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user