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