temporary disable translation checks due records without an author, MKWS-400
[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 // temporary disabled due records without an author, MKWS-400
562 xdescribe("Check translations", function () {
563     var $ = mkws.$;
564
565     // handle html entities, "Zur&uuml;ck" => "Zurück"
566     var M = function (string) {
567             var text = $("<span/>").html(mkws.M(string)).text()
568             debug("translate check for: " + text);
569             return text;
570         };
571     var lang = function () {
572             return mkws.config.lang
573         };
574
575     function check_translation(list, text) {
576         expect(list.length).toBe(text.length);
577
578         for (var i = 0; i < text.length; i++) {
579             expect($(list[i]).text().match(M(text[i]))).not.toBeNull();
580         }
581     }
582
583     it("check language", function () {
584         var lang = mkws.config.lang;
585         debug("lang: " + lang);
586         expect(lang).toMatch(/^(de|da|)$/);
587     });
588
589 /*
590   locale_lang: {
591     "de": {
592       "Authors": "Autoren",
593       "Subjects": "Schlagw&ouml;rter",
594       "Sources": "Daten und Quellen",
595       "source": "datenquelle",
596       "Facets": "Termlisten",
597       "Next": "Weiter",
598       "Prev": "Zur&uuml;ck",
599       "Search": "Suche",
600       "Sort by": "Sortieren nach",
601       "and show": "und zeige",
602       "per page": "pro Seite",
603       "Displaying": "Zeige",
604       "to": "von",
605       "of": "aus",
606       "found": "gefunden",
607       "Title": "Titel",
608       "Author": "Autor",
609       "author": "autor",
610       "Date": "Datum",
611       "Subject": "Schlagwort",
612       "subject": "schlagwort",
613       "Location": "Ort",
614       "Records": "Datens&auml;tze",
615       "Targets": "Datenbanken",
616       "relevance": "Relevanz",
617       "title": "Titel",
618       "newest": "Neueste",
619       "oldest": "&Auml;lteste",
620
621       "dummy": "dummy"
622     },
623 */
624
625     it("search button", function () {
626         var list = $(".mkws-pager-desc > span");
627         expect($("form > input[type=submit]").attr("value")).toBe(M("Search"));
628     });
629
630     it("switch", function () {
631         var list = $(".mkws-switch > a")
632         var text = ["Records", "Targets"];
633
634         check_translation(list, text);
635     });
636
637
638     it("ranking form", function () {
639         var list = $(".mkws-ranking > form > span");
640         var text = ["Sort by", "and show", "per page"];
641
642         check_translation(list, text);
643
644         // double check
645         if (lang == "de") {
646             expect("Sortieren nach").toBe(M("Sort by"));
647             expect("Sortieren nach").toBe($(list[0]).text());
648         } else if (lang == "da") {
649             expect("Sorter efter").toBe(M("Sort by"));
650             expect("Sorter efter").toBe($(list[0]).text());
651         }
652
653
654         expect($("select.mkws-sort > option[selected=selected]").text()).toBe(M("relevance"));
655     });
656
657     it("facets sidebar", function () {
658         var list = $(".mkws-facet-title");
659         var text = ["Sources", "Subjects", "Authors"];
660
661         check_translation(list, text);
662     });
663
664     it("facets navigation/filter", function () {
665         var list = $(".mkws-navi > span");
666         var text = ["source", "author"];
667
668         check_translation(list, text);
669     });
670
671     it("navigation", function () {
672         var list = $(".mkws-pager-desc > span");
673         var text = ["Displaying", "to", "of", "found"];
674
675         check_translation(list, text);
676
677         expect($(".mkws-next").text().match(M("Next"))).not.toBeNull();
678         expect($(".mkws-next").text().match(M("NextXXX"))).toBeNull();
679         expect($(".mkws-prev").text().match(M("Prev"))).not.toBeNull();
680     });
681
682     it("record details", function () {
683         var text = ["Title", "Date", "Author"]; // , "Subject", "Locations"];
684         var list = $("div.mkws-details table > tbody > tr > th");
685
686         // compare only the first 3 elements
687         list = list.splice(0, text.length)
688
689         check_translation(list, text);
690     });
691
692 /* not tested
693      *
694      * Status line:  -- Active clients : 0/1 -- Retrieved records : 4/4
695      *
696      */
697 });
698
699 describe("Check status client counter", function () {
700     var $ = mkws.$;
701
702     function get_time() {
703         var date = new Date();
704         return date.getTime();
705     }
706     var time = get_time();
707
708     it("check status clients", function () {
709         if (!jasmine_status.source_click) {
710             debug("skip clients check due missing source click");
711             return;
712         }
713
714         waitsFor(function () {
715             var clients = $("div.mkws-stat span.mkws-client-count");
716             debug("clients: " + clients.text());
717             if (clients.length == 1 && clients.text().match("0/1$")) {
718                 return true;
719             } else {
720                 return false;
721             }
722         }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
723
724         runs(function () {
725             var clients = $("div.mkws-stat span.mkws-client-count");
726             debug("span.mkws-client-count: " + clients.text());
727             expect(clients.text()).toMatch("0/1$");
728         });
729     });
730 });
731
732 /* remove the "source" and "author" facet link to get more records again */
733 describe("Check removable facets links", function () {
734     var $ = mkws.$;
735
736     it("remove links for source and author", function () {
737         var waitcount = 0;
738         if (!jasmine_config.check_sortby) {
739             debug("ignore check for removable facets");
740             return;
741         }
742
743
744         runs(function () {
745             var click = $("a.mkws-removable").eq(0).trigger("click");
746             debug("Removed first facets link: " + click.length);
747             expect(click.length).toBe(1);
748         });
749
750         runs(function () {
751             $("div.mkws-records").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
752                 waitcount++;
753                 if (waitcount <= 5 || (waitcount % 5 == 0)) {
754                     debug("DOM change mkws-records for removeable: " + waitcount);
755                 }
756             });
757         });
758
759         waitsFor(function () {
760             return waitcount >= 2 && $("a.mkws-removable").length == 1 ? 1 : 0;
761         }, "Records DOM change mkws-records, removable", 2 * jasmine_config.second);
762
763         runs(function () {
764             debug("unbind removable");
765             $("div.mkws-records").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
766             waitcount = 0;
767
768             $("div.mkws-records").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
769                 waitcount++;
770                 if (waitcount <= 5 || (waitcount % 5 == 0)) {
771                     debug("DOM change mkws-records for removeable2: " + waitcount);
772                 }
773             });
774
775             var click = $("a.mkws-removable").eq(0).trigger("click");
776             debug("Removed second facets link: " + click.length);
777             expect(click.length).toBe(1);
778         });
779
780         waitsFor(function () {
781             return waitcount >= 2 && $("a.mkws-removable").length == 0 ? true : false;
782         }, "DOM change mkws-records, removable2", 2 * jasmine_config.second);
783
784         runs(function () {
785             debug("unbind removable2");
786             $("div.mkws-records").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
787         });
788     });
789 });
790
791
792 describe("Check per page options", function () {
793     var $ = mkws.$;
794
795     it("show per page", function () {
796         if (!jasmine_config.check_sortby) {
797             debug("ignore check for per page select");
798             return;
799         }
800         var waitcount = 0;
801         var per_page_number = 20;
802
803
804         runs(function () {
805             var select = $("select.mkws-perpage option[selected='selected']");
806             debug("per page default is: " + select.text() + " and unselect it");
807             select.removeAttr('selected');
808
809             select = $("select.mkws-perpage option[value='" + per_page_number + "']").attr('selected', true);
810             debug("per page is set to: " + select.text());
811             select.trigger("change");
812
813             $("div.mkws-records").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
814                 waitcount++;
815                 if (waitcount <= 5 || (waitcount % 10 == 0)) {
816                     debug("DOM change mkws-records, per page: " + waitcount);
817                 }
818             });
819         });
820
821         waitsFor(function () {
822             // debug("per page waitcounter: " + waitcount)
823             return waitcount >= (per_page_number + 10) ? true : false;
824         }, "DOM change mkws-records, by per page", 3 * jasmine_config.second);
825
826         runs(function () {
827             debug("unbind per page");
828             $("div.mkws-records").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
829
830             var records = $("div.mkws-records > div.mkws-summary");
831             debug("Per page got now " + records.length + " records");
832             expect(records.length).toBe(per_page_number);
833         });
834     });
835 });
836
837 describe("Check SortBy options", function () {
838     var $ = mkws.$;
839
840     it("sort by title", function () {
841         if (!jasmine_config.check_sortby) {
842             debug("ignore check for sort by");
843             return;
844         }
845
846         var waitcount = 0;
847         var sort_value = 'title:1';
848         var per_page_number = 20;
849
850         // keep current title list
851         var title_list_old = title_list("xxx ");
852
853         function title_list(prefix) {
854             var list = [];
855             var terms = $("div.mkws-records > div.mkws-summary > div.mkws-field-data span.mkws-field-title");
856             for (var i = 0; i < terms.length; i++) {
857                 var term = $(terms[i]).text().trim();
858                 list.push(term);
859                 // debug(prefix + "title: " + term);
860             }
861             return list;
862         }
863
864         runs(function () {
865             $("div.mkws-records").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
866                 waitcount++;
867                 if (waitcount <= 5 || (waitcount % 10 == 0)) {
868                     debug("DOM change mkws-records, sort by: " + waitcount);
869                 }
870             });
871
872             var select = $("select.mkws-sort option[selected='selected']");
873             debug("Sort by default is: " + select.text() + " and unselect it");
874             select.removeAttr('selected');
875
876             select = $("select.mkws-sort option[value='" + sort_value + "']").attr('selected', true);
877             debug("sort by is set to: " + select.text());
878             select.trigger("change");
879         });
880
881         waitsFor(function () {
882             //debug("wait for2: " + waitcount);
883             return waitcount >= (per_page_number + 10) ? true : false;
884         }, "DOM change mkws-records, by sort page", 3 * jasmine_config.second);
885
886         runs(function () {
887             $("div.mkws-records").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
888             debug("unbind by sort");
889
890             var records = $("div.mkws-records > div.mkws-summary");
891             debug("Sort by got now " + records.length + " records");
892             expect(records.length).toBe(per_page_number);
893         });
894
895         runs(function () {
896             var title_list_new = title_list("yyy ");
897             var diff_flag = 0;
898             for (var i = 0; i < title_list_old.length; i++) {
899                 debug((i + 1) + ". " + title_list_old[i] + " :: " + title_list_new[i]);
900
901                 if (title_list_old[i] != title_list_new[i]) {
902                     diff_flag++;
903                 }
904             }
905             debug("Title changes: " + diff_flag + " out of " + per_page_number);
906             expect(diff_flag).not.toBe(0);
907         });
908     });
909 });
910
911
912 xdescribe("Check async widget discovery", function () {
913     var $ = mkws.$;
914     it("initialises a new widget", function () {
915         $("div.mkws-search").after('<div id="asyncSearch"><div class="mkws-search mkws-team-async"></div></div>');
916         mkws.init("Another search box", "#asyncSearch");
917         waitsFor(function () {
918             return $("#asyncSearch input").length >= 1 ? true : false;
919         }, "Call init() to build an .mkws-search", 750);
920         runs(function () {
921             var numInput = $("div.mkws-search input").length;
922             debug("Input elements present: " + numInput);
923             expect(numInput).toBe(4);
924             var numRec = $("div.mkws-records > div.mkws-summary").length;
925             debug("Records should still be present. There are: " + numRec);
926             expect(numRec).toBeGreaterThan(0);
927         });
928     });
929 });
930
931 /* done */
932 describe("All tests are done", function () {
933     it(">>> hooray <<<", function () {
934         mkws.jasmine_done = true;
935         debug(">>> hooray <<<");
936     });
937 });