reorder test, status check last
[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", 5 * 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 = 10; // in seconds
98         var expected_hits = 116; // 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 N hits in x seconds", 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     function show_record() {
118         var click = $("div#mkwsRecords div.record:nth-child(3) :nth-child(2)").trigger("click");
119         debug("show click is success: " + click.length);
120         expect(click.length == 1).toBe(true);
121     }
122
123     // show_record();
124     it("found Termlist", function () {
125         var termlist = $("div#mkwsTermlists");
126         debug("Termlist success: " + termlist.length);
127         expect(termlist.length == 1).toBe(true);
128
129         var sources = $("div#mkwsFacetSources");
130         expect(sources.length == 1).toBe(true);
131
132         var subjects = $("div#mkwsFacetSubjects");
133         expect(subjects.length == 1).toBe(true);
134
135         var authors = $("div#mkwsFacetAuthors");
136         expect(authors.length == 1).toBe(true);
137     });
138
139     it("limit search to first source", function () {
140         var hits_all_targets = get_hit_counter();
141
142         var click = $("div#mkwsFacetSources div.term:nth-child(2) a").trigger("click");
143         debug("limit source click is success: " + click.length);
144         expect(click.length == 1).toBe(true);
145
146         waitsFor(function () {
147             if ($("div#mkwsNavi").length && $("div#mkwsNavi").text().match(/^Source/)) {
148                 return true;
149             } else {
150                 return false;
151             }
152         }, "Search for source in navi bar", 1000);
153
154         waitsFor(function () {
155             return get_hit_counter() < hits_all_targets ? true : false;
156         }, "Search for with less hits", 9 * 1000);
157
158         runs(function () {
159             var hits_single_target = get_hit_counter();
160             debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
161             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
162         });
163     });
164
165     it("limit search to first author", function () {
166         var hits_all_targets = get_hit_counter();
167
168         var click = $("div#mkwsFacetAuthors div.term:nth-child(2) a").trigger("click");
169         debug("limit author click is success: " + click.length);
170         expect(click.length == 1).toBe(true);
171
172         waitsFor(function () {
173             return get_hit_counter() < hits_all_targets ? true : false;
174         }, "Search for with less hits", 9 * 1000);
175
176         runs(function () {
177             var hits_single_target = get_hit_counter();
178             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
179             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
180         });
181     });
182
183     it("show record author", function () {
184         show_record();
185     });
186 });
187
188 describe("Check switch menu Records/Targets", function () {
189     it("check mkwsSwitch", function () {
190         expect($("div#mkwsSwitch").length).toBe(1);
191
192         // expect 2 clickable links
193         expect($("div#mkwsSwitch a").length).toBe(2);
194     });
195
196     it("switch to target view", function () {
197         var click = $("a#mkwsSwitch_targets").trigger("click");
198         debug("target click is success: " + click.length);
199         expect(click.length == 1).toBe(true);
200
201         // now the target table must be visible
202         expect($("div#mkwsBytarget").is(":visible")).toBe(true);
203         expect($("div#mkwsResults").is(":visible")).toBe(false);
204
205         // wait a half second, to show the target view
206         var time = (new Date).getTime();
207         waitsFor(function () {
208             return (new Date).getTime() - time > 700 ? true : false;
209         }, "wait some miliseconds", 1 * 1000);
210         // look for table header
211         runs(function () {
212             expect($("div#mkwsBytarget").html()).toMatch(/Target ID/);
213         });
214     });
215
216     it("switch back to record view", function () {
217         var click = $("a#mkwsSwitch_records").trigger("click");
218         debug("record click is success: " + click.length);
219         expect(click.length == 1).toBe(true);
220
221         // now the target table must be visible
222         expect($("div#mkwsBytarget").is(":visible")).toBe(false);
223         expect($("div#mkwsResults").is(":visible")).toBe(true);
224     });
225 });
226
227 describe("Check status client counter", function () {
228     function get_time() {
229         var date = new Date();
230         return date.getTime();
231     }
232     var time = get_time();
233
234     it("check status clients", function () {
235
236         waitsFor(function () {
237             var clients = $("div#mkwsStat span.clients");
238             if (clients.length == 1 && clients.text() == "0/1") {
239                 return true;
240             } else {
241                 return false;
242             }
243
244         }, "wait for status", 4 * 1000);
245
246     });
247     runs(function () {
248         var clients = $("div#mkwsStat span.clients");
249         debug("span.clients: " + clients.text());
250         expect(clients.text()).toEqual("0/1");
251     });
252
253 });
254
255 /* dummy EOF */
256 describe("All tests are done", function () {
257     it(">>> hooray <<<", function () {});
258 });