remove facets links one after each other, not a the same time
[mkws-moved-to-github.git] / test / spec / mkws-pazpar2.js
1 /* Copyright (c) 2013-2014 IndexData 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         mkws.log("Jasmine: " + text)
10     }
11
12     // Define empty jasmine_config for simple applications that don't define it.
13 if (jasmine_config == null || typeof jasmine_config != 'object') {
14     var jasmine_config = {};
15 }
16
17 var jasmine_status = {
18     source_click: 0
19 };
20
21 /* check config for jasmine test
22  *
23  * you can override the default values in the config
24  * object: jasmine_config = {};
25  *
26  */
27 function init_jasmine_config() {
28
29     var jasmine_config_default = {
30         search_query: "freebsd",
31         max_time: 17,
32         // in seconds
33         expected_hits: 80,
34         // at least expected hit counter
35         second: 1000,
36         // miliseconds to seconds
37         show_record_url: true,
38         // check for valid URL in records
39         check_motd: true,
40         dummy: false
41     };
42
43     // use default values for undefined values
44     for (var key in jasmine_config_default) {
45         if (!jasmine_config.hasOwnProperty(key)) {
46             jasmine_config[key] = jasmine_config_default[key];
47         }
48         debug("jasmine config: " + key + " => " + jasmine_config[key]);
49     }
50
51     mkws.jasmine_done = false;
52 }
53
54 var get_hit_counter = function () {
55         // not yet here
56         if (mkws.$(".mkwsPager").length == 0) return -1;
57
58         var found = mkws.$(".mkwsPager").text();
59         var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
60         re.exec(found);
61         var hits = -1;
62
63         if (RegExp.$1) {
64             hits = parseInt(RegExp.$1);
65             if (hits <= 0) {
66                 debug("Oooops in get_hit_counter: " + RegExp.$1 + " '" + found + "'");
67             }
68         }
69
70         //debug("Hits: " + hits);
71         return hits;
72     }
73
74 describe("Init jasmine config", function () {
75     it("jasmine was successfully initialized", function () {
76         init_jasmine_config();
77
78         expect(jasmine_config.search_query).toMatch(/\w/);
79         expect(jasmine_config.second).toBeGreaterThan(100);
80         expect(jasmine_config.max_time).toBeGreaterThan(1);
81         expect(jasmine_config.expected_hits).toBeGreaterThan(1);
82     });
83 });
84
85 //disabled
86 xdescribe("Check MOTD before search", function () {
87     // Check that the MOTD has been moved into its container, and
88     // is visible before the search.
89     // the mkwsMOTD div was originally inside a testMOTD div, which should
90     // now be empty
91     // Note that the testMOTD is a regular div, and uses #testMOTD,
92     // since the automagic class-making does not apply to it.
93     it("MOTD is hidden", function () {
94         expect(mkws.$(".mkwsMOTD").length).toBe(1);
95         expect(mkws.$("#testMOTD").length).toBe(1);
96         expect(mkws.$("#testMOTD").text()).toMatch("^ *$");
97     });
98
99     it("mkwsMOTDContainer has received the text", function () {
100         expect(mkws.$(".mkwsMOTDContainer").length).toBe(1);
101         expect(mkws.$(".mkwsMOTDContainer").text()).toMatch(/MOTD/);
102     });
103 });
104
105 describe("Check pazpar2 search", function () {
106     it("pazpar2 was successfully initialized", function () {
107         expect(mkws.config.error).toBe(undefined);
108     });
109
110     it("validate HTML id's", function () {
111         expect(mkws.$("input.mkwsQuery").length).toBe(1);
112         expect(mkws.$("input.mkwsButton").length).toBe(1);
113
114         expect(mkws.$(".mkwsNext").length).not.toBe(1);
115         expect(mkws.$(".mkwsPrev").length).not.toBe(1);
116     });
117
118     it("run search query", function () {
119         var search_query = jasmine_config.search_query; // short hit counter with some paging
120         mkws.$("input.mkwsQuery").val(search_query);
121         debug("set search query: " + search_query)
122         expect(mkws.$("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
123
124         if (mkws.config.use_service_proxy) {
125             // wait for service proxy auth
126             waitsFor(function () {
127                 return mkws.authenticated;
128             }, "SP auth done", 10 * jasmine_config.second);
129         } else {
130             debug("running raw pp2, don't wait for mkws auth");
131         }
132
133         runs(function () {
134             debug("Click on submit button");
135             mkws.$("input.mkwsButton").trigger("click");
136         })
137     });
138 });
139
140 describe("Check MOTD after search", function () {
141     it("MOTD is hidden", function () {
142         if (!jasmine_config.check_motd) {
143             return;
144         }
145
146         expect(mkws.$(".mkwsMOTD").length).toBe(1);
147         expect(mkws.$(".mkwsMOTD").is(":hidden")).toBe(true);
148         debug("motd t=" + mkws.$(".mkwsMOTD").text());
149         debug("motd v=" + mkws.$(".mkwsMOTD").is(":visible"));
150     });
151 });
152
153
154 /*
155  * This part runs in background. It should be rewritten with
156  * async jasmine functions
157  *
158  */
159 describe("Check pazpar2 navigation", function () {
160     // Asynchronous part
161     it("check running search next/prev", function () {
162         expect(mkws.$(".mkwsPager").length).toBe(1);
163
164         function my_click(id, time) {
165             setTimeout(function () {
166                 debug("trigger click on id: " + id);
167                 mkws.$(id).trigger("click");
168             }, time * jasmine_config.second);
169         }
170
171         waitsFor(function () {
172             return mkws.$("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
173         }, "Expect next link 2", 10 * jasmine_config.second);
174
175         runs(function () {
176             // click next/prev after N seconds
177             my_click(".mkwsNext", 0);
178         });
179
180         waitsFor(function () {
181             return mkws.$("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
182         }, "Expect next link 3", 5 * jasmine_config.second);
183
184         runs(function () {
185             // click next/prev after N seconds
186             my_click(".mkwsNext", 0);
187             my_click(".mkwsPrev", 0.2);
188         });
189     });
190 });
191
192 describe("Check pazpar2 hit counter", function () {
193     it("check running search hit counter", function () {
194         var max_time = jasmine_config.max_time; // in seconds
195         var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
196         var hits = 0;
197
198         waitsFor(function () {
199             hits = get_hit_counter();
200             return hits > expected_hits;
201         }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
202
203         runs(function () {
204             debug("mkws pager found records: '" + hits + "'");
205             expect(mkws.$(".mkwsPager").length).toBe(1);
206             expect(hits).toBeGreaterThan(expected_hits);
207         });
208     });
209 });
210
211 describe("Check Termlist", function () {
212     it("found Termlist", function () {
213         var termlist = mkws.$("div.mkwsTermlists");
214         debug("Termlist success: " + termlist.length);
215         expect(termlist.length).toBe(1);
216
217         waitsFor(function () {
218             return mkws.$("div.mkwsFacet[data-mkws-facet='xtargets']").length == 1 ? true : false;
219         }, "check for facet sources", 4 * jasmine_config.second);
220
221         // everything displayed?
222         runs(function () {
223             var sources = mkws.$("div.mkwsFacet[data-mkws-facet='xtargets']");
224             debug("Termlist sources success: " + sources.length);
225             expect(sources.length).toBe(1);
226
227             var subjects = mkws.$("div.mkwsFacet[data-mkws-facet='subject']");
228             expect(subjects.length).toBe(1);
229
230             var authors = mkws.$("div.mkwsFacet[data-mkws-facet='author']");
231             expect(authors.length).toBe(1);
232         });
233
234         waitsFor(function () {
235             return mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm").length >= 2 ? true : false;
236         }, "At least one author link displayed", 4 * jasmine_config.second);
237
238         runs(function () {
239             expect(mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm").length).toBeGreaterThan(1);
240         });
241     });
242 });
243
244
245
246 describe("Check Author Facets", function () {
247     it("limit search to first author", function () {
248         if (mkws.config.disable_facet_authors_search) {
249             debug("Facets: ignore limit search for authors");
250             return;
251         }
252
253         var hits_all_targets = get_hit_counter();
254         var author_number = 2; // 2=first author
255         // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
256         // do not click on author names without a comma, e.g.: "Joe Barbara"
257         // because searching on such authors won't find anything.
258         runs(function () {
259             var terms = mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm a");
260             for (var i = 0; i < terms.length; i++) {
261                 var term = mkws.$(terms[i]).text();
262                 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
263                     debug("ignore author facet: " + term);
264                     author_number++;
265                 } else {
266                     break;
267                 }
268             }
269             if (mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text().length == 0) {
270                 debug("No good authors found. Not clicking on the bad ones");
271                 return;
272             }
273
274             debug("Clicking on author (" + author_number + ") " + mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text());
275             mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").trigger("click");
276         });
277
278         waitsFor(function () {
279             var hits_single_target = get_hit_counter();
280             console.log("hits_single_target='" + hits_single_target + "' cf. hits_all_targets='" + hits_all_targets + "'");
281             return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
282         }, "Limited author search for less than " + hits_all_targets + " hits", 4.5 * jasmine_config.second);
283
284         runs(function () {
285             var hits_single_target = get_hit_counter();
286             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
287         });
288     });
289 });
290
291 describe("Check active clients author", function () {
292     it("check for active clients after limited author search", function () {
293         waitsFor(function () {
294             var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
295             // debug("clients: " + clients.text());
296             return clients.length == 1 && clients.text().match("/[1-9]+[0-9]*$");
297         }, "wait for Active clients: x/y", 5.5 * jasmine_config.second);
298
299         runs(function () {
300             var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
301             debug("span.mkwsClientCount: " + clients.text());
302             expect(clients.text()).toMatch("/[1-9]+[0-9]*$");
303
304             // exact match of active clients (e.g. a SP misconfiguration)
305             if (jasmine_config.active_clients) {
306                 debug("check for " + jasmine_config.active_clients + " active connections");
307                 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
308             }
309         });
310     });
311 });
312
313 describe("Check Source Facets", function () {
314     it("limit search to first source", function () {
315         var hits_all_targets = get_hit_counter();
316         var source_number = 2; // 2=first source
317         // wait for a stat response
318         var waitcount = 0;
319         // do not click on wikipedia link - no author or subject facets possible
320         var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm a";
321
322         // wait for a visible source link in facets
323         waitsFor(function () {
324             var terms = mkws.$(link);
325             return terms && terms.length > 0;
326         }, "wait for source facets after author search", 5 * jasmine_config.second);
327
328
329         runs(function () {
330             var terms = mkws.$(link);
331             for (var i = 0; i < terms.length; i++) {
332                 var term = mkws.$(terms[i]).text();
333                 debug("check for good source: " + term);
334
335                 if (term.match(/wikipedia/i)) {
336                     debug("ignore source facet: " + term);
337                     source_number++;
338                 } else {
339                     break;
340                 }
341             }
342             debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
343
344             if (mkws.$("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").text().length == 0) {
345                 debug("No good source found. Not clicking on the bad ones");
346                 return;
347             }
348
349             debug("click on source link nth-child(): " + source_number);
350             mkws.$("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").trigger("click");
351
352             mkws.$(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
353                 waitcount++;
354                 debug("DOM wait for stat: " + waitcount);
355             });
356         });
357
358         waitsFor(function () {
359             if (mkws.$("div.mkwsNavi").length && mkws.$("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
360                 return true;
361             } else {
362                 return false;
363             }
364         }, "Search for source in navi bar", 4 * jasmine_config.second);
365
366         // Note: it may happens that limited source search returns the same number of hits
367         // as before. Thats not really an error, but unfortunate
368         waitsFor(function () {
369             var hits_single_target = get_hit_counter();
370
371             return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
372         }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
373
374         runs(function () {
375             var hits_single_target = get_hit_counter();
376             debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
377             expect(hits_all_targets).not.toBeLessThan(hits_single_target);
378             jasmine_status.source_click = 1;
379
380             mkws.$(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
381         });
382     });
383 });
384
385
386 describe("Check record list", function () {
387     it("check for single active client", function () {
388         if (!jasmine_status.source_click) {
389             debug("skip clients check due missing source click");
390             return;
391         }
392
393         waitsFor(function () {
394             var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
395             //debug("clients: " + clients.text());
396             return clients.length == 1 && clients.text().match("/1$");
397         }, "wait for Active clients: x/1", 5 * jasmine_config.second);
398
399         runs(function () {
400             var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
401             debug("span.mkwsClientCount: " + clients.text());
402             expect(clients.text()).toMatch("/1$");
403         });
404     });
405
406     it("got a record", function () {
407         var linkaddr = "div.mkwsRecords div.mkwsSummary:nth-child(1) a";
408
409         waitsFor(function () {
410             // remove + insert node: must be at least 2
411             return mkws.$(linkaddr).length > 0;
412         }, "wait until we see a new record", 2.5 * jasmine_config.second);
413
414         runs(function () {
415             expect(mkws.$(linkaddr).length).toBeGreaterThan(0);
416         });
417     });
418 });
419
420 describe("Show record", function () {
421     var record_number = 1; // the Nth record in hit list
422     it("show record author", function () {
423         var click = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") a").trigger("click");
424         debug("show record click is success: " + click.length);
425         expect(click.length).toBe(1);
426
427         // wait until the record pops up
428         waitsFor(function () {
429             var show = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") > div.mkwsDetails");
430             //debug("poprecord: " + (show ? show.length : -1) + " " + mkws.$("div.mkwsRecords div.mkwsSummary").text());
431             return show != null && show.length ? true : false;
432         }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
433
434         runs(function () {
435             debug("show record pop up");
436             expect(mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div")).not.toBe(null);
437         });
438     });
439
440     it("extract URL", function () {
441         if (jasmine_config.show_record_url == false) {
442             debug("ignore test for URL in record")
443             return;
444         }
445
446         var urls = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div table tbody tr td a");
447         debug("number of extracted URL from record: " + urls.length);
448         // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
449         for (var i = 0; i < urls.length; i++) {
450             var url = mkws.$(urls[i]);
451             debug("URL: " + url.attr('href') + " text: " + url.text());
452
453             expect(url.attr('href')).not.toBe(null);
454             expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
455             expect(url.text()).not.toBe("");
456         }
457     });
458 });
459
460 describe("Check switch menu Records/Targets", function () {
461     it("check mkwsSwitch", function () {
462         expect(mkws.$("div.mkwsSwitch").length).toBe(1);
463
464         // expect 2 clickable links
465         expect(mkws.$("div.mkwsSwitch > a").length).toBe(2);
466     });
467
468     it("switch to target view", function () {
469         mkws.$("div.mkwsSwitch > a").eq(1).trigger("click");
470
471         // now the target table must be visible
472         expect(mkws.$("div.mkwsTargets").is(":visible")).toBe(true);
473         expect(mkws.$("div.mkwsRecords").is(":visible")).toBe(false);
474
475         // wait a half second, to show the target view
476         var time = (new Date).getTime();
477         waitsFor(function () {
478             return (new Date).getTime() - time > 700 ? true : false;
479         }, "wait some miliseconds", 1 * jasmine_config.second);
480
481         // look for table header
482         runs(function () {
483             expect(mkws.$("div.mkwsTargets").html()).toMatch(/Target ID/);
484         });
485     });
486
487     it("switch back to record view", function () {
488         mkws.$("div.mkwsSwitch > a").eq(0).trigger("click");
489
490         // now the target table must be visible
491         expect(mkws.$("div.mkwsTargets").is(":visible")).toBe(false);
492         expect(mkws.$("div.mkwsRecords").is(":visible")).toBe(true);
493     });
494 });
495
496 describe("Check status client counter", function () {
497     function get_time() {
498         var date = new Date();
499         return date.getTime();
500     }
501     var time = get_time();
502
503     it("check status clients", function () {
504         if (!jasmine_status.source_click) {
505             debug("skip clients check due missing source click");
506             return;
507         }
508
509         waitsFor(function () {
510             var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
511             debug("clients: " + clients.text());
512             if (clients.length == 1 && clients.text().match("0/1$")) {
513                 return true;
514             } else {
515                 return false;
516             }
517         }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
518
519         runs(function () {
520             var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
521             debug("span.mkwsClientCount: " + clients.text());
522             expect(clients.text()).toMatch("0/1$");
523         });
524     });
525 });
526
527 /* remove the "source" and "author" facet link to get more records again */
528 describe("Check removable facets links", function () {
529     var $ = mkws.$;
530
531     it("remove links for source and author", function () {
532         var waitcount = 0;
533
534         runs(function () {
535             var click = $("a.mkwsRemovable").eq(0).trigger("click");
536             debug("Removed first facets link: " + click.length);
537             expect(click.length).toBe(1);
538         });
539
540         runs(function () {
541             $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
542                 waitcount++;
543                 debug("DOM change for removeable: " + waitcount);
544             });
545         });
546
547         waitsFor(function () {
548             return $("a.mkwsRemovable").length == 1 ? 1 : 0;
549         });
550
551         runs(function () {
552             var click = $("a.mkwsRemovable").eq(0).trigger("click");
553             debug("Removed second facets link: " + click.length);
554             expect(click.length).toBe(1);
555         });
556
557         waitsFor(function () {
558             // debug("wait for: " + waitcount);
559             return waitcount >= 2 ? true : false;
560         }, "Records DOM change, by per page", 2 * jasmine_config.second);
561
562
563         runs(function () {
564             debug("unbind removable");
565             $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
566         });
567     });
568 });
569
570
571 describe("Check per page options", function () {
572     var $ = mkws.$;
573
574     it("show per page", function () {
575         var waitcount = 0;
576         var per_page_number = 20;
577
578         runs(function () {
579             var select = $("select.mkwsPerpage option[selected='selected']");
580             debug("per page default is: " + select.text() + " and unselect it");
581             select.removeAttr('selected');
582
583             select = $("select.mkwsPerpage option[value='" + per_page_number + "']").attr('selected', true);
584             debug("per page is set to: " + select.text());
585             select.trigger("change");
586
587             $("div.mkwsRecords").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
588                 waitcount++;
589                 // debug("DOM wait for change, per page: " + waitcount);
590             });
591         });
592
593         waitsFor(function () {
594             debug("wait for: " + waitcount);
595             return waitcount >= 6 ? true : false;
596         }, "Records DOM change, by per page", 3 * jasmine_config.second);
597
598         runs(function () {
599             $("div.mkwsRecords").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
600             debug("unbind per page");
601         });
602
603         runs(function () {
604             var records = $("div.mkwsRecords > div.mkwsSummary");
605             debug("Got now " + records.length + " records");
606             expect(records.length).toBe(per_page_number);
607         });
608     });
609 });
610
611 describe("Check SortBy options", function () {
612     var $ = mkws.$;
613
614     it("show per page", function () {
615         var waitcount = 0;
616         var sort_value = 'title:1';
617         var per_page_number = 20;
618         var title_list_old = title_list("xxx ");
619
620         function title_list(prefix) {
621             var list = [];
622             var terms = $("div.mkwsRecords > div.mkwsSummary > a");
623             for (var i = 0; i < terms.length; i++) {
624                 var term = $(terms[i]).text();
625                 list.push(term);
626                 // debug(prefix + "title: " + term);
627             }
628             return list;
629         }
630
631         runs(function () {
632             $("div.mkwsRecords").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
633                 waitcount++;
634                 //debug("DOM wait for change, sort by: " + waitcount);
635             });
636
637             var select = $("select.mkwsSort option[selected='selected']");
638             debug("Sort by default is: " + select.text() + " and unselect it");
639             select.removeAttr('selected');
640
641             select = $("select.mkwsSort option[value='" + sort_value + "']").attr('selected', true);
642             debug("srot by is set to: " + select.text());
643             select.trigger("change");
644         });
645
646         waitsFor(function () {
647             debug("wait for2: " + waitcount);
648             return waitcount >= 6 ? true : false;
649         }, "Records DOM change, by sort page", 3 * jasmine_config.second);
650
651         runs(function () {
652             $("div.mkwsRecords").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
653             debug("unbind per page");
654         });
655
656         runs(function () {
657             var records = $("div.mkwsRecords > div.mkwsSummary a");
658             debug("Got now " + records.length + " records");
659             expect(records.length).toBe(per_page_number);
660         });
661
662         runs(function () {
663             var title_list_new = title_list("yyy ");
664             var diff_flag = 0;
665             for (var i = 0; i < title_list_old.length; i++) {
666                 debug(title_list_old[i] + " :: " + title_list_new[i]);
667
668                 if (title_list_old[i] != title_list_new[i]) {
669                     diff_flag++;
670                 }
671             }
672             debug("Title changes: " + diff_flag + " out of " + per_page_number);
673             expect(diff_flag).not.toBe(0);
674         });
675     });
676 });
677
678
679 /* done */
680 describe("All tests are done", function () {
681     it(">>> hooray <<<", function () {
682         mkws.jasmine_done = true;
683     });
684 });