1 /* Copyright (c) 2013 IndexData ApS. http://indexdata.com
3 * jQuery test with DOM/windows object
8 var fs = require("fs");
11 * combine arrays, return a flat list
12 * [["a","b"], ["c"], "d"] => ["a", "b", "c", "d"]
15 function flat_list (list) {
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]);
33 * simple test with string matching of the HTML page
37 function html_check (file, tags_array, ignore_doctype) {
38 var html = fs.readFileSync(file, "utf-8");
39 var tags = flat_list(tags_array);
41 describe("index-full.html string test for " + file, function() {
42 it("html test", function() {
43 expect(html).toBeDefined();
45 // forgotten doctype declaration
46 if (!ignore_doctype) {
47 expect(html).toMatch(/<html.*?>/);
48 expect(html).toMatch(/<\/html.*?>/);
50 expect(html).toMatch(/<head.*?>/);
51 expect(html).toMatch(/<body.*?>/);
52 expect(html).toMatch(/<\/head.*?>/);
53 expect(html).toMatch(/<\/body.*?>/);
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"/);
60 for(var i = 0, data = ""; i < tags.length; i++) {
61 data = '<div id="' + tags[i] + '">';
63 expect(html).toMatch(data);
71 * parse HTML data to DOM, and run jQuery request on it
75 function jsdom_check (file, tags_array, ignore_doctype) {
76 var html = fs.readFileSync(file, "utf-8");
77 var tags = flat_list(tags_array);
79 describe("index-full.html jsdom + jquery for " + file, function() {
80 var window = require('jsdom').jsdom(html, null, {
82 FetchExternalResources: false,
83 ProcessExternalResources: false,
84 MutationEvents: false,
88 /* apply jquery to the window */
89 var $ = jQuery = require('jquery').create(window);
92 it("html jquery test", function() {
93 expect(html).toBeDefined();
95 expect($("body").length == 0).toEqual(false);
96 expect($("body").length == 1).toEqual(true);
97 expect($("head").length == 1).toEqual(true);
99 for(var i = 0; i < tags.length; i++) {
100 expect($("#" + tags[i]).length == 1).toEqual(true);
104 it("html jquery fail test", function() {
105 expect(html).toBeDefined();
107 expect($("body_does_not_exists").length == 1).toEqual(false);
108 expect($("#body_does_not_exists").length == 1).toEqual(false);
113 var mkws_tags_required = ["mkwsSearch", "mkwsResults"];
114 var mkws_tags_optional = ["mkwsSwitch", "mkwsLang", "mkwsTargets"];
115 var mkws_tags_optional2 = ["mkwsMOTD", "mkwsStat", "footer"];
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);
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);