blob: 07183f2c7ea9534a0235ddf5f224b30a311275d6 [file] [log] [blame]
Akron4510a3e2021-09-10 15:09:56 +02001define(['buttongroup','buttongroup/menu','menu/item','state'], function (buttonGroupClass, buttonGroupMenuClass, defaultItemClass, stateClass) {
Akrondefa5e82018-07-10 12:09:46 +02002
3 var FunObj = {
4 count : 0,
5 create : function () {
6 return Object.create(this);
7 },
8 incr : function () {
9 this.count++;
10 }
11 };
12
13 describe('KorAP.ButtonGroup', function () {
14
15 it('should be initializable', function () {
16 var group = buttonGroupClass.create(['action', 'bottom']);
17 var el = group.element();
18 expect(el.tagName).toEqual('DIV');
19 expect(el.firstChild).toBeNull();
20 expect(el.classList.contains('action')).toBeTruthy();
21 expect(el.classList.contains('bottom')).toBeTruthy();
22 expect(el.classList.contains('button-group')).toBeTruthy();
23 });
24
25 it('should be expandable', function () {
26 var group = buttonGroupClass.create();
27 expect(group.element().classList.contains('button-group')).toBeTruthy();
28
Akron792b1a42020-09-14 18:56:38 +020029 group.add('Meta', { 'cls':['meta', 'top']}, function (e) {});
Akrondefa5e82018-07-10 12:09:46 +020030
31 var btn = group.element().firstChild;
32 expect(btn.tagName).toEqual('SPAN');
33 expect(btn.classList.contains('meta')).toBeTruthy();
34 expect(btn.classList.contains('top')).toBeTruthy();
35 expect(btn.innerText).toEqual('Meta');
36 });
37
Akrond141a362018-07-10 18:12:13 +020038 it('should be clearable', function () {
39 var group = buttonGroupClass.create();
40 expect(group.element().classList.contains('button-group')).toBeTruthy();
41
Akron792b1a42020-09-14 18:56:38 +020042 group.add('Meta', {'cls':['meta', 'top']}, function (e) {});
43 group.add('Mate', {'cls':['mate']}, function (e) {});
Akrond141a362018-07-10 18:12:13 +020044
45 var btn = group.element().children[0];
46 expect(btn.tagName).toEqual('SPAN');
47 expect(btn.classList.contains('meta')).toBeTruthy();
48 expect(btn.classList.contains('top')).toBeTruthy();
49 expect(btn.innerText).toEqual('Meta');
50
51 btn = group.element().children[1];
52 expect(btn.tagName).toEqual('SPAN');
53 expect(btn.classList.contains('mate')).toBeTruthy();
54 expect(btn.classList.contains('top')).toBeFalsy();
55 expect(btn.innerText).toEqual('Mate');
56
57 // clear button
58 group.clear();
59
60 expect(group.element().children.length).toEqual(0);
61
Akron792b1a42020-09-14 18:56:38 +020062 group.add('New', {'cls':['new']}, function (e) {});
Akrond141a362018-07-10 18:12:13 +020063
64 btn = group.element().children[0];
65 expect(btn.tagName).toEqual('SPAN');
66 expect(btn.classList.contains('new')).toBeTruthy();
67 expect(btn.classList.contains('top')).toBeFalsy();
68 expect(btn.innerText).toEqual('New');
69 });
70
71
Akrondefa5e82018-07-10 12:09:46 +020072 it('should listen to button clicks', function () {
73 var group = buttonGroupClass.create();
74
75 var count = 0;
76
77 group.add('Meta', undefined, function () {
78 count++;
79 });
80
81 expect(count).toEqual(0);
82
83 // Click on the button
84 group.element().firstChild.click();
85
86 expect(count).toEqual(1);
87 });
88
89 it('should respect binds', function () {
90 var group = buttonGroupClass.create();
91 fun = FunObj.create();
92 expect(fun.count).toEqual(0);
93
94 // Bind group to another object
Akronb23e2712018-07-13 18:17:37 +020095 var group2 = group.bind(fun);
96 expect(group2).toEqual(group);
97 expect(group.bind()).toEqual(fun);
Akrondefa5e82018-07-10 12:09:46 +020098
99 group.add('Incr', undefined, function (e) {
100 // increment on bind object
101 this.incr()
102 });
103
104 // Click on the button
105 group.element().firstChild.click();
106 expect(fun.count).toEqual(1);
107 });
Akron52ed22d2018-07-11 17:05:19 +0200108
hebasta40a85cf2020-07-15 18:10:08 +0200109 it('should add icon', function () {
110 var group = buttonGroupClass.create();
111 expect(group.element().classList.contains('button-group')).toBeTruthy();
112
Akron792b1a42020-09-14 18:56:38 +0200113 group.add('Meta', {'cls':['meta'], 'icon': 'metaicon'}, function (e) {});
hebasta40a85cf2020-07-15 18:10:08 +0200114
115 var btn = group.element().firstChild;
116 expect(btn.tagName).toEqual('SPAN');
117 expect(btn.getAttribute('data-icon')).toEqual('metaicon');
118 expect(btn.classList.contains('meta')).toBeTruthy();
119 expect(btn.innerText).toEqual('Meta');
120 });
121
Akron52ed22d2018-07-11 17:05:19 +0200122 it('should open lists', function () {
Akron8120ac12020-09-14 18:05:33 +0200123 var group = buttonGroupClass.create();
124 expect(group.element().classList.contains('button-group')).toBeTruthy();
125
Akron792b1a42020-09-14 18:56:38 +0200126 var list = group.addList('More', {'cls':['more']});
Akron8120ac12020-09-14 18:05:33 +0200127
128 list.readItems([
129 ['cool', 'cool', function () { }],
130 ['very cool', 'veryCool', function () { }]
131 ]);
132
133 var btn = group.element().firstChild;
134 expect(btn.tagName).toEqual('SPAN');
135 expect(btn.classList.contains('more')).toBeTruthy();
136 expect(btn.innerText).toEqual('More');
137
138 expect(list.element().classList.contains('visible')).toBeFalsy();
Akron52ed22d2018-07-11 17:05:19 +0200139
Akron8120ac12020-09-14 18:05:33 +0200140 // Click to show menu
141 btn.click();
142
143 expect(list.element().classList.contains('visible')).toBeTruthy();
144
145 expect(list.element().children[1].children[0].innerText).toEqual('cool--');
146 expect(list.element().children[1].children[1].innerText).toEqual('very cool--');
147
148 document.body.removeChild(list.element());
Akron52ed22d2018-07-11 17:05:19 +0200149 });
Akron858cbc82019-12-05 16:53:13 +0100150
Akron20b6d312021-07-26 15:14:42 +0200151 it('should add to lists', function () {
152 var group = buttonGroupClass.create();
153 expect(group.element().classList.contains('button-group')).toBeTruthy();
154
155 var list = group.addList('More', {'cls':['more']});
156
157 let x = 'empty';
158 list.add('Meta1', {'cls':['meta'], 'icon': 'metaicon'}, function (e) {
159 x = 'meta1';
160 });
161 list.add('Meta2', {'cls':['meta'], 'icon': 'metaicon'}, function (e) {
162 x = 'meta2'
163 });
164
165 var btn = group.element().firstChild;
166 expect(btn.tagName).toEqual('SPAN');
167 expect(btn.classList.contains('more')).toBeTruthy();
168 expect(btn.innerText).toEqual('More');
169
170 expect(list.element().classList.contains('visible')).toBeFalsy();
171
172 // Click to show menu
173 btn.click();
174
175 expect(list.element().classList.contains('visible')).toBeTruthy();
Leo Reppdedcf1a2021-08-18 18:57:47 +0200176 expect(directElementChildrenByTagName(list.element(),"li")[0].innerHTML).toEqual('Meta1');
177 expect(directElementChildrenByTagName(list.element(),"li")[1].innerHTML).toEqual('Meta2');
Akron20b6d312021-07-26 15:14:42 +0200178
179 expect(x).toEqual('empty');
Leo Reppdedcf1a2021-08-18 18:57:47 +0200180 directElementChildrenByTagName(list.element(),"li")[0].click();
Akron20b6d312021-07-26 15:14:42 +0200181 expect(x).toEqual('meta1');
182
183 expect(list.element().classList.contains('visible')).toBeFalsy();
184
185 // Click to show menu
186 btn.click();
187
188 expect(list.element().classList.contains('visible')).toBeTruthy();
Leo Reppdedcf1a2021-08-18 18:57:47 +0200189 directElementChildrenByTagName(list.element(),"li")[1].click();
Akron20b6d312021-07-26 15:14:42 +0200190 expect(x).toEqual('meta2');
191
192 expect(list.element().classList.contains('visible')).toBeFalsy();
193
194 expect(list.element().children[1].children[0].innerText).toEqual('Meta1--');
195 expect(list.element().children[1].children[1].innerText).toEqual('Meta2--');
196
197 document.body.removeChild(list.element());
198 });
199
200
Akron858cbc82019-12-05 16:53:13 +0100201 it('should support toggle buttons', function () {
202 var group = buttonGroupClass.create();
203
204 let s = stateClass.create();
205
206 expect(s.get()).toBeFalsy();
207
Akron792b1a42020-09-14 18:56:38 +0200208 group.addToggle('example',{'cls':["examplecls"]}, s);
Akron858cbc82019-12-05 16:53:13 +0100209
210 let e = group.element();
211
212 expect(e.firstChild.getAttribute("title")).toBe("example");
213 expect(e.firstChild.classList.contains("examplecls")).toBeTruthy();
214
215 expect(e.firstChild.firstChild.classList.contains("check")).toBeTruthy();
216 expect(e.firstChild.firstChild.classList.contains("button-icon")).toBeTruthy();
217 expect(e.firstChild.lastChild.textContent).toBe("example");
218
219 // Check state
220 expect(s.get()).toBeFalsy();
221 expect(e.firstChild.firstChild.classList.contains("checked")).toBeFalsy();
222
223 // Click on the button
224 e.firstChild.click();
225
226 // Check state
227 expect(s.get()).toBeTruthy();
228 expect(e.firstChild.firstChild.classList.contains("checked")).toBeTruthy();
229 });
Akronf8af3b82021-07-21 20:24:00 +0200230
231 it('should allow adoption', function () {
232
233 const el = document.createElement('div');
234
235 const group = buttonGroupClass.adopt(el);
236
237 group.add('Meta', {'cls':['meta', 'top']}, function (e) {});
238 group.add('Mate', {'cls':['mate']}, function (e) {});
239
240 var btn = group.element().children[0];
241 expect(btn.tagName).toEqual('SPAN');
242 expect(btn.classList.contains('meta')).toBeTruthy();
243 expect(btn.classList.contains('top')).toBeTruthy();
244 expect(btn.innerText).toEqual('Meta');
245
246 btn = group.element().children[1];
247 expect(btn.tagName).toEqual('SPAN');
248 expect(btn.classList.contains('mate')).toBeTruthy();
249 expect(btn.classList.contains('top')).toBeFalsy();
250 expect(btn.innerText).toEqual('Mate');
251 });
252
253 it('should make anchor element definable', function () {
254
255 const el = document.createElement('div');
256 const c1 = el.appendChild(document.createElement('c1'));
257 const c2 = el.appendChild(document.createElement('c2'));
258 const c3 = el.appendChild(document.createElement('c3'));
Akronc8c8bf12021-09-24 11:30:45 +0200259
260 let group = buttonGroupClass.adopt(el);
261 expect(group.anchor(null)).toBeFalsy();
Akronf8af3b82021-07-21 20:24:00 +0200262
Akronc8c8bf12021-09-24 11:30:45 +0200263 group = buttonGroupClass.adopt(el);
Akronf8af3b82021-07-21 20:24:00 +0200264 expect(group.anchor(c3)).toBeTruthy();
265
266 group.add('Meta', {'cls':['meta', 'top']}, function (e) {});
267 group.add('Mate', {'cls':['mate']}, function (e) {});
268
269 let btn = group.element().children[0];
270 expect(btn.tagName).toEqual('C1');
271
272 btn = group.element().children[1];
273 expect(btn.tagName).toEqual('C2');
274
275 btn = group.element().children[2];
276 expect(btn.tagName).toEqual('SPAN');
277 expect(btn.classList.contains('meta')).toBeTruthy();
278 expect(btn.classList.contains('top')).toBeTruthy();
279 expect(btn.innerText).toEqual('Meta');
280
281 btn = group.element().children[3];
282 expect(btn.tagName).toEqual('SPAN');
283 expect(btn.classList.contains('mate')).toBeTruthy();
284 expect(btn.classList.contains('top')).toBeFalsy();
285 expect(btn.innerText).toEqual('Mate');
286
287 btn = group.element().children[4];
288 expect(btn.tagName).toEqual('C3');
289 });
Akrondefa5e82018-07-10 12:09:46 +0200290 });
Akron4510a3e2021-09-10 15:09:56 +0200291
292 describe('KorAP.ButtonGroup.Menu', function () {
293 it('should reposition', function () {
294 const menu = buttonGroupMenuClass.create([["hallo", undefined, function () {}]], defaultItemClass);
295 const div = document.createElement('div');
296
297 document.body.appendChild(div);
298
299 div.style.position = 'absolute';
300 div.style.display = 'block';
301 div.style.left = '14px';
302 div.style.top = '20px';
303 div.style.width = '40px';
304 div.style.height = '30px';
305
306 menu.show();
307
308 // 000
309 menu._repos(div);
310 let elem = menu.element();
311 const fffl = elem.style.left;
312 const ffft = elem.style.top;
313
314 // 100
315 menu.openAt(true, false, false);
316 menu._repos(div);
317 elem = menu.element();
318 const tffl = elem.style.left;
319 expect(tffl).not.toEqual(fffl);
320
321
322 // 011
323 menu.openAt(false, true, true);
324 menu._repos(div);
325 elem = menu.element();
326 const fttt = elem.style.top;
327 expect(fttt).not.toEqual(ffft);
328
329 document.body.removeChild(div);
330 document.body.removeChild(elem);
331 });
332 });
Akrondefa5e82018-07-10 12:09:46 +0200333});