Fix jQuery selectors for new, cleaner representation of facets.
[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 /* check config for jasmine test
18  *
19  * you can override the default values in the config
20  * object: jasmine_config = {};
21  *
22  */
23 function init_jasmine_config() {
24
25     var jasmine_config_default = {
26         search_query: "freebsd",
27         max_time: 16,
28         // in seconds
29         expected_hits: 80,
30         // at least expected hit counter
31         second: 1000,
32         // miliseconds to seconds
33         show_record_url: true,
34         // check for valid URL in records
35         dummy: false
36     };
37
38     // use default values for undefined values
39     for (var key in jasmine_config_default) {
40         if (!jasmine_config.hasOwnProperty(key)) {
41             jasmine_config[key] = jasmine_config_default[key];
42         }
43         debug("jasmine config: " + key + " => " + jasmine_config[key]);
44     }
45
46     mkws.jasmine_done = false;
47 }
48
49 var get_hit_counter = function () {
50         // not yet here
51         if ($(".mkwsPager").length == 0) return -1;
52
53         var found = $(".mkwsPager").text();
54         var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
55         re.exec(found);
56         var hits = -1;
57
58         if (RegExp.$1) {
59             hits = parseInt(RegExp.$1);
60             expect(hits).toBeGreaterThan(0);
61         }
62
63         //debug("Hits: " + hits);
64         return hits;
65     }
66
67 describe("Init jasmine config", function () {
68     it("jasmine was successfully initialized", function () {
69         init_jasmine_config();
70
71         expect(jasmine_config.search_query).toMatch(/\w/);
72         expect(jasmine_config.second).toBeGreaterThan(100);
73         expect(jasmine_config.max_time).toBeGreaterThan(1);
74         expect(jasmine_config.expected_hits).toBeGreaterThan(1);
75     });
76 });
77
78 describe("Check MOTD before search", function () {
79     // Check that the MOTD has been moved into its container, and
80     // is visible before the search.
81     // the mkwsMOTD div was originally inside a testMOTD div, which should
82     // now be empty
83     // Note that the testMOTD is a regular div, and uses #testMOTD,
84     // since the automagic class-making does not apply to it.
85     it("MOTD is hidden", function () {
86         expect($(".mkwsMOTD").length).toBe(1);
87         expect($("#testMOTD").length).toBe(1);
88         expect($("#testMOTD").text()).toMatch("^ *$");
89     });
90
91     it("mkwsMOTDContainer has received the text", function () {
92         expect($(".mkwsMOTDContainer").length).toBe(1);
93         expect($(".mkwsMOTDContainer").text()).toMatch(/MOTD/);
94     });
95 });
96
97 describe("Check pazpar2 search", function () {
98     it("pazpar2 was successfully initialized", function () {
99         expect(mkws.config.error).toBe(undefined);
100     });
101
102     it("validate HTML id's", function () {
103         expect($("input.mkwsQuery").length).toBe(1);
104         expect($("input.mkwsButton").length).toBe(1);
105
106         expect($(".mkwsNext").length).not.toBe(1);
107         expect($(".mkwsPrev").length).not.toBe(1);
108     });
109
110     it("run search query", function () {
111         var search_query = jasmine_config.search_query; // short hit counter with some paging
112         $("input.mkwsQuery").val(search_query);
113         debug("set search query: " + search_query)
114         expect($("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
115
116         if (mkws.config.use_service_proxy) {
117             // wait for service proxy auth
118             waitsFor(function () {
119                 return mkws.authenticated;
120             }, "SP auth done", 10 * jasmine_config.second);
121         } else {
122             debug("running raw pp2, don't wait for mkws auth");
123         }
124
125         runs(function () {
126             debug("Click on submit button");
127             $("input.mkwsButton").trigger("click");
128         })
129     });
130 });
131
132 describe("Check MOTD after search", function () {
133     it("MOTD is hidden", function () {
134         expect($(".mkwsMOTD").length).toBe(1);
135         expect($(".mkwsMOTD").is(":hidden")).toBe(true);
136         debug("motd t=" + $(".mkwsMOTD").text());
137         debug("motd v=" + $(".mkwsMOTD").is(":visible"));
138     });
139 });
140
141
142 /*
143  * This part runs in background. It should be rewritten with
144  * async jasmine functions
145  *
146  */
147 describe("Check pazpar2 navigation", function () {
148     // Asynchronous part
149     it("check running search next/prev", function () {
150         expect($(".mkwsPager").length).toBe(1);
151
152         function my_click(id, time) {
153             setTimeout(function () {
154                 debug("trigger click on id: " + id);
155                 $(id).trigger("click");
156             }, time * jasmine_config.second);
157         }
158
159         waitsFor(function () {
160             return $("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
161         }, "Expect next link 2", 10 * jasmine_config.second);
162
163         runs(function () {
164             // click next/prev after N seconds
165             my_click(".mkwsNext", 0);
166         });
167
168         waitsFor(function () {
169             return $("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
170         }, "Expect next link 3", 5 * jasmine_config.second);
171
172         runs(function () {
173             // click next/prev after N seconds
174             my_click(".mkwsNext", 0);
175             my_click(".mkwsPrev", 0.2);
176         });
177     });
178 });
179
180 describe("Check pazpar2 hit counter", function () {
181     it("check running search hit counter", function () {
182         var max_time = jasmine_config.max_time; // in seconds
183         var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
184         var hits = 0;
185
186         waitsFor(function () {
187             hits = get_hit_counter();
188             return hits > expected_hits;
189         }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
190
191         runs(function () {
192             debug("mkws pager found records: '" + hits + "'");
193             expect($(".mkwsPager").length).toBe(1);
194             expect(hits).toBeGreaterThan(expected_hits);
195         });
196     });
197 });
198
199 describe("Check Termlist", function () {
200     it("found Termlist", function () {
201         var termlist = $("div.mkwsTermlists");
202         debug("Termlist success: " + termlist.length);
203         expect(termlist.length).toBe(1);
204
205         waitsFor(function () {
206             return $('div.mkwsFacet[data-mkws-facet="xtargets"]').length == 1 ? true : false;
207         }, "check for facet sources", 4 * jasmine_config.second);
208
209         // everything displayed?
210         runs(function () {
211             var sources = $('div.mkwsFacet[data-mkws-facet="xtargets"]');
212             debug("Termlist sources success: " + sources.length);
213             expect(sources.length).toBe(1);
214
215             var subjects = $('div.mkwsFacet[data-mkws-facet="subject"]');
216             expect(subjects.length).toBe(1);
217
218             var authors = $('div.mkwsFacet[data-mkws-facet="author"]');
219             expect(authors.length).toBe(1);
220         });
221
222         waitsFor(function () {
223             return $('div.mkwsFacet[data-mkws-facet="author"] div.term').length >= 2 ? true : false;
224         }, "At least one author link displayed", 4 * jasmine_config.second);
225
226         runs(function () {
227             expect($('div.mkwsFacet[data-mkws-facet="author"] div.term').length).toBeGreaterThan(1);
228         });
229     });
230
231     it("limit search to first author", function () {
232         if (mkws.config.disable_facet_authors_search) {
233             debug("Facets: ignore limit search for authors");
234             return;
235         }
236
237         var hits_all_targets = get_hit_counter();
238         var author_number = 2; // 2=first author
239         // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
240         // do not click on author names without a comma, e.g.: "Joe Barbara"
241         // because searching on such authors won't find anything.
242         var terms = $("div.mkwsFacetAuthors div.term a");
243         for (var i = 0; i < terms.length; i++) {
244             var term = $(terms[i]).text();
245             if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
246                 debug("ignore author facet: " + term);
247                 author_number++;
248             } else {
249                 break;
250             }
251         }
252         if ($("div.mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").text().length == 0) {
253             debug("No good authors found. Not clicking on the bad ones");
254             return;
255         }
256
257         debug("Clicking on author (" + author_number + ") " + $("div.mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").text());
258         $("div.mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").trigger("click");
259
260         waitsFor(function () {
261             return get_hit_counter() < hits_all_targets ? true : false;
262         }, "Limited author search for less than " + hits_all_targets + " hits", 6 * jasmine_config.second);
263
264         runs(function () {
265             var hits_single_target = get_hit_counter();
266             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
267             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
268         });
269     });
270
271     it("limit search to first source", function () {
272         var hits_all_targets = get_hit_counter();
273         var source_number = 2; // 2=first source
274         // do not click on wikipedia link - no author or subject facets possible
275         var terms = $("div.mkwsFacetSources div.term a");
276         for (var i = 0; i < terms.length; i++) {
277             var term = $(terms[i]).text();
278             if (term.match(/wikipedia/i)) {
279                 debug("ignore source facet: " + term);
280                 source_number++;
281             } else {
282                 break;
283             }
284         }
285         if ($("div.mkwsFacetSources div.term:nth-child(" + source_number + ") a").text().length == 0) {
286             debug("No good source found. Not clicking on the bad ones");
287             return;
288         }
289
290         $("div.mkwsFacetSources div.term:nth-child(" + source_number + ") a").trigger("click");
291
292         // wait for a stat response
293         var waitcount = 0;
294         $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
295             waitcount++;
296             debug("DOM wait for stat: " + waitcount);
297         });
298
299         waitsFor(function () {
300             if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
301                 return true;
302             } else {
303                 return false;
304             }
305         }, "Search for source in navi bar", 4 * jasmine_config.second);
306
307         // Note: it may happens that limited source search returns the same number of hits
308         // as before. Thats not really an error, but unfortunate
309         waitsFor(function () {
310             return waitcount >= 2 && get_hit_counter() <= hits_all_targets ? true : false;
311         }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
312
313         runs(function () {
314             var hits_single_target = get_hit_counter();
315             debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
316             expect(hits_all_targets).not.toBeLessThan(hits_single_target);
317
318             $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
319         });
320     });
321 });
322
323
324 describe("Check record list", function () {
325     it("check for single active client", function () {
326         waitsFor(function () {
327             var clients = $("div#mkwsStat span.clients");
328             //debug("clients: " + clients.text());
329
330             return clients.length == 1 && clients.text().match("/1$");
331         }, "wait for Active clients: x/1", 5 * jasmine_config.second);
332
333         runs(function () {
334             var clients = $("div#mkwsStat span.clients");
335             debug("span.clients: " + clients.text());
336             expect(clients.text()).toMatch("/1$");
337         });
338     });
339
340     it("got a record", function () {
341         var linkaddr = "div.mkwsRecords div.record:nth-child(1) a";
342
343         waitsFor(function () {
344             // remove + insert node: must be at least 2
345             return $(linkaddr).length > 0;
346         }, "wait until we see a new record", 2.5 * jasmine_config.second);
347
348         runs(function () {
349             expect($(linkaddr).length).toBeGreaterThan(0);
350         });
351     });
352 });
353
354 describe("Show record", function () {
355     var record_number = 1; // the Nth record in hit list
356     it("show record author", function () {
357         var click = $("div.mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
358         debug("show record click is success: " + click.length);
359         expect(click.length).toBe(1);
360
361         // wait until the record pops up
362         waitsFor(function () {
363             var show = $("div.mkwsRecords div.record:nth-child(" + record_number + ") > div.details");
364             //debug("poprecord: " + (show ? show.length : -1) + " " + $("div.mkwsRecords div.record").text());
365
366             return show != null && show.length ? true : false;
367         }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
368
369         runs(function () {
370             debug("show record pop up");
371             expect($("div.mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
372         });
373     });
374
375     it("extract URL", function () {
376         if (jasmine_config.show_record_url == false) {
377             debug("ignore test for URL in record")
378             return;
379         }
380
381         var urls = $("div.mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
382         debug("number of extracted URL from record: " + urls.length);
383         // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
384         for (var i = 0; i < urls.length; i++) {
385             var url = $(urls[i]);
386             debug("URL: " + url.attr('href') + " text: " + url.text());
387
388             expect(url.attr('href')).not.toBe(null);
389             expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
390             expect(url.text()).not.toBe("");
391         }
392     });
393 });
394
395 describe("Check switch menu Records/Targets", function () {
396     it("check mkwsSwitch", function () {
397         expect($("div.mkwsSwitch").length).toBe(1);
398
399         // expect 2 clickable links
400         expect($("div.mkwsSwitch a").length).toBe(2);
401     });
402
403     it("switch to target view", function () {
404         $("div.mkwsSwitch").children('a').eq(1).trigger("click");
405
406         // now the target table must be visible
407         expect($("div.mkwsBytarget").is(":visible")).toBe(true);
408         expect($("div.mkwsRecords").is(":visible")).toBe(false);
409
410         // wait a half second, to show the target view
411         var time = (new Date).getTime();
412         waitsFor(function () {
413             return (new Date).getTime() - time > 700 ? true : false;
414         }, "wait some miliseconds", 1 * jasmine_config.second);
415
416         // look for table header
417         runs(function () {
418             expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
419         });
420     });
421
422     it("switch back to record view", function () {
423         $("div.mkwsSwitch").children('a').eq(0).trigger("click");
424
425         // now the target table must be visible
426         expect($("div.mkwsBytarget").is(":visible")).toBe(false);
427         expect($("div.mkwsRecords").is(":visible")).toBe(true);
428     });
429 });
430
431 describe("Check status client counter", function () {
432     function get_time() {
433         var date = new Date();
434         return date.getTime();
435     }
436     var time = get_time();
437
438     it("check status clients", function () {
439         waitsFor(function () {
440             var clients = $("div#mkwsStat span.clients");
441             debug("clients: " + clients.text());
442             if (clients.length == 1 && clients.text().match("0/1$")) {
443                 return true;
444             } else {
445                 return false;
446             }
447         }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
448
449         runs(function () {
450             var clients = $("div#mkwsStat span.clients");
451             debug("span.clients: " + clients.text());
452             expect(clients.text()).toMatch("0/1$");
453         });
454     });
455 });
456
457 /* done */
458 describe("All tests are done", function () {
459     it(">>> hooray <<<", function () {
460         mkws.jasmine_done = true;
461     });
462 });