rename
[mkws-moved-to-github.git] / test / spec / mkws-index-simple.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 var utils = require("./mkws_utils.js");
10
11 /*
12  * simple test with string matching of the HTML page
13  *
14  */
15
16 function html_check (file, tags_array, ignore_doctype) {
17   var html = fs.readFileSync(file, "utf-8");
18   var tags = utils.flat_list(tags_array);
19
20   describe("index-full.html string test for " + file, function() {
21     it("html test", function() {
22       expect(html).toBeDefined();
23
24       // forgotten doctype declaration
25       if (!ignore_doctype) {
26         expect(html).toMatch(/<html.*?>/);
27         expect(html).toMatch(/<\/html.*?>/);
28       }
29       expect(html).toMatch(/<head.*?>/);
30       expect(html).toMatch(/<body.*?>/);
31       expect(html).toMatch(/<\/head.*?>/);
32       expect(html).toMatch(/<\/body.*?>/);
33
34       expect(html).toMatch(/<meta .*?charset=utf-8/i);
35       expect(html).toMatch(/<title>.+<\/title>/i);
36       expect(html).toMatch(/<link .*?type="text\/css" href=".*?\/?mkwsStyle.css"/);
37
38
39       for(var i = 0, data = ""; i < tags.length; i++) {
40         data = '<div id="' + tags[i] + '">';
41         // console.log(data)
42         expect(html).toMatch(data);
43       }
44     });
45   });
46 }
47
48
49 /*
50  * parse HTML data to DOM, and run jQuery request on it
51  *
52  */
53
54 function jsdom_check (file, tags_array, ignore_doctype) {
55   var html = fs.readFileSync(file, "utf-8");
56   var tags = utils.flat_list(tags_array);
57
58   describe("index-full.html jsdom + jquery for " + file, function() {
59     var window = require('jsdom').jsdom(html, null, {
60
61       FetchExternalResources: false,
62       ProcessExternalResources: false,
63       MutationEvents: false,
64       QuerySelector: false
65     }).createWindow();
66
67     /* apply jquery to the window */
68     var $ = jQuery = require('jquery').create(window);
69
70
71     it("html jquery test", function() {
72       expect(html).toBeDefined();
73
74       expect($("body").length == 0).toEqual(false);
75       expect($("body").length == 1).toEqual(true);
76       expect($("head").length == 1).toEqual(true);
77
78       for(var i = 0; i < tags.length; i++) {
79         expect($("#" + tags[i]).length == 1).toEqual(true);
80       }
81     });
82
83     it("html jquery fail test", function() {
84       expect(html).toBeDefined();
85
86       expect($("body_does_not_exists").length == 1).toEqual(false);
87       expect($("#body_does_not_exists").length == 1).toEqual(false);
88     });
89   });
90 }
91
92 html_check('../examples/htdocs/index-full.html', [utils.tags.required, utils.tags.optional, utils.tags.optional2]);
93 html_check('../examples/htdocs/index-mobile.html', [utils.tags.required, utils.tags.optional]);
94 html_check('../examples/htdocs/index-popup.html', [], true);
95 html_check('../examples/htdocs/index-jquery.html', []);
96 html_check('../examples/htdocs/index-mike.html', [utils.tags.required, utils.tags.optional], true);
97
98 jsdom_check('../examples/htdocs/index-full.html', [utils.tags.required, utils.tags.optional, utils.tags.optional2]);
99 jsdom_check('../examples/htdocs/index-mobile.html', [utils.tags.required, utils.tags.optional]);
100 jsdom_check('../examples/htdocs/index-popup.html', [], true);
101 jsdom_check('../examples/htdocs/index-jquery.html', []);
102 jsdom_check('../examples/htdocs/index-mike.html', [utils.tags.required, utils.tags.optional], true);