1717551e9e8997c96099c014a04c028c8a55beb2
[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
356 describe("Check Source Facets", function () {
357     var $ = mkws.$;
358
359     it("limit search to first source", function () {
360         var hits_all_targets = get_hit_counter();
361         var source_number = 2; // 2=first source
362         // wait for a stat response
363         var waitcount = 0;
364         // do not click on wikipedia link - no author or subject facets possible
365         var link = "div.mkws-facet[data-mkws-facet='xtargets'] div.mkws-term a";
366
367         // wait for a visible source link in facets
368         waitsFor(function () {
369             var terms = $(link);
370             return terms && terms.length > 0;
371         }, "wait for source facets after author search", 5 * jasmine_config.second);
372
373
374         runs(function () {
375             var terms = $(link);
376             for (var i = 0; i < terms.length; i++) {
377                 var term = $(terms[i]).text();
378                 debug("check for good source: " + term);
379
380                 if (term.match(/wikipedia/i)) {
381                     debug("ignore source facet: " + term);
382                     source_number++;
383                 } else {
384                     break;
385                 }
386             }
387             debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
388
389             if ($("div.mkws-facet[data-mkws-facet='xtargets'] div.mkws-term:nth-child(" + source_number + ") a").text().length == 0) {
390                 debug("No good source found. Not clicking on the bad ones");
391                 return;
392             }
393
394             debug("click on source link nth-child(): " + source_number);
395             $("div.mkws-facet[data-mkws-facet='xtargets'] div.mkws-term:nth-child(" + source_number + ") a").trigger("click");
396
397             $(".mkws-pager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
398                 waitcount++;
399                 debug("DOM change mkws-pager, for stat: " + waitcount);
400             });
401         });
402
403         waitsFor(function () {
404             if ($("div.mkws-navi").length && $("div.mkws-navi").text().match(/(Source|datenquelle|kilder): /i)) {
405                 return true;
406             } else {
407                 return false;
408             }
409         }, "Search for source in navi bar", 4 * jasmine_config.second);
410
411         // Note: it may happens that limited source search returns the same number of hits
412         // as before. Thats not really an error, but unfortunate
413         waitsFor(function () {
414             var hits_single_target = get_hit_counter();
415
416             return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
417         }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
418
419         runs(function () {
420             var hits_single_target = get_hit_counter();
421             debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
422             expect(hits_all_targets).not.toBeLessThan(hits_single_target);
423             jasmine_status.source_click = 1;
424
425             $(".mkws-pager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
426         });
427     });
428 });
429
430
431 describe("Check record list", function () {
432     var $ = mkws.$;
433
434     it("check for single active client", function () {
435         if (!jasmine_status.source_click) {
436             debug("skip clients check due missing source click");
437             return;
438         }
439
440         waitsFor(function () {
441             var clients = $("div.mkws-stat span.mkws-client-count");
442             //debug("clients: " + clients.text());
443             return clients.length == 1 && clients.text().match("/1$");
444         }, "wait for Active clients: x/1", 5 * jasmine_config.second);
445
446         runs(function () {
447             var clients = $("div.mkws-stat span.mkws-client-count");
448             debug("span.mkws-client-count: " + clients.text());
449             expect(clients.text()).toMatch("/1$");
450         });
451     });
452
453     it("got a record", function () {
454         var linkaddr = "div.mkws-records div.mkws-summary:nth-child(1) a";
455
456         waitsFor(function () {
457             // remove + insert node: must be at least 2
458             return $(linkaddr).length > 0;
459         }, "wait until we see a new record", 2.5 * jasmine_config.second);
460
461         runs(function () {
462             expect($(linkaddr).length).toBeGreaterThan(0);
463         });
464     });
465 });
466
467 describe("Show record", function () {
468     var $ = mkws.$;
469
470     var record_number = 1; // the Nth record in hit list
471     it("show record author", function () {
472         var click = $("div.mkws-records div.mkws-summary:nth-child(" + record_number + ") a").trigger("click");
473         debug("show record click is success: " + click.length);
474         expect(click.length).toBe(1);
475
476         // wait until the record pops up
477         waitsFor(function () {
478             var show = $("div.mkws-records div.mkws-summary:nth-child(" + record_number + ") > div.mkws-details");
479             //debug("poprecord: " + (show ? show.length : -1) + " " + $("div.mkws-records div.mkws-summary").text());
480             return show != null && show.length ? true : false;
481         }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
482
483         runs(function () {
484             debug("show record pop up");
485             expect($("div.mkws-records div.mkws-summary:nth-child(" + record_number + ") div")).not.toBe(null);
486         });
487     });
488
489     it("extract URL", function () {
490         if (jasmine_config.show_record_url == false) {
491             debug("ignore test for URL in record")
492             return;
493         }
494
495         var urls = $("div.mkws-records div.mkws-summary:nth-child(" + record_number + ") div table tbody tr td a");
496         debug("number of extracted URL from record: " + urls.length);
497         // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
498         for (var i = 0; i < urls.length; i++) {
499             var url = $(urls[i]);
500             debug("URL: " + url.attr('href') + " text: " + url.text());
501
502             expect(url.attr('href')).not.toBe(null);
503             expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
504             expect(url.text()).not.toBe("");
505         }
506     });
507 });
508
509 describe("Check switch menu Records/Targets", function () {
510     var $ = mkws.$;
511
512     it("check mkws-switch", function () {
513         expect($("div.mkws-switch").length).toBe(1);
514
515         // expect 2 clickable links
516         expect($("div.mkws-switch > a").length).toBe(2);
517     });
518
519     it("switch to target view", function () {
520         $("div.mkws-switch > a").eq(1).trigger("click");
521
522         // now the target table must be visible
523         expect($("div.mkws-targets").is(":visible")).toBe(true);
524         expect($("div.mkws-records").is(":visible")).toBe(false);
525
526         // wait a half second, to show the target view
527         var time = (new Date).getTime();
528         waitsFor(function () {
529             return (new Date).getTime() - time > 700 ? true : false;
530         }, "wait some miliseconds", 1 * jasmine_config.second);
531
532         // look for table header
533         runs(function () {
534             expect($("div.mkws-targets").html()).toMatch(/Target ID/);
535         });
536     });
537
538     it("switch back to record view", function () {
539         $("div.mkws-switch > a").eq(0).trigger("click");
540
541         // now the target table must be visible
542         expect($("div.mkws-targets").is(":visible")).toBe(false);
543         expect($("div.mkws-records").is(":visible")).toBe(true);
544     });
545 });
546
547 describe("Check translations", function () {
548     var $ = mkws.$;
549
550     // handle html entities, "Zur&uuml;ck" => "Zurück"
551     var M = function (string) {
552             var text = $("<span/>").html(mkws.M(string)).text()
553             debug("translate check for: " + text);
554             return text;
555         };
556     var lang = function () {
557             return mkws.config.lang
558         };
559
560     function check_translation(list, text) {
561         expect(list.length).toBe(text.length);
562
563         for (var i = 0; i < text.length; i++) {
564             expect($(list[i]).text()).toBe(M(text[i]));
565         }
566     }
567
568     it("check language", function () {
569         var lang = mkws.config.lang;
570         debug("lang: " + lang);
571         expect(lang).toMatch(/^(de|da|)$/);
572     });
573
574 /*
575   locale_lang: {
576     "de": {
577       "Authors": "Autoren",
578       "Subjects": "Schlagw&ouml;rter",
579       "Sources": "Daten und Quellen",
580       "source": "datenquelle",
581       "Facets": "Termlisten",
582       "Next": "Weiter",
583       "Prev": "Zur&uuml;ck",
584       "Search": "Suche",
585       "Sort by": "Sortieren nach",
586       "and show": "und zeige",
587       "per page": "pro Seite",
588       "Displaying": "Zeige",
589       "to": "von",
590       "of": "aus",
591       "found": "gefunden",
592       "Title": "Titel",
593       "Author": "Autor",
594       "author": "autor",
595       "Date": "Datum",
596       "Subject": "Schlagwort",
597       "subject": "schlagwort",
598       "Location": "Ort",
599       "Records": "Datens&auml;tze",
600       "Targets": "Datenbanken",
601       "relevance": "Relevanz",
602       "title": "Titel",
603       "newest": "Neueste",
604       "oldest": "&Auml;lteste",
605
606       "dummy": "dummy"
607     },
608 */
609
610     it("search button", function () {
611         var list = $(".mkws-pager-desc > span");
612         expect($("form > input[type=submit]").attr("value")).toBe(M("Search"));
613     });
614
615     it("switch", function () {
616         var list = $(".mkws-switch > a")
617         var text = ["Records", "Targets"];
618
619         check_translation(list, text);
620     });
621
622
623     it("ranking form", function () {
624         var list = $(".mkws-ranking > form > span");
625         var text = ["Sort by", "and show", "per page"];
626
627         check_translation(list, text);
628
629         // double check
630         if (lang == "de") {
631             expect("Sortieren nach").toBe(M("Sort by"));
632             expect("Sortieren nach").toBe($(list[0]).text());
633         } else if (lang == "da") {
634             expect("Sorter efter").toBe(M("Sort by"));
635             expect("Sorter efter").toBe($(list[0]).text());
636         }
637
638
639         expect($("select.mkws-sort > option[selected=selected]").text()).toBe(M("relevance"));
640     });
641
642     it("facets sidebar", function () {
643         var list = $(".mkws-facet-title");
644         var text = ["Sources", "Subjects", "Authors"];
645
646         check_translation(list, text);
647     });
648
649     it("facets navigation/filter", function () {
650         var list = $(".mkws-navi > span");
651         var text = ["source", "author"];
652
653         check_translation(list, text);
654     });
655
656     it("navigation", function () {
657         var list = $(".mkws-pager-desc > span");
658         var text = ["Displaying", "to", "of", "found"];
659
660         check_translation(list, text);
661
662         expect($(".mkws-next").text()).toBe(M("Next"));
663         expect($(".mkws-prev").text()).toBe(M("Prev"));
664     });
665
666     it("record details", function () {
667         var text = ["Title", "Date", "Author"]; // , "Subject", "Locations"];
668         var list = $("div.mkws-details table > tbody > tr > th");
669
670         // compare only the first 3 elements
671         list = list.splice(0, text.length)
672
673         check_translation(list, text);
674     });
675
676 /* not tested
677      *
678      * Status line:  -- Active clients : 0/1 -- Retrieved records : 4/4
679      *
680      */
681 });
682
683 describe("Check status client counter", function () {
684     var $ = mkws.$;
685
686     function get_time() {
687         var date = new Date();
688         return date.getTime();
689     }
690     var time = get_time();
691
692     it("check status clients", function () {
693         if (!jasmine_status.source_click) {
694             debug("skip clients check due missing source click");
695             return;
696         }
697
698         waitsFor(function () {
699             var clients = $("div.mkws-stat span.mkws-client-count");
700             debug("clients: " + clients.text());
701             if (clients.length == 1 && clients.text().match("0/1$")) {
702                 return true;
703             } else {
704                 return false;
705             }
706         }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
707
708         runs(function () {
709             var clients = $("div.mkws-stat span.mkws-client-count");
710             debug("span.mkws-client-count: " + clients.text());
711             expect(clients.text()).toMatch("0/1$");
712         });
713     });
714 });
715
716 /* remove the "source" and "author" facet link to get more records again */
717 describe("Check removable facets links", function () {
718     var $ = mkws.$;
719
720     it("remove links for source and author", function () {
721         var waitcount = 0;
722         if (!jasmine_config.check_sortby) {
723             debug("ignore check for removable facets");
724             return;
725         }
726
727
728         runs(function () {
729             var click = $("a.mkws-removable").eq(0).trigger("click");
730             debug("Removed first facets link: " + click.length);
731             expect(click.length).toBe(1);
732         });
733
734         runs(function () {
735             $("div.mkws-records").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
736                 waitcount++;
737                 if (waitcount <= 5 || (waitcount % 5 == 0)) {
738                     debug("DOM change mkws-records for removeable: " + waitcount);
739                 }
740             });
741         });
742
743         waitsFor(function () {
744             return waitcount >= 2 && $("a.mkws-removable").length == 1 ? 1 : 0;
745         }, "Records DOM change mkws-records, removable", 2 * jasmine_config.second);
746
747         runs(function () {
748             debug("unbind removable");
749             $("div.mkws-records").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
750             waitcount = 0;
751
752             $("div.mkws-records").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
753                 waitcount++;
754                 if (waitcount <= 5 || (waitcount % 5 == 0)) {
755                     debug("DOM change mkws-records for removeable2: " + waitcount);
756                 }
757             });
758
759             var click = $("a.mkws-removable").eq(0).trigger("click");
760             debug("Removed second facets link: " + click.length);
761             expect(click.length).toBe(1);
762         });
763
764         waitsFor(function () {
765             return waitcount >= 2 && $("a.mkws-removable").length == 0 ? true : false;
766         }, "DOM change mkws-records, removable2", 2 * jasmine_config.second);
767
768         runs(function () {
769             debug("unbind removable2");
770             $("div.mkws-records").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
771         });
772     });
773 });
774
775
776 describe("Check per page options", function () {
777     var $ = mkws.$;
778
779     it("show per page", function () {
780         if (!jasmine_config.check_sortby) {
781             debug("ignore check for per page select");
782             return;
783         }
784         var waitcount = 0;
785         var per_page_number = 20;
786
787
788         runs(function () {
789             var select = $("select.mkws-perpage option[selected='selected']");
790             debug("per page default is: " + select.text() + " and unselect it");
791             select.removeAttr('selected');
792
793             select = $("select.mkws-perpage option[value='" + per_page_number + "']").attr('selected', true);
794             debug("per page is set to: " + select.text());
795             select.trigger("change");
796
797             $("div.mkws-records").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
798                 waitcount++;
799                 if (waitcount <= 5 || (waitcount % 10 == 0)) {
800                     debug("DOM change mkws-records, per page: " + waitcount);
801                 }
802             });
803         });
804
805         waitsFor(function () {
806             // debug("per page waitcounter: " + waitcount)
807             return waitcount >= (per_page_number + 10) ? true : false;
808         }, "DOM change mkws-records, by per page", 3 * jasmine_config.second);
809
810         runs(function () {
811             debug("unbind per page");
812             $("div.mkws-records").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
813
814             var records = $("div.mkws-records > div.mkws-summary");
815             debug("Per page got now " + records.length + " records");
816             expect(records.length).toBe(per_page_number);
817         });
818     });
819 });
820
821 describe("Check SortBy options", function () {
822     var $ = mkws.$;
823
824     it("sort by title", function () {
825         if (!jasmine_config.check_sortby) {
826             debug("ignore check for sort by");
827             return;
828         }
829
830         var waitcount = 0;
831         var sort_value = 'title:1';
832         var per_page_number = 20;
833
834         // keep current title list
835         var title_list_old = title_list("xxx ");
836
837         function title_list(prefix) {
838             var list = [];
839             var terms = $("div.mkws-records > div.mkws-summary > div.mkws-field-data span.mkws-field-title");
840             for (var i = 0; i < terms.length; i++) {
841                 var term = $(terms[i]).text().trim();
842                 list.push(term);
843                 // debug(prefix + "title: " + term);
844             }
845             return list;
846         }
847
848         runs(function () {
849             $("div.mkws-records").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
850                 waitcount++;
851                 if (waitcount <= 5 || (waitcount % 10 == 0)) {
852                     debug("DOM change mkws-records, sort by: " + waitcount);
853                 }
854             });
855
856             var select = $("select.mkws-sort option[selected='selected']");
857             debug("Sort by default is: " + select.text() + " and unselect it");
858             select.removeAttr('selected');
859
860             select = $("select.mkws-sort option[value='" + sort_value + "']").attr('selected', true);
861             debug("sort by is set to: " + select.text());
862             select.trigger("change");
863         });
864
865         waitsFor(function () {
866             //debug("wait for2: " + waitcount);
867             return waitcount >= (per_page_number + 10) ? true : false;
868         }, "DOM change mkws-records, by sort page", 3 * jasmine_config.second);
869
870         runs(function () {
871             $("div.mkws-records").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
872             debug("unbind by sort");
873
874             var records = $("div.mkws-records > div.mkws-summary");
875             debug("Sort by got now " + records.length + " records");
876             expect(records.length).toBe(per_page_number);
877         });
878
879         runs(function () {
880             var title_list_new = title_list("yyy ");
881             var diff_flag = 0;
882             for (var i = 0; i < title_list_old.length; i++) {
883                 debug((i + 1) + ". " + title_list_old[i] + " :: " + title_list_new[i]);
884
885                 if (title_list_old[i] != title_list_new[i]) {
886                     diff_flag++;
887                 }
888             }
889             debug("Title changes: " + diff_flag + " out of " + per_page_number);
890             expect(diff_flag).not.toBe(0);
891         });
892     });
893 });
894
895
896 xdescribe("Check async widget discovery", function () {
897     var $ = mkws.$;
898     it("initialises a new widget", function () {
899         $("div.mkws-search").after('<div id="asyncSearch"><div class="mkws-search mkws-team-async"></div></div>');
900         mkws.init("Another search box", "#asyncSearch");
901         waitsFor(function () {
902             return $("#asyncSearch input").length >= 1 ? true : false;
903         }, "Call init() to build an .mkws-search", 750);
904         runs(function () {
905             var numInput = $("div.mkws-search input").length;
906             debug("Input elements present: " + numInput);
907             expect(numInput).toBe(4);
908             var numRec = $("div.mkws-records > div.mkws-summary").length;
909             debug("Records should still be present. There are: " + numRec);
910             expect(numRec).toBeGreaterThan(0);
911         });
912     });
913 });
914
915 /* done */
916 describe("All tests are done", function () {
917     it(">>> hooray <<<", function () {
918         mkws.jasmine_done = true;
919         debug(">>> hooray <<<");
920     });
921 });