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