From 65daaeb61798d704be6490c38cb47adf8b6542a1 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sat, 19 Dec 2015 14:30:43 +0000 Subject: [PATCH] add attribute test to HTML parser node tests --- test/nodes/core/parsers/70-HTML_spec.js | 121 +++++++++++++++++------- test/resources/70-HTML-test-file.html | 6 +- 2 files changed, 94 insertions(+), 33 deletions(-) diff --git a/test/nodes/core/parsers/70-HTML_spec.js b/test/nodes/core/parsers/70-HTML_spec.js index 13a342825..7d9da4b57 100644 --- a/test/nodes/core/parsers/70-HTML_spec.js +++ b/test/nodes/core/parsers/70-HTML_spec.js @@ -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, ''); + 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("
  • 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 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"}); + }); + }); + }); + }); - + }); diff --git a/test/resources/70-HTML-test-file.html b/test/resources/70-HTML-test-file.html index a0932e15e..2187b8e31 100644 --- a/test/resources/70-HTML-test-file.html +++ b/test/resources/70-HTML-test-file.html @@ -21,5 +21,9 @@
  • Parsnip
  • + + + + - \ No newline at end of file +