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

Fix accessing global context from jsonata expressions

Fixes #1335
This commit is contained in:
Nick O'Leary 2017-07-09 10:40:23 +01:00
parent fc9906624e
commit c93870316c
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 45 additions and 1 deletions

View File

@ -338,7 +338,7 @@ function prepareJSONataExpression(value,node) {
return node.context().flow.get(val);
});
expr.assign('globalContext',function(val) {
return node.context().global(val);
return node.context().global.get(val);
});
expr._legacyMode = /(^|[^a-zA-Z0-9_'"])msg([^a-zA-Z0-9_'"]|$)/.test(value);
return expr;

View File

@ -381,4 +381,48 @@ describe("red/util", function() {
it('pass http request',function() { normalise("http request", "httpRequest") });
it('pass HttpRequest',function() { normalise("HttpRequest", "httpRequest") });
});
describe('prepareJSONataExpression', function() {
it('prepares an expression', function() {
var result = util.prepareJSONataExpression('payload',{});
result.should.have.property('evaluate');
result.should.have.property('assign');
result.should.have.property('_legacyMode', false);
});
it('prepares a legacyMode expression', function() {
var result = util.prepareJSONataExpression('msg.payload',{});
result.should.have.property('evaluate');
result.should.have.property('assign');
result.should.have.property('_legacyMode', true);
});
});
describe('evaluateJSONataExpression', function() {
it('evaluates an expression', function() {
var expr = util.prepareJSONataExpression('payload',{});
var result = util.evaluateJSONataExpression(expr,{payload:"hello"});
result.should.eql("hello");
});
it('evaluates a legacyMode expression', function() {
var expr = util.prepareJSONataExpression('msg.payload',{});
var result = util.evaluateJSONataExpression(expr,{payload:"hello"});
result.should.eql("hello");
});
it('accesses flow context from an expression', function() {
var expr = util.prepareJSONataExpression('$flowContext("foo")',{context:function() { return {flow:{get: function(key) { return {'foo':'bar'}[key]}}}}});
var result = util.evaluateJSONataExpression(expr,{payload:"hello"});
result.should.eql("bar");
});
it('handles non-existant flow context variable', function() {
var expr = util.prepareJSONataExpression('$flowContext("nonExistant")',{context:function() { return {flow:{get: function(key) { return {'foo':'bar'}[key]}}}}});
var result = util.evaluateJSONataExpression(expr,{payload:"hello"});
should.not.exist(result);
});
it('handles non-existant global context variable', function() {
var expr = util.prepareJSONataExpression('$globalContext("nonExistant")',{context:function() { return {global:{get: function(key) { return {'foo':'bar'}[key]}}}}});
var result = util.evaluateJSONataExpression(expr,{payload:"hello"});
should.not.exist(result);
});
});
});