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

add attribute test to HTML parser node tests

This commit is contained in:
Dave Conway-Jones 2015-12-19 14:30:43 +00:00
parent 08b39f50b3
commit 65daaeb617
2 changed files with 94 additions and 33 deletions

View File

@ -26,19 +26,19 @@ describe('html node', function() {
var resourcesDir = __dirname+ path.sep + ".." + path.sep + ".." + path.sep + ".." + path.sep + "resources" + path.sep;
var file = path.join(resourcesDir, "70-HTML-test-file.html");
before(function(done) {
helper.startServer(done);
helper.startServer(done);
});
beforeEach(function() {
fs.existsSync(file).should.be.true;
});
afterEach(function() {
helper.unload();
});
it('should be loaded', function(done) {
var flow = [{id:"htmlNode1", type:"html", name: "htmlNode" }];
helper.load(htmlNode, flow, function() {
@ -52,7 +52,7 @@ describe('html node', function() {
fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],func:"return msg;"},
{id:"n2", type:"helper"}];
helper.load(htmlNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
@ -62,15 +62,15 @@ describe('html node', function() {
done();
});
n1.receive({payload:data,topic: "bar"});
});
});
});
});
it('should retrieve paragraph contents when specified', function(done) {
fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],ret:"text",tag:"p"},
{id:"n2", type:"helper"}];
helper.load(htmlNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
@ -80,15 +80,15 @@ describe('html node', function() {
done();
});
n1.receive({payload:data,topic: "bar"});
});
});
});
});
it('should retrieve list contents as an array of html as default', function(done) {
fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ol"},
{id:"n2", type:"helper"}];
helper.load(htmlNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
@ -99,7 +99,7 @@ describe('html node', function() {
done();
});
n1.receive({payload:data,topic: "bar"});
});
});
});
});
@ -107,19 +107,56 @@ describe('html node', function() {
fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ol",ret:"text"},
{id:"n2", type:"helper"}];
helper.load(htmlNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property('topic', 'bar');
msg.payload.should.be.instanceof(Array).and.have.lengthOf(1);
msg.payload[0].indexOf("Blue").should.be.above(-1);
msg.payload[0].indexOf("Red").should.be.above(-1);
done();
});
n1.receive({payload:data,topic: "bar"});
});
});
});
});
it('should fix up a unclosed tag', function(done) {
fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"span"},
{id:"n2", type:"helper"}];
helper.load(htmlNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property('topic', 'bar');
should.equal(msg.payload, '<img src="foo.png"/>');
done();
});
n1.receive({payload:data,topic: "bar"});
});
});
});
it('should retrive an attribute from a tag', function(done) {
fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],ret:"attr",tag:"span img"},
{id:"n2", type:"helper"}];
helper.load(htmlNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property('payload');
msg.payload[0].should.have.property('src','foo.png');
msg.should.have.property('topic', 'bar');
done();
});
n1.receive({payload:data,topic: "bar"});
});
});
});
@ -127,7 +164,7 @@ describe('html node', function() {
fs.readFile(file,function(err, data) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"p"},
{id:"n2", type:"helper"}];
helper.load(htmlNode, flow, function() {
try {
var n1 = helper.getNode("n1");
@ -141,12 +178,12 @@ describe('html node', function() {
// Each logEvent is the array of args passed to the function.
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].should.have.a.property('level',helper.log().ERROR);
done();
} catch(err) {
done(err);
}
});
});
});
});
@ -170,17 +207,17 @@ describe('html node', function() {
describe('multiple messages', function(){
var cnt = 0;
afterEach(function() {
cnt.should.be.exactly(2);
cnt = 0;
});
it('should retrieve list contents as html as default with output as multiple msgs ', function(done) {
fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ul",as:"multi"},
{id:"n2", type:"helper"}];
helper.load(htmlNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
@ -192,23 +229,23 @@ describe('html node', function() {
}
if (cnt === 1) {
msg.payload.indexOf("<li>Apple</li>").should.be.above(-1);
msg.payload.indexOf("<li>Pear</li>").should.be.above(-1);
msg.payload.indexOf("<li>Pear</li>").should.be.above(-1);
} else if (cnt === 2) {
msg.payload.indexOf("<li>Potato</li>").should.be.above(-1);
msg.payload.indexOf("<li>Parsnip</li>").should.be.above(-1);
done();
}
}
});
n1.receive({payload:data,topic: "bar"});
});
});
});
});
});
it('should retrieve list contents as text with output as multiple msgs ', function(done) {
fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ul",ret:"text",as:"multi"},
{id:"n2", type:"helper"}];
helper.load(htmlNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
@ -220,18 +257,38 @@ describe('html node', function() {
}
if (cnt === 1) {
msg.payload.indexOf("Apple").should.be.above(-1);
msg.payload.indexOf("Pear").should.be.above(-1);
msg.payload.indexOf("Pear").should.be.above(-1);
} else if (cnt === 2) {
msg.payload.indexOf("Potato").should.be.above(-1);
msg.payload.indexOf("Parsnip").should.be.above(-1);
done();
}
}
});
n1.receive({payload:data,topic: "bar"});
});
});
});
});
it('should retrieve an attribute from a tag', function(done) {
fs.readFile(file, 'utf8', function(err, data) {
var flow = [{id:"n1",type:"html",wires:[["n2"]],ret:"attr",tag:"span img",as:"multi"},
{id:"n2", type:"helper"}];
helper.load(htmlNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property('payload');
msg.payload.should.have.property('src','foo.png');
msg.should.have.property('topic', 'bar');
cnt = 2; // frig the answer as only one img tag
done();
});
n1.receive({payload:data,topic: "bar"});
});
});
});
});
});

View File

@ -21,5 +21,9 @@
<li>Parsnip</li>
</ul>
<span>
<img src="foo.png">
</span>
</body>
</html>
</html>