blob: 3e95e55f5735c42a746dfe37505e2114b2ba11ce [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();
275
276 expect(cont.getElementsByTagName('div').length).toBe(1);
277 expect(cont.getElementsByTagName('ul').length).toBe(0);
278 expect(cont.firstChild).toEqual(cont.firstChild);
279
280 // Show menu, if a relevant context exists
281 // There is a menu for corenlp/
Akron02360e42016-06-07 13:41:12 +0200282 hint.show(false);
283
Akron8eaeb2e2016-08-29 18:26:28 +0200284 expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1);
285 expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(3);
Akron65c74352016-09-02 17:23:39 +0200286
Akron8eaeb2e2016-08-29 18:26:28 +0200287 // Hide the menu and focus on the input
288 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200289
290 expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(1);
291 expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(0);
Akronee9ef4a2016-06-03 12:50:08 +0200292
Akron02360e42016-06-07 13:41:12 +0200293 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200294
Akronee9ef4a2016-06-03 12:50:08 +0200295 hint.inputField().insert(' hhhh');
Akron65c74352016-09-02 17:23:39 +0200296
Akronee9ef4a2016-06-03 12:50:08 +0200297 // show with context
Akron02360e42016-06-07 13:41:12 +0200298 hint.show(false);
Akron65c74352016-09-02 17:23:39 +0200299
Akronee9ef4a2016-06-03 12:50:08 +0200300 expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(4);
301 expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1);
Akron02360e42016-06-07 13:41:12 +0200302 expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(2);
303
Akronee9ef4a2016-06-03 12:50:08 +0200304 hint.unshow();
305 hint.inputField().insert(' aaaa/');
306
307 // show with context
308 hint.show(true);
309
Akron1a5a5872016-09-05 20:17:14 +0200310 expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(1);
311 expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(0);
312 });
313
Akron95abaf42018-04-26 15:33:22 +0200314
315 it('should open menus depending on the context', function () {
316 var hint = hintClass.create({
317 inputField : input
318 });
319 hint.inputField().reset();
320
321 expect(hint.active()).toBeFalsy();
322
323 // show with context
324 hint.show(false);
325
326 expect(hint.active()).toBeTruthy();
327
328 expect(hint.inputField().container().getElementsByTagName('li')[0].firstChild.innerText).toEqual("Base Annotation");
329
330 // Type in prefix
331 hint.active().prefix("cor").show();
332 expect(hint.active().prefix()).toEqual("cor");
333
334 // Click first step
335 expect(hint.inputField().container().getElementsByTagName('li')[0].firstChild.firstChild.innerText).toEqual("Cor");
336 hint.inputField().container().getElementsByTagName('li')[0].click();
337
338 expect(hint.active()).toBeTruthy();
339
340 // Click second step
341 expect(hint.inputField().container().getElementsByTagName('li')[0].firstChild.innerText).toEqual("Named Entity");
342 hint.inputField().container().getElementsByTagName('li')[0].click()
343
344 // Invisible menu
345 expect(hint.inputField().container().getElementsByTagName('li')[0]).toBeUndefined();
346
347 // Inactive menu
348 expect(hint.active()).toBeFalsy();
349
350 // show with context
351 hint.show(false);
352
353 // No prefix
354 expect(hint.active().prefix()).toEqual("");
355 });
356
357
Akron1a5a5872016-09-05 20:17:14 +0200358 it('should not view main menu if context is mandatory', function () {
359 var hint = hintClass.create({
360 inputField : input
361 });
362
363 expect(hint.active()).toBeFalsy();
364
365 // Fine
366 hint.inputField().insert('der Baum corenlp/');
367 hint.show(true);
368 expect(hint.active()).toBeTruthy();
369
370 // Not analyzable
371 hint.inputField().insert('jhgjughjfhgnhfcvgnhj');
372 hint.show(true);
373 expect(hint.active()).toBeFalsy();
374
375 // Not available
376 hint.inputField().insert('jhgjughjfhgnhfcvgnhj/');
377 hint.show(true);
378 expect(hint.active()).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200379 });
Akron5746ecf2018-06-23 10:57:24 +0200380
381
382 it('should show the assistant bar on blur', function () {
383 var hint = hintClass.create({
384 inputField : input
385 });
386 // Fine
387 hint.inputField().insert('der Baum corenlp/');
388 hint.show(true);
389 expect(hint.active()).toBeTruthy();
390
391 // Blur
Akrone0789112018-08-31 14:32:04 +0200392 hint.active().hide();
Akron5746ecf2018-06-23 10:57:24 +0200393 expect(hint.active()).toBeFalsy();
394 });
Akron954c6a52020-11-10 14:26:29 +0100395
396 it('should support prefix', function () {
397 const hint = hintClass.create({
398 inputField : input
399 });
400 hint.inputField().reset();
401
402 expect(hint.active()).toBeFalsy();
403
404 // show with context
405 hint.show(false);
406
407 expect(hint.active()).toBeTruthy();
408
409 const menu = hint.active();
410
411 expect(menu.element().nodeName).toEqual('UL');
412
413 menu.limit(8);
414
415 // view
416 menu.show();
417
418 expect(menu.prefix()).toBe('');
419 expect(hint.active()).toBeTruthy();
420
421 // Type in prefix
422 hint.active().prefix("cor").show();
423 expect(hint.active().prefix()).toEqual("cor");
424
425 expect(input.value).toEqual("");
426 hint.active()._prefix.element().click();
427 expect(input.value).toEqual("cor");
428 expect(hint.active()).toBeFalsy();
429
430 // view
431 menu.show();
432 expect(menu.prefix()).toBe('');
433
434 });
435
Akron65c74352016-09-02 17:23:39 +0200436
Akron02360e42016-06-07 13:41:12 +0200437 xit('should remove all menus on escape');
Nils Diewald19ccee92014-12-08 11:30:08 +0000438 });
439
Akron65c74352016-09-02 17:23:39 +0200440
Nils Diewald7c8ced22015-04-15 19:21:00 +0000441 describe('KorAP.HintMenuItem', function () {
Akronfac16472018-07-26 16:47:21 +0200442 beforeAll(beforeAllFunc);
443 afterAll(afterAllFunc);
444
Nils Diewald7c8ced22015-04-15 19:21:00 +0000445 it('should be initializable', function () {
446 expect(
Akronfac16472018-07-26 16:47:21 +0200447 function() { menuItemClass.create([]) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000448 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000449
Nils Diewald7c8ced22015-04-15 19:21:00 +0000450 expect(
Akronfac16472018-07-26 16:47:21 +0200451 function() { menuItemClass.create(['CoreNLP']) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000452 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000453
Nils Diewald7c8ced22015-04-15 19:21:00 +0000454 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
455 expect(menuItem.name()).toEqual('CoreNLP');
456 expect(menuItem.action()).toEqual('corenlp/');
457 expect(menuItem.desc()).toBeUndefined();
Nils Diewald19ccee92014-12-08 11:30:08 +0000458
Nils Diewald7c8ced22015-04-15 19:21:00 +0000459 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200460 ['CoreNLP', 'corenlp/', 'It\'s funny']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000461 );
462 expect(menuItem.name()).toEqual('CoreNLP');
463 expect(menuItem.action()).toEqual('corenlp/');
464 expect(menuItem.desc()).not.toBeUndefined();
465 expect(menuItem.desc()).toEqual('It\'s funny');
466 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000467
Akronfac16472018-07-26 16:47:21 +0200468
Nils Diewald7c8ced22015-04-15 19:21:00 +0000469 it('should have an element', function () {
470 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
471 expect(menuItem.element()).not.toBe(undefined);
472 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000473
Nils Diewald7c8ced22015-04-15 19:21:00 +0000474 var title = menuItem.element().firstChild;
475 expect(title.nodeName).toEqual("SPAN");
476 expect(title.firstChild.nodeType).toEqual(3);
477 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
478 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
479 expect(menuItem.element().childNodes[1]).toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000480
Nils Diewald7c8ced22015-04-15 19:21:00 +0000481 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200482 ['CoreNLP', 'corenlp/', 'my DescRiption']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000483 );
484 expect(menuItem.element()).not.toBe(undefined);
485 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000486
Nils Diewald7c8ced22015-04-15 19:21:00 +0000487 title = menuItem.element().firstChild;
488 expect(title.nodeName).toEqual("SPAN");
489 expect(title.firstChild.nodeType).toEqual(3); // TextNode
490 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
Nils Diewald19ccee92014-12-08 11:30:08 +0000491
Nils Diewald7c8ced22015-04-15 19:21:00 +0000492 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
493 expect(menuItem.element().childNodes[1]).not.toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000494
Nils Diewald7c8ced22015-04-15 19:21:00 +0000495 var desc = menuItem.element().lastChild;
496 expect(desc.nodeName).toEqual("SPAN");
497 expect(desc.firstChild.nodeType).toEqual(3); // TextNode
498 expect(desc.firstChild.nodeValue).toEqual("my DescRiption");
499 });
Nils Diewald1c546922015-04-13 01:56:19 +0000500
Nils Diewald19ccee92014-12-08 11:30:08 +0000501
Nils Diewald7c8ced22015-04-15 19:21:00 +0000502 it('should be activatable and deactivateable by class', function () {
503 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
504 expect(menuItem.active()).toBe(false);
505 expect(menuItem.element().getAttribute("class")).toBe(null);
506 menuItem.active(true);
507 expect(menuItem.active()).toBe(true);
508 expect(menuItem.element().getAttribute("class")).toEqual("active");
509 menuItem.active(false); // Is active
510 expect(menuItem.active()).toBe(false);
511 expect(menuItem.element().getAttribute("class")).toEqual("");
512 menuItem.active(true);
513 expect(menuItem.active()).toBe(true);
514 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000515
Nils Diewald7c8ced22015-04-15 19:21:00 +0000516 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
517 expect(menuItem.active()).toBe(false);
518 expect(menuItem.element().getAttribute("class")).toBe(null);
519 menuItem.active(false); // Is not active
520 expect(menuItem.active()).toBe(false);
521 expect(menuItem.element().getAttribute("class")).toBe(null);
522 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000523
Akronfac16472018-07-26 16:47:21 +0200524
Nils Diewald7c8ced22015-04-15 19:21:00 +0000525 it('should be set to boundary', function () {
526 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
527 expect(menuItem.active()).toBe(false);
528 expect(menuItem.element().getAttribute("class")).toBe(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000529
Nils Diewald7c8ced22015-04-15 19:21:00 +0000530 // Set active
531 menuItem.active(true);
532 expect(menuItem.active()).toBe(true);
533 expect(menuItem.noMore()).toBe(false);
534 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000535
Nils Diewald7c8ced22015-04-15 19:21:00 +0000536 // Set no more
537 menuItem.noMore(true);
538 expect(menuItem.active()).toBe(true);
539 expect(menuItem.noMore()).toBe(true);
540 expect(menuItem.element().getAttribute("class")).toEqual("active no-more");
Nils Diewald1c546922015-04-13 01:56:19 +0000541
Nils Diewald7c8ced22015-04-15 19:21:00 +0000542 // No no more
543 menuItem.noMore(false);
544 expect(menuItem.active()).toBe(true);
545 expect(menuItem.noMore()).toBe(false);
546 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald1c546922015-04-13 01:56:19 +0000547
Nils Diewald7c8ced22015-04-15 19:21:00 +0000548 // Set no more, deactivate
549 menuItem.noMore(true);
550 menuItem.active(false);
551 expect(menuItem.active()).toBe(false);
552 expect(menuItem.noMore()).toBe(true);
553 expect(menuItem.element().getAttribute("class")).toEqual("no-more");
Nils Diewald19ccee92014-12-08 11:30:08 +0000554
Nils Diewald7c8ced22015-04-15 19:21:00 +0000555 // Set active
556 menuItem.active(true);
557 expect(menuItem.active()).toBe(true);
558 expect(menuItem.noMore()).toBe(true);
559 expect(menuItem.element().getAttribute("class")).toEqual("no-more active");
560 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000561
Akronfac16472018-07-26 16:47:21 +0200562
Nils Diewald7c8ced22015-04-15 19:21:00 +0000563 it('should be highlightable', function () {
564 // Highlight in the middle
565 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
566 menuItem.highlight("ren");
567 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000568
Nils Diewald7c8ced22015-04-15 19:21:00 +0000569 menuItem.lowlight();
570 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000571
Nils Diewald7c8ced22015-04-15 19:21:00 +0000572 // Starting highlight
573 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
574 menuItem.highlight("cor");
575 expect(menuItem.element().innerHTML).toEqual("<span><mark>Cor</mark>eNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000576
Nils Diewald7c8ced22015-04-15 19:21:00 +0000577 menuItem.lowlight();
578 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
579
580 // Starting highlight - short
581 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
582 menuItem.highlight("c");
583 expect(menuItem.element().innerHTML).toEqual("<span><mark>C</mark>oreNLP</span>");
584
585 menuItem.lowlight();
586 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
587
588 // Highlight at the end
589 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
590 menuItem.highlight("nlp");
591 expect(menuItem.element().innerHTML).toEqual("<span>Core<mark>NLP</mark></span>");
592
593 menuItem.lowlight();
594 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
595
596 // Highlight at the end - short
597 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
598 menuItem.highlight("p");
599 expect(menuItem.element().innerHTML).toEqual("<span>CoreNL<mark>P</mark></span>");
600
601 menuItem.lowlight();
602 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
603
604 // No highlight
605 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
606 menuItem.highlight("xp");
607 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
608
609 menuItem.lowlight();
610 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
611
612 // Highlight in the middle - first
613 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
614
615 menuItem.highlight("ren");
616 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span><span class=\"desc\">This is my Example</span>");
617
618 menuItem.lowlight();
619 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
620
621 // Highlight in the middle - second
622 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
623 menuItem.highlight("ampl");
624 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Ex<mark>ampl</mark>e</span>');
625
626 menuItem.lowlight();
627 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
628
629 // Highlight in the middle - both
630 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
631
632 menuItem.highlight("e");
633 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>');
634
635 menuItem.lowlight();
636 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
637
638 // Highlight in the end - second
639 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
640 menuItem.highlight("le");
641 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Examp<mark>le</mark></span>");
642
643 menuItem.lowlight();
644 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
645
646 // Highlight at the beginning - second
647 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
648 menuItem.highlight("this");
649 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\"><mark>This</mark> is my Example</span>");
650
651 menuItem.lowlight();
652 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
653 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000654 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000655
Akronfac16472018-07-26 16:47:21 +0200656
Nils Diewald7c8ced22015-04-15 19:21:00 +0000657 describe('KorAP.HintMenu', function () {
Akronfac16472018-07-26 16:47:21 +0200658 beforeAll(beforeAllFunc);
659 afterAll(afterAllFunc);
660
Nils Diewald7c8ced22015-04-15 19:21:00 +0000661 var list = [
662 ["Constituency", "c=", "Example 1"],
663 ["Lemma", "l="],
664 ["Morphology", "m=", "Example 2"],
665 ["Part-of-Speech", "p="],
666 ["Syntax", "syn="]
667 ];
Nils Diewald19ccee92014-12-08 11:30:08 +0000668
Nils Diewald7c8ced22015-04-15 19:21:00 +0000669 it('should be initializable', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000670 var menu = menuClass.create(null, "cnx/", list);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000671 expect(menu.element().nodeName).toEqual('UL');
Akron65c74352016-09-02 17:23:39 +0200672 // expect(menu.element().style.opacity).toEqual("0");
Nils Diewald19ccee92014-12-08 11:30:08 +0000673
Nils Diewald7c8ced22015-04-15 19:21:00 +0000674 menu.limit(8);
Nils Diewald19ccee92014-12-08 11:30:08 +0000675
Nils Diewald7c8ced22015-04-15 19:21:00 +0000676 // view
677 menu.show();
Akron6ed13992016-05-23 18:06:05 +0200678 expect(menu.prefix()).toBe('');
Nils Diewald19ccee92014-12-08 11:30:08 +0000679
Nils Diewald7c8ced22015-04-15 19:21:00 +0000680 // First element in list
681 expect(menu.item(0).active()).toBe(true);
682 expect(menu.item(0).noMore()).toBe(true);
683
684 // Middle element in list
685 expect(menu.item(2).active()).toBe(false);
686 expect(menu.item(2).noMore()).toBe(false);
Nils Diewald19ccee92014-12-08 11:30:08 +0000687
Nils Diewald7c8ced22015-04-15 19:21:00 +0000688 // Last element in list
689 expect(menu.item(menu.length() - 1).active()).toBe(false);
690 expect(menu.item(menu.length() - 1).noMore()).toBe(true);
Akron6ed13992016-05-23 18:06:05 +0200691
692 expect(menu.shownItem(0).active()).toBeTruthy();
693 expect(menu.shownItem(0).lcField()).toEqual(' constituency example 1');
694 expect(menu.shownItem(1).lcField()).toEqual(' lemma');
695 expect(menu.shownItem(2).lcField()).toEqual(' morphology example 2');
696
697 menu.next();
698 expect(menu.shownItem(1).active()).toBeTruthy();
699
700 menu.next();
701 expect(menu.shownItem(2).active()).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000702 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000703 });
704});