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