41ad086b8b9dbc4db8deddfcab01248576ab7fd5
[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.debug(text)
10     }
11
12     // Define empty mkws_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 pazpar2 search", function () {
79     it("pazpar2 was successfully initialized", function () {
80         expect(mkws_config.error).toBe(undefined);
81     });
82
83     it("validate HTML id's", function () {
84         expect($("input.mkwsQuery").length).toBe(1);
85         expect($("input.mkwsButton").length).toBe(1);
86
87         expect($(".mkwsNext").length).not.toBe(1);
88         expect($(".mkwsPrev").length).not.toBe(1);
89     });
90
91     it("run search query", function () {
92         var search_query = jasmine_config.search_query; // short hit counter with some paging
93         $("input.mkwsQuery").val(search_query);
94         debug("set search query: " + search_query)
95         expect($("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
96
97         if (mkws_config.use_service_proxy) {
98             // wait for service proxy auth
99             waitsFor(function () {
100                 return mkws.authenticated;
101             }, "SP auth done", 10 * jasmine_config.second);
102         } else {
103             debug("running raw pp2, don't wait for mkws auth");
104         }
105
106         runs(function () {
107             debug("Click on submit button");
108             var click = $("input.mkwsButton").trigger("click");
109             expect(click.length).toBe(1);
110         })
111     });
112 });
113
114
115 /*
116  * This part runs in background. It should be rewritten with
117  * async jasmine functions
118  *
119  */
120 describe("Check pazpar2 navigation", function () {
121     // Asynchronous part
122     it("check running search next/prev", function () {
123         expect($(".mkwsPager").length).toBe(1);
124
125         function my_click(id, time) {
126             setTimeout(function () {
127                 debug("trigger click on id: " + id);
128                 var click = $(id).trigger("click");
129
130                 debug("next/prev: " + id + " click is success: " + click.length);
131                 expect(click.length).toBe(1);
132             }, time * jasmine_config.second);
133         }
134
135         waitsFor(function () {
136             return $("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
137         }, "Expect next link 2", 10 * jasmine_config.second);
138
139         runs(function () {
140             // click next/prev after N seconds
141             my_click(".mkwsNext", 0);
142         });
143
144         waitsFor(function () {
145             return $("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
146         }, "Expect next link 3", 5 * jasmine_config.second);
147
148         runs(function () {
149             // click next/prev after N seconds
150             my_click(".mkwsNext", 0);
151             my_click(".mkwsPrev", 0.2);
152         });
153     });
154 });
155
156 describe("Check pazpar2 hit counter", function () {
157     it("check running search hit counter", function () {
158         var max_time = jasmine_config.max_time; // in seconds
159         var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
160         var hits = 0;
161
162         waitsFor(function () {
163             hits = get_hit_counter();
164
165             return hits > expected_hits;
166         }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
167
168
169         runs(function () {
170             debug("mkws pager found records: '" + hits + "'");
171             expect($(".mkwsPager").length).toBe(1);
172             expect(hits).toBeGreaterThan(expected_hits);
173         });
174     });
175 });
176
177 describe("Check Termlist", function () {
178     it("found Termlist", function () {
179         var termlist = $("div.mkwsTermlists");
180         debug("Termlist success: " + termlist.length);
181         expect(termlist.length).toBe(1);
182
183         waitsFor(function () {
184             return $("div.mkwsFacetSources").length == 1 ? true : false;
185         }, "check for facet sources", 4 * jasmine_config.second);
186
187
188         // everything displayed?
189         runs(function () {
190             var sources = $("div.mkwsFacetSources");
191             debug("Termlist sources success: " + sources.length);
192             expect(sources.length).toBe(1);
193
194             var subjects = $("div.mkwsFacetSubjects");
195             expect(subjects.length).toBe(1);
196
197             var authors = $("div.mkwsFacetAuthors");
198             expect(authors.length).toBe(1);
199         });
200
201         waitsFor(function () {
202             return $("div.mkwsFacetAuthors div.term").length >= 2 ? true : false;
203         }, "At least one author link displayed", 4 * jasmine_config.second);
204
205         runs(function () {
206             expect($("div.mkwsFacetAuthors div.term").length).toBeGreaterThan(1);
207         });
208     });
209
210     it("limit search to first author", function () {
211         if (mkws_config.disable_facet_authors_search) {
212             debug("Facets: ignore limit search for authors");
213             return;
214         }
215
216         var hits_all_targets = get_hit_counter();
217         var author_number = 2; // 2=first author
218         // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
219         // do not click on author names without a comma, e.g.: "Joe Barbara"
220         var terms = $("div.mkwsFacetAuthors div.term a");
221         for (var i = 0; i < terms.length; i++) {
222             var term = $(terms[i]).text();
223             if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
224                 debug("ignore author facet: " + term);
225                 author_number++;
226             } else {
227                 break;
228             }
229         }
230
231         var click = $("div.mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").trigger("click");
232         debug("limit author click is success: " + click.length);
233         expect(click.length).toBe(1);
234
235         waitsFor(function () {
236             return get_hit_counter() < hits_all_targets ? true : false;
237         }, "Limited author search for less than " + hits_all_targets + " hits", 6 * jasmine_config.second);
238
239         runs(function () {
240             var hits_single_target = get_hit_counter();
241             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
242             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
243         });
244     });
245
246     it("limit search to first source", function () {
247         var hits_all_targets = get_hit_counter();
248         var source_number = 2; // 2=first source
249         // do not click on wikipedia link - no author or subject facets possible
250         var terms = $("div.mkwsFacetSources div.term a");
251         for (var i = 0; i < terms.length; i++) {
252             var term = $(terms[i]).text();
253             if (term.match(/wikipedia/i)) {
254                 debug("ignore source facet: " + term);
255                 source_number++;
256             } else {
257                 break;
258             }
259         }
260
261         var click = $("div.mkwsFacetSources div.term:nth-child(" + source_number + ") a").trigger("click");
262         debug("limit source click " + (source_number - 1) + " is success: " + click.length);
263         expect(click.length).toBe(1);
264
265         waitsFor(function () {
266             if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
267                 return true;
268             } else {
269                 return false;
270             }
271         }, "Search for source in navi bar", 4 * jasmine_config.second);
272
273         // Note: it may happens that limited source search returns the same number of hits
274         // as before. Thats not really an error, but unfortunate
275         waitsFor(function () {
276             return get_hit_counter() <= hits_all_targets ? true : false;
277         }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
278
279         runs(function () {
280             var hits_single_target = get_hit_counter();
281             debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
282             expect(hits_all_targets).not.toBeLessThan(hits_single_target);
283         });
284     });
285 });
286
287 describe("Show record", function () {
288     var record_number = 1; // the Nth record in hit list
289     it("show record author", function () {
290         var click = $("div.mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
291         debug("show record click is success: " + click.length);
292         expect(click.length).toBe(1);
293
294         // wait until the record pops up
295         waitsFor(function () {
296             var show = $("div.mkwsRecords div.record:nth-child(" + record_number + ") div");
297             return show != null && show.length ? true : false;
298         }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
299
300         runs(function () {
301             debug("show record pop up");
302             expect($("div.mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
303         });
304     });
305
306     it("extract URL", function () {
307         if (jasmine_config.show_record_url == false) {
308             debug("ignore test for URL in record")
309             return;
310         }
311
312         var urls = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
313         debug("number of extracted URL from record: " + urls.length);
314         for (var i = 0; i < urls.length; i++) {
315             var url = $(urls[i]);
316             debug("URL: " + url.attr('href'));
317             expect(url.attr('href')).not.toBe(null);
318             expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9]+\.[0-9a-z].*\//i);
319             expect(url.attr('href')).toBe(url.text());
320         }
321     });
322 });
323
324 describe("Check switch menu Records/Targets", function () {
325     it("check mkwsSwitch", function () {
326         expect($("div.mkwsSwitch").length).toBe(1);
327
328         // expect 2 clickable links
329         expect($("div.mkwsSwitch a").length).toBe(2);
330     });
331
332     it("switch to target view", function () {
333         var click = $("div.mkwsSwitch").children('a').eq(1).trigger("click");
334         debug("target view click is success: " + click.length);
335         expect(click.length).toBe(1);
336
337         // now the target table must be visible
338         expect($("div.mkwsBytarget").is(":visible")).toBe(true);
339         expect($("div.mkwsRecords").is(":visible")).toBe(false);
340
341         // wait a half second, to show the target view
342         var time = (new Date).getTime();
343         waitsFor(function () {
344             return (new Date).getTime() - time > 700 ? true : false;
345         }, "wait some miliseconds", 1 * jasmine_config.second);
346
347         // look for table header
348         runs(function () {
349             expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
350         });
351     });
352
353     it("switch back to record view", function () {
354         var click = $("div.mkwsSwitch").children('a').eq(0).trigger("click");
355         debug("record view click is success: " + click.length);
356         expect(click.length).toBe(1);
357
358         // now the target table must be visible
359         expect($("div.mkwsBytarget").is(":visible")).toBe(false);
360         expect($("div.mkwsRecords").is(":visible")).toBe(true);
361     });
362 });
363
364 describe("Check status client counter", function () {
365     function get_time() {
366         var date = new Date();
367         return date.getTime();
368     }
369     var time = get_time();
370
371     it("check status clients", function () {
372         waitsFor(function () {
373             var clients = $("div#mkwsStat span.clients");
374             if (clients.length == 1 && clients.text().match("0/1$")) {
375                 return true;
376             } else {
377                 return false;
378             }
379
380         }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
381
382 /*
383         runs(function () {
384             var clients = $("div#mkwsStat span.clients");
385             debug("span.clients: " + clients.text());
386             expect(clients.text()).toEqual("0/1");
387         });
388         */
389
390     });
391
392 });
393
394 /* done */
395 describe("All tests are done", function () {
396     it(">>> hooray <<<", function () {
397         mkws.jasmine_done = true;
398     });
399 });