refactor to check more files with jquery
[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
70 /*
71  * parse HTML data to DOM, and run jQuery request on it
72  *
73  */
74
75 function jsdom_check (file, tags_array, ignore_doctype) {
76   var html = fs.readFileSync(file, "utf-8");
77   var tags = flat_list(tags_array);
78
79   describe("index-full.html jsdom + jquery for " + file, 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       for(var i = 0; i < tags.length; i++) {
100         expect($("#" + tags[i]).length == 1).toEqual(true);
101       }
102     });
103
104     it("html jquery fail test", function() {
105       expect(html).toBeDefined();
106
107       expect($("body_does_not_exists").length == 1).toEqual(false);
108       expect($("#body_does_not_exists").length == 1).toEqual(false);
109     });
110   });
111 }
112
113 var mkws_tags_required = ["mkwsSearch", "mkwsResults"];
114 var mkws_tags_optional = ["mkwsSwitch", "mkwsLang", "mkwsTargets"];
115 var mkws_tags_optional2 = ["mkwsMOTD", "mkwsStat", "footer"];
116
117 html_check('../examples/htdocs/index-full.html', [mkws_tags_required, mkws_tags_optional, mkws_tags_optional2]);
118 html_check('../examples/htdocs/index-mobile.html', [mkws_tags_required, mkws_tags_optional]);
119 html_check('../examples/htdocs/index-popup.html', [], true);
120 html_check('../examples/htdocs/index-jquery.html', []);
121 html_check('../examples/htdocs/index-mike.html', [mkws_tags_required, mkws_tags_optional], true);
122
123 jsdom_check('../examples/htdocs/index-full.html', [mkws_tags_required, mkws_tags_optional, mkws_tags_optional2]);
124 jsdom_check('../examples/htdocs/index-mobile.html', [mkws_tags_required, mkws_tags_optional]);
125 jsdom_check('../examples/htdocs/index-popup.html', [], true);
126 jsdom_check('../examples/htdocs/index-jquery.html', []);
127 jsdom_check('../examples/htdocs/index-mike.html', [mkws_tags_required, mkws_tags_optional], true);