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

Add test cases for change node (#1476)

This commit is contained in:
Kazuhito Yokoi 2017-11-18 02:26:36 +09:00 committed by Nick O'Leary
parent 2b9aa94f3a
commit ab61a95f83

View File

@ -76,6 +76,25 @@ describe('change Node', function() {
});
});
it('sets the value of global context property', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{ "t":"set","p":"globalValue","pt":"global","to":"changed","tot":"str"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
changeNode1.context().global.get("globalValue").should.equal("changed");
done();
} catch(err) {
done(err);
}
});
changeNode1.context().global.set("globalValue","changeMe");
changeNode1.receive({payload:""});
});
});
it('sets the value and type of the message property', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{ "t": "set", "p": "payload", "pt": "msg", "to": "12345", "tot": "num" }],"reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
@ -261,6 +280,44 @@ describe('change Node', function() {
});
});
it('changes the value to flow context property', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"flowValue","tot":"flow"}],"name":"changeNode","wires":[["helperNode1"]],"z":"flow"},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.eql("Hello World!");
done();
} catch(err) {
done(err);
}
});
changeNode1.context().flow.set("flowValue","Hello World!");
changeNode1.receive({payload:""});
});
});
it('changes the value to global context property', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"globalValue","tot":"global"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.eql("Hello World!");
done();
} catch(err) {
done(err);
}
});
changeNode1.context().global.set("globalValue","Hello World!");
changeNode1.receive({payload:""});
});
});
it('changes the value to a number', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"123","tot":"num"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
@ -279,6 +336,24 @@ describe('change Node', function() {
});
});
it('changes the value to a boolean value', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"true","tot":"bool"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.eql(true);
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:""});
});
});
it('changes the value to a js object', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":'{"a":123}',"tot":"json"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
@ -297,6 +372,25 @@ describe('change Node', function() {
});
});
it('changes the value to a buffer object', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"[72,101,108,108,111,32,87,111,114,108,100]","tot":"bin"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
var buff = Buffer.from("Hello World");
msg.payload.should.eql(buff);
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:""});
});
});
it('sets the value of the message property to the current timestamp', function(done) {
var flow = [{"id":"changeNode1","type":"change","rules":[{"t":"set","p":"ts","pt":"msg","to":"","tot":"date"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
@ -315,6 +409,24 @@ describe('change Node', function() {
});
});
it('changes the value using jsonata', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"$length(payload)","tot":"jsonata"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.eql(12);
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"Hello World!"});
});
});
});
describe('#change', function() {
it('changes the value of the message property', function(done) {
@ -574,6 +686,25 @@ describe('change Node', function() {
});
});
it('changes the number using global context property', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"change","p":"payload","from":"topic","to":"ABC","fromt":"global","tot":"str"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.equal("ABC");
done();
} catch(err) {
done(err);
}
});
changeNode1.context().global.set("topic",123);
changeNode1.receive({payload:123});
});
});
it('changes the value using number - string payload', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"change","p":"payload","from":"123","to":"456","fromt":"num","tot":"str"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
@ -763,6 +894,25 @@ describe('change Node', function() {
});
});
it('deletes the value of global context property', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{ "t": "delete", "p": "globalValue", "pt": "global"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
changeNode1.context().global.should.not.have.property("globalValue");
done();
} catch(err) {
done(err);
}
});
changeNode1.context().global.set("globalValue","Hello World!");
changeNode1.receive({payload:""});
});
});
it('deletes the value of a multi-level message property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"delete","property":"foo.bar","from":"","to":"","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];