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