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