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