2014-05-03 23:26:35 +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.
|
|
|
|
**/
|
|
|
|
|
2013-10-20 22:42:01 +02:00
|
|
|
var should = require("should");
|
2014-08-01 23:05:49 +02:00
|
|
|
var sinon = require("sinon");
|
2013-10-20 22:42:01 +02:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
var RedNodes = require("../../../red/nodes");
|
2014-07-17 09:06:30 +02:00
|
|
|
var RedNode = require("../../../red/nodes/Node");
|
2014-08-01 23:05:49 +02:00
|
|
|
var typeRegistry = require("../../../red/nodes/registry");
|
|
|
|
var events = require("../../../red/events");
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
typeRegistry.clear();
|
|
|
|
});
|
2013-10-20 22:42:01 +02:00
|
|
|
|
|
|
|
describe('NodeRegistry', function() {
|
|
|
|
it('automatically registers new nodes',function() {
|
2014-03-31 15:41:51 +02:00
|
|
|
var testNode = RedNodes.getNode('123');
|
|
|
|
should.not.exist(n);
|
2013-10-20 22:42:01 +02:00
|
|
|
var n = new RedNode({id:'123',type:'abc'});
|
2014-03-31 15:41:51 +02:00
|
|
|
|
|
|
|
var newNode = RedNodes.getNode('123');
|
|
|
|
|
|
|
|
should.strictEqual(n,newNode);
|
2013-10-20 22:42:01 +02:00
|
|
|
});
|
2014-08-01 23:05:49 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}).catch(function(e) {
|
|
|
|
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);
|
2014-07-30 19:07:20 +02:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
list[0].should.have.property("err","fail");
|
2014-07-30 19:07:20 +02:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
var nodeConstructor = typeRegistry.get("test-node-3");
|
|
|
|
(typeof nodeConstructor).should.be.equal("undefined");
|
|
|
|
|
2014-07-30 19:07:20 +02:00
|
|
|
done();
|
2014-08-01 23:05:49 +02:00
|
|
|
}).catch(function(e) {
|
|
|
|
done(e);
|
2014-07-30 19:07:20 +02:00
|
|
|
});
|
2014-08-01 23:05:49 +02:00
|
|
|
|
2014-07-30 19:07:20 +02:00
|
|
|
});
|
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
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");
|
2014-07-30 19:07:20 +02:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
nodeConstructor = typeRegistry.get("test-node-multiple-1b");
|
|
|
|
(typeof nodeConstructor).should.be.equal("function");
|
|
|
|
|
2014-07-30 19:07:20 +02:00
|
|
|
done();
|
2014-08-01 23:05:49 +02:00
|
|
|
}).catch(function(e) {
|
|
|
|
done(e);
|
2014-07-30 19:07:20 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2014-07-30 19:07:20 +02:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
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");
|
|
|
|
|
2014-08-01 23:19:33 +02:00
|
|
|
eventEmitSpy.callCount.should.equal(2);
|
2014-08-01 23:05:49 +02:00
|
|
|
|
|
|
|
eventEmitSpy.firstCall.args[0].should.be.equal("node-icon-dir");
|
|
|
|
eventEmitSpy.firstCall.args[1].should.be.equal(__dirname+"/resources/NestedDirectoryNode/NestedNode/icons");
|
2014-07-30 19:07:20 +02:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
eventEmitSpy.secondCall.args[0].should.be.equal("type-registered");
|
|
|
|
eventEmitSpy.secondCall.args[1].should.be.equal("nested-node-1");
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(function(e) {
|
2014-08-01 23:19:33 +02:00
|
|
|
console.log(eventEmitSpy);
|
2014-08-01 23:05:49 +02:00
|
|
|
done(e);
|
|
|
|
}).finally(function() {
|
|
|
|
eventEmitSpy.restore();
|
|
|
|
});
|
2014-07-30 19:07:20 +02:00
|
|
|
});
|
2014-08-01 23:05:49 +02:00
|
|
|
|
|
|
|
it('rejects a duplicate node type registration', function(done) {
|
|
|
|
typeRegistry.init({
|
|
|
|
nodesDir:[__dirname+"/resources/TestNode1",__dirname+"/resources/DuplicateTestNode"]
|
2014-07-30 19:07:20 +02:00
|
|
|
});
|
2014-08-01 23:05:49 +02:00
|
|
|
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);
|
2014-07-30 19:07:20 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
it('handles nodesDir as a string', function(done) {
|
2014-07-30 19:07:20 +02:00
|
|
|
var settings = {
|
2014-08-01 23:05:49 +02:00
|
|
|
nodesDir : __dirname+"/resources/TestNode1"
|
2014-07-30 19:07:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
typeRegistry.init(settings);
|
2014-08-01 23:05:49 +02:00
|
|
|
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();
|
2014-07-30 19:07:20 +02:00
|
|
|
}).catch(function(e) {
|
2014-08-01 23:05:49 +02:00
|
|
|
done(e);
|
2014-07-30 19:07:20 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|