submit search after SP auth was done
[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("Submit search");
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
73             }, time * 1000);
74         }
75
76         runs(function () {
77             // click next/prev after N seconds
78             my_click("#mkwsNext", 7);
79             my_click("#mkwsNext", 8);
80             my_click("#mkwsPrev", 9);
81         });
82     });
83 });
84
85
86 describe("Check pazpar2 hit counter", function () {
87
88     it("check running search hit counter", function () {
89         var max_time = 10; // in seconds
90         var expected_hits = 116; //
91         var j_time = 0;
92         var j_hits = 0;
93
94         function found(time, none) {
95             setTimeout(function () {
96                 j_time = time;
97                 hits = get_hit_counter();
98
99                 // debug("found: " + found);
100                 if (none) {
101                     expect(hits < 0).toBeTruthy();
102                 } else {
103                     j_hits = hits;
104                 }
105
106                 debug("mkws pager found records: '" + hits + "'");
107                 debug("time state: " + j_time);
108
109                 expect(time >= 0).toBeTruthy();
110             }, time * 1000);
111         }
112
113         runs(function () {
114             // check hit counter after N seconds
115             found(0, true);
116             found(3);
117             found(6);
118             found(8);
119             found(max_time);
120         });
121
122         waitsFor(function () {
123             return j_time == max_time ? true : false;
124         }, "The Value should be 20 seconds", max_time * 1000);
125
126
127         runs(function () {
128             expect($("#mkwsPager").length == 1).toBe(true);
129         })
130
131         runs(function () {
132             expect(j_time <= max_time).toBeTruthy();
133             expect(j_hits).toBeGreaterThan(expected_hits);
134         });
135     });
136 });
137
138 describe("Check Termlist", function () {
139     function show_record() {
140         var click = $("div#mkwsRecords div.record:nth-child(3) :nth-child(2)").trigger("click");
141         debug("show click is success: " + click.length);
142         expect(click.length == 1).toBe(true);
143     }
144
145     // show_record();
146     it("found Termlist", function () {
147         var termlist = $("div#mkwsTermlists");
148         debug("Termlist success: " + termlist.length);
149         expect(termlist.length == 1).toBe(true);
150
151         var sources = $("div#mkwsFacetSources");
152         expect(sources.length == 1).toBe(true);
153
154         var subjects = $("div#mkwsFacetSubjects");
155         expect(subjects.length == 1).toBe(true);
156
157         var authors = $("div#mkwsFacetAuthors");
158         expect(authors.length == 1).toBe(true);
159     });
160
161     it("limit search to first source", function () {
162         var hits_all_targets = get_hit_counter();
163
164         var click = $("div#mkwsFacetSources div.term:nth-child(2) a").trigger("click");
165         debug("limit source click is success: " + click.length);
166         expect(click.length == 1).toBe(true);
167
168         waitsFor(function () {
169             if ($("div#mkwsNavi").length && $("div#mkwsNavi").text().match(/^Source/)) {
170                 return true;
171             } else {
172                 return false;
173             }
174         }, "Search for source in navi bar", 1000);
175
176         waitsFor(function () {
177             return get_hit_counter() < hits_all_targets ? true : false;
178         }, "Search for with less hits", 9 * 1000);
179
180         runs(function () {
181             var hits_single_target = get_hit_counter();
182             debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
183             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
184         });
185     });
186
187     it("limit search to first author", function () {
188         var hits_all_targets = get_hit_counter();
189
190         var click = $("div#mkwsFacetAuthors div.term:nth-child(2) a").trigger("click");
191         debug("limit author click is success: " + click.length);
192         expect(click.length == 1).toBe(true);
193
194         waitsFor(function () {
195             return get_hit_counter() < hits_all_targets ? true : false;
196         }, "Search for with less hits", 9 * 1000);
197
198         runs(function () {
199             var hits_single_target = get_hit_counter();
200             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
201             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
202         });
203     });
204
205     it("show record author", function () {
206         show_record();
207     });
208 });
209
210 describe("Check status client counter", function () {
211     function get_time() {
212         var date = new Date();
213         return date.getTime();
214     }
215     var time = get_time();
216
217     it("check status clients", function () {
218
219         waitsFor(function () {
220             var clients = $("div#mkwsStat span.clients");
221             if (clients.length == 1 && clients.text() == "0/1") {
222                 return true;
223             } else {
224                 return false;
225             }
226
227         }, "wait for status", 4 * 1000);
228
229     });
230     runs(function () {
231         var clients = $("div#mkwsStat span.clients");
232         debug("span.clients: " + clients.text());
233         expect(clients.text()).toEqual("0/1");
234     });
235
236 });