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, ignore_doctype) {
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       // forgotten doctype declaration
46       if (!ignore_doctype) {
47         expect(html).toMatch(/<html.*?>/);
48         expect(html).toMatch(/<\/html.*?>/);
49       }
50       expect(html).toMatch(/<head.*?>/);
51       expect(html).toMatch(/<body.*?>/);
52       expect(html).toMatch(/<\/head.*?>/);
53       expect(html).toMatch(/<\/body.*?>/);
54
55       expect(html).toMatch(/<meta .*?charset=utf-8/i);
56       expect(html).toMatch(/<title>.+<\/title>/i);
57       expect(html).toMatch(/<link .*?type="text\/css" href=".*?\/?mkwsStyle.css"/);
58
59
60       for(var i = 0, data = ""; i < tags.length; i++) {
61         data = '<div id="' + tags[i] + '">';
62         // console.log(data)
63         expect(html).toMatch(data);
64       }
65     });
66   });
67 }
68
69 var mkws_tags_required = ["mkwsSearch", "mkwsResults"];
70 var mkws_tags_optional = ["mkwsSwitch", "mkwsLang", "mkwsTargets"];
71 var mkws_tags_optional2 = ["mkwsMOTD", "mkwsStat", "footer"];
72 html_check('../examples/htdocs/index-full.html', [mkws_tags_required, mkws_tags_optional, mkws_tags_optional2]);
73 html_check('../examples/htdocs/index-mobile.html', [mkws_tags_required, mkws_tags_optional]);
74 html_check('../examples/htdocs/index-popup.html', [], true);
75 // html_check('../examples/htdocs/index-wolfram.html', [mkws_tags_required, mkws_tags_optional]);
76 html_check('../examples/htdocs/index-jquery.html', []);
77
78 var file = '../examples/htdocs/index-full.html';
79 var html = fs.readFileSync(file, "utf-8");
80 /*
81  * parse HTML data to DOM, and run jQuery request on it
82  *
83  */
84 describe("index-full.html jsdom + jquery", function() {
85   var window = require('jsdom').jsdom(html, null, {
86
87     FetchExternalResources: false,
88     ProcessExternalResources: false,
89     MutationEvents: false,
90     QuerySelector: false
91   }).createWindow();
92
93   /* apply jquery to the window */
94   var $ = jQuery = require('jquery').create(window);
95
96
97   it("html jquery test", function() {
98     expect(html).toBeDefined();
99
100     expect($("body").length == 0).toEqual(false);
101     expect($("body").length == 1).toEqual(true);
102     expect($("head").length == 1).toEqual(true);
103
104     var tags = flat_list([mkws_tags_required, mkws_tags_optional, mkws_tags_optional2]);
105     for(var i = 0; i < tags.length; i++) {
106       expect($("#" + tags[i]).length == 1).toEqual(true);
107     }
108   });
109
110   it("html jquery fail test", function() {
111     expect(html).toBeDefined();
112
113     expect($("body_does_not_exists").length == 1).toEqual(false);
114     expect($("#body_does_not_exists").length == 1).toEqual(false);
115   });
116 });