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