mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Move tests to match source hierarchy.
This commit is contained in:
239
test/red/nodes/Node_spec.js
Normal file
239
test/red/nodes/Node_spec.js
Normal file
@@ -0,0 +1,239 @@
|
||||
/**
|
||||
* 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 RedNode = require("../../../red/nodes/Node");
|
||||
|
||||
describe('Node', function() {
|
||||
describe('#constructor',function() {
|
||||
it('is called with an id and a type',function() {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
n.should.have.property('id','123');
|
||||
n.should.have.property('type','abc');
|
||||
n.should.not.have.property('name');
|
||||
n.wires.should.be.empty;
|
||||
});
|
||||
|
||||
it('is called with an id, a type and a name',function() {
|
||||
var n = new RedNode({id:'123',type:'abc',name:'barney'});
|
||||
n.should.have.property('id','123');
|
||||
n.should.have.property('type','abc');
|
||||
n.should.have.property('name','barney');
|
||||
n.wires.should.be.empty;
|
||||
});
|
||||
|
||||
it('is called with an id, a type and some wires',function() {
|
||||
var n = new RedNode({id:'123',type:'abc',wires:['123','456']});
|
||||
n.should.have.property('id','123');
|
||||
n.should.have.property('type','abc');
|
||||
n.should.not.have.property('name');
|
||||
n.wires.should.have.length(2);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#close', function() {
|
||||
it('emits close event when closed',function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
n.on('close',function() {
|
||||
done();
|
||||
});
|
||||
var p = n.close();
|
||||
should.not.exist(p);
|
||||
});
|
||||
|
||||
it('returns a promise when provided a callback with a done parameter',function(testdone) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
n.on('close',function(done) {
|
||||
setTimeout(function() {
|
||||
done();
|
||||
},200);
|
||||
});
|
||||
var p = n.close();
|
||||
should.exist(p);
|
||||
p.then(function() {
|
||||
testdone();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('#receive', function() {
|
||||
it('emits input event when called', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
var message = {payload:"hello world"};
|
||||
n.on('input',function(msg) {
|
||||
should.deepEqual(msg,message);
|
||||
done();
|
||||
});
|
||||
n.receive(message);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#send', function() {
|
||||
|
||||
it('emits a single message', function(done) {
|
||||
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
|
||||
var n2 = new RedNode({id:'n2',type:'abc'});
|
||||
var message = {payload:"hello world"};
|
||||
|
||||
n2.on('input',function(msg) {
|
||||
// msg equals message, but is a new copy
|
||||
should.deepEqual(msg,message);
|
||||
should.notStrictEqual(msg,message);
|
||||
done();
|
||||
});
|
||||
|
||||
n1.send(message);
|
||||
});
|
||||
|
||||
it('emits multiple messages on a single output', function(done) {
|
||||
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
|
||||
var n2 = new RedNode({id:'n2',type:'abc'});
|
||||
|
||||
var messages = [
|
||||
{payload:"hello world"},
|
||||
{payload:"hello world again"}
|
||||
];
|
||||
|
||||
var rcvdCount = 0;
|
||||
|
||||
n2.on('input',function(msg) {
|
||||
should.deepEqual(msg,messages[rcvdCount]);
|
||||
should.notStrictEqual(msg,messages[rcvdCount]);
|
||||
rcvdCount += 1;
|
||||
if (rcvdCount == 2) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
n1.send([messages]);
|
||||
});
|
||||
|
||||
it('emits messages to multiple outputs', function(done) {
|
||||
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2'],['n3'],['n4','n5']]});
|
||||
var n2 = new RedNode({id:'n2',type:'abc'});
|
||||
var n3 = new RedNode({id:'n3',type:'abc'});
|
||||
var n4 = new RedNode({id:'n4',type:'abc'});
|
||||
var n5 = new RedNode({id:'n5',type:'abc'});
|
||||
|
||||
var messages = [
|
||||
{payload:"hello world"},
|
||||
null,
|
||||
{payload:"hello world again"}
|
||||
];
|
||||
|
||||
var rcvdCount = 0;
|
||||
|
||||
n2.on('input',function(msg) {
|
||||
should.deepEqual(msg,messages[0]);
|
||||
should.notStrictEqual(msg,messages[0]);
|
||||
rcvdCount += 1;
|
||||
if (rcvdCount == 3) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
n3.on('input',function(msg) {
|
||||
should.fail(null,null,"unexpected message");
|
||||
});
|
||||
|
||||
n4.on('input',function(msg) {
|
||||
should.deepEqual(msg,messages[2]);
|
||||
should.notStrictEqual(msg,messages[2]);
|
||||
rcvdCount += 1;
|
||||
if (rcvdCount == 3) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
n5.on('input',function(msg) {
|
||||
should.deepEqual(msg,messages[2]);
|
||||
should.notStrictEqual(msg,messages[2]);
|
||||
rcvdCount += 1;
|
||||
if (rcvdCount == 3) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
n1.send(messages);
|
||||
});
|
||||
|
||||
it('emits no messages', function(done) {
|
||||
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
|
||||
var n2 = new RedNode({id:'n2',type:'abc'});
|
||||
|
||||
n2.on('input',function(msg) {
|
||||
should.fail(null,null,"unexpected message");
|
||||
});
|
||||
|
||||
setTimeout(function() {
|
||||
done();
|
||||
}, 200);
|
||||
|
||||
n1.send();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#log', function() {
|
||||
it('emits a log message', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
n.on('log',function(obj) {
|
||||
should.deepEqual({level:"log", id:n.id,
|
||||
type:n.type, msg:"a log message"}, obj);
|
||||
done();
|
||||
});
|
||||
n.log("a log message");
|
||||
});
|
||||
});
|
||||
|
||||
describe('#log', function() {
|
||||
it('emits a log message with a name', function(done) {
|
||||
var n = new RedNode({id:'123', type:'abc', name:"barney"});
|
||||
n.on('log',function(obj) {
|
||||
should.deepEqual({level:"log", id:n.id, name: "barney",
|
||||
type:n.type, msg:"a log message"}, obj);
|
||||
done();
|
||||
});
|
||||
n.log("a log message");
|
||||
});
|
||||
});
|
||||
|
||||
describe('#warn', function() {
|
||||
it('emits a warning', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
n.on('log',function(obj) {
|
||||
should.deepEqual({level:"warn", id:n.id,
|
||||
type:n.type, msg:"a warning"}, obj);
|
||||
done();
|
||||
});
|
||||
n.warn("a warning");
|
||||
});
|
||||
});
|
||||
|
||||
describe('#error', function() {
|
||||
it('emits an error message', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
n.on('log',function(obj) {
|
||||
should.deepEqual({level:"error", id:n.id,
|
||||
type:n.type, msg:"an error message"}, obj);
|
||||
done();
|
||||
});
|
||||
n.error("an error message");
|
||||
});
|
||||
});
|
||||
|
||||
});
|
119
test/red/nodes/credentials_spec.js
Normal file
119
test/red/nodes/credentials_spec.js
Normal file
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* 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");
|
||||
var when = require("when");
|
||||
|
||||
var credentials = require("../red/nodes/credentials");
|
||||
|
||||
describe('Credentials', function() {
|
||||
|
||||
it('loads from storage',function(done) {
|
||||
|
||||
var storage = {
|
||||
getCredentials: function() {
|
||||
console.log("ONE");
|
||||
return when.promise(function(resolve,reject) {
|
||||
resolve({"a":{"b":1,"c":2}});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
credentials.init(storage);
|
||||
|
||||
credentials.load().then(function() {
|
||||
|
||||
credentials.get("a").should.have.property('b',1);
|
||||
credentials.get("a").should.have.property('c',2);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('saves to storage', function(done) {
|
||||
var storage = {
|
||||
getCredentials: function() {
|
||||
return when.promise(function(resolve,reject) {
|
||||
resolve({"a":{"b":1,"c":2}});
|
||||
});
|
||||
},
|
||||
saveCredentials: function(creds) {
|
||||
return when(true);
|
||||
}
|
||||
};
|
||||
sinon.spy(storage,"saveCredentials");
|
||||
credentials.init(storage);
|
||||
credentials.load().then(function() {
|
||||
should.not.exist(credentials.get("b"))
|
||||
credentials.add('b',{"d":3});
|
||||
storage.saveCredentials.callCount.should.be.exactly(1);
|
||||
credentials.get("b").should.have.property('d',3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('deletes from storage', function(done) {
|
||||
var storage = {
|
||||
getCredentials: function() {
|
||||
return when.promise(function(resolve,reject) {
|
||||
resolve({"a":{"b":1,"c":2}});
|
||||
});
|
||||
},
|
||||
saveCredentials: function(creds) {
|
||||
return when(true);
|
||||
}
|
||||
};
|
||||
sinon.spy(storage,"saveCredentials");
|
||||
credentials.init(storage);
|
||||
credentials.load().then(function() {
|
||||
should.exist(credentials.get("a"))
|
||||
credentials.delete('a');
|
||||
storage.saveCredentials.callCount.should.be.exactly(1);
|
||||
should.not.exist(credentials.get("a"));
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('clean up from storage', function(done) {
|
||||
var storage = {
|
||||
getCredentials: function() {
|
||||
return when.promise(function(resolve,reject) {
|
||||
resolve({"a":{"b":1,"c":2}});
|
||||
});
|
||||
},
|
||||
saveCredentials: function(creds) {
|
||||
return when(true);
|
||||
}
|
||||
};
|
||||
sinon.spy(storage,"saveCredentials");
|
||||
credentials.init(storage);
|
||||
credentials.load().then(function() {
|
||||
should.exist(credentials.get("a"));
|
||||
credentials.clean(function() {
|
||||
return false;
|
||||
});
|
||||
storage.saveCredentials.callCount.should.be.exactly(1);
|
||||
should.not.exist(credentials.get("a"));
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
|
121
test/red/nodes/flows_spec.js
Normal file
121
test/red/nodes/flows_spec.js
Normal file
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* 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;
|
||||
},
|
||||
saveCredentials: function (creds) {
|
||||
return when(true);
|
||||
}
|
||||
};
|
||||
RED.init({}, storage);
|
||||
flows.setFlows(testFlows);
|
||||
events.once('nodes-started', function() { done(); });
|
||||
});
|
||||
});
|
||||
|
||||
});
|
34
test/red/nodes/registry_spec.js
Normal file
34
test/red/nodes/registry_spec.js
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* 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 RedNodes = require("../../../red/nodes");
|
||||
|
||||
var RedNode = require("../../../red/nodes/Node");
|
||||
|
||||
describe('NodeRegistry', function() {
|
||||
it('automatically registers new nodes',function() {
|
||||
var testNode = RedNodes.getNode('123');
|
||||
should.not.exist(n);
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
|
||||
var newNode = RedNodes.getNode('123');
|
||||
|
||||
should.strictEqual(n,newNode);
|
||||
});
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user