lowercase name for checks
[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 function get_hit_counter() {
8     if ($("#mkwsPager").length == 0) return -1;
9
10     var found = $("#mkwsPager").text();
11     var re = /found: ([0-9]+)/;
12     re.exec(found);
13     var hits = -1;
14
15     if (RegExp.$1) {
16         hits = parseInt(RegExp.$1);
17         expect(hits).toBeGreaterThan(0);
18     }
19
20     //debug("Hits: " + hits);
21     return hits;
22 }
23
24 describe("Check pazpar2 search", function () {
25     it("pazpar2 was successfully initialize", function () {
26         expect(mkws_config.error).toBe(undefined);
27     });
28
29     it("validate HTML id's", function () {
30         expect($("input#mkwsQuery").length == 1).toBe(true);
31         expect($("input#mkwsButton").length == 1).toBe(true);
32
33         expect($("#mkwsNext").length == 1).toBe(false);
34         expect($("#mkwsPrev").length == 1).toBe(false);
35     });
36
37     it("run search query", function () {
38         $("input#mkwsQuery").val("freebsd");
39         expect($("input#mkwsQuery").val()).toMatch(/^freebsd$/);
40
41         setTimeout(function () {
42             $("input#mkwsButton").trigger("click");
43         }, 3 * 1000);
44     });
45 });
46
47
48 describe("Check pazpar2 navigation", function () {
49     // Asynchronous part
50     it("check running search next/prev", function () {
51         expect($("#mkwsPager").length == 1).toBe(true);
52
53         function my_click(id, time) {
54             setTimeout(function () {
55                 debug("trigger click on id: " + id);
56                 var click = $(id).trigger("click");
57
58                 debug("next click is success: " + click.length);
59                 expect(click.length == 1).toBe(true);
60
61             }, time * 1000);
62         }
63
64         runs(function () {
65             // click next/prev after N seconds
66             my_click("#mkwsNext", 7);
67             my_click("#mkwsNext", 8);
68             my_click("#mkwsPrev", 9);
69         });
70     });
71 });
72
73
74 describe("Check pazpar2 hit counter", function () {
75
76     it("check running search hit counter", function () {
77         var max_time = 10; // in seconds
78         var expected_hits = 116; //
79         var j_time = 0;
80         var j_hits = 0;
81
82         function found(time, none) {
83             setTimeout(function () {
84                 j_time = time;
85                 hits = get_hit_counter();
86
87                 // debug("found: " + found);
88                 if (none) {
89                     expect(hits < 0).toBeTruthy();
90                 } else {
91                     j_hits = hits;
92                 }
93
94                 debug("mkws pager found records: '" + hits + "'");
95                 debug("time state: " + j_time);
96
97                 expect(time >= 0).toBeTruthy();
98             }, time * 1000);
99         }
100
101         runs(function () {
102             // check hit counter after N seconds
103             found(0, true);
104             found(3);
105             found(6);
106             found(8);
107             found(max_time);
108         });
109
110         waitsFor(function () {
111             return j_time == max_time ? true : false;
112         }, "The Value should be 20 seconds", max_time * 1000);
113
114
115         runs(function () {
116             expect($("#mkwsPager").length == 1).toBe(true);
117         })
118
119         runs(function () {
120             expect(j_time <= max_time).toBeTruthy();
121             expect(j_hits).toBeGreaterThan(expected_hits);
122         });
123     });
124 });
125
126 describe("Check Termlist", function () {
127     function show_record() {
128         var click = $("div#mkwsRecords div.record:nth-child(3) :nth-child(2)").trigger("click");
129         debug("show click is success: " + click.length);
130         expect(click.length == 1).toBe(true);
131     }
132
133     // show_record();
134     it("found Termlist", function () {
135         var termlist = $("div#mkwsTermlists");
136         debug("Termlist success: " + termlist.length);
137         expect(termlist.length == 1).toBe(true);
138
139         var sources = $("div#mkwsFacetSources");
140         expect(sources.length == 1).toBe(true);
141
142         var subjects = $("div#mkwsFacetSubjects");
143         expect(subjects.length == 1).toBe(true);
144
145         var authors = $("div#mkwsFacetAuthors");
146         expect(authors.length == 1).toBe(true);
147     });
148
149     it("limit search to first source", function () {
150         var hits_all_targets = get_hit_counter();
151
152         var click = $("div#mkwsFacetSources div.term:nth-child(2) a").trigger("click");
153         debug("limit source click is success: " + click.length);
154         expect(click.length == 1).toBe(true);
155
156         waitsFor(function () {
157             if ($("div#mkwsNavi").length && $("div#mkwsNavi").text().match(/^Source/)) {
158                 return true;
159             } else {
160                 return false;
161             }
162         }, "Search for source in navi bar", 1000);
163
164         waitsFor(function () {
165             return get_hit_counter() < hits_all_targets ? true : false;
166         }, "Search for with less hits", 9 * 1000);
167
168         runs(function () {
169             var hits_single_target = get_hit_counter();
170             debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
171             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
172         });
173     });
174
175     it("limit search to first author", function () {
176         var hits_all_targets = get_hit_counter();
177
178         var click = $("div#mkwsFacetAuthors div.term:nth-child(2) a").trigger("click");
179         debug("limit author click is success: " + click.length);
180         expect(click.length == 1).toBe(true);
181
182         waitsFor(function () {
183             return get_hit_counter() < hits_all_targets ? true : false;
184         }, "Search for with less hits", 9 * 1000);
185
186         runs(function () {
187             var hits_single_target = get_hit_counter();
188             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
189             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
190         });
191     });
192
193     it("show record author", function () {
194         show_record();
195     });
196 });
197
198 describe("Check status client counter", function () {
199     function get_time() {
200         var date = new Date();
201         return date.getTime();
202     }
203     var time = get_time();
204
205     it("limit search to first author", function () {
206
207         waitsFor(function () {
208             var clients = $("div#mkwsStat span.clients");
209             if (clients.length == 1 && clients.text() == "0/1") {
210                 return true;
211             } else {
212                 return false;
213             }
214
215         }, "wait for status", 4 * 1000);
216
217     });
218     runs(function () {
219         var clients = $("div#mkwsStat span.clients");
220         debug("span.clients: " + clients.text());
221         expect(clients.text()).toEqual("0/1");
222     });
223
224 });