From f80cbf729a27e83fe179668f973484677abaa9b5 Mon Sep 17 00:00:00 2001 From: Nicholas O'Leary Date: Sun, 20 Oct 2013 21:42:01 +0100 Subject: [PATCH] Add some initial tests --- package.json | 55 ++++++++------ test/node_registry_spec.js | 14 ++++ test/node_spec.js | 143 +++++++++++++++++++++++++++++++++++++ 3 files changed, 191 insertions(+), 21 deletions(-) create mode 100644 test/node_registry_spec.js create mode 100644 test/node_spec.js diff --git a/package.json b/package.json index a29aa7fa3..98ac1a934 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,36 @@ { - "name": "node-red", - "version": "0.2.0", - "description": "A visual tool for wiring the Internet of Things", - "homepage": "http://nodered.org", - "scripts": { - "start": "node red.js" - }, - "main": "red/red.js", - "author": "Nick O'Leary", - "contributors": [ {"name": "Dave Conway-Jones"} ], - "keywords": ["editor", "messaging", "iot", "m2m", "pi", "arduino", "beaglebone", "ibm"], - "license": "Apache", - "dependencies": { - "express": "3.x", - "mqtt": "*", - "ws": "*", - "mustache": "*", - "cron":"*" - }, - "engines": { "node": ">=0.8" }, - "repository": {"type":"git","url":"https://github.com/node-red/node-red.git"} + "name" : "node-red", + "version" : "0.2.0", + "description" : "A visual tool for wiring the Internet of Things", + "homepage" : "http://nodered.org", + "license" : "Apache", + "repository" : { + "type":"git", + "url":"https://github.com/node-red/node-red.git" + }, + "main" : "red/red.js", + "scripts" : { + "start": "node red.js" + }, + "contributors": [ + {"name": "Nick O'Leary"} + {"name": "Dave Conway-Jones"} + ], + "keywords": [ + "editor", "messaging", "iot", "m2m", "pi", "arduino", "beaglebone", "ibm + ], + "dependencies": { + "express": "3.x", + "mqtt": "~0.3.3", + "ws": "~0.4.31", + "mustache": "~0.7.2", + "cron":"1.x" + }, + "devDependencies": { + "mocha": "~1.12.0", + "should": "~1.2.2" + }, + "engines": { + "node": ">=0.8" + } } diff --git a/test/node_registry_spec.js b/test/node_registry_spec.js new file mode 100644 index 000000000..9b195822d --- /dev/null +++ b/test/node_registry_spec.js @@ -0,0 +1,14 @@ +var should = require("should"); +var RedNodes = require("../red/nodes.js"); + +var RedNode = RedNodes.Node; + +describe('NodeRegistry', function() { + it('automatically registers new nodes',function() { + should.not.exist(RedNodes.getNode('123')); + var n = new RedNode({id:'123',type:'abc'}); + should.strictEqual(n,RedNodes.getNode('123')); + }); +}) + + diff --git a/test/node_spec.js b/test/node_spec.js new file mode 100644 index 000000000..561ff3881 --- /dev/null +++ b/test/node_spec.js @@ -0,0 +1,143 @@ +var should = require("should"); +var RedNodes = require("../red/nodes.js"); + +var RedNode = RedNodes.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.a.length(2); + }); + + }); + + describe('#close', function() { + it('emits close event when closed',function(done) { + var n = new RedNode({id:'123',type:'abc'}); + n.on('close',done); + n.close(); + }); + }); + + 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); + }); + }); + +});