event based check if the prev/next links are working
[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
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 == 1).toBe(true);
55         })
56     });
57 });
58
59
60 describe("Check pazpar2 navigation", function () {
61     // Asynchronous part
62     it("check running search next/prev", function () {
63         expect($("#mkwsPager").length == 1).toBe(true);
64
65         function my_click(id, time) {
66             setTimeout(function () {
67                 debug("trigger click on id: " + id);
68                 var click = $(id).trigger("click");
69
70                 debug("next click is success: " + click.length);
71                 expect(click.length == 1).toBe(true);
72             }, time * 1000);
73         }
74
75         waitsFor(function () {
76             return $("div#mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
77         }, "Expect next link 2", 5 * 1000);
78
79         runs(function () {
80             // click next/prev after N seconds
81             my_click("#mkwsNext", 0);
82         });
83
84         waitsFor(function () {
85             return $("div#mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
86         }, "Expect next link 3", 5 * 1000);
87
88         runs(function () {
89             // click next/prev after N seconds
90             my_click("#mkwsNext", 0);
91             my_click("#mkwsPrev", 0.2);
92         });
93     });
94 });
95
96 describe("Check pazpar2 hit counter", function () {
97     it("check running search hit counter", function () {
98         var max_time = 10; // in seconds
99         var expected_hits = 116; // at least expected hit counter
100
101         var hits = 0;
102
103         waitsFor(function () {
104             hits = get_hit_counter();
105
106             return hits >= expected_hits;
107         }, "Expect N hits in x seconds", max_time * 1000);
108
109
110         runs(function () {
111             debug("mkws pager found records: '" + hits + "'");
112             expect($("#mkwsPager").length == 1).toBe(true);
113             expect(hits).toBeGreaterThan(expected_hits);
114         });
115     });
116 });
117
118 describe("Check Termlist", function () {
119     function show_record() {
120         var click = $("div#mkwsRecords div.record:nth-child(3) :nth-child(2)").trigger("click");
121         debug("show click is success: " + click.length);
122         expect(click.length == 1).toBe(true);
123     }
124
125     // show_record();
126     it("found Termlist", function () {
127         var termlist = $("div#mkwsTermlists");
128         debug("Termlist success: " + termlist.length);
129         expect(termlist.length == 1).toBe(true);
130
131         var sources = $("div#mkwsFacetSources");
132         expect(sources.length == 1).toBe(true);
133
134         var subjects = $("div#mkwsFacetSubjects");
135         expect(subjects.length == 1).toBe(true);
136
137         var authors = $("div#mkwsFacetAuthors");
138         expect(authors.length == 1).toBe(true);
139     });
140
141     it("limit search to first source", function () {
142         var hits_all_targets = get_hit_counter();
143
144         var click = $("div#mkwsFacetSources div.term:nth-child(2) a").trigger("click");
145         debug("limit source click is success: " + click.length);
146         expect(click.length == 1).toBe(true);
147
148         waitsFor(function () {
149             if ($("div#mkwsNavi").length && $("div#mkwsNavi").text().match(/^Source/)) {
150                 return true;
151             } else {
152                 return false;
153             }
154         }, "Search for source in navi bar", 1000);
155
156         waitsFor(function () {
157             return get_hit_counter() < hits_all_targets ? true : false;
158         }, "Search for with less hits", 9 * 1000);
159
160         runs(function () {
161             var hits_single_target = get_hit_counter();
162             debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
163             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
164         });
165     });
166
167     it("limit search to first author", function () {
168         var hits_all_targets = get_hit_counter();
169
170         var click = $("div#mkwsFacetAuthors div.term:nth-child(2) a").trigger("click");
171         debug("limit author click is success: " + click.length);
172         expect(click.length == 1).toBe(true);
173
174         waitsFor(function () {
175             return get_hit_counter() < hits_all_targets ? true : false;
176         }, "Search for with less hits", 9 * 1000);
177
178         runs(function () {
179             var hits_single_target = get_hit_counter();
180             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
181             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
182         });
183     });
184
185     it("show record author", function () {
186         show_record();
187     });
188 });
189
190 describe("Check status client counter", function () {
191     function get_time() {
192         var date = new Date();
193         return date.getTime();
194     }
195     var time = get_time();
196
197     it("check status clients", function () {
198
199         waitsFor(function () {
200             var clients = $("div#mkwsStat span.clients");
201             if (clients.length == 1 && clients.text() == "0/1") {
202                 return true;
203             } else {
204                 return false;
205             }
206
207         }, "wait for status", 4 * 1000);
208
209     });
210     runs(function () {
211         var clients = $("div#mkwsStat span.clients");
212         debug("span.clients: " + clients.text());
213         expect(clients.text()).toEqual("0/1");
214     });
215
216 });
217
218 //describe("Check switch menu Records/Targets", function () {
219 //    var div = $("div#mkwsSwitch");
220 ////    if (div.length == 0) {
221 ////    debug("mkwsSwitch does not exists, skip test");
222 ////    return;
223 ////    }
224 //
225 //
226 //    it("check mkwsSwitch", function () {
227 //      expect($("div#mkwsSwitch").length).toBe(1);
228 //
229 //      // expect 2 clickable links
230 //      expect( $("div#mkwsSwitch a").length).toBe(2);
231 //    });
232 //
233 //    it("switch to target view", function () {
234 //      var click = $("a#mkwsSwitch_targets").trigger("click");
235 //      debug("target click is success: " + click.length);
236 //      expect(click.length == 1).toBe(true);
237 //
238 //      // now the target table must be visible
239 //      /*
240 //      expect($("div#mkwsBytarget").is(":visible")).toBe(true);
241 //      */
242 //      expect($("div#mkwsResults").is(":visible")).toBe(true);
243 //
244 //      // look for table header
245 //      expect($("div#mkwsBytarget").html()).toMatch(/Target ID/);
246 //    });
247 //
248 //    /*
249 //    it("switch back to record view", function () {
250 //      var click = $("div#mkwsSwitch a:nth-child(0)").trigger("click");
251 //      debug("record click is success: " + click.length);
252 //      expect(click.length == 1).toBe(true);
253 //
254 //      // now the target table must be in-visible again
255 //      var bytarget = $("div#mkwsBytarget:visible");
256 //      var byrecords = $("div#mkwsResults:visible");
257 //      expect(bytarget.length == 1).toBe(false);
258 //      expect(byrecords.length == 1).toBe(true);
259 //    });
260 //    */
261 //});
262
263 /* dummy EOF */
264 describe("All tests are done", function () {
265     it("xxx >>> hooray!!! <<<", function () {
266     });
267 });