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