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

Merge pull request #262 from hindessm/improve-node-coverage

Improve red/nodes/Node.js coverage and minor refactoring
This commit is contained in:
Dave Conway-Jones 2014-07-11 14:04:27 +01:00
commit 9158766297
2 changed files with 80 additions and 17 deletions

View File

@ -62,7 +62,7 @@ Node.prototype.close = function() {
Node.prototype.send = function(msg) {
// instanceof doesn't work for some reason here
if (msg == null) {
msg = [];
return;
} else if (!util.isArray(msg)) {
msg = [msg];
}
@ -119,27 +119,26 @@ Node.prototype.receive = function(msg) {
this.emit("input",msg);
}
function log_helper(self, level, msg) {
var o = {level:level, id:self.id, type:self.type, msg:msg};
if (self.name) {
o.name = self.name;
}
self.emit("log",o);
}
Node.prototype.log = function(msg) {
var o = {level:'log',id:this.id, type:this.type, msg:msg};
if (this.name) {
o.name = this.name;
}
this.emit("log",o);
log_helper(this, 'log', msg);
}
Node.prototype.warn = function(msg) {
var o = {level:'warn',id:this.id, type:this.type, msg:msg};
if (this.name) {
o.name = this.name;
}
this.emit("log",o);
log_helper(this, 'warn', msg);
}
Node.prototype.error = function(msg) {
var o = {level:'error',id:this.id, type:this.type, msg:msg};
if (this.name) {
o.name = this.name;
}
this.emit("log",o);
log_helper(this, 'error', msg);
}
/**
* status: { fill:"red|green", shape:"dot|ring", text:"blah" }
*/

View File

@ -170,6 +170,70 @@ describe('Node', function() {
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");
});
});
});