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