2 * Copyright 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var should = require("should");
18 var functionNode = require("../../../../nodes/core/core/80-function.js");
19 var helper = require("../../helper.js");
21 describe('function node', function() {
23 before(function(done) {
24 helper.startServer(done);
27 afterEach(function() {
31 it('should be loaded', function(done) {
32 var flow = [{id:"n1", type:"function", name: "function" }];
33 helper.load(functionNode, flow, function() {
34 var n1 = helper.getNode("n1");
35 n1.should.have.property('name', 'function');
40 it('should send returned message', function(done) {
41 var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"return msg;"},
42 {id:"n2", type:"helper"}];
43 helper.load(functionNode, flow, function() {
44 var n1 = helper.getNode("n1");
45 var n2 = helper.getNode("n2");
46 n2.on("input", function(msg) {
47 msg.should.have.property('topic', 'bar');
48 msg.should.have.property('payload', 'foo');
51 n1.receive({payload:"foo",topic: "bar"});
55 it('should pass through _topic', function(done) {
56 var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"return msg;"},
57 {id:"n2", type:"helper"}];
58 helper.load(functionNode, flow, function() {
59 var n1 = helper.getNode("n1");
60 var n2 = helper.getNode("n2");
61 n2.on("input", function(msg) {
62 msg.should.have.property('topic', 'bar');
63 msg.should.have.property('payload', 'foo');
64 msg.should.have.property('_topic', 'baz');
67 n1.receive({payload:"foo",topic: "bar", _topic: "baz"});
71 it('should send to multiple outputs', function(done) {
72 var flow = [{id:"n1",type:"function",wires:[["n2"],["n3"]],
73 func:"return [{payload: '1'},{payload: '2'}];"},
74 {id:"n2", type:"helper"}, {id:"n3", type:"helper"} ];
75 helper.load(functionNode, flow, function() {
76 var n1 = helper.getNode("n1");
77 var n2 = helper.getNode("n2");
78 var n3 = helper.getNode("n3");
80 n2.on("input", function(msg) {
81 should(msg).have.property('payload', '1');
87 n3.on("input", function(msg) {
88 should(msg).have.property('payload', '2');
94 n1.receive({payload:"foo",topic: "bar"});
98 it('should send to multiple messages', function(done) {
99 var flow = [{id:"n1",type:"function",wires:[["n2"]],
100 func:"return [[{payload: 1},{payload: 2}]];"},
101 {id:"n2", type:"helper"} ];
102 helper.load(functionNode, flow, function() {
103 var n1 = helper.getNode("n1");
104 var n2 = helper.getNode("n2");
106 n2.on("input", function(msg) {
108 should(msg).have.property('payload', count);
109 should(msg).have.property('_topic', 'baz');
114 n1.receive({payload:"foo", topic: "bar", _topic:"baz"});
118 it('should allow input to be discarded by returning null', function(done) {
119 var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"return null"},
120 {id:"n2", type:"helper"}];
121 helper.load(functionNode, flow, function() {
122 var n1 = helper.getNode("n1");
123 var n2 = helper.getNode("n2");
124 setTimeout(function() {
127 n2.on("input", function(msg) {
128 should.fail(null,null,"unexpected message");
130 n1.receive({payload:"foo",topic: "bar"});
134 it('should handle and log script error', function(done) {
135 var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"retunr"}];
136 helper.load(functionNode, flow, function() {
137 var n1 = helper.getNode("n1");
138 n1.on("log", function(msg) {
139 msg.should.have.property('level', 'error');
140 msg.should.have.property('id', 'n1');
141 msg.should.have.property('type', 'function');
142 msg.should.have.property('msg', 'ReferenceError: retunr is not defined');
145 n1.receive({payload:"foo",topic: "bar"});