better description
[mkws-moved-to-github.git] / test / spec / mkws-pazpar2.js
1 /* Copyright (c) 2013-2014 Index Data ApS. http://indexdata.com
2  *
3  * perform papzpar2 / pz2.js search & retrieve request in the browser
4  *
5  */
6
7 // get references from mkws.js, lazy evaluation
8 var debug = function (text) {
9         // use a debug function with time stamps
10         mkws.teams["AUTO"].info("Jasmine: " + text);
11
12         //mkws.log("Jasmine: " + text)
13     }
14
15     // Define empty jasmine_config for simple applications that don't define it.
16 if (jasmine_config == null || typeof jasmine_config != 'object') {
17     var jasmine_config = {};
18 }
19
20 var jasmine_status = {
21     source_click: 0
22 };
23
24 /* check config for jasmine test
25  *
26  * you can override the default values in the config
27  * object: jasmine_config = {};
28  *
29  */
30 function init_jasmine_config() {
31
32     var jasmine_config_default = {
33         // tune parameter for batch testing
34         batch_tuning: true,
35
36         search_query: "freebsd",
37         max_time: 17,
38         // in seconds
39         expected_hits: 80,
40         // at least expected hit counter
41         second: 1000,
42         // miliseconds to seconds
43         show_record_url: true,
44         // check for valid URL in records
45         check_motd: true,
46
47         // check sort by and per page menu
48         check_sortby: false,
49
50         dummy: false
51     };
52
53     // use default values for undefined values
54     for (var key in jasmine_config_default) {
55         if (!jasmine_config.hasOwnProperty(key)) {
56             jasmine_config[key] = jasmine_config_default[key];
57         }
58         debug("jasmine config: " + key + " => " + jasmine_config[key]);
59     }
60
61     // jenkins batch tests
62     if (jasmine_config.batch_tuning) {
63         var sec = mkws.getParameterByName("second", document.location);
64
65         // run on localhost
66         if (!sec && document.location.href.match(/^http:\/\/localhost:4040/)) {
67             sec = 2000;
68         }
69
70         if (sec && parseInt(sec) >= 100) {
71             jasmine_config.second = parseInt(sec);
72             debug("longer timeouts for batch testing: " + jasmine_config.second);
73         }
74     }
75
76     mkws.jasmine_done = false;
77 };
78
79 function get_hit_counter() {
80     var $ = mkws.$;
81     // not yet here
82     if ($(".mkws-pager").length == 0) return -1;
83
84     var found = $(".mkws-pager").text();
85     var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
86     re.exec(found);
87     var hits = -1;
88
89     if (RegExp.$1) {
90         hits = parseInt(RegExp.$1);
91         if (hits <= 0) {
92             debug("Oooops in get_hit_counter: " + RegExp.$1 + " '" + found + "'");
93         }
94     }
95
96     //debug("Hits: " + hits);
97     return hits;
98 };
99
100 /******************************************************************************/
101 describe("Init jasmine config", function () {
102     it("jasmine was successfully initialized", function () {
103         init_jasmine_config();
104
105         expect(jasmine_config.search_query).toMatch(/\w/);
106         expect(jasmine_config.second).toBeGreaterThan(100);
107         expect(jasmine_config.max_time).toBeGreaterThan(1);
108         expect(jasmine_config.expected_hits).toBeGreaterThan(1);
109     });
110 });
111
112 //disabled
113 xdescribe("Check MOTD before search", function () {
114     var $ = mkws.$;
115
116     // Check that the MOTD has been moved into its container, and
117     // is visible before the search.
118     // the mkws-motd div was originally inside a testMOTD div, which should
119     // now be empty
120     // Note that the testMOTD is a regular div, and uses #testMOTD,
121     // since the automagic class-making does not apply to it.
122     it("MOTD is hidden", function () {
123         expect($(".mkws-motd").length).toBe(1);
124         expect($("#testMOTD").length).toBe(1);
125         expect($("#testMOTD").text()).toMatch("^ *$");
126     });
127
128     it("mkws-motd-container has received the text", function () {
129         expect($(".mkws-motd-container").length).toBe(1);
130         expect($(".mkws-motd-container").text()).toMatch(/MOTD/);
131     });
132 });
133
134 describe("Check pazpar2 search", function () {
135     var $ = mkws.$;
136
137     it("pazpar2 was successfully initialized", function () {
138         expect(mkws.config.error).toBe(undefined);
139     });
140
141     it("validate HTML id's", function () {
142         expect($("input.mkws-query").length).toBe(1);
143         expect($("input.mkws-button").length).toBe(1);
144
145         expect($(".mkws-next").length).not.toBe(1);
146         expect($(".mkws-prev").length).not.toBe(1);
147     });
148
149     it("run search query", function () {
150         var search_query = jasmine_config.search_query; // short hit counter with some paging
151         $("input.mkws-query").val(search_query);
152         debug("set search query: " + search_query)
153         expect($("input.mkws-query").val()).toMatch("^" + search_query + "$");
154
155         if (mkws.config.use_service_proxy) {
156             // wait for service proxy auth
157             waitsFor(function () {
158                 return mkws.authenticated;
159             }, "SP auth done", 10 * jasmine_config.second);
160         } else {
161             debug("running raw pp2, don't wait for mkws auth");
162         }
163
164         runs(function () {
165             debug("Click on submit button");
166             $("input.mkws-button").trigger("click");
167         })
168     });
169 });
170
171 describe("Check MOTD after search", function () {
172     var $ = mkws.$;
173
174     it("MOTD is hidden", function () {
175         if (!jasmine_config.check_motd) {
176             return;
177         }
178
179         expect($(".mkws-motd").length).toBe(1);
180         expect($(".mkws-motd").is(":hidden")).toBe(true);
181         debug("motd t=" + $(".mkws-motd").text());
182         debug("motd v=" + $(".mkws-motd").is(":visible"));
183     });
184 });
185
186
187 /*
188  * This part runs in background. It should be rewritten with
189  * async jasmine functions
190  *
191  */
192 describe("Check pazpar2 navigation", function () {
193     var $ = mkws.$;
194
195     // Asynchronous part
196     it("check running search next/prev", function () {
197         expect($(".mkws-pager").length).toBe(1);
198
199         function my_click(id, time) {
200             setTimeout(function () {
201                 debug("trigger click on id: " + id);
202                 $(id).trigger("click");
203             }, time * jasmine_config.second);
204         }
205
206         waitsFor(function () {
207             return $("div.mkws-pager div:nth-child(2) a").length >= 2 ? true : false;
208         }, "Expect next link 2", 10 * jasmine_config.second);
209
210         runs(function () {
211             // click next/prev after N seconds
212             my_click(".mkws-next", 0);
213         });
214
215         waitsFor(function () {
216             return $("div.mkws-pager div:nth-child(2) a").length >= 3 ? true : false;
217         }, "Expect next link 3", 5 * jasmine_config.second);
218
219         runs(function () {
220             // click next/prev after N seconds
221             my_click(".mkws-next", 0);
222             my_click(".mkws-prev", 0.2);
223         });
224     });
225 });
226
227 describe("Check pazpar2 hit counter", function () {
228     var $ = mkws.$;
229
230     it("check running search hit counter", function () {
231         var max_time = jasmine_config.max_time; // in seconds
232         var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
233         var hits = 0;
234
235         waitsFor(function () {
236             hits = get_hit_counter();
237             return hits > expected_hits;
238         }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
239
240         runs(function () {
241             debug("mkws pager found records: '" + hits + "'");
242             expect($(".mkws-pager").length).toBe(1);
243             expect(hits).toBeGreaterThan(expected_hits);
244         });
245     });
246 });
247
248 describe("Check Facets", function () {
249     var $ = mkws.$;
250
251     it("found Facets", function () {
252         var facets = $("div.mkws-facets");
253         debug("Facet success: " + facets.length);
254         expect(facets.length).toBe(1);
255
256         waitsFor(function () {
257             return $("div.mkws-facet[data-mkws-facet='xtargets']").length == 1 ? true : false;
258         }, "check for facet sources", 4 * jasmine_config.second);
259
260         // everything displayed?
261         runs(function () {
262             var sources = $("div.mkws-facet[data-mkws-facet='xtargets']");
263             debug("Facet sources success: " + sources.length);
264             expect(sources.length).toBe(1);
265
266             var subjects = $("div.mkws-facet[data-mkws-facet='subject']");
267             expect(subjects.length).toBe(1);
268
269             var authors = $("div.mkws-facet[data-mkws-facet='author']");
270             expect(authors.length).toBe(1);
271         });
272
273         waitsFor(function () {
274             return $("div.mkws-facet[data-mkws-facet='author'] div.mkws-term").length >= 2 ? true : false;
275         }, "At least two author link displayed", 4 * jasmine_config.second);
276
277         runs(function () {
278             expect($("div.mkws-facet[data-mkws-facet='author'] div.mkws-term").length).toBeGreaterThan(1);
279         });
280     });
281 });
282
283
284
285 describe("Check Author Facets", function () {
286     var $ = mkws.$;
287
288     it("limit search to first author", function () {
289         if (mkws.config.disable_facet_authors_search) {
290             debug("Facets: ignore limit search for authors");
291             return;
292         }
293
294         var hits_all_targets = get_hit_counter();
295         var author_number = 2; // 2=first author
296         // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
297         // do not click on author names without a comma, e.g.: "Joe Barbara"
298         // because searching on such authors won't find anything.
299         runs(function () {
300             var terms = $("div.mkws-facet[data-mkws-facet='author'] div.mkws-term a");
301             for (var i = 0; i < terms.length; i++) {
302                 var term = $(terms[i]).text();
303                 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
304                     debug("ignore author facet: " + term);
305                     author_number++;
306                 } else {
307                     break;
308                 }
309             }
310             if ($("div.mkws-facet[data-mkws-facet='author'] div.mkws-term:nth-child(" + author_number + ") a").text().length == 0) {
311                 debug("No good authors found. Not clicking on the bad ones");
312                 return;
313             }
314
315             debug("Clicking on author (" + author_number + ") " + $("div.mkws-facet[data-mkws-facet='author'] div.mkws-term:nth-child(" + author_number + ") a").text());
316             $("div.mkws-facet[data-mkws-facet='author'] div.mkws-term:nth-child(" + author_number + ") a").trigger("click");
317         });
318
319         waitsFor(function () {
320             var hits_single_target = get_hit_counter();
321             // debug("hits_single_target='" + hits_single_target + "' cf. hits_all_targets='" + hits_all_targets + "'");
322             return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
323         }, "Limited author search for less than " + hits_all_targets + " hits", 4.5 * jasmine_config.second);
324
325         runs(function () {
326             var hits_single_target = get_hit_counter();
327             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
328         });
329     });
330 });
331
332 describe("Check active clients author", function () {
333     var $ = mkws.$;
334
335     it("check for active clients after limited author search", function () {
336         waitsFor(function () {
337             var clients = $("div.mkws-stat span.mkws-client-count");
338             // debug("clients: " + clients.text());
339             return clients.length == 1 && clients.text().match("/[1-9]+[0-9]*$");
340         }, "wait for Active clients: x/y", 5.5 * jasmine_config.second);
341
342         runs(function () {
343             var clients = $("div.mkws-stat span.mkws-client-count");
344             debug("span.mkws-client-count: " + clients.text());
345             expect(clients.text()).toMatch("/[1-9]+[0-9]*$");
346
347             // exact match of active clients (e.g. a SP misconfiguration)
348             if (jasmine_config.active_clients) {
349                 debug("check for " + jasmine_config.active_clients + " active connections");
350                 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
351             }
352         });
353     });
354
355     // avoid race conditions of source facets updates
356     it("wait a little bit for a source facets update", function () {
357         // wait a half second, to show the target view
358         var waittime = 0.5;
359         var time = (new Date).getTime();
360
361         waitsFor(function () {
362             return (new Date).getTime() - time > (waittime * jasmine_config.second) ? true : false;
363         }, "wait some miliseconds", (waittime + 0.5) * jasmine_config.second);
364
365         runs(function () {});
366     });
367 });
368
369 describe("Check Source Facets", function () {
370     var $ = mkws.$;
371
372     it("limit search to first source", function () {
373         var hits_all_targets = get_hit_counter();
374         var source_number = 2; // 2=first source
375         // wait for a stat response
376         var waitcount = 0;
377         // do not click on wikipedia link - no author or subject facets possible
378         var link = "div.mkws-facet[data-mkws-facet='xtargets'] div.mkws-term a";
379
380         // wait for a visible source link in facets
381         waitsFor(function () {
382             var terms = $(link);
383             return terms && terms.length > 0;
384         }, "wait for source facets after author search", 5 * jasmine_config.second);
385
386
387         runs(function () {
388             var terms = $(link);
389             for (var i = 0; i < terms.length; i++) {
390                 var term = $(terms[i]).text();
391                 debug("check for good source: " + term);
392
393                 if (term.match(/wikipedia/i)) {
394                     debug("ignore source facet: " + term);
395                     source_number++;
396                 } else {
397                     break;
398                 }
399             }
400             debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
401
402             if ($("div.mkws-facet[data-mkws-facet='xtargets'] div.mkws-term:nth-child(" + source_number + ") a").text().length == 0) {
403                 debug("No good source found. Not clicking on the bad ones");
404                 return;
405             }
406
407             debug("click on source link nth-child(): " + source_number);
408             $("div.mkws-facet[data-mkws-facet='xtargets'] div.mkws-term:nth-child(" + source_number + ") a").trigger("click");
409
410             $(".mkws-pager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
411                 waitcount++;
412                 debug("DOM change mkws-pager, for stat: " + waitcount);
413             });
414         });
415
416         waitsFor(function () {
417             if ($("div.mkws-navi").length && $("div.mkws-navi").text().match(/(Source|datenquelle|kilder): /i)) {
418                 return true;
419             } else {
420                 return false;
421             }
422         }, "Search for source in navi bar", 4 * jasmine_config.second);
423
424         // Note: it may happens that limited source search returns the same number of hits
425         // as before. Thats not really an error, but unfortunate
426         waitsFor(function () {
427             var hits_single_target = get_hit_counter();
428
429             return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
430         }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
431
432         runs(function () {
433             var hits_single_target = get_hit_counter();
434             debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
435             expect(hits_all_targets).not.toBeLessThan(hits_single_target);
436             jasmine_status.source_click = 1;
437
438             $(".mkws-pager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
439         });
440     });
441 });
442
443
444 describe("Check record list", function () {
445     var $ = mkws.$;
446
447     it("check for single active client", function () {
448         if (!jasmine_status.source_click) {
449             debug("skip clients check due missing source click");
450             return;
451         }
452
453         waitsFor(function () {
454             var clients = $("div.mkws-stat span.mkws-client-count");
455             //debug("clients: " + clients.text());
456             return clients.length == 1 && clients.text().match("/1$");
457         }, "wait for Active clients: x/1", 5 * jasmine_config.second);
458
459         runs(function () {
460             var clients = $("div.mkws-stat span.mkws-client-count");
461             debug("span.mkws-client-count: " + clients.text());
462             expect(clients.text()).toMatch("/1$");
463         });
464     });
465
466     it("got a record", function () {
467         var linkaddr = "div.mkws-records div.mkws-summary:nth-child(1) a";
468
469         waitsFor(function () {
470             // remove + insert node: must be at least 2
471             return $(linkaddr).length > 0;
472         }, "wait until we see a new record", 2.5 * jasmine_config.second);
473
474         runs(function () {
475             expect($(linkaddr).length).toBeGreaterThan(0);
476         });
477     });
478 });
479
480 describe("Show record", function () {
481     var $ = mkws.$;
482
483     var record_number = 1; // the Nth record in hit list
484     it("show record author", function () {
485         var click = $("div.mkws-records div.mkws-summary:nth-child(" + record_number + ") a").trigger("click");
486         debug("show record click is success: " + click.length);
487         expect(click.length).toBe(1);
488
489         // wait until the record pops up
490         waitsFor(function () {
491             var show = $("div.mkws-records div.mkws-summary:nth-child(" + record_number + ") > div.mkws-details");
492             //debug("poprecord: " + (show ? show.length : -1) + " " + $("div.mkws-records div.mkws-summary").text());
493             return show != null && show.length ? true : false;
494         }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
495
496         runs(function () {
497             debug("show record pop up");
498             expect($("div.mkws-records div.mkws-summary:nth-child(" + record_number + ") div")).not.toBe(null);
499         });
500     });
501
502     it("extract URL", function () {
503         if (jasmine_config.show_record_url == false) {
504             debug("ignore test for URL in record")
505             return;
506         }
507
508         var urls = $("div.mkws-records div.mkws-summary:nth-child(" + record_number + ") div table tbody tr td a");
509         debug("number of extracted URL from record: " + urls.length);
510         // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
511         for (var i = 0; i < urls.length; i++) {
512             var url = $(urls[i]);
513             debug("URL: " + url.attr('href') + " text: " + url.text());
514
515             expect(url.attr('href')).not.toBe(null);
516             expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
517             expect(url.text()).not.toBe("");
518         }
519     });
520 });
521
522 describe("Check switch menu Records/Targets", function () {
523     var $ = mkws.$;
524
525     it("check mkws-switch", function () {
526         expect($("div.mkws-switch").length).toBe(1);
527
528         // expect 2 clickable links
529         expect($("div.mkws-switch > a").length).toBe(2);
530     });
531
532     it("switch to target view", function () {
533         $("div.mkws-switch > a").eq(1).trigger("click");
534
535         // now the target table must be visible
536         expect($("div.mkws-targets").is(":visible")).toBe(true);
537         expect($("div.mkws-records").is(":visible")).toBe(false);
538
539         // wait a half second, to show the target view
540         var waittime = 0.7;
541         var time = (new Date).getTime();
542         waitsFor(function () {
543             return (new Date).getTime() - time > (waittime * jasmine_config.second) ? true : false;
544         }, "wait some miliseconds", (waittime + 0.3) * jasmine_config.second);
545
546         // look for table header
547         runs(function () {
548             expect($("div.mkws-targets").html()).toMatch(/Target ID/);
549         });
550     });
551
552     it("switch back to record view", function () {
553         $("div.mkws-switch > a").eq(0).trigger("click");
554
555         // now the target table must be visible
556         expect($("div.mkws-targets").is(":visible")).toBe(false);
557         expect($("div.mkws-records").is(":visible")).toBe(true);
558     });
559 });
560
561 describe("Check translations", function () {
562     var $ = mkws.$;
563
564     // handle html entities, "Zur&uuml;ck" => "Zurück"
565     var M = function (string) {
566             var text = $("<span/>").html(mkws.M(string)).text()
567             debug("translate check for: " + text);
568             return text;
569         };
570     var lang = function () {
571             return mkws.config.lang
572         };
573
574     function check_translation(list, text) {
575         expect(list.length).toBe(text.length);
576
577         for (var i = 0; i < text.length; i++) {
578             expect($(list[i]).text()).toBe(M(text[i]));
579         }
580     }
581
582     it("check language", function () {
583         var lang = mkws.config.lang;
584         debug("lang: " + lang);
585         expect(lang).toMatch(/^(de|da|)$/);
586     });
587
588 /*
589   locale_lang: {
590     "de": {
591       "Authors": "Autoren",
592       "Subjects": "Schlagw&ouml;rter",
593       "Sources": "Daten und Quellen",
594       "source": "datenquelle",
595       "Facets": "Termlisten",
596       "Next": "Weiter",
597       "Prev": "Zur&uuml;ck",
598       "Search": "Suche",
599       "Sort by": "Sortieren nach",
600       "and show": "und zeige",
601       "per page": "pro Seite",
602       "Displaying": "Zeige",
603       "to": "von",
604       "of": "aus",
605       "found": "gefunden",
606       "Title": "Titel",
607       "Author": "Autor",
608       "author": "autor",
609       "Date": "Datum",
610       "Subject": "Schlagwort",
611       "subject": "schlagwort",
612       "Location": "Ort",
613       "Records": "Datens&auml;tze",
614       "Targets": "Datenbanken",
615       "relevance": "Relevanz",
616       "title": "Titel",
617       "newest": "Neueste",
618       "oldest": "&Auml;lteste",
619
620       "dummy": "dummy"
621     },
622 */
623
624     it("search button", function () {
625         var list = $(".mkws-pager-desc > span");
626         expect($("form > input[type=submit]").attr("value")).toBe(M("Search"));
627     });
628
629     it("switch", function () {
630         var list = $(".mkws-switch > a")
631         var text = ["Records", "Targets"];
632
633         check_translation(list, text);
634     });
635
636
637     it("ranking form", function () {
638         var list = $(".mkws-ranking > form > span");
639         var text = ["Sort by", "and show", "per page"];
640
641         check_translation(list, text);
642
643         // double check
644         if (lang == "de") {
645             expect("Sortieren nach").toBe(M("Sort by"));
646             expect("Sortieren nach").toBe($(list[0]).text());
647         } else if (lang == "da") {
648             expect("Sorter efter").toBe(M("Sort by"));
649             expect("Sorter efter").toBe($(list[0]).text());
650         }
651
652
653         expect($("select.mkws-sort > option[selected=selected]").text()).toBe(M("relevance"));
654     });
655
656     it("facets sidebar", function () {
657         var list = $(".mkws-facet-title");
658         var text = ["Sources", "Subjects", "Authors"];
659
660         check_translation(list, text);
661     });
662
663     it("facets navigation/filter", function () {
664         var list = $(".mkws-navi > span");
665         var text = ["source", "author"];
666
667         check_translation(list, text);
668     });
669
670     it("navigation", function () {
671         var list = $(".mkws-pager-desc > span");
672         var text = ["Displaying", "to", "of", "found"];
673
674         check_translation(list, text);
675
676         expect($(".mkws-next").text()).toBe(M("Next"));
677         expect($(".mkws-prev").text()).toBe(M("Prev"));
678     });
679
680     it("record details", function () {
681         var text = ["Title", "Date", "Author"]; // , "Subject", "Locations"];
682         var list = $("div.mkws-details table > tbody > tr > th");
683
684         // compare only the first 3 elements
685         list = list.splice(0, text.length)
686
687         check_translation(list, text);
688     });
689
690 /* not tested
691      *
692      * Status line:  -- Active clients : 0/1 -- Retrieved records : 4/4
693      *
694      */
695 });
696
697 describe("Check status client counter", function () {
698     var $ = mkws.$;
699
700     function get_time() {
701         var date = new Date();
702         return date.getTime();
703     }
704     var time = get_time();
705
706     it("check status clients", function () {
707         if (!jasmine_status.source_click) {
708             debug("skip clients check due missing source click");
709             return;
710         }
711
712         waitsFor(function () {
713             var clients = $("div.mkws-stat span.mkws-client-count");
714             debug("clients: " + clients.text());
715             if (clients.length == 1 && clients.text().match("0/1$")) {
716                 return true;
717             } else {
718                 return false;
719             }
720         }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
721
722         runs(function () {
723             var clients = $("div.mkws-stat span.mkws-client-count");
724             debug("span.mkws-client-count: " + clients.text());
725             expect(clients.text()).toMatch("0/1$");
726         });
727     });
728 });
729
730 /* remove the "source" and "author" facet link to get more records again */
731 describe("Check removable facets links", function () {
732     var $ = mkws.$;
733
734     it("remove links for source and author", function () {
735         var waitcount = 0;
736         if (!jasmine_config.check_sortby) {
737             debug("ignore check for removable facets");
738             return;
739         }
740
741
742         runs(function () {
743             var click = $("a.mkws-removable").eq(0).trigger("click");
744             debug("Removed first facets link: " + click.length);
745             expect(click.length).toBe(1);
746         });
747
748         runs(function () {
749             $("div.mkws-records").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
750                 waitcount++;
751                 if (waitcount <= 5 || (waitcount % 5 == 0)) {
752                     debug("DOM change mkws-records for removeable: " + waitcount);
753                 }
754             });
755         });
756
757         waitsFor(function () {
758             return waitcount >= 2 && $("a.mkws-removable").length == 1 ? 1 : 0;
759         }, "Records DOM change mkws-records, removable", 2 * jasmine_config.second);
760
761         runs(function () {
762             debug("unbind removable");
763             $("div.mkws-records").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
764             waitcount = 0;
765
766             $("div.mkws-records").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
767                 waitcount++;
768                 if (waitcount <= 5 || (waitcount % 5 == 0)) {
769                     debug("DOM change mkws-records for removeable2: " + waitcount);
770                 }
771             });
772
773             var click = $("a.mkws-removable").eq(0).trigger("click");
774             debug("Removed second facets link: " + click.length);
775             expect(click.length).toBe(1);
776         });
777
778         waitsFor(function () {
779             return waitcount >= 2 && $("a.mkws-removable").length == 0 ? true : false;
780         }, "DOM change mkws-records, removable2", 2 * jasmine_config.second);
781
782         runs(function () {
783             debug("unbind removable2");
784             $("div.mkws-records").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
785         });
786     });
787 });
788
789
790 describe("Check per page options", function () {
791     var $ = mkws.$;
792
793     it("show per page", function () {
794         if (!jasmine_config.check_sortby) {
795             debug("ignore check for per page select");
796             return;
797         }
798         var waitcount = 0;
799         var per_page_number = 20;
800
801
802         runs(function () {
803             var select = $("select.mkws-perpage option[selected='selected']");
804             debug("per page default is: " + select.text() + " and unselect it");
805             select.removeAttr('selected');
806
807             select = $("select.mkws-perpage option[value='" + per_page_number + "']").attr('selected', true);
808             debug("per page is set to: " + select.text());
809             select.trigger("change");
810
811             $("div.mkws-records").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
812                 waitcount++;
813                 if (waitcount <= 5 || (waitcount % 10 == 0)) {
814                     debug("DOM change mkws-records, per page: " + waitcount);
815                 }
816             });
817         });
818
819         waitsFor(function () {
820             // debug("per page waitcounter: " + waitcount)
821             return waitcount >= (per_page_number + 10) ? true : false;
822         }, "DOM change mkws-records, by per page", 3 * jasmine_config.second);
823
824         runs(function () {
825             debug("unbind per page");
826             $("div.mkws-records").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
827
828             var records = $("div.mkws-records > div.mkws-summary");
829             debug("Per page got now " + records.length + " records");
830             expect(records.length).toBe(per_page_number);
831         });
832     });
833 });
834
835 describe("Check SortBy options", function () {
836     var $ = mkws.$;
837
838     it("sort by title", function () {
839         if (!jasmine_config.check_sortby) {
840             debug("ignore check for sort by");
841             return;
842         }
843
844         var waitcount = 0;
845         var sort_value = 'title:1';
846         var per_page_number = 20;
847
848         // keep current title list
849         var title_list_old = title_list("xxx ");
850
851         function title_list(prefix) {
852             var list = [];
853             var terms = $("div.mkws-records > div.mkws-summary > div.mkws-field-data span.mkws-field-title");
854             for (var i = 0; i < terms.length; i++) {
855                 var term = $(terms[i]).text().trim();
856                 list.push(term);
857                 // debug(prefix + "title: " + term);
858             }
859             return list;
860         }
861
862         runs(function () {
863             $("div.mkws-records").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
864                 waitcount++;
865                 if (waitcount <= 5 || (waitcount % 10 == 0)) {
866                     debug("DOM change mkws-records, sort by: " + waitcount);
867                 }
868             });
869
870             var select = $("select.mkws-sort option[selected='selected']");
871             debug("Sort by default is: " + select.text() + " and unselect it");
872             select.removeAttr('selected');
873
874             select = $("select.mkws-sort option[value='" + sort_value + "']").attr('selected', true);
875             debug("sort by is set to: " + select.text());
876             select.trigger("change");
877         });
878
879         waitsFor(function () {
880             //debug("wait for2: " + waitcount);
881             return waitcount >= (per_page_number + 10) ? true : false;
882         }, "DOM change mkws-records, by sort page", 3 * jasmine_config.second);
883
884         runs(function () {
885             $("div.mkws-records").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
886             debug("unbind by sort");
887
888             var records = $("div.mkws-records > div.mkws-summary");
889             debug("Sort by got now " + records.length + " records");
890             expect(records.length).toBe(per_page_number);
891         });
892
893         runs(function () {
894             var title_list_new = title_list("yyy ");
895             var diff_flag = 0;
896             for (var i = 0; i < title_list_old.length; i++) {
897                 debug((i + 1) + ". " + title_list_old[i] + " :: " + title_list_new[i]);
898
899                 if (title_list_old[i] != title_list_new[i]) {
900                     diff_flag++;
901                 }
902             }
903             debug("Title changes: " + diff_flag + " out of " + per_page_number);
904             expect(diff_flag).not.toBe(0);
905         });
906     });
907 });
908
909
910 xdescribe("Check async widget discovery", function () {
911     var $ = mkws.$;
912     it("initialises a new widget", function () {
913         $("div.mkws-search").after('<div id="asyncSearch"><div class="mkws-search mkws-team-async"></div></div>');
914         mkws.init("Another search box", "#asyncSearch");
915         waitsFor(function () {
916             return $("#asyncSearch input").length >= 1 ? true : false;
917         }, "Call init() to build an .mkws-search", 750);
918         runs(function () {
919             var numInput = $("div.mkws-search input").length;
920             debug("Input elements present: " + numInput);
921             expect(numInput).toBe(4);
922             var numRec = $("div.mkws-records > div.mkws-summary").length;
923             debug("Records should still be present. There are: " + numRec);
924             expect(numRec).toBeGreaterThan(0);
925         });
926     });
927 });
928
929 /* done */
930 describe("All tests are done", function () {
931     it(">>> hooray <<<", function () {
932         mkws.jasmine_done = true;
933         debug(">>> hooray <<<");
934     });
935 });