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