blob: e81033db5170cb4e747f06188835dedc26873ef1 [file] [log] [blame]
Akron954c6a52020-11-10 14:26:29 +01001"use strict";
2
Akronfac16472018-07-26 16:47:21 +02003define(['hint', 'hint/input', 'hint/contextanalyzer', 'hint/menu', 'hint/item'], function (hintClass, inputClass, contextClass, menuClass, menuItemClass) {
Nils Diewald19ccee92014-12-08 11:30:08 +00004
Akronfac16472018-07-26 16:47:21 +02005 function emitKeyboardEvent (element, type, keyCode) {
6 // event type : keydown, keyup, keypress
7 // http://stackoverflow.com/questions/596481/simulate-javascript-key-events
8 // http://stackoverflow.com/questions/961532/firing-a-keyboard-event-in-javascript
9 var keyboardEvent = document.createEvent("KeyboardEvent");
10 var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ?
11 "initKeyboardEvent" : "initKeyEvent";
12 keyboardEvent[initMethod](
13 type,
14 true, // bubbles
15 true, // cancelable
16 window, // viewArg: should be window
17 false, // ctrlKeyArg
18 false, // altKeyArg
19 false, // shiftKeyArg
20 false, // metaKeyArg
21 keyCode, // keyCodeArg : unsigned long the virtual key code, else 0
22 0 // charCodeArgs : unsigned long the Unicode character
23 // associated with the depressed key, else 0
24 );
25 element.dispatchEvent(keyboardEvent);
26 };
Nils Diewald19ccee92014-12-08 11:30:08 +000027
Akronfac16472018-07-26 16:47:21 +020028 var afterAllFunc = function () {
29 try {
30 var mirrors = document.querySelectorAll(".hint.mirror");
31 for (var i in mirrors) {
32 mirrors[i].parentNode.removeChild(mirrors[i])
33 };
34 }
35 catch (e) {};
36
37 var body = document.body;
38 for (var i in body.children) {
39 if (body.children[i].nodeType && body.children[i].nodeType === 1) {
40 if (!body.children[i].classList.contains("jasmine_html-reporter")) {
41 body.removeChild(body.children[i]);
42 };
43 };
44 };
45 KorAP.API.getMatchInfo = undefined;
46 KorAP.context = undefined;
47 // KorAP.annotationHelper = undefined;
48 };
49
50 var beforeAllFunc = function () {
51 KorAP.annotationHelper = KorAP.annotationHelper || {};
52 KorAP.annotationHelper["-"] = [
53 ["Base Annotation", "base/s=", "Structure"],
54 ["CoreNLP", "corenlp/", "Constituency, Named Entities, Part-of-Speech"]
55 ];
56 KorAP.annotationHelper["corenlp/"] = [
57 ["Named Entity", "ne=" , "Combined"],
58 ["Named Entity", "ne_dewac_175m_600=" , "ne_dewac_175m_600"],
59 ["Named Entity", "ne_hgc_175m_600=", "ne_hgc_175m_600"]
60 ];
61 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000062
Nils Diewald7c8ced22015-04-15 19:21:00 +000063 describe('KorAP.InputField', function () {
Akronfac16472018-07-26 16:47:21 +020064 beforeAll(beforeAllFunc);
65 afterAll(afterAllFunc);
Akron5336fd42020-10-09 18:13:51 +020066 let input;
Nils Diewald1c546922015-04-13 01:56:19 +000067
Nils Diewald7c8ced22015-04-15 19:21:00 +000068 beforeEach(function () {
69 input = document.createElement("input");
70 input.setAttribute('type', "text");
71 input.setAttribute("value", "abcdefghijklmno");
72 input.style.position = 'absolute';
73 document.getElementsByTagName('body')[0].appendChild(input);
74 input.style.top = "20px";
75 input.style.left = "30px";
76 input.focus();
77 input.selectionStart = 5;
Nils Diewald5c5a7472015-04-02 22:13:38 +000078 });
79
Nils Diewald7c8ced22015-04-15 19:21:00 +000080 afterEach(function () {
81 document.getElementsByTagName("body")[0].removeChild(
Akron95abaf42018-04-26 15:33:22 +020082 input
Nils Diewald7c8ced22015-04-15 19:21:00 +000083 );
84 });
Nils Diewald5c5a7472015-04-02 22:13:38 +000085
Nils Diewald7c8ced22015-04-15 19:21:00 +000086 it('should be initializable', function () {
87 // Supports: context, searchField
88 var inputField = inputClass.create(input);
Akron24aa0052020-11-10 11:00:34 +010089 expect(inputField._el).not.toBe(undefined);
Nils Diewald7c8ced22015-04-15 19:21:00 +000090 });
Nils Diewald19ccee92014-12-08 11:30:08 +000091
Nils Diewald7c8ced22015-04-15 19:21:00 +000092 it('should have text', function () {
93 expect(input.value).toEqual('abcdefghijklmno');
94 var inputField = inputClass.create(input);
Nils Diewald19ccee92014-12-08 11:30:08 +000095
Nils Diewald7c8ced22015-04-15 19:21:00 +000096 expect(inputField.value()).toEqual("abcdefghijklmno");
Nils Diewald19ccee92014-12-08 11:30:08 +000097
Nils Diewald7c8ced22015-04-15 19:21:00 +000098 expect(input.selectionStart).toEqual(5);
99 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000100 expect(inputField._split()[0]).toEqual("abcde");
101 expect(inputField._split()[1]).toEqual("fghijklmno");
Nils Diewald19ccee92014-12-08 11:30:08 +0000102
Nils Diewald7c8ced22015-04-15 19:21:00 +0000103 inputField.insert("xyz");
104 expect(inputField.value()).toEqual('abcdexyzfghijklmno');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000105 expect(inputField._split()[0]).toEqual("abcdexyz");
106 expect(inputField._split()[1]).toEqual("fghijklmno");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000107 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000108
Nils Diewald7c8ced22015-04-15 19:21:00 +0000109 it('should be correctly positioned', function () {
110 expect(input.value).toEqual('abcdefghijklmno');
111 var inputField = inputClass.create(input);
112 document.getElementsByTagName("body")[0].appendChild(input);
113 inputField.reposition();
114 expect(input.style.left).toEqual("30px");
115 expect(inputField.mirror().style.left.match(/^(\d+)px$/)[1]).toBeGreaterThan(29);
116 expect(inputField.mirror().style.top.match(/^(\d+)px$/)[1]).toBeGreaterThan(20);
117 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000118
Nils Diewald7c8ced22015-04-15 19:21:00 +0000119 it('should have a correct context', function () {
120 expect(input.value).toEqual('abcdefghijklmno');
121 var inputField = inputClass.create(input);
122 expect(inputField.value()).toEqual("abcdefghijklmno");
123 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000124 expect(inputField._split()[0]).toEqual("abcde");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000125 expect(inputField.context()).toEqual("abcde");
126 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000127
Nils Diewald7c8ced22015-04-15 19:21:00 +0000128 /*
129 it('should be correctly triggerable', function () {
130 // https://developer.mozilla.org/samples/domref/dispatchEvent.html
131 var hint = KorAP.Hint.create({ "inputField" : input });
132 emitKeyboardEvent(hint.inputField.element, "keypress", 20);
133 });
134 */
Nils Diewald19ccee92014-12-08 11:30:08 +0000135 });
136
Nils Diewald1c546922015-04-13 01:56:19 +0000137
Nils Diewald7c8ced22015-04-15 19:21:00 +0000138 describe('KorAP.ContextAnalyzer', function () {
Akronfac16472018-07-26 16:47:21 +0200139 beforeAll(beforeAllFunc);
140 afterAll(afterAllFunc);
141
Nils Diewald7c8ced22015-04-15 19:21:00 +0000142 it('should be initializable', function () {
143 var analyzer = contextClass.create(")");
144 expect(analyzer).toBe(undefined);
145 analyzer = contextClass.create(".+?");
146 expect(analyzer).not.toBe(undefined);
147 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000148
Nils Diewald7c8ced22015-04-15 19:21:00 +0000149 it('should check correctly', function () {
Akronfac16472018-07-26 16:47:21 +0200150
151 // Intialize KorAP.context
152 hintClass.create();
153
Akron954c6a52020-11-10 14:26:29 +0100154 const analyzer = contextClass.create(KorAP.context);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000155 expect(analyzer.test("cnx/]cnx/c=")).toEqual("cnx/c=");
156 expect(analyzer.test("cnx/c=")).toEqual("cnx/c=");
157 expect(analyzer.test("cnx/c=np mate/m=mood:")).toEqual("mate/m=mood:");
158 expect(analyzer.test("impcnx/")).toEqual("impcnx/");
159 expect(analyzer.test("cnx/c=npcnx/")).toEqual("npcnx/");
160 expect(analyzer.test("mate/m=degree:pos corenlp/ne_dewac_175m_600="))
Akronfac16472018-07-26 16:47:21 +0200161 .toEqual("corenlp/ne_dewac_175m_600=");
Akron113cc1a2016-01-22 21:17:57 +0100162 expect(analyzer.test("corenlp/")).toEqual("corenlp/");
163 expect(analyzer.test("corenlp/c=")).toEqual("corenlp/c=");
164 expect(analyzer.test("corenlp/c=PP-")).toEqual("corenlp/c=PP-");
165 expect(analyzer.test("corenlp/c=XY-")).toEqual("corenlp/c=XY-");
Akroncff9bac2016-01-25 21:39:38 +0100166 expect(analyzer.test("sgbr/l=")).toEqual("sgbr/l=");
167 expect(analyzer.test("sgbr/lv=")).toEqual("sgbr/lv=");
168 expect(analyzer.test("sgbr/p=")).toEqual("sgbr/p=");
Akronee9ef4a2016-06-03 12:50:08 +0200169 expect(analyzer.test("")).toEqual(undefined);
170 expect(analyzer.test("abcdecnx/")).toEqual("abcdecnx/");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000171 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000172 });
173
Nils Diewald19ccee92014-12-08 11:30:08 +0000174
Nils Diewald7c8ced22015-04-15 19:21:00 +0000175 describe('KorAP.Hint', function () {
Akron954c6a52020-11-10 14:26:29 +0100176
177 let input;
178
Akronfac16472018-07-26 16:47:21 +0200179 beforeAll(beforeAllFunc);
180 afterAll(afterAllFunc);
Nils Diewald19ccee92014-12-08 11:30:08 +0000181
Nils Diewald7c8ced22015-04-15 19:21:00 +0000182 beforeEach(function () {
183 input = document.createElement("input");
184 input.setAttribute("type", "text");
185 input.setAttribute("value", "abcdefghijklmno");
186 input.style.position = 'absolute';
187 input.style.top = "20px";
188 input.style.left = "30px";
189 input.focus();
190 input.selectionStart = 5;
191 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000192
Nils Diewald7c8ced22015-04-15 19:21:00 +0000193 it('should be initializable', function () {
Akronfac16472018-07-26 16:47:21 +0200194
Nils Diewald7c8ced22015-04-15 19:21:00 +0000195 // Supports: context, searchField
196 var hint = hintClass.create({
Akronfac16472018-07-26 16:47:21 +0200197 inputField : input
Nils Diewald7c8ced22015-04-15 19:21:00 +0000198 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000199
Nils Diewald7c8ced22015-04-15 19:21:00 +0000200 expect(hint).toBeTruthy();
201 });
Akron00cd4d12016-05-31 21:01:11 +0200202
Akron1a5a5872016-09-05 20:17:14 +0200203
Akron00cd4d12016-05-31 21:01:11 +0200204 it('should alert at char pos', function () {
205 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200206 inputField : input
Akron00cd4d12016-05-31 21:01:11 +0200207 });
208
209 expect(hint.active()).toBeFalsy();
210
211 expect(hint.alert(4, 'That does not work!')).toBeTruthy();
212
213 expect(hint.active()).toBeTruthy();
214
215 var container = hint.inputField().container();
216 expect(container.firstChild.classList.contains('hint')).toBe(true);
217 expect(container.firstChild.classList.contains('alert')).toBe(true);
218 expect(container.firstChild.textContent).toEqual('That does not work!');
219 expect(hint.inputField().mirrorValue()).toEqual('abcd');
220
221 expect(hint.alert(4, 'That does not work!')).toBeFalsy();
222
223 // Update - meaning: hide alert
224 hint.update();
225
226 expect(hint.alert().active).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200227 expect(hint.active()).toBeFalsy();
Akron1a5a5872016-09-05 20:17:14 +0200228
229 // Show again
230 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
231 expect(hint.inputField().mirrorValue()).toEqual('abcde');
232 expect(hint.alert().active).toBeTruthy();
233 expect(hint.active()).toBeTruthy();
234
235 // Show menu, hide alert!
236 hint.show(false);
237 expect(hint.active()).toBeTruthy();
238 expect(hint.inputField().mirrorValue()).toEqual('abcde');
239 expect(hint.alert().active).toBeFalsy();
240
Akronfac16472018-07-26 16:47:21 +0200241 // Show again
Akron1a5a5872016-09-05 20:17:14 +0200242 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
243 expect(hint.inputField().mirrorValue()).toEqual('abcde');
244 expect(hint.alert().active).toBeTruthy();
245 expect(hint.active()).toBeTruthy();
246
247 // Show menu, hide alert!
248 hint.show(false);
Akronee9ef4a2016-06-03 12:50:08 +0200249 });
Akron00cd4d12016-05-31 21:01:11 +0200250
Akronda5bd3a2020-10-16 17:37:49 +0200251 it('should work both in Chrome and Firefox', function () {
252 var hint = hintClass.create({
253 inputField : input
254 });
255 hint.show(false);
256 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
257
258 // Show menu, hide alert!
259 hint.show(false);
260
261 expect(hint.active()).toBeFalsy();
262 });
263
264
Akronee9ef4a2016-06-03 12:50:08 +0200265 it('should view main menu on default', function () {
266 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200267 inputField : input
Akronee9ef4a2016-06-03 12:50:08 +0200268 });
269
270 expect(hint.active()).toBeFalsy();
271
272 hint.inputField().insert('der Baum corenlp/');
Akronee9ef4a2016-06-03 12:50:08 +0200273
Akron8eaeb2e2016-08-29 18:26:28 +0200274 var cont = hint.inputField().container();
Akron8eaeb2e2016-08-29 18:26:28 +0200275 expect(cont.getElementsByTagName('div').length).toBe(1);
276 expect(cont.getElementsByTagName('ul').length).toBe(0);
277 expect(cont.firstChild).toEqual(cont.firstChild);
278
279 // Show menu, if a relevant context exists
280 // There is a menu for corenlp/
Akron02360e42016-06-07 13:41:12 +0200281 hint.show(false);
282
Leo Repp57997402021-08-18 16:37:52 +0200283 expect(cont.getElementsByTagName('ul').length).toEqual(1+1); //+1 from containermenu (see container/container.js)
284 expect(cont.getElementsByTagName('li').length).toEqual(3);
Akron65c74352016-09-02 17:23:39 +0200285
Akron8eaeb2e2016-08-29 18:26:28 +0200286 // Hide the menu and focus on the input
287 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200288
Leo Repp57997402021-08-18 16:37:52 +0200289 expect(cont.getElementsByTagName('div').length).toEqual(1);
290 expect(cont.getElementsByTagName('li').length).toEqual(0);
Akronee9ef4a2016-06-03 12:50:08 +0200291
Akron02360e42016-06-07 13:41:12 +0200292 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200293
Akronee9ef4a2016-06-03 12:50:08 +0200294 hint.inputField().insert(' hhhh');
Akron65c74352016-09-02 17:23:39 +0200295
Leo Repp57997402021-08-18 16:37:52 +0200296 // show with context if possible
Akron02360e42016-06-07 13:41:12 +0200297 hint.show(false);
Akron65c74352016-09-02 17:23:39 +0200298
Leo Repp57997402021-08-18 16:37:52 +0200299 expect(cont.getElementsByTagName('div').length).toEqual(4);
300 expect(cont.getElementsByTagName('ul').length).toEqual(1+1);//+1 from containermenu (see container/container.js)
301 expect(cont.getElementsByTagName('li').length).toEqual(2);
Akron02360e42016-06-07 13:41:12 +0200302
Akronee9ef4a2016-06-03 12:50:08 +0200303 hint.unshow();
304 hint.inputField().insert(' aaaa/');
305
Leo Repp57997402021-08-18 16:37:52 +0200306 // show with context necessarily
Akronee9ef4a2016-06-03 12:50:08 +0200307 hint.show(true);
308
Leo Repp57997402021-08-18 16:37:52 +0200309 expect(cont.getElementsByTagName('div').length).toEqual(1);
310 expect(cont.getElementsByTagName('ul').length).toEqual(0); //here not +1: context doesnt fit
Akron1a5a5872016-09-05 20:17:14 +0200311 });
312
Akron95abaf42018-04-26 15:33:22 +0200313
314 it('should open menus depending on the context', function () {
315 var hint = hintClass.create({
316 inputField : input
317 });
318 hint.inputField().reset();
319
320 expect(hint.active()).toBeFalsy();
321
322 // show with context
323 hint.show(false);
324
325 expect(hint.active()).toBeTruthy();
Leo Repp57997402021-08-18 16:37:52 +0200326 var cont = hint.inputField().container();
327 expect(cont.getElementsByTagName('li')[0].firstChild.innerText).toEqual("Base Annotation");
Akron95abaf42018-04-26 15:33:22 +0200328
329 // Type in prefix
330 hint.active().prefix("cor").show();
331 expect(hint.active().prefix()).toEqual("cor");
332
333 // Click first step
Leo Repp57997402021-08-18 16:37:52 +0200334 expect(cont.getElementsByTagName('li')[0].firstChild.firstChild.innerText).toEqual("Cor");
335 cont.getElementsByTagName('li')[0].click();
Akron95abaf42018-04-26 15:33:22 +0200336
337 expect(hint.active()).toBeTruthy();
338
339 // Click second step
Leo Repp57997402021-08-18 16:37:52 +0200340 expect(cont.getElementsByTagName('li')[0].firstChild.innerText).toEqual("Named Entity");
341 cont.getElementsByTagName('li')[0].click()
Akron95abaf42018-04-26 15:33:22 +0200342
343 // Invisible menu
Leo Repp57997402021-08-18 16:37:52 +0200344 expect(cont.getElementsByTagName('li')[0]).toBeUndefined();
Akron95abaf42018-04-26 15:33:22 +0200345
346 // Inactive menu
347 expect(hint.active()).toBeFalsy();
348
349 // show with context
350 hint.show(false);
351
352 // No prefix
353 expect(hint.active().prefix()).toEqual("");
354 });
355
356
Akron1a5a5872016-09-05 20:17:14 +0200357 it('should not view main menu if context is mandatory', function () {
358 var hint = hintClass.create({
359 inputField : input
360 });
361
362 expect(hint.active()).toBeFalsy();
363
364 // Fine
365 hint.inputField().insert('der Baum corenlp/');
366 hint.show(true);
367 expect(hint.active()).toBeTruthy();
368
369 // Not analyzable
370 hint.inputField().insert('jhgjughjfhgnhfcvgnhj');
371 hint.show(true);
372 expect(hint.active()).toBeFalsy();
373
374 // Not available
375 hint.inputField().insert('jhgjughjfhgnhfcvgnhj/');
376 hint.show(true);
377 expect(hint.active()).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200378 });
Akron5746ecf2018-06-23 10:57:24 +0200379
380
381 it('should show the assistant bar on blur', function () {
382 var hint = hintClass.create({
383 inputField : input
384 });
385 // Fine
386 hint.inputField().insert('der Baum corenlp/');
387 hint.show(true);
388 expect(hint.active()).toBeTruthy();
389
390 // Blur
Akrone0789112018-08-31 14:32:04 +0200391 hint.active().hide();
Akron5746ecf2018-06-23 10:57:24 +0200392 expect(hint.active()).toBeFalsy();
393 });
Akron954c6a52020-11-10 14:26:29 +0100394
395 it('should support prefix', function () {
396 const hint = hintClass.create({
397 inputField : input
398 });
399 hint.inputField().reset();
400
401 expect(hint.active()).toBeFalsy();
402
403 // show with context
404 hint.show(false);
405
406 expect(hint.active()).toBeTruthy();
407
408 const menu = hint.active();
409
410 expect(menu.element().nodeName).toEqual('UL');
411
412 menu.limit(8);
413
414 // view
415 menu.show();
416
417 expect(menu.prefix()).toBe('');
418 expect(hint.active()).toBeTruthy();
419
420 // Type in prefix
421 hint.active().prefix("cor").show();
Leo Repp57997402021-08-18 16:37:52 +0200422 expect(hint.active()._prefix.value()).toBe("cor");
Akron954c6a52020-11-10 14:26:29 +0100423 expect(hint.active().prefix()).toEqual("cor");
Akron954c6a52020-11-10 14:26:29 +0100424 expect(input.value).toEqual("");
Leo Repp57997402021-08-18 16:37:52 +0200425 expect(hint.active()._prefix["isSelectable"]).not.toBeNull();
426 expect(hint._menuCollection['-']._prefix["isSelectable"]).not.toBeNull();
427 expect(hint.active()._prefix).toBe(hint._menuCollection['-']._prefix);
428 expect(hint.active()._prefix.element()).toBe(hint._menuCollection['-']._prefix.element());
Akron954c6a52020-11-10 14:26:29 +0100429 hint.active()._prefix.element().click();
Leo Repp57997402021-08-18 16:37:52 +0200430
431
Akron954c6a52020-11-10 14:26:29 +0100432 expect(input.value).toEqual("cor");
433 expect(hint.active()).toBeFalsy();
434
435 // view
436 menu.show();
437 expect(menu.prefix()).toBe('');
438
439 });
440
Leo Repp57997402021-08-18 16:37:52 +0200441
Akron65c74352016-09-02 17:23:39 +0200442
Akron02360e42016-06-07 13:41:12 +0200443 xit('should remove all menus on escape');
Nils Diewald19ccee92014-12-08 11:30:08 +0000444 });
445
Akron65c74352016-09-02 17:23:39 +0200446
Nils Diewald7c8ced22015-04-15 19:21:00 +0000447 describe('KorAP.HintMenuItem', function () {
Akronfac16472018-07-26 16:47:21 +0200448 beforeAll(beforeAllFunc);
449 afterAll(afterAllFunc);
450
Nils Diewald7c8ced22015-04-15 19:21:00 +0000451 it('should be initializable', function () {
452 expect(
Akronfac16472018-07-26 16:47:21 +0200453 function() { menuItemClass.create([]) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000454 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000455
Nils Diewald7c8ced22015-04-15 19:21:00 +0000456 expect(
Akronfac16472018-07-26 16:47:21 +0200457 function() { menuItemClass.create(['CoreNLP']) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000458 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000459
Nils Diewald7c8ced22015-04-15 19:21:00 +0000460 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
461 expect(menuItem.name()).toEqual('CoreNLP');
462 expect(menuItem.action()).toEqual('corenlp/');
463 expect(menuItem.desc()).toBeUndefined();
Nils Diewald19ccee92014-12-08 11:30:08 +0000464
Nils Diewald7c8ced22015-04-15 19:21:00 +0000465 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200466 ['CoreNLP', 'corenlp/', 'It\'s funny']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000467 );
468 expect(menuItem.name()).toEqual('CoreNLP');
469 expect(menuItem.action()).toEqual('corenlp/');
470 expect(menuItem.desc()).not.toBeUndefined();
471 expect(menuItem.desc()).toEqual('It\'s funny');
472 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000473
Akronfac16472018-07-26 16:47:21 +0200474
Nils Diewald7c8ced22015-04-15 19:21:00 +0000475 it('should have an element', function () {
476 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
477 expect(menuItem.element()).not.toBe(undefined);
478 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000479
Nils Diewald7c8ced22015-04-15 19:21:00 +0000480 var title = menuItem.element().firstChild;
481 expect(title.nodeName).toEqual("SPAN");
482 expect(title.firstChild.nodeType).toEqual(3);
483 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
484 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
485 expect(menuItem.element().childNodes[1]).toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000486
Nils Diewald7c8ced22015-04-15 19:21:00 +0000487 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200488 ['CoreNLP', 'corenlp/', 'my DescRiption']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000489 );
490 expect(menuItem.element()).not.toBe(undefined);
491 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000492
Nils Diewald7c8ced22015-04-15 19:21:00 +0000493 title = menuItem.element().firstChild;
494 expect(title.nodeName).toEqual("SPAN");
495 expect(title.firstChild.nodeType).toEqual(3); // TextNode
496 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
Nils Diewald19ccee92014-12-08 11:30:08 +0000497
Nils Diewald7c8ced22015-04-15 19:21:00 +0000498 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
499 expect(menuItem.element().childNodes[1]).not.toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000500
Nils Diewald7c8ced22015-04-15 19:21:00 +0000501 var desc = menuItem.element().lastChild;
502 expect(desc.nodeName).toEqual("SPAN");
503 expect(desc.firstChild.nodeType).toEqual(3); // TextNode
504 expect(desc.firstChild.nodeValue).toEqual("my DescRiption");
505 });
Nils Diewald1c546922015-04-13 01:56:19 +0000506
Nils Diewald19ccee92014-12-08 11:30:08 +0000507
Nils Diewald7c8ced22015-04-15 19:21:00 +0000508 it('should be activatable and deactivateable by class', function () {
509 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
510 expect(menuItem.active()).toBe(false);
511 expect(menuItem.element().getAttribute("class")).toBe(null);
512 menuItem.active(true);
513 expect(menuItem.active()).toBe(true);
514 expect(menuItem.element().getAttribute("class")).toEqual("active");
515 menuItem.active(false); // Is active
516 expect(menuItem.active()).toBe(false);
517 expect(menuItem.element().getAttribute("class")).toEqual("");
518 menuItem.active(true);
519 expect(menuItem.active()).toBe(true);
520 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000521
Nils Diewald7c8ced22015-04-15 19:21:00 +0000522 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
523 expect(menuItem.active()).toBe(false);
524 expect(menuItem.element().getAttribute("class")).toBe(null);
525 menuItem.active(false); // Is not active
526 expect(menuItem.active()).toBe(false);
527 expect(menuItem.element().getAttribute("class")).toBe(null);
528 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000529
Akronfac16472018-07-26 16:47:21 +0200530
Nils Diewald7c8ced22015-04-15 19:21:00 +0000531 it('should be set to boundary', function () {
532 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
533 expect(menuItem.active()).toBe(false);
534 expect(menuItem.element().getAttribute("class")).toBe(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000535
Nils Diewald7c8ced22015-04-15 19:21:00 +0000536 // Set active
537 menuItem.active(true);
538 expect(menuItem.active()).toBe(true);
539 expect(menuItem.noMore()).toBe(false);
540 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000541
Nils Diewald7c8ced22015-04-15 19:21:00 +0000542 // Set no more
543 menuItem.noMore(true);
544 expect(menuItem.active()).toBe(true);
545 expect(menuItem.noMore()).toBe(true);
546 expect(menuItem.element().getAttribute("class")).toEqual("active no-more");
Nils Diewald1c546922015-04-13 01:56:19 +0000547
Nils Diewald7c8ced22015-04-15 19:21:00 +0000548 // No no more
549 menuItem.noMore(false);
550 expect(menuItem.active()).toBe(true);
551 expect(menuItem.noMore()).toBe(false);
552 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald1c546922015-04-13 01:56:19 +0000553
Nils Diewald7c8ced22015-04-15 19:21:00 +0000554 // Set no more, deactivate
555 menuItem.noMore(true);
556 menuItem.active(false);
557 expect(menuItem.active()).toBe(false);
558 expect(menuItem.noMore()).toBe(true);
559 expect(menuItem.element().getAttribute("class")).toEqual("no-more");
Nils Diewald19ccee92014-12-08 11:30:08 +0000560
Nils Diewald7c8ced22015-04-15 19:21:00 +0000561 // Set active
562 menuItem.active(true);
563 expect(menuItem.active()).toBe(true);
564 expect(menuItem.noMore()).toBe(true);
565 expect(menuItem.element().getAttribute("class")).toEqual("no-more active");
566 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000567
Akronfac16472018-07-26 16:47:21 +0200568
Nils Diewald7c8ced22015-04-15 19:21:00 +0000569 it('should be highlightable', function () {
570 // Highlight in the middle
571 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
572 menuItem.highlight("ren");
573 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000574
Nils Diewald7c8ced22015-04-15 19:21:00 +0000575 menuItem.lowlight();
576 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000577
Nils Diewald7c8ced22015-04-15 19:21:00 +0000578 // Starting highlight
579 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
580 menuItem.highlight("cor");
581 expect(menuItem.element().innerHTML).toEqual("<span><mark>Cor</mark>eNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000582
Nils Diewald7c8ced22015-04-15 19:21:00 +0000583 menuItem.lowlight();
584 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
585
586 // Starting highlight - short
587 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
588 menuItem.highlight("c");
589 expect(menuItem.element().innerHTML).toEqual("<span><mark>C</mark>oreNLP</span>");
590
591 menuItem.lowlight();
592 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
593
594 // Highlight at the end
595 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
596 menuItem.highlight("nlp");
597 expect(menuItem.element().innerHTML).toEqual("<span>Core<mark>NLP</mark></span>");
598
599 menuItem.lowlight();
600 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
601
602 // Highlight at the end - short
603 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
604 menuItem.highlight("p");
605 expect(menuItem.element().innerHTML).toEqual("<span>CoreNL<mark>P</mark></span>");
606
607 menuItem.lowlight();
608 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
609
610 // No highlight
611 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
612 menuItem.highlight("xp");
613 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
614
615 menuItem.lowlight();
616 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
617
618 // Highlight in the middle - first
619 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
620
621 menuItem.highlight("ren");
622 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span><span class=\"desc\">This is my Example</span>");
623
624 menuItem.lowlight();
625 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
626
627 // Highlight in the middle - second
628 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
629 menuItem.highlight("ampl");
630 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Ex<mark>ampl</mark>e</span>');
631
632 menuItem.lowlight();
633 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
634
635 // Highlight in the middle - both
636 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
637
638 menuItem.highlight("e");
639 expect(menuItem.element().innerHTML).toEqual('<span>Cor<mark>e</mark>NLP</span><span class="desc">This is my <mark>E</mark>xampl<mark>e</mark></span>');
640
641 menuItem.lowlight();
642 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
643
644 // Highlight in the end - second
645 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
646 menuItem.highlight("le");
647 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Examp<mark>le</mark></span>");
648
649 menuItem.lowlight();
650 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
651
652 // Highlight at the beginning - second
653 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
654 menuItem.highlight("this");
655 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\"><mark>This</mark> is my Example</span>");
656
657 menuItem.lowlight();
658 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
659 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000660 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000661
Akronfac16472018-07-26 16:47:21 +0200662
Nils Diewald7c8ced22015-04-15 19:21:00 +0000663 describe('KorAP.HintMenu', function () {
Akronfac16472018-07-26 16:47:21 +0200664 beforeAll(beforeAllFunc);
665 afterAll(afterAllFunc);
666
Nils Diewald7c8ced22015-04-15 19:21:00 +0000667 var list = [
668 ["Constituency", "c=", "Example 1"],
669 ["Lemma", "l="],
670 ["Morphology", "m=", "Example 2"],
671 ["Part-of-Speech", "p="],
672 ["Syntax", "syn="]
673 ];
Nils Diewald19ccee92014-12-08 11:30:08 +0000674
Nils Diewald7c8ced22015-04-15 19:21:00 +0000675 it('should be initializable', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000676 var menu = menuClass.create(null, "cnx/", list);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000677 expect(menu.element().nodeName).toEqual('UL');
Akron65c74352016-09-02 17:23:39 +0200678 // expect(menu.element().style.opacity).toEqual("0");
Nils Diewald19ccee92014-12-08 11:30:08 +0000679
Nils Diewald7c8ced22015-04-15 19:21:00 +0000680 menu.limit(8);
Nils Diewald19ccee92014-12-08 11:30:08 +0000681
Nils Diewald7c8ced22015-04-15 19:21:00 +0000682 // view
683 menu.show();
Akron6ed13992016-05-23 18:06:05 +0200684 expect(menu.prefix()).toBe('');
Nils Diewald19ccee92014-12-08 11:30:08 +0000685
Nils Diewald7c8ced22015-04-15 19:21:00 +0000686 // First element in list
687 expect(menu.item(0).active()).toBe(true);
688 expect(menu.item(0).noMore()).toBe(true);
689
690 // Middle element in list
691 expect(menu.item(2).active()).toBe(false);
692 expect(menu.item(2).noMore()).toBe(false);
Nils Diewald19ccee92014-12-08 11:30:08 +0000693
Nils Diewald7c8ced22015-04-15 19:21:00 +0000694 // Last element in list
695 expect(menu.item(menu.length() - 1).active()).toBe(false);
696 expect(menu.item(menu.length() - 1).noMore()).toBe(true);
Akron6ed13992016-05-23 18:06:05 +0200697
698 expect(menu.shownItem(0).active()).toBeTruthy();
699 expect(menu.shownItem(0).lcField()).toEqual(' constituency example 1');
700 expect(menu.shownItem(1).lcField()).toEqual(' lemma');
701 expect(menu.shownItem(2).lcField()).toEqual(' morphology example 2');
702
703 menu.next();
704 expect(menu.shownItem(1).active()).toBeTruthy();
705
706 menu.next();
707 expect(menu.shownItem(2).active()).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000708 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000709 });
710});