1 /* Copyright (c) 2013-2014 IndexData ApS. http://indexdata.com
3 * perform papzpar2 / pz2.js search & retrieve request in the browser
7 // get references from mkws.js, lazy evaluation
8 var debug = function (text) {
9 mkws.debug("Jasmine: " + text)
12 // Define empty mkws_config for simple applications that don't define it.
13 if (jasmine_config == null || typeof jasmine_config != 'object') {
14 var jasmine_config = {};
17 /* check config for jasmine test
19 * you can override the default values in the config
20 * object: jasmine_config = {};
23 function init_jasmine_config() {
25 var jasmine_config_default = {
26 search_query: "freebsd",
30 // at least expected hit counter
32 // miliseconds to seconds
33 show_record_url: true,
34 // check for valid URL in records
38 // use default values for undefined values
39 for (var key in jasmine_config_default) {
40 if (!jasmine_config.hasOwnProperty(key)) {
41 jasmine_config[key] = jasmine_config_default[key];
43 debug("jasmine config: " + key + " => " + jasmine_config[key]);
46 mkws.jasmine_done = false;
49 var get_hit_counter = function () {
51 if ($(".mkwsPager").length == 0) return -1;
53 var found = $(".mkwsPager").text();
54 var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
59 hits = parseInt(RegExp.$1);
60 expect(hits).toBeGreaterThan(0);
63 //debug("Hits: " + hits);
67 describe("Init jasmine config", function () {
68 it("jasmine was successfully initialized", function () {
69 init_jasmine_config();
71 expect(jasmine_config.search_query).toMatch(/\w/);
72 expect(jasmine_config.second).toBeGreaterThan(100);
73 expect(jasmine_config.max_time).toBeGreaterThan(1);
74 expect(jasmine_config.expected_hits).toBeGreaterThan(1);
78 describe("Check MOTD before search", function () {
79 // Check that the MOTD has been moved into its container, and
80 // is visible before the search.
81 // the mkwsMOTD div was originally inside a testMOTD div, which should
83 // Note that the testMOTD is a regular div, and uses #testMOTD,
84 // since the automagic class-making does not apply to it.
85 it("MOTD is hidden", function () {
86 expect($(".mkwsMOTD").length).toBe(1);
87 expect($("#testMOTD").length).toBe(1);
88 expect($("#testMOTD").text()).toMatch("^ *$");
91 it("mkwsMOTDContainer has received the text", function () {
92 expect($(".mkwsMOTDContainer").length).toBe(1);
93 expect($(".mkwsMOTDContainer").text()).toMatch(/MOTD/);
99 describe("Check pazpar2 search", function () {
100 it("pazpar2 was successfully initialized", function () {
101 expect(mkws_config.error).toBe(undefined);
104 it("validate HTML id's", function () {
105 expect($("input.mkwsQuery").length).toBe(1);
106 expect($("input.mkwsButton").length).toBe(1);
108 expect($(".mkwsNext").length).not.toBe(1);
109 expect($(".mkwsPrev").length).not.toBe(1);
112 it("run search query", function () {
113 var search_query = jasmine_config.search_query; // short hit counter with some paging
114 $("input.mkwsQuery").val(search_query);
115 debug("set search query: " + search_query)
116 expect($("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
118 if (mkws_config.use_service_proxy) {
119 // wait for service proxy auth
120 waitsFor(function () {
121 return mkws.authenticated;
122 }, "SP auth done", 10 * jasmine_config.second);
124 debug("running raw pp2, don't wait for mkws auth");
128 debug("Click on submit button");
129 var click = $("input.mkwsButton").trigger("click");
130 expect(click.length).toBe(1);
135 describe("Check MOTD after search", function () {
136 it("MOTD is hidden", function () {
137 expect($(".mkwsMOTD").length).toBe(1);
138 expect($(".mkwsMOTD").is(":hidden")).toBe(true);
139 debug("motd " + $(".mkwsMOTD") );
140 debug("motd t=" + $(".mkwsMOTD").text() );
141 debug("motd v=" + $(".mkwsMOTD").is(":visible") );
142 //expect($("div.mkwsBytarget").is(":visible")).toBe(true);
149 * This part runs in background. It should be rewritten with
150 * async jasmine functions
153 describe("Check pazpar2 navigation", function () {
155 it("check running search next/prev", function () {
156 expect($(".mkwsPager").length).toBe(1);
158 function my_click(id, time) {
159 setTimeout(function () {
160 debug("trigger click on id: " + id);
161 var click = $(id).trigger("click");
163 debug("next/prev: " + id + " click is success: " + click.length);
164 expect(click.length).toBe(1);
165 }, time * jasmine_config.second);
168 waitsFor(function () {
169 return $("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
170 }, "Expect next link 2", 10 * jasmine_config.second);
173 // click next/prev after N seconds
174 my_click(".mkwsNext", 0);
177 waitsFor(function () {
178 return $("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
179 }, "Expect next link 3", 5 * jasmine_config.second);
182 // click next/prev after N seconds
183 my_click(".mkwsNext", 0);
184 my_click(".mkwsPrev", 0.2);
189 describe("Check pazpar2 hit counter", function () {
190 it("check running search hit counter", function () {
191 var max_time = jasmine_config.max_time; // in seconds
192 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
195 waitsFor(function () {
196 hits = get_hit_counter();
198 return hits > expected_hits;
199 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
203 debug("mkws pager found records: '" + hits + "'");
204 expect($(".mkwsPager").length).toBe(1);
205 expect(hits).toBeGreaterThan(expected_hits);
210 describe("Check Termlist", function () {
211 it("found Termlist", function () {
212 var termlist = $("div.mkwsTermlists");
213 debug("Termlist success: " + termlist.length);
214 expect(termlist.length).toBe(1);
216 waitsFor(function () {
217 return $("div.mkwsFacetSources").length == 1 ? true : false;
218 }, "check for facet sources", 4 * jasmine_config.second);
221 // everything displayed?
223 var sources = $("div.mkwsFacetSources");
224 debug("Termlist sources success: " + sources.length);
225 expect(sources.length).toBe(1);
227 var subjects = $("div.mkwsFacetSubjects");
228 expect(subjects.length).toBe(1);
230 var authors = $("div.mkwsFacetAuthors");
231 expect(authors.length).toBe(1);
234 waitsFor(function () {
235 return $("div.mkwsFacetAuthors div.term").length >= 2 ? true : false;
236 }, "At least one author link displayed", 4 * jasmine_config.second);
239 expect($("div.mkwsFacetAuthors div.term").length).toBeGreaterThan(1);
243 it("limit search to first author", function () {
244 if (mkws_config.disable_facet_authors_search) {
245 debug("Facets: ignore limit search for authors");
249 var hits_all_targets = get_hit_counter();
250 var author_number = 2; // 2=first author
251 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
252 // do not click on author names without a comma, e.g.: "Joe Barbara"
253 var terms = $("div.mkwsFacetAuthors div.term a");
254 for (var i = 0; i < terms.length; i++) {
255 var term = $(terms[i]).text();
256 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
257 debug("ignore author facet: " + term);
264 var click = $("div.mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").trigger("click");
265 debug("limit author click is success: " + click.length);
266 expect(click.length).toBe(1);
268 waitsFor(function () {
269 return get_hit_counter() < hits_all_targets ? true : false;
270 }, "Limited author search for less than " + hits_all_targets + " hits", 6 * jasmine_config.second);
273 var hits_single_target = get_hit_counter();
274 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
275 expect(hits_all_targets).toBeGreaterThan(hits_single_target);
279 it("limit search to first source", function () {
280 var hits_all_targets = get_hit_counter();
281 var source_number = 2; // 2=first source
282 // do not click on wikipedia link - no author or subject facets possible
283 var terms = $("div.mkwsFacetSources div.term a");
284 for (var i = 0; i < terms.length; i++) {
285 var term = $(terms[i]).text();
286 if (term.match(/wikipedia/i)) {
287 debug("ignore source facet: " + term);
294 var click = $("div.mkwsFacetSources div.term:nth-child(" + source_number + ") a").trigger("click");
295 debug("limit source click " + (source_number - 1) + " is success: " + click.length);
296 expect(click.length).toBe(1);
298 waitsFor(function () {
299 if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
304 }, "Search for source in navi bar", 4 * jasmine_config.second);
306 // Note: it may happens that limited source search returns the same number of hits
307 // as before. Thats not really an error, but unfortunate
308 waitsFor(function () {
309 return get_hit_counter() <= hits_all_targets ? true : false;
310 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
313 var hits_single_target = get_hit_counter();
314 debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
315 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
320 describe("Show record", function () {
321 var record_number = 1; // the Nth record in hit list
322 it("show record author", function () {
323 var click = $("div.mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
324 debug("show record click is success: " + click.length);
325 expect(click.length).toBe(1);
327 // wait until the record pops up
328 waitsFor(function () {
329 var show = $("div.mkwsRecords div.record:nth-child(" + record_number + ") div");
330 return show != null && show.length ? true : false;
331 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
334 debug("show record pop up");
335 expect($("div.mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
339 it("extract URL", function () {
340 if (jasmine_config.show_record_url == false) {
341 debug("ignore test for URL in record")
345 var urls = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
346 debug("number of extracted URL from record: " + urls.length);
347 for (var i = 0; i < urls.length; i++) {
348 var url = $(urls[i]);
349 debug("URL: " + url.attr('href'));
350 expect(url.attr('href')).not.toBe(null);
351 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9]+\.[0-9a-z].*\//i);
352 expect(url.attr('href')).toBe(url.text());
357 describe("Check switch menu Records/Targets", function () {
358 it("check mkwsSwitch", function () {
359 expect($("div.mkwsSwitch").length).toBe(1);
361 // expect 2 clickable links
362 expect($("div.mkwsSwitch a").length).toBe(2);
365 it("switch to target view", function () {
366 var click = $("div.mkwsSwitch").children('a').eq(1).trigger("click");
367 debug("target view click is success: " + click.length);
368 expect(click.length).toBe(1);
370 // now the target table must be visible
371 expect($("div.mkwsBytarget").is(":visible")).toBe(true);
372 expect($("div.mkwsRecords").is(":visible")).toBe(false);
374 // wait a half second, to show the target view
375 var time = (new Date).getTime();
376 waitsFor(function () {
377 return (new Date).getTime() - time > 700 ? true : false;
378 }, "wait some miliseconds", 1 * jasmine_config.second);
380 // look for table header
382 expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
386 it("switch back to record view", function () {
387 var click = $("div.mkwsSwitch").children('a').eq(0).trigger("click");
388 debug("record view click is success: " + click.length);
389 expect(click.length).toBe(1);
391 // now the target table must be visible
392 expect($("div.mkwsBytarget").is(":visible")).toBe(false);
393 expect($("div.mkwsRecords").is(":visible")).toBe(true);
397 describe("Check status client counter", function () {
398 function get_time() {
399 var date = new Date();
400 return date.getTime();
402 var time = get_time();
404 it("check status clients", function () {
405 waitsFor(function () {
406 var clients = $("div#mkwsStat span.clients");
407 if (clients.length == 1 && clients.text().match("0/1$")) {
413 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
417 var clients = $("div#mkwsStat span.clients");
418 debug("span.clients: " + clients.text());
419 expect(clients.text()).toEqual("0/1");
428 describe("All tests are done", function () {
429 it(">>> hooray <<<", function () {
430 mkws.jasmine_done = true;