1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add test cases accessing context with JSONata to Switch Node

This commit is contained in:
HirokiUchikawa 2018-07-17 17:46:21 +09:00
parent 3a8aaee5d7
commit 83a8979309

View File

@ -19,6 +19,7 @@ var should = require("should");
var switchNode = require("../../../../nodes/core/logic/10-switch.js"); var switchNode = require("../../../../nodes/core/logic/10-switch.js");
var helper = require("node-red-node-test-helper"); var helper = require("node-red-node-test-helper");
var RED = require("../../../../red/red.js"); var RED = require("../../../../red/red.js");
var Context = require("../../../../red/runtime/nodes/context");
describe('switch Node', function() { describe('switch Node', function() {
@ -26,10 +27,24 @@ describe('switch Node', function() {
helper.startServer(done); helper.startServer(done);
}); });
function initContext(done) {
Context.init({
contextStorage: {
memory: {
module: "memory"
}
}
});
Context.load().then(function () {
done();
});
}
afterEach(function(done) { afterEach(function(done) {
helper.unload(); helper.unload().then(function(){
RED.settings.nodeMessageBufferMaxLength = 0; RED.settings.nodeMessageBufferMaxLength = 0;
helper.stopServer(done); helper.stopServer(done);
});
}); });
it('should be loaded with some defaults', function(done) { it('should be loaded with some defaults', function(done) {
@ -653,6 +668,53 @@ describe('switch Node', function() {
customFlowSwitchTest(flow, true, -5, done); customFlowSwitchTest(flow, true, -5, done);
}); });
it('should handle flow and global contexts with JSONata expression', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"$abs($flowContext(\"payload\"))",propertyType:"jsonata",rules:[{"t":"btwn","v":"$flowContext(\"vt\")","vt":"jsonata","v2":"$globalContext(\"v2t\")","v2t":"jsonata"}],checkall:true,outputs:1,wires:[["helperNode1"]],z:"flow"},
{id:"helperNode1", type:"helper", wires:[],z:"flow"},
{id:"flow",type:"tab"}];
helper.load(switchNode, flow, function() {
var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1");
switchNode1.context().flow.set("payload",-5);
switchNode1.context().flow.set("vt",4);
switchNode1.context().global.set("v2t",6);
helperNode1.on("input", function(msg) {
try {
should.equal(msg.payload,"pass");
done();
} catch(err) {
done(err);
}
});
switchNode1.receive({payload:"pass"});
});
});
it('should handle persistable flow and global contexts with JSONata expression', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"$abs($flowContext(\"payload\",\"memory\"))",propertyType:"jsonata",rules:[{"t":"btwn","v":"$flowContext(\"vt\",\"memory\")","vt":"jsonata","v2":"$globalContext(\"v2t\",\"memory\")","v2t":"jsonata"}],checkall:true,outputs:1,wires:[["helperNode1"]],z:"flow"},
{id:"helperNode1", type:"helper", wires:[],z:"flow"},
{id:"flow",type:"tab"}];
helper.load(switchNode, flow, function() {
initContext(function () {
var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1");
switchNode1.context().flow.set(["payload","vt"],[-7,6],"memory",function(){
switchNode1.context().global.set("v2t",8,"memory",function(){
helperNode1.on("input", function(msg) {
try {
should.equal(msg.payload,"pass");
done();
} catch(err) {
done(err);
}
});
switchNode1.receive({payload:"pass"});
});
});
});
});
});
it('should take head of message sequence (no repair)', function(done) { it('should take head of message sequence (no repair)', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"head","v":3}],checkall:false,repair:false,outputs:1,wires:[["helperNode1"]]}, var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"head","v":3}],checkall:false,repair:false,outputs:1,wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];