[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / test / nodes / core / parsers / 70-HTML_spec.js
1 /**
2  * Copyright 2014 IBM Corp.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  **/
16
17 var should = require("should");
18 var sinon = require("sinon");
19 var path = require("path");
20 var fs = require('fs-extra');
21
22 var htmlNode = require("../../../../nodes/core/parsers/70-HTML.js");
23 var helper = require("../../helper.js");
24
25 describe('html node', function() {
26
27     var resourcesDir = __dirname+ path.sep + ".." + path.sep + ".." + path.sep + ".." + path.sep + "resources" + path.sep;
28     var file = path.join(resourcesDir, "70-HTML-test-file.html");
29     
30     before(function(done) {
31         helper.startServer(done);   
32     });
33
34     beforeEach(function() {
35         fs.existsSync(file).should.be.true;
36     });
37     
38     afterEach(function() {
39         helper.unload();
40     });
41   
42     it('should be loaded', function(done) {
43         var flow = [{id:"htmlNode1", type:"html", name: "htmlNode" }];
44         helper.load(htmlNode, flow, function() {
45             var htmlNode1 = helper.getNode("htmlNode1");
46             htmlNode1.should.have.property('name', 'htmlNode');
47             done();
48         });
49     });
50
51     it('should retrieve header contents as default', function(done) {
52         fs.readFile(file, 'utf8', function(err, data) {
53             var flow = [{id:"n1",type:"html",wires:[["n2"]],func:"return msg;"},
54                         {id:"n2", type:"helper"}];
55             
56             helper.load(htmlNode, flow, function() {
57                 var n1 = helper.getNode("n1");
58                 var n2 = helper.getNode("n2");
59                 n2.on("input", function(msg) {
60                     msg.should.have.property('topic', 'bar');
61                     should.equal(msg.payload, 'This is a test page for node 70-HTML');
62                     done();
63                 });
64                 n1.receive({payload:data,topic: "bar"});
65             });          
66         });
67     });
68     
69     it('should retrieve paragraph contents when specified', function(done) {
70         fs.readFile(file, 'utf8', function(err, data) {
71             var flow = [{id:"n1",type:"html",wires:[["n2"]],ret:"text",tag:"p"},
72                         {id:"n2", type:"helper"}];
73             
74             helper.load(htmlNode, flow, function() {
75                 var n1 = helper.getNode("n1");
76                 var n2 = helper.getNode("n2");
77                 n2.on("input", function(msg) {
78                     msg.should.have.property('topic', 'bar');
79                     should.equal(msg.payload, 'There\'s nothing to read here.');
80                     done();
81                 });
82                 n1.receive({payload:data,topic: "bar"});
83             });          
84         });
85     });
86     
87     it('should retrieve list contents as an array of html as default', function(done) {
88         fs.readFile(file, 'utf8', function(err, data) {
89             var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ol"},
90                         {id:"n2", type:"helper"}];
91             
92             helper.load(htmlNode, flow, function() {
93                 var n1 = helper.getNode("n1");
94                 var n2 = helper.getNode("n2");
95                 n2.on("input", function(msg) {
96                     msg.should.have.property('topic', 'bar');
97                     msg.payload[0].indexOf("<li>Blue</li>").should.be.above(-1);
98                     msg.payload[0].indexOf("<li>Red</li>").should.be.above(-1);
99                     done();
100                 });
101                 n1.receive({payload:data,topic: "bar"});
102             });          
103         });
104     });
105
106     it('should retrieve list contents as an array of text', function(done) {
107         fs.readFile(file, 'utf8', function(err, data) {
108             var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ol",ret:"text"},
109                         {id:"n2", type:"helper"}];
110             
111             helper.load(htmlNode, flow, function() {
112                 var n1 = helper.getNode("n1");
113                 var n2 = helper.getNode("n2");
114                 n2.on("input", function(msg) {
115                     msg.should.have.property('topic', 'bar');
116                     msg.payload.should.be.instanceof(Array).and.have.lengthOf(1);
117                     msg.payload[0].indexOf("Blue").should.be.above(-1);
118                     msg.payload[0].indexOf("Red").should.be.above(-1);
119                     done();
120                 });
121                 n1.receive({payload:data,topic: "bar"});
122             });          
123         });
124     });
125
126     it('should log on error', function(done) {
127         fs.readFile(file,function(err, data) {
128             var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"p"},
129                         {id:"n2", type:"helper"}];
130             
131             helper.load(htmlNode, flow, function() {
132                 var n1 = helper.getNode("n1");
133                 var n2 = helper.getNode("n2");
134                 n1.on("log", function(msg) {
135                     msg.should.have.property('msg');
136                     msg.msg.indexOf("Error:").should.be.above(-1);
137                     msg.msg.should.startWith("Error:");
138                     done();
139                 });
140                 n1.receive({payload:null,topic: "bar"});
141             });          
142         });
143     });
144     
145     describe('multiple messages', function(){
146         var cnt = 0;
147         
148         afterEach(function() {
149             cnt.should.be.exactly(2);
150             cnt = 0;
151         });
152         
153         it('should retrieve list contents as html as default with output as multiple msgs ', function(done) {
154             fs.readFile(file, 'utf8', function(err, data) {
155                 var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ul",as:"multi"},
156                             {id:"n2", type:"helper"}];
157                 
158                 helper.load(htmlNode, flow, function() {
159                     var n1 = helper.getNode("n1");
160                     var n2 = helper.getNode("n2");
161                     n2.on("input", function(msg) {
162                         cnt++;
163                         msg.should.have.property('topic', 'bar');
164                         if (cnt !== 1 && cnt !== 2) {
165                             return false;
166                         }
167                         if (cnt === 1) {
168                             msg.payload.indexOf("<li>Apple</li>").should.be.above(-1);
169                             msg.payload.indexOf("<li>Pear</li>").should.be.above(-1);     
170                         } else if (cnt === 2) {
171                             msg.payload.indexOf("<li>Potato</li>").should.be.above(-1);
172                             msg.payload.indexOf("<li>Parsnip</li>").should.be.above(-1);
173                             done();
174                         } 
175                     });
176                     n1.receive({payload:data,topic: "bar"});
177                 });   
178             });
179         });    
180         
181         it('should retrieve list contents as text with output as multiple msgs ', function(done) {
182             fs.readFile(file, 'utf8', function(err, data) {
183                 var flow = [{id:"n1",type:"html",wires:[["n2"]],tag:"ul",ret:"text",as:"multi"},
184                             {id:"n2", type:"helper"}];
185                 
186                 helper.load(htmlNode, flow, function() {
187                     var n1 = helper.getNode("n1");
188                     var n2 = helper.getNode("n2");
189                     n2.on("input", function(msg) {
190                         cnt++;
191                         msg.should.have.property('topic', 'bar');
192                         if (cnt !== 1 && cnt !== 2) {
193                             return false;
194                         }
195                         if (cnt === 1) {
196                             msg.payload.indexOf("Apple").should.be.above(-1);
197                             msg.payload.indexOf("Pear").should.be.above(-1);     
198                         } else if (cnt === 2) {
199                             msg.payload.indexOf("Potato").should.be.above(-1);
200                             msg.payload.indexOf("Parsnip").should.be.above(-1);
201                             done();
202                         } 
203                     });
204                     n1.receive({payload:data,topic: "bar"});
205                 });          
206             });
207         });
208         
209     });
210  
211 });