mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	add extra tests to debug, delay & template
This commit is contained in:
		| @@ -25,14 +25,11 @@ module.exports = function(RED) { | ||||
|     function DebugNode(n) { | ||||
|         RED.nodes.createNode(this,n); | ||||
|         this.name = n.name; | ||||
|         this.complete = n.complete||"payload"; | ||||
|         this.complete = (n.complete||"payload").toString(); | ||||
|  | ||||
|         if (this.complete === "false") { | ||||
|             this.complete = "payload"; | ||||
|         } | ||||
|         if (this.complete === true) { | ||||
|             this.complete = "true"; | ||||
|         } | ||||
|  | ||||
|         this.console = n.console; | ||||
|         this.active = (n.active === null || typeof n.active === "undefined") || n.active; | ||||
|   | ||||
| @@ -31,32 +31,29 @@ module.exports = function(RED) { | ||||
|  | ||||
|         if (n.timeoutUnits === "milliseconds") { | ||||
|             this.timeout = n.timeout; | ||||
|         } else if (n.timeoutUnits === "seconds") { | ||||
|             this.timeout = n.timeout * 1000; | ||||
|         }  else if (n.timeoutUnits === "minutes") { | ||||
|             this.timeout = n.timeout * (60 * 1000); | ||||
|         } else if (n.timeoutUnits === "hours") { | ||||
|             this.timeout = n.timeout * (60 * 60 * 1000); | ||||
|         } else if (n.timeoutUnits === "days") { | ||||
|             this.timeout = n.timeout * (24 * 60 * 60 * 1000); | ||||
|         } else {   // Default to seconds | ||||
|             this.timeout = n.timeout * 1000; | ||||
|         } | ||||
|  | ||||
|         if (n.rateUnits === "second") { | ||||
|             this.rate = 1000/n.rate; | ||||
|         } else if (n.rateUnits === "minute") { | ||||
|         if (n.rateUnits === "minute") { | ||||
|             this.rate = (60 * 1000)/n.rate; | ||||
|         } else if (n.rateUnits === "hour") { | ||||
|             this.rate = (60 * 60 * 1000)/n.rate; | ||||
|         } else if (n.rateUnits === "day") { | ||||
|             this.rate = (24 * 60 * 60 * 1000)/n.rate; | ||||
|         } else {  // Default to seconds | ||||
|             this.rate = 1000/n.rate; | ||||
|         } | ||||
|  | ||||
|         if (n.randomUnits === "milliseconds") { | ||||
|             this.randomFirst = n.randomFirst * 1; | ||||
|             this.randomLast = n.randomLast * 1; | ||||
|         } else if (n.randomUnits === "seconds") { | ||||
|             this.randomFirst = n.randomFirst * 1000; | ||||
|             this.randomLast = n.randomLast * 1000; | ||||
|         } else if (n.randomUnits === "minutes") { | ||||
|             this.randomFirst = n.randomFirst * (60 * 1000); | ||||
|             this.randomLast = n.randomLast * (60 * 1000); | ||||
| @@ -66,6 +63,9 @@ module.exports = function(RED) { | ||||
|         } else if (n.randomUnits === "days") { | ||||
|             this.randomFirst = n.randomFirst * (24 * 60 * 60 * 1000); | ||||
|             this.randomLast = n.randomLast * (24 * 60 * 60 * 1000); | ||||
|         } else {  // Default to seconds | ||||
|             this.randomFirst = n.randomFirst * 1000; | ||||
|             this.randomLast = n.randomLast * 1000; | ||||
|         } | ||||
|  | ||||
|         this.diff = this.randomLast - this.randomFirst; | ||||
|   | ||||
| @@ -31,10 +31,11 @@ describe('debug node', function() { | ||||
|  | ||||
|  | ||||
|     it('should be loaded', function(done) { | ||||
|         var flow = [{id:"n1", type:"debug", name: "Debug" }]; | ||||
|         var flow = [{id:"n1", type:"debug", name: "Debug", complete:"false" }]; | ||||
|         helper.load(debugNode, flow, function() { | ||||
|             var n1 = helper.getNode("n1"); | ||||
|             n1.should.have.property('name', 'Debug'); | ||||
|             n1.should.have.property('complete', "payload"); | ||||
|             done(); | ||||
|         }); | ||||
|     }); | ||||
|   | ||||
| @@ -43,4 +43,51 @@ describe('template node', function() { | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
|     it('should handle if the field isn\'t set', function(done) { | ||||
|         var flow = [{id:"n1", type:"template", template: "payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}]; | ||||
|         helper.load(templateNode, flow, function() { | ||||
|             var n1 = helper.getNode("n1"); | ||||
|             var n2 = helper.getNode("n2"); | ||||
|             n2.on("input", function(msg) { | ||||
|                 msg.should.have.property('topic', 'bar'); | ||||
|                 msg.should.have.property('payload', 'payload=foo'); | ||||
|                 done(); | ||||
|             }); | ||||
|             n1.receive({payload:"foo",topic: "bar"}); | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
|     it('should handle deeper objects', function(done) { | ||||
|         var flow = [{id:"n1", type:"template", field: "topic.foo.bar", template: "payload={{payload.doh.rei.me}}",wires:[["n2"]]},{id:"n2",type:"helper"}]; | ||||
|         helper.load(templateNode, flow, function() { | ||||
|             var n1 = helper.getNode("n1"); | ||||
|             var n2 = helper.getNode("n2"); | ||||
|             n2.on("input", function(msg) { | ||||
|                 msg.should.have.property('topic'); | ||||
|                 msg.topic.should.have.property('foo'); | ||||
|                 msg.topic.foo.should.have.a.property('bar', 'payload=foo'); | ||||
|                 done(); | ||||
|             }); | ||||
|             n1.receive({payload:{doh:{rei:{me:"foo"}}}, topic:"bar"}); | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
|     it('should raise error if passed bad template', function(done) { | ||||
|         var flow = [{id:"n1", type:"template", field: "payload", template: "payload={{payload",wires:[["n2"]]},{id:"n2",type:"helper"}]; | ||||
|         helper.load(templateNode, flow, function() { | ||||
|             var n1 = helper.getNode("n1"); | ||||
|             var n2 = helper.getNode("n2"); | ||||
|             setTimeout(function() { | ||||
|                 var logEvents = helper.log().args.filter(function(evt) { | ||||
|                     return evt[0].type == "template"; | ||||
|                 }); | ||||
|                 logEvents.should.have.length(1); | ||||
|                 logEvents[0][0].should.have.a.property('msg'); | ||||
|                 logEvents[0][0].msg.toString().should.startWith("Unclosed tag at "); | ||||
|                 done(); | ||||
|             },25); | ||||
|             n1.receive({payload:"foo"}); | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
| }); | ||||
|   | ||||
| @@ -41,10 +41,31 @@ describe('delayNode', function() { | ||||
|     }); | ||||
|  | ||||
|     it('should be loaded', function(done) { | ||||
|         var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[[]]}]; | ||||
|         var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"day","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[[]]}]; | ||||
|         helper.load(delayNode, flow, function() { | ||||
|             var delayNode1 = helper.getNode("delayNode1"); | ||||
|             delayNode1.should.have.property('name', 'delayNode'); | ||||
|             delayNode1.should.have.property('rate', 86400000); | ||||
|             done(); | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
|     it('should be able to set rate to hour', function(done) { | ||||
|         var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"hour","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[[]]}]; | ||||
|         helper.load(delayNode, flow, function() { | ||||
|             var delayNode1 = helper.getNode("delayNode1"); | ||||
|             delayNode1.should.have.property('name', 'delayNode'); | ||||
|             delayNode1.should.have.property('rate', 3600000); | ||||
|             done(); | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
|     it('should be able to set rate to minute', function(done) { | ||||
|         var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[[]]}]; | ||||
|         helper.load(delayNode, flow, function() { | ||||
|             var delayNode1 = helper.getNode("delayNode1"); | ||||
|             delayNode1.should.have.property('name', 'delayNode'); | ||||
|             delayNode1.should.have.property('rate', 60000); | ||||
|             done(); | ||||
|         }); | ||||
|     }); | ||||
| @@ -417,4 +438,42 @@ describe('delayNode', function() { | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
|     it('handles delay queue', function(done) { | ||||
|         this.timeout(6000); | ||||
|  | ||||
|         var flow = [{"id":"delayNode1","type":"delay","name":"delayNode","pauseType":"queue","timeout":5,"timeoutUnits":"seconds","rate":1000,"rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"wires":[["helperNode1"]]}, | ||||
|                     {id:"helperNode1", type:"helper", wires:[]}]; | ||||
|         helper.load(delayNode, flow, function() { | ||||
|             var delayNode1 = helper.getNode("delayNode1"); | ||||
|             var helperNode1 = helper.getNode("helperNode1"); | ||||
|             var messages = 2; | ||||
|             var c = 0; | ||||
|  | ||||
|             helperNode1.on("input", function(msg) { | ||||
|                 c += 1; | ||||
|                 msg.should.have.a.property('payload'); | ||||
|                 msg.should.have.a.property('topic'); | ||||
|                 if (msg.topic === "A") { | ||||
|                     msg.payload.should.equal(4); | ||||
|                 } | ||||
|                 else { | ||||
|                     msg.topic.should.equal("_none_"); | ||||
|                     msg.payload.should.equal(2); | ||||
|                 } | ||||
|                 if (c == 2) { | ||||
|                     done(); // it will timeout if we don't receive both messages | ||||
|                 } | ||||
|             }); | ||||
|  | ||||
|             // send test messages | ||||
|             delayNode1.receive({payload:1,topic:"A"}); | ||||
|             delayNode1.receive({payload:1}); | ||||
|             delayNode1.receive({payload:2,topic:"A"}); | ||||
|             delayNode1.receive({payload:3,topic:"A"}); | ||||
|             delayNode1.receive({payload:2});            // only this should get out | ||||
|             delayNode1.receive({payload:4,topic:"A"});  // and this one also | ||||
|  | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
| }); | ||||
|   | ||||
| @@ -18,7 +18,7 @@ var should = require("should"); | ||||
| var commentNode = require("../../../../nodes/core/core/90-comment.js"); | ||||
| var helper = require("../../helper.js"); | ||||
|  | ||||
| describe('comment node', function() { | ||||
| describe('comment Node', function() { | ||||
|  | ||||
|     afterEach(function() { | ||||
|         helper.unload(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user