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