refactor to check more HTML pages in mkws/examples/htdocs
[mkws-moved-to-github.git] / test / spec / mkws-index-full.spec.js
1 /* Copyright (c) 2013 IndexData ApS. http://indexdata.com
2  *
3  * jQuery test with DOM/windows object
4  *
5  */
6
7
8 var fs = require("fs");
9
10 /*
11  * combine arrays, return a flat list
12  * [["a","b"], ["c"], "d"] => ["a", "b", "c", "d"]
13  *
14  */
15 function flat_list (list) {
16   var data = [];
17
18   for(var i = 0; i < list.length; i++) {
19       if (typeof list[i] == 'object') {
20         for(var j = 0; j < list[i].length; j++) {
21           data.push(list[i][j]);
22         }
23
24       } else {
25         data.push(list[i]);
26       }
27   }
28
29   return data;
30 }
31
32 /*
33  * simple test with string matching of the HTML page
34  *
35  */
36
37 function html_check (file, tags_array) {
38   var html = fs.readFileSync(file, "utf-8");
39   var tags = flat_list(tags_array);
40
41   describe("index-full.html string test for " + file, function() {
42     it("html test", function() {
43       expect(html).toBeDefined();
44
45       expect(html).toMatch(/<html.*?>/); // forgotten doctype?
46       expect(html).toMatch(/<\/html.*?>/);
47       expect(html).toMatch(/<head.*?>/);
48       expect(html).toMatch(/<body.*?>/);
49       expect(html).toMatch(/<\/head.*?>/);
50       expect(html).toMatch(/<\/body.*?>/);
51
52       expect(html).toMatch(/<meta .*?charset=utf-8/i);
53       expect(html).toMatch(/<title>.+<\/title>/i);
54       expect(html).toMatch(/<link .*?type="text\/css" href=".*?\/mkwsStyle.css"/);
55
56
57       for(var i = 0, data = ""; i < tags.length; i++) {
58         data = '<div id="' + tags[i] + '">';
59         // console.log(data)
60         expect(html).toMatch(data);
61       }
62     });
63   });
64 }
65
66 var mkws_tags_required = ["mkwsSearch", "mkwsResults"];
67 var mkws_tags_optional = ["mkwsSwitch", "mkwsLang", "mkwsTargets"];
68 var mkws_tags_optional2 = ["mkwsMOTD", "mkwsStat", "footer"];
69 html_check('../examples/htdocs/index-full.html', [mkws_tags_required, mkws_tags_optional, mkws_tags_optional2]);
70 html_check('../examples/htdocs/index-mobile.html', [mkws_tags_required, mkws_tags_optional]);
71 html_check('../examples/htdocs/index-jquery.html', []);
72
73 var file = '../examples/htdocs/index-full.html';
74 var html = fs.readFileSync(file, "utf-8");
75 /*
76  * parse HTML data to DOM, and run jQuery request on it
77  *
78  */
79 describe("index-full.html jsdom + jquery", function() {
80   var window = require('jsdom').jsdom(html, null, {
81
82     FetchExternalResources: false,
83     ProcessExternalResources: false,
84     MutationEvents: false,
85     QuerySelector: false
86   }).createWindow();
87
88   /* apply jquery to the window */
89   var $ = jQuery = require('jquery').create(window);
90
91
92   it("html jquery test", function() {
93     expect(html).toBeDefined();
94
95     expect($("body").length == 0).toEqual(false);
96     expect($("body").length == 1).toEqual(true);
97     expect($("head").length == 1).toEqual(true);
98
99     var tags = flat_list([mkws_tags_required, mkws_tags_optional, mkws_tags_optional2]);
100     for(var i = 0; i < tags.length; i++) {
101       expect($("#" + tags[i]).length == 1).toEqual(true);
102     }
103   });
104
105   it("html jquery fail test", function() {
106     expect(html).toBeDefined();
107
108     expect($("body_does_not_exists").length == 1).toEqual(false);
109     expect($("#body_does_not_exists").length == 1).toEqual(false);
110   });
111 });