diff --git a/test/nodes/core/core/80-function_spec.js b/test/nodes/core/core/80-function_spec.js index f31d0f903..d9dc4001d 100644 --- a/test/nodes/core/core/80-function_spec.js +++ b/test/nodes/core/core/80-function_spec.js @@ -1,18 +1,18 @@ /** - * Copyright JS Foundation and other contributors, http://js.foundation - * - * 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. - **/ +* Copyright JS Foundation and other contributors, http://js.foundation +* +* 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 functionNode = require("../../../../nodes/core/core/80-function.js"); @@ -65,7 +65,7 @@ describe('function node', function() { it('should send returned message', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -80,7 +80,7 @@ describe('function node', function() { it('should send returned message using send()', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"node.send(msg);"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -95,7 +95,7 @@ describe('function node', function() { it('should pass through _topic', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -111,8 +111,8 @@ describe('function node', function() { it('should send to multiple outputs', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"],["n3"]], - func:"return [{payload: '1'},{payload: '2'}];"}, - {id:"n2", type:"helper"}, {id:"n3", type:"helper"} ]; + func:"return [{payload: '1'},{payload: '2'}];"}, + {id:"n2", type:"helper"}, {id:"n3", type:"helper"} ]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -138,8 +138,8 @@ describe('function node', function() { it('should send to multiple messages', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]], - func:"return [[{payload: 1},{payload: 2}]];"}, - {id:"n2", type:"helper"} ]; + func:"return [[{payload: 1},{payload: 2}]];"}, + {id:"n2", type:"helper"} ]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -162,7 +162,7 @@ describe('function node', function() { it('should allow input to be discarded by returning null', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"return null"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -178,8 +178,8 @@ describe('function node', function() { it('should handle null amongst valid messages', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"return [[msg,null,msg],null]"}, - {id:"n2", type:"helper"}, - {id:"n3", type:"helper"}]; + {id:"n2", type:"helper"}, + {id:"n3", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -203,7 +203,7 @@ describe('function node', function() { it('should get keys in global context', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=global.keys();return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -219,7 +219,7 @@ describe('function node', function() { function testNonObjectMessage(functionText,done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:functionText}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -312,7 +312,7 @@ describe('function node', function() { it('should set node context', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.set('count','0');return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -328,138 +328,196 @@ describe('function node', function() { it('should set persistable node context (w/o callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.set('count','0','memory1');return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', 'foo'); - n1.context().get("count", "memory1", function (err, val) { - val.should.equal("0"); - done(); - }); - } - catch (e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n1.context().get("count", "memory1", function (err, val) { + val.should.equal("0"); + done(); + }); + } + catch (e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should set two persistable node context (w/o callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.set('count','0','memory1');context.set('count','1','memory2');return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', 'foo'); - n1.context().get("count", "memory1", function (err, val1) { - val1.should.equal("0"); - n1.context().get("count", "memory2", function (err, val2) { - val2.should.equal("1"); - done(); - }); - }); - } - catch (e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n1.context().get("count", "memory1", function (err, val1) { + val1.should.equal("0"); + n1.context().get("count", "memory2", function (err, val2) { + val2.should.equal("1"); + done(); + }); + }); + } + catch (e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); + it('should set two persistable node context (single call, w/o callback)', function(done) { + var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.set(['count1','count2'],['0','1'],'memory1');return msg;"}, + {id:"n2", type:"helper"}]; + helper.load(functionNode, flow, function() { + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n1.context().get("count1", "memory1", function (err, val1) { + val1.should.equal("0"); + n1.context().get("count2", "memory1", function (err, val2) { + val2.should.equal("1"); + done(); + }); + }); + } + catch (e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); + }); + }); + + it('should set persistable node context (w callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.set('count','0','memory1', function (err) { node.send(msg); });"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', 'foo'); - n1.context().get("count", "memory1", function (err, val) { - val.should.equal("0"); - done(); - }); - } - catch (e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n1.context().get("count", "memory1", function (err, val) { + val.should.equal("0"); + done(); + }); + } + catch (e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should set two persistable node context (w callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.set('count','0','memory1', function (err) { context.set('count', '1', 'memory2', function (err) { node.send(msg); }); });"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', 'foo'); - n1.context().get("count", "memory1", function (err, val1) { - val1.should.equal("0"); - n1.context().get("count", "memory1", function (err, val2) { - val2.should.equal("0"); - done(); - }); - }); - } - catch (e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n1.context().get("count", "memory1", function (err, val1) { + val1.should.equal("0"); + n1.context().get("count", "memory1", function (err, val2) { + val2.should.equal("0"); + done(); + }); + }); + } + catch (e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); + it('should set two persistable node context (single call, w callback)', function(done) { + var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.set(['count1','count2'],['0','1'],'memory1', function(err) { node.send(msg); });"}, + {id:"n2", type:"helper"}]; + helper.load(functionNode, flow, function() { + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n1.context().get("count1", "memory1", function (err, val1) { + val1.should.equal("0"); + n1.context().get("count2", "memory1", function (err, val2) { + val2.should.equal("1"); + done(); + }); + }); + } + catch (e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); + }); + }); + + it('should set default persistable node context', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.set('count','0');return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', 'foo'); - n1.context().get("count", "memory1", function (err, val) { - val.should.equal("0"); - done(); - }); - } - catch (e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n1.context().get("count", "memory1", function (err, val) { + val.should.equal("0"); + done(); + }); + } + catch (e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should get node context', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.get('count');return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -474,7 +532,7 @@ describe('function node', function() { }); function checkCallbackError(name, done) { - try { + try { helper.log().called.should.be.true(); var logEvents = helper.log().args.filter(function (evt) { return evt[0].type == "function"; @@ -484,29 +542,29 @@ describe('function node', function() { msg.should.have.property('level', helper.log().ERROR); msg.should.have.property('id', name); msg.should.have.property('type', 'function'); - msg.should.have.property('msg', 'Error: Callback must be a function'); + msg.should.have.property('msg', 'Error: Callback must be a function'); done(); - } - catch (e) { - done(e); - } + } + catch (e) { + done(e); + } } - + it('should handle error on get persistable node context (w/o callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.get('count','memory1');return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); n1.context().set("count","0","memory1"); n1.receive({payload:"foo",topic: "bar"}); - checkCallbackError('n1', done); + checkCallbackError('n1', done); }); }); it('should get persistable node context (w/ callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.get('count','memory1',function (err, val) { msg.payload=val; node.send(msg); });"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -522,7 +580,7 @@ describe('function node', function() { it('should get keys in node context', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.keys();return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -538,68 +596,68 @@ describe('function node', function() { it('should handle error on get keys in persistable node context (w/o callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.keys('memory1');return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n1.context().set("count","0","memory1"); - n1.receive({payload:"foo",topic: "bar"}); - checkCallbackError('n1', done); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.context().set("count","0","memory1"); + n1.receive({payload:"foo",topic: "bar"}); + checkCallbackError('n1', done); + }); }); }); it('should get keys in persistable node context (w/ callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.keys('memory1', function(err, keys) { msg.payload=keys; node.send(msg); });"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n1.context().set("count","0","memory1"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', ['count']); - done(); - } - catch(e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.context().set("count","0","memory1"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', ['count']); + done(); + } + catch(e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should get keys in default persistable node context', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.keys();return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n1.context().set("count","0","memory1"); - n1.context().set("number","1","memory2"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', ['count']); - done(); - } - catch(e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.context().set("count","0","memory1"); + n1.context().set("number","1","memory2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', ['count']); + done(); + } + catch(e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should set flow context', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"flow.set('count','0');return msg;"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -615,113 +673,113 @@ describe('function node', function() { it('should set persistable flow context (w/o callback)', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"flow.set('count','0','memory1');return msg;"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', 'foo'); - n2.context().flow.get("count", "memory1", function (err, val) { - val.should.equal("0"); - done(); - }); - } - catch(e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n2.context().flow.get("count", "memory1", function (err, val) { + val.should.equal("0"); + done(); + }); + } + catch(e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should set two persistable flow context (w/o callback)', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"flow.set('count','0','memory1');flow.set('count','1','memory2');return msg;"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', 'foo'); - n2.context().flow.get("count", "memory1", function (err, val1) { - val1.should.equal("0"); - n2.context().flow.get("count", "memory2", function (err, val2) { - val2.should.equal("1"); - done(); - }); - }); - } - catch(e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n2.context().flow.get("count", "memory1", function (err, val1) { + val1.should.equal("0"); + n2.context().flow.get("count", "memory2", function (err, val2) { + val2.should.equal("1"); + done(); + }); + }); + } + catch(e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should set persistable flow context (w/ callback)', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"flow.set('count','0','memory1', function (err) { node.send(msg); });"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', 'foo'); - n2.context().flow.get("count", "memory1", function (err, val) { - val.should.equal("0"); - done(); - }); - } - catch(e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n2.context().flow.get("count", "memory1", function (err, val) { + val.should.equal("0"); + done(); + }); + } + catch(e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should set two persistable flow context (w/ callback)', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"flow.set('count','0','memory1', function (err) { flow.set('count','1','memory2', function (err) { node.send(msg); }); });"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', 'foo'); - n2.context().flow.get("count", "memory1", function (err, val1) { - val1.should.equal("0"); - n2.context().flow.get("count", "memory2", function (err, val2) { - val2.should.equal("1"); - done(); - }); - }); - } - catch(e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n2.context().flow.get("count", "memory1", function (err, val1) { + val1.should.equal("0"); + n2.context().flow.get("count", "memory2", function (err, val2) { + val2.should.equal("1"); + done(); + }); + }); + } + catch(e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should get flow context', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=flow.get('count');return msg;"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -737,19 +795,19 @@ describe('function node', function() { it('should handle error on get persistable flow context (w/o callback)', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=flow.get('count','memory1');return msg;"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); n1.context().flow.set("count","0","memory1"); n1.receive({payload:"foo",topic: "bar"}); - checkCallbackError('n1', done); + checkCallbackError('n1', done); }); }); it('should get persistable flow context (w/ callback)', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"flow.get('count','memory1', function(err, val) { msg.payload=val; node.send(msg); });"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -765,7 +823,7 @@ describe('function node', function() { it('should get flow context', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=context.flow.get('count');return msg;"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -781,21 +839,21 @@ describe('function node', function() { it('should handle error on get persistable flow context', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=context.flow.get('count','memory1');return msg;"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n1.context().flow.set("count","0","memory1"); - n1.receive({payload:"foo",topic: "bar"}); - checkCallbackError('n1', done); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.context().flow.set("count","0","memory1"); + n1.receive({payload:"foo",topic: "bar"}); + checkCallbackError('n1', done); + }); }); }); it('should get keys in flow context', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=flow.keys();return msg;"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -811,44 +869,44 @@ describe('function node', function() { it('should handle error on get keys in persistable flow context (w/o callback)', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"msg.payload=flow.keys('memory1');return msg;"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n1.context().flow.set("count","0","memory1"); - n1.receive({payload:"foo",topic: "bar"}); - checkCallbackError('n1', done); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.context().flow.set("count","0","memory1"); + n1.receive({payload:"foo",topic: "bar"}); + checkCallbackError('n1', done); + }); }); }); it('should get keys in persistable flow context (w/ callback)', function(done) { var flow = [{id:"n1",type:"function",z:"flowA",wires:[["n2"]],func:"flow.keys('memory1', function (err, val) { msg.payload=val; node.send(msg); });"}, - {id:"n2", type:"helper",z:"flowA"}]; + {id:"n2", type:"helper",z:"flowA"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n1.context().flow.set("count","0","memory1"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', ['count']); - done(); - } - catch(e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.context().flow.set("count","0","memory1"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', ['count']); + done(); + } + catch(e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should set global context', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"global.set('count','0');return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -864,57 +922,57 @@ describe('function node', function() { it('should set persistable global context (w/o callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"global.set('count','0','memory1');return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', 'foo'); - n2.context().global.get("count", "memory1", function(err, val) { - val.should.equal("0"); - done(); - }); - } - catch(e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n2.context().global.get("count", "memory1", function(err, val) { + val.should.equal("0"); + done(); + }); + } + catch(e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should set persistable global context (w/ callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"global.set('count','0','memory1', function (err) { node.send(msg); });"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', 'foo'); - n2.context().global.get("count", "memory1", function(err, val) { - val.should.equal("0"); - done(); - }); - } - catch(e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', 'foo'); + n2.context().global.get("count", "memory1", function(err, val) { + val.should.equal("0"); + done(); + }); + } + catch(e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should get global context', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=global.get('count');return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -930,39 +988,39 @@ describe('function node', function() { it('should handle error on get persistable global context (w/o callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=global.get('count', 'memory1');return msg;"}, - {id:"n2", type:"helper"}]; - initContext(function () { + {id:"n2", type:"helper"}]; + initContext(function () { helper.load(functionNode, flow, function() { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n1.context().global.set("count","0", 'memory1'); - n1.receive({payload:"foo",topic: "bar"}); - checkCallbackError('n1', done); + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.context().global.set("count","0", 'memory1'); + n1.receive({payload:"foo",topic: "bar"}); + checkCallbackError('n1', done); }); - }); + }); }); it('should get persistable global context (w/ callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"global.get('count', 'memory1', function (err, val) { msg.payload=val; node.send(msg); });"}, - {id:"n2", type:"helper"}]; - initContext(function () { + {id:"n2", type:"helper"}]; + initContext(function () { helper.load(functionNode, flow, function() { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n1.context().global.set("count","0", 'memory1'); - n2.on("input", function(msg) { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.context().global.set("count","0", 'memory1'); + n2.on("input", function(msg) { msg.should.have.property('topic', 'bar'); msg.should.have.property('payload', '0'); done(); - }); - n1.receive({payload:"foo",topic: "bar"}); + }); + n1.receive({payload:"foo",topic: "bar"}); }); - }); + }); }); it('should get global context', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.global.get('count');return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -978,44 +1036,44 @@ describe('function node', function() { it('should handle error on get persistable global context (w/o callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=context.global.get('count','memory1');return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n1.context().global.set("count","0", "memory1"); - n1.receive({payload:"foo",topic: "bar"}); - checkCallbackError('n1', done); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.context().global.set("count","0", "memory1"); + n1.receive({payload:"foo",topic: "bar"}); + checkCallbackError('n1', done); + }); }); }); it('should get persistable global context (w/ callback)', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"context.global.get('count','memory1', function (err, val) { msg.payload = val; node.send(msg); });"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { - initContext(function () { - var n1 = helper.getNode("n1"); - var n2 = helper.getNode("n2"); - n1.context().global.set("count","0", "memory1"); - n2.on("input", function(msg) { - try { - msg.should.have.property('topic', 'bar'); - msg.should.have.property('payload', '0'); - done(); - } - catch(e) { - done(e); - } - }); - n1.receive({payload:"foo",topic: "bar"}); - }); + initContext(function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.context().global.set("count","0", "memory1"); + n2.on("input", function(msg) { + try { + msg.should.have.property('topic', 'bar'); + msg.should.have.property('payload', '0'); + done(); + } + catch(e) { + done(e); + } + }); + n1.receive({payload:"foo",topic: "bar"}); + }); }); }); it('should handle setTimeout()', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"setTimeout(function(){node.send(msg);},1000);"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -1041,7 +1099,7 @@ describe('function node', function() { it('should handle setInterval()', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"setInterval(function(){node.send(msg);},100);"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -1060,7 +1118,7 @@ describe('function node', function() { it('should handle clearInterval()', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"var id=setInterval(null,100);setTimeout(function(){clearInterval(id);node.send(msg);},1000);"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -1075,7 +1133,7 @@ describe('function node', function() { it('should allow accessing node.id', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload = node.id; return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -1089,7 +1147,7 @@ describe('function node', function() { it('should allow accessing node.name', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload = node.name; return msg;", "name":"name of node"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2"); @@ -1103,7 +1161,7 @@ describe('function node', function() { it('should use the same Date object from outside the sandbox', function(done) { var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload=global.get('typeTest')(new Date());return msg;"}, - {id:"n2", type:"helper"}]; + {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); var n2 = helper.getNode("n2");