blob: b9047ebb97d9b9a0090e734237a59634d6cb8470 [file] [log] [blame]
Akron644ad9f2021-07-26 16:12:59 +02001define(['panel','view','panel/result','panel/pagination','util'], function (panelClass,viewClass,resultClass,paginationClass) {
Akron56d42072018-07-24 11:17:24 +02002
3 var controlStr = "";
4
5 var helloViewClass = {
6 create : function () {
7 return Object.create(viewClass)._init(['myview']).upgradeTo(this);
8 },
9
10 show : function () {
11 if (this._show)
12 return this._show;
13
14 var e = document.createElement('span');
15 e.addT("Hello World!");
16
17 this._show = e;
18 return e;
19 }
20 };
hebastad025eb92019-12-07 21:04:42 +010021
22 var secViewClass = {
23 create : function () {
24 return Object.create(viewClass)._init(['secview']).upgradeTo(this);
25 },
26 };
Akron56d42072018-07-24 11:17:24 +020027
28 describe('KorAP.Panel', function () {
29
30 it('should be initializable', function () {
31 var panel = panelClass.create();
32 var e = panel.element();
Akron386f1222022-12-21 16:26:11 +010033
34 expect(panel.type).toEqual('base');
Akron56d42072018-07-24 11:17:24 +020035 expect(e.tagName).toEqual("DIV");
36 expect(e.classList.contains("panel")).toBeTruthy();
37 expect(e.firstChild.tagName).toEqual("DIV");
38
39 // No children in the empty view element
40 expect(e.firstChild.firstChild).toBeFalsy();
41 expect(e.lastChild.tagName).toEqual("DIV");
42 expect(e.lastChild.classList.contains("button-panel")).toBeTruthy();
43 expect(e.lastChild.classList.contains("button-group")).toBeTruthy();
44 expect(e.lastChild.firstChild).toBeFalsy();
45
Akron37ea1192021-07-28 10:40:14 +020046 expect(panel.actions()).toBeTruthy();
Akron56d42072018-07-24 11:17:24 +020047 });
48
49
50 it('should be extensible', function () {
51 var panel = panelClass.create();
52
53 controlStr = "";
Akron37ea1192021-07-28 10:40:14 +020054 panel.actions().add("New", {'cls':["new"]}, function () {
Akron56d42072018-07-24 11:17:24 +020055 controlStr = 'New!!!';
56 });
57
58 var e = panel.element();
59
60 expect(e.tagName).toEqual("DIV");
61 expect(e.firstChild.firstChild).toBeFalsy();
62
63 expect(e.lastChild.firstChild.tagName).toEqual("SPAN");
64 expect(e.lastChild.firstChild.getAttribute("title")).toEqual("New");
65 expect(e.lastChild.firstChild.classList.contains("new")).toBeTruthy();
66 expect(e.lastChild.firstChild.firstChild.tagName).toEqual("SPAN");
67 expect(e.lastChild.firstChild.firstChild.firstChild.data).toEqual("New");
68
69 expect(controlStr).toEqual("");
70 e.lastChild.firstChild.click();
71 expect(controlStr).toEqual("New!!!");
72 });
73
74 it('should be classable', function () {
75 var panel = panelClass.create(["versuch"]);
76 var e = panel.element();
77 expect(e.tagName).toEqual("DIV");
78 expect(e.classList.contains("panel")).toBeTruthy();
79 expect(e.classList.contains("versuch")).toBeTruthy();
80 expect(e.lastChild.classList.contains("button-panel")).toBeTruthy();
81 expect(e.lastChild.classList.contains("versuch")).toBeTruthy();
82 });
83
84 it('should be extensible by a view', function () {
85 var panel = panelClass.create();
86 var view = helloViewClass.create();
87 var e = panel.element();
88
89 panel.add(view);
90
91 var viewE = e.firstChild.firstChild;
92 expect(viewE.classList.contains('view')).toBeTruthy();
93 expect(viewE.classList.contains('myview')).toBeTruthy();
94 expect(viewE.firstChild.tagName).toEqual("SPAN");
95 expect(viewE.firstChild.firstChild.data).toEqual("Hello World!");
96 });
hebastad025eb92019-12-07 21:04:42 +010097
98 it('views should be appended or prepended', function () {
99 let panel = panelClass.create();
100 let view = helloViewClass.create();
101 let e = panel.element();
102 panel.add(view);
103 let secview = secViewClass.create();
104 panel.add(secview);
105 let viewFirst = e.firstChild.firstChild;
106 expect(viewFirst.classList.contains('myview')).toBeTruthy();
107
108 let prependPanel = panelClass.create();
109 prependPanel.prepend = true;
110 prependPanel.add(view);
111 prependPanel.add(secview);
112 viewFirst = prependPanel.element().firstChild.firstChild;
113 expect(viewFirst.classList.contains('secview')).toBeTruthy();
114 });
115
Akron56d42072018-07-24 11:17:24 +0200116 });
Akron362c11a2018-08-29 20:01:30 +0200117
118 describe('KorAP.Panel.Result', function () {
119
120 it('should be initializable', function () {
121 var show = {};
122 var result = resultClass.create(show);
Akron386f1222022-12-21 16:26:11 +0100123 expect(result.type).toEqual('result');
Akron362c11a2018-08-29 20:01:30 +0200124 expect(result.element().children.length).toEqual(2);
125 expect(result.element().firstChild.children.length).toEqual(0);
126 });
127
128 it('should open KQAction', function () {
129 var show = {};
130 var result = resultClass.create(show);
131
132 result.addKqAction();
133
134 expect(result.element().lastChild.firstChild.textContent).toEqual("show KoralQuery");
135 expect(show["kq"]).toBeFalsy();
136
137 // Open KQ view
138 result.element().lastChild.firstChild.click();
139
140 expect(result.element().querySelector('#koralquery').textContent).toEqual("{}");
141 expect(show["kq"]).toBeTruthy();
142
143 var close = result.element().firstChild.firstChild.lastChild.firstChild;
144 expect(close.textContent).toEqual("Close");
145
146 // Close view
147 close.click();
148
149 expect(result.element().querySelector('#koralquery')).toBeFalsy();
150 expect(show["kq"]).toBeFalsy();
151 });
Akronece4bb72020-10-19 12:24:20 +0200152
153 it('should open toggler', function () {
154 const show = {};
155
156 const div = document.body.addE("div");
157 div.setAttribute("id","search");
158 const ol = div.addE("ol");
159 ol.classList.add("align-left");
160
161 const result = resultClass.create(show);
162
163 result.addAlignAction();
164
165 const b = result.element().lastChild.firstChild;
166
167 expect(b.textContent).toEqual("toggle alignment");
168 expect(b.classList.contains('right')).toBeTruthy();
169 expect(ol.classList.contains('align-left')).toBeTruthy();
170
171 b.click();
172
173 expect(b.textContent).toEqual("toggle alignment");
174 expect(b.classList.contains('center')).toBeTruthy();
175 expect(ol.classList.contains('align-right')).toBeTruthy();
176
177 b.click();
178
179 expect(b.textContent).toEqual("toggle alignment");
180 expect(b.classList.contains('left')).toBeTruthy();
181 expect(ol.classList.contains('align-center')).toBeTruthy();
182
183 b.click();
184
185 expect(b.textContent).toEqual("toggle alignment");
186 expect(b.classList.contains('right')).toBeTruthy();
187 expect(ol.classList.contains('align-left')).toBeTruthy();
188
189 document.body.removeChild(div);
190 });
Akron362c11a2018-08-29 20:01:30 +0200191 });
Akron644ad9f2021-07-26 16:12:59 +0200192
193 describe('KorAP.Panel.Pagination', function () {
194 it('should be initializable', function () {
195 // Create pagination element for pagination information
196 let p = document.createElement('div');
197 p.setAttribute('id', 'pagination')
198 p.setAttribute('data-page',3);
199 p.setAttribute('data-total',30);
200 p.setAttribute('data-count',25);
201
202 document.body.appendChild(p);
203
204 // Create pagination class object
205 var pagination = paginationClass.create();
206 let list = pagination.actions().element();
Akron386f1222022-12-21 16:26:11 +0100207 expect(pagination.type).toEqual('pagination');
Akron644ad9f2021-07-26 16:12:59 +0200208 expect(list.classList.contains('button-group-list')).toBeTruthy();
209 expect(list.classList.contains('visible')).toBeFalsy();
210
Akron0ac1b5f2024-09-25 14:03:39 +0200211 expect(pagination.addRandomPage()).toBeTruthy();
Akron644ad9f2021-07-26 16:12:59 +0200212
213 let clicked = false;
214 pagination.actions().add(
215 "test", {}, function () { clicked = true }
216 );
217
218 pagination.buttonGroup().element().firstChild.click();
219 expect(list.classList.contains('visible')).toBeTruthy();
220
221 expect(clicked).toBeFalsy();
222 pagination.actions().element().children[4].click();
223 expect(clicked).toBeTruthy();
224
225 document.body.removeChild(p);
226 document.body.removeChild(pagination.actions().element());
227 });
Akron0ac1b5f2024-09-25 14:03:39 +0200228
229 it('shouldn\'t show random page on single pages', function () {
230
231 // Create pagination element for pagination information
232 let p = document.createElement('div');
233 p.setAttribute('id', 'pagination')
234 p.setAttribute('data-page',1);
235 p.setAttribute('data-total',1);
236 p.setAttribute('data-count',25);
237
238 document.body.appendChild(p);
239
240 // Create pagination class object
241 var pagination = paginationClass.create();
242 let list = pagination.actions().element();
243 expect(pagination.type).toEqual('pagination');
244 expect(list.classList.contains('button-group-list')).toBeTruthy();
245 expect(list.classList.contains('visible')).toBeFalsy();
246
247 expect(pagination.addRandomPage()).toBeFalsy();
248 });
249
Akron644ad9f2021-07-26 16:12:59 +0200250 });
Akron56d42072018-07-24 11:17:24 +0200251});