1 /* Copyright (c) 2013 IndexData ApS. http://indexdata.com
3 * jQuery test with DOM/windows object
7 var file = '../examples/htdocs/index-full.html'
9 var fs = require("fs");
10 var html = fs.readFileSync(file, "utf-8");
12 var mkws_tags_required = ["mkwsSearch", "mkwsResults"];
13 var mkws_tags_optional = ["mkwsSwitch", "mkwsLang", "mkwsTargets"];
14 var mkws_tags_optional2 = ["mkwsMOTD", "mkwsStat", "footer"];
17 * combine arrays, return a flat list
18 * [["a","b"], ["c"], "d"] => ["a", "b", "c", "d"]
21 function flat_list (list) {
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]);
39 * simple test with string matching of the HTML page
42 describe("index-full.html string test", function() {
43 it("html test", function() {
44 expect(html).toBeDefined();
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.*?>/);
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"/);
57 var tags = flat_list([mkws_tags_required, mkws_tags_optional, mkws_tags_optional2]);
59 for(var i = 0, data = ""; i < tags.length; i++) {
60 data = '<div id="' + tags[i] + '">';
62 expect(html).toMatch(data);
69 * parse HTML data to DOM, and run jQuery request on it
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,
80 /* apply jquery to the window */
81 var $ = jQuery = require('jquery').create(window);
84 it("html jquery test", function() {
85 expect(html).toBeDefined();
87 expect($("body").length == 0).toEqual(false);
88 expect($("body").length == 1).toEqual(true);
89 expect($("head").length == 1).toEqual(true);
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);
97 it("html jquery fail test", function() {
98 expect(html).toBeDefined();
100 expect($("body_does_not_exists").length == 1).toEqual(false);
101 expect($("#body_does_not_exists").length == 1).toEqual(false);