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