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