nearly working version
[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 // global state object
8 var jasmine_state = {
9     time: 0
10 };
11
12 function my_click(id, time) {
13     setTimeout(function () {
14         debug("trigger click on id: " + id);
15         $(id).trigger("click");
16     }, time * 1000);
17 }
18
19 function found(time, none) {
20     setTimeout(function () {
21         jasmine_state.time = time;
22
23         var found = $("#mkwsPager").html().match(/found: ([0-9]+)/);
24
25         describe("pazpar2 hit count", function () {
26             if (none) {
27                 it("no results yet", function () {
28                     expect(found).toBe(null);
29                 });
30             } else {
31                 it("got results", function () {
32                     expect(found[0]).toMatch(/^[0-9]+$/);
33                 });
34             }
35             debug("mkws pager found records: " + (found != null ? found[0] : "unknown"));
36             debug("time state: " + jasmine_state.time);
37         });
38
39     }, time * 1000);
40 }
41
42 describe("Check pazpar2 search", function () {
43     it("pazpar2 was successfully initialize", function () {
44         expect(mkws_config.error).toBe(undefined);
45     });
46
47     it("validate HTML id's", function () {
48         expect($("input#mkwsQuery").length == 1).toBe(true);
49         expect($("input#mkwsButton").length == 1).toBe(true);
50
51         expect($("#mkwsNext").length == 1).toBe(false);
52         expect($("#mkwsPrev").length == 1).toBe(false);
53     });
54
55     it("run search query", function () {
56         $("input#mkwsQuery").val("freebsd");
57         expect($("input#mkwsQuery").val()).toMatch(/^freebsd$/);
58
59         setTimeout(function () {
60             $("input#mkwsButton").trigger("click");
61         }, 3 * 1000);
62     });
63
64
65     // Asynchronous part
66     it("check running search", function () {
67         var max_time = 10;
68
69         expect($("#mkwsPager").length == 1).toBe(true);
70
71         runs(function () {
72             // click next/prev after N seconds
73             my_click("#mkwsNext", 10);
74             my_click("#mkwsNext", 13);
75             my_click("#mkwsPrev", 15);
76
77             // check hit counter after N seconds
78             found(0, true);
79             found(5);
80             found(10);
81             found(15);
82             found(max_time);
83         });
84
85 /*
86         waitsFor(function () {
87             return jasmine_state.time == max_time ? true : false;
88         }, "The Value should be 20 seconds", 30 * 1000); // (max_time + 1) * 1000);
89
90         runs(function () {
91             expect($("#mkwsPager").length == 1).toBe(true);
92         })
93         */
94
95 /* runs(function () {
96             expect(jasmine_state.time).toEqual(max_time);
97         });
98         */
99     });
100 });