From 590506e306b73286c7ee4baa1e9feeb6b1c68611 Mon Sep 17 00:00:00 2001 From: Kazuhito Yokoi Date: Fri, 13 Jul 2018 15:26:07 +0900 Subject: [PATCH 1/4] Add test cases of persistable context for trigger node --- test/nodes/core/core/20-inject_spec.js | 2 +- test/nodes/core/core/89-trigger_spec.js | 37 +++++++++++++++++++++++++ test/nodes/core/logic/15-change_spec.js | 2 +- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/test/nodes/core/core/20-inject_spec.js b/test/nodes/core/core/20-inject_spec.js index 798d59fb3..111feae3a 100644 --- a/test/nodes/core/core/20-inject_spec.js +++ b/test/nodes/core/core/20-inject_spec.js @@ -22,7 +22,6 @@ var helper = require("node-red-node-test-helper"); describe('inject node', function() { before(function(done) { - helper.startServer(done); Context.init({ contextStorage: { memory: { @@ -31,6 +30,7 @@ describe('inject node', function() { } }); Context.load(); + helper.startServer(done); }); after(function(done) { diff --git a/test/nodes/core/core/89-trigger_spec.js b/test/nodes/core/core/89-trigger_spec.js index f19bc42fe..1a6fa4ba8 100644 --- a/test/nodes/core/core/89-trigger_spec.js +++ b/test/nodes/core/core/89-trigger_spec.js @@ -18,16 +18,27 @@ var should = require("should"); var sinon = require("sinon"); var helper = require("node-red-node-test-helper"); var triggerNode = require("../../../../nodes/core/core/89-trigger.js"); +var Context = require("../../../../red/runtime/nodes/context"); var RED = require("../../../../red/red.js"); describe('trigger node', function() { beforeEach(function(done) { + Context.init({ + contextStorage: { + memory: { + module: "memory" + } + } + }); + Context.load(); helper.startServer(done); }); afterEach(function(done) { helper.unload().then(function() { + Context.clean({allNodes: {}}); + Context.close(); helper.stopServer(done); }); }); @@ -312,7 +323,33 @@ describe('trigger node', function() { }); n1.emit("input", {payload:null}); }); + }); + it('should be able to return things from persistable flow and global context variables', function (done) { + var flow = [{"id": "n1", "type": "trigger", "name": "triggerNode", "op1": "#:(memory)::foo", "op1type": "flow", + "op2": "#:(memory)::bar", "op2type": "global", "duration": "20", "wires": [["n2"]], "z": "flow" }, + {"id": "n2", "type": "helper"}]; + helper.load(triggerNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + var c = 0; + n2.on("input", function (msg) { + try { + if (c === 0) { + msg.should.have.a.property("payload", "foo"); + c += 1; + } else { + msg.should.have.a.property("payload", "bar"); + done(); + } + } catch (err) { + done(err); + } + }); + n1.context().flow.set("foo", "foo"); + n1.context().global.set("bar", "bar"); + n1.emit("input", { payload: null }); + }); }); it('should be able to not output anything on first trigger', function(done) { diff --git a/test/nodes/core/logic/15-change_spec.js b/test/nodes/core/logic/15-change_spec.js index ecaa009b5..6b98c697f 100644 --- a/test/nodes/core/logic/15-change_spec.js +++ b/test/nodes/core/logic/15-change_spec.js @@ -24,7 +24,6 @@ var helper = require("node-red-node-test-helper"); describe('change Node', function() { beforeEach(function(done) { - helper.startServer(done); Context.init({ contextStorage: { memory: { @@ -33,6 +32,7 @@ describe('change Node', function() { } }); Context.load(); + helper.startServer(done); }); afterEach(function(done) { From 761161a8e50f5a107686c99ef5c9662c1987dde6 Mon Sep 17 00:00:00 2001 From: Kazuhito Yokoi Date: Fri, 13 Jul 2018 17:34:04 +0900 Subject: [PATCH 4/4] Fix async problem in test cases --- test/nodes/core/core/20-inject_spec.js | 23 ++++++++++++----------- test/nodes/core/core/89-trigger_spec.js | 13 ++++++++----- test/nodes/core/logic/15-change_spec.js | 16 ++++++++++------ 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/test/nodes/core/core/20-inject_spec.js b/test/nodes/core/core/20-inject_spec.js index 111feae3a..7a9d424bd 100644 --- a/test/nodes/core/core/20-inject_spec.js +++ b/test/nodes/core/core/20-inject_spec.js @@ -21,7 +21,7 @@ var helper = require("node-red-node-test-helper"); describe('inject node', function() { - before(function(done) { + beforeEach(function(done) { Context.init({ contextStorage: { memory: { @@ -29,18 +29,19 @@ describe('inject node', function() { } } }); - Context.load(); - helper.startServer(done); + Context.load().then(function () { + helper.startServer(done); + }); }); - after(function(done) { - helper.stopServer(done); - }); - - afterEach(function() { - helper.unload(); - Context.clean({allNodes: {}}); - Context.close(); + afterEach(function(done) { + helper.unload().then(function () { + return Context.clean({allNodes: {}}); + }).then(function () { + return Context.close(); + }).then(function () { + helper.stopServer(done); + }); }); it('sets the value of flow context property', function (done) { diff --git a/test/nodes/core/core/89-trigger_spec.js b/test/nodes/core/core/89-trigger_spec.js index 1a6fa4ba8..4d83d1b68 100644 --- a/test/nodes/core/core/89-trigger_spec.js +++ b/test/nodes/core/core/89-trigger_spec.js @@ -31,14 +31,17 @@ describe('trigger node', function() { } } }); - Context.load(); - helper.startServer(done); + Context.load().then(function () { + helper.startServer(done); + }); }); afterEach(function(done) { - helper.unload().then(function() { - Context.clean({allNodes: {}}); - Context.close(); + helper.unload().then(function () { + return Context.clean({allNodes: {}}); + }).then(function () { + return Context.close(); + }).then(function () { helper.stopServer(done); }); }); diff --git a/test/nodes/core/logic/15-change_spec.js b/test/nodes/core/logic/15-change_spec.js index 6b98c697f..d002a266d 100644 --- a/test/nodes/core/logic/15-change_spec.js +++ b/test/nodes/core/logic/15-change_spec.js @@ -31,15 +31,19 @@ describe('change Node', function() { } } }); - Context.load(); - helper.startServer(done); + Context.load().then(function () { + helper.startServer(done); + }); }); afterEach(function(done) { - helper.unload(); - helper.stopServer(done); - Context.clean({allNodes:{}}); - Context.close(); + helper.unload().then(function () { + return Context.clean({allNodes: {}}); + }).then(function () { + return Context.close(); + }).then(function () { + helper.stopServer(done); + }); }); it('should load node with defaults', function(done) {