add support of initialization & finalization to function node

This commit is contained in:
Hiroyasu Nishiyama
2020-03-07 01:55:45 +09:00
parent c9ad5bea93
commit 84d2b8ad6d
8 changed files with 269 additions and 26 deletions

View File

@@ -53,7 +53,6 @@ describe('function node', function() {
});
});
it('should be loaded', function(done) {
var flow = [{id:"n1", type:"function", name: "function" }];
helper.load(functionNode, flow, function() {
@@ -1336,6 +1335,32 @@ describe('function node', function() {
});
});
it('should execute initialization', function(done) {
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload = global.get('X'); return msg;",initialize:"global.set('X','bar');"},
{id:"n2", type:"helper"}];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property("payload", "bar");
done();
});
n1.receive({payload: "foo"});
});
});
it('should execute finalization', function(done) {
var flow = [{id:"n1",type:"function",wires:[],func:"return msg;",finalize:"global.set('X','bar');"}];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var ctx = n1.context().global;
helper.unload().then(function () {
ctx.get('X').should.equal("bar");
done();
});
});
});
describe('Logger', function () {
it('should log an Info Message', function (done) {
var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "node.log('test');"}];