blob: 79726f3c0e7471464c818080b13e4ff54e12427f [file] [log] [blame]
Akronfac16472018-07-26 16:47:21 +02001define(['hint', 'hint/input', 'hint/contextanalyzer', 'hint/menu', 'hint/item'], function (hintClass, inputClass, contextClass, menuClass, menuItemClass) {
Nils Diewald19ccee92014-12-08 11:30:08 +00002
Akronfac16472018-07-26 16:47:21 +02003 function emitKeyboardEvent (element, type, keyCode) {
4 // event type : keydown, keyup, keypress
5 // http://stackoverflow.com/questions/596481/simulate-javascript-key-events
6 // http://stackoverflow.com/questions/961532/firing-a-keyboard-event-in-javascript
7 var keyboardEvent = document.createEvent("KeyboardEvent");
8 var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ?
9 "initKeyboardEvent" : "initKeyEvent";
10 keyboardEvent[initMethod](
11 type,
12 true, // bubbles
13 true, // cancelable
14 window, // viewArg: should be window
15 false, // ctrlKeyArg
16 false, // altKeyArg
17 false, // shiftKeyArg
18 false, // metaKeyArg
19 keyCode, // keyCodeArg : unsigned long the virtual key code, else 0
20 0 // charCodeArgs : unsigned long the Unicode character
21 // associated with the depressed key, else 0
22 );
23 element.dispatchEvent(keyboardEvent);
24 };
Nils Diewald19ccee92014-12-08 11:30:08 +000025
Akronfac16472018-07-26 16:47:21 +020026 var afterAllFunc = function () {
27 try {
28 var mirrors = document.querySelectorAll(".hint.mirror");
29 for (var i in mirrors) {
30 mirrors[i].parentNode.removeChild(mirrors[i])
31 };
32 }
33 catch (e) {};
34
35 var body = document.body;
36 for (var i in body.children) {
37 if (body.children[i].nodeType && body.children[i].nodeType === 1) {
38 if (!body.children[i].classList.contains("jasmine_html-reporter")) {
39 body.removeChild(body.children[i]);
40 };
41 };
42 };
43 KorAP.API.getMatchInfo = undefined;
44 KorAP.context = undefined;
45 // KorAP.annotationHelper = undefined;
46 };
47
48 var beforeAllFunc = function () {
49 KorAP.annotationHelper = KorAP.annotationHelper || {};
50 KorAP.annotationHelper["-"] = [
51 ["Base Annotation", "base/s=", "Structure"],
52 ["CoreNLP", "corenlp/", "Constituency, Named Entities, Part-of-Speech"]
53 ];
54 KorAP.annotationHelper["corenlp/"] = [
55 ["Named Entity", "ne=" , "Combined"],
56 ["Named Entity", "ne_dewac_175m_600=" , "ne_dewac_175m_600"],
57 ["Named Entity", "ne_hgc_175m_600=", "ne_hgc_175m_600"]
58 ];
59 };
Nils Diewald5c5a7472015-04-02 22:13:38 +000060
Nils Diewald7c8ced22015-04-15 19:21:00 +000061 describe('KorAP.InputField', function () {
Akronfac16472018-07-26 16:47:21 +020062 beforeAll(beforeAllFunc);
63 afterAll(afterAllFunc);
Akron5336fd42020-10-09 18:13:51 +020064 let input;
Nils Diewald1c546922015-04-13 01:56:19 +000065
Nils Diewald7c8ced22015-04-15 19:21:00 +000066 beforeEach(function () {
67 input = document.createElement("input");
68 input.setAttribute('type', "text");
69 input.setAttribute("value", "abcdefghijklmno");
70 input.style.position = 'absolute';
71 document.getElementsByTagName('body')[0].appendChild(input);
72 input.style.top = "20px";
73 input.style.left = "30px";
74 input.focus();
75 input.selectionStart = 5;
Nils Diewald5c5a7472015-04-02 22:13:38 +000076 });
77
Nils Diewald7c8ced22015-04-15 19:21:00 +000078 afterEach(function () {
79 document.getElementsByTagName("body")[0].removeChild(
Akron95abaf42018-04-26 15:33:22 +020080 input
Nils Diewald7c8ced22015-04-15 19:21:00 +000081 );
82 });
Nils Diewald5c5a7472015-04-02 22:13:38 +000083
Nils Diewald7c8ced22015-04-15 19:21:00 +000084 it('should be initializable', function () {
85 // Supports: context, searchField
86 var inputField = inputClass.create(input);
87 expect(inputField._element).not.toBe(undefined);
88 });
Nils Diewald19ccee92014-12-08 11:30:08 +000089
Nils Diewald7c8ced22015-04-15 19:21:00 +000090 it('should have text', function () {
91 expect(input.value).toEqual('abcdefghijklmno');
92 var inputField = inputClass.create(input);
Nils Diewald19ccee92014-12-08 11:30:08 +000093
Nils Diewald7c8ced22015-04-15 19:21:00 +000094 expect(inputField.value()).toEqual("abcdefghijklmno");
Nils Diewald19ccee92014-12-08 11:30:08 +000095
Nils Diewald7c8ced22015-04-15 19:21:00 +000096 expect(input.selectionStart).toEqual(5);
97 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +000098 expect(inputField._split()[0]).toEqual("abcde");
99 expect(inputField._split()[1]).toEqual("fghijklmno");
Nils Diewald19ccee92014-12-08 11:30:08 +0000100
Nils Diewald7c8ced22015-04-15 19:21:00 +0000101 inputField.insert("xyz");
102 expect(inputField.value()).toEqual('abcdexyzfghijklmno');
Nils Diewald7148c6f2015-05-04 15:07:53 +0000103 expect(inputField._split()[0]).toEqual("abcdexyz");
104 expect(inputField._split()[1]).toEqual("fghijklmno");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000105 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000106
Nils Diewald7c8ced22015-04-15 19:21:00 +0000107 it('should be correctly positioned', function () {
108 expect(input.value).toEqual('abcdefghijklmno');
109 var inputField = inputClass.create(input);
110 document.getElementsByTagName("body")[0].appendChild(input);
111 inputField.reposition();
112 expect(input.style.left).toEqual("30px");
113 expect(inputField.mirror().style.left.match(/^(\d+)px$/)[1]).toBeGreaterThan(29);
114 expect(inputField.mirror().style.top.match(/^(\d+)px$/)[1]).toBeGreaterThan(20);
115 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000116
Nils Diewald7c8ced22015-04-15 19:21:00 +0000117 it('should have a correct context', function () {
118 expect(input.value).toEqual('abcdefghijklmno');
119 var inputField = inputClass.create(input);
120 expect(inputField.value()).toEqual("abcdefghijklmno");
121 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000122 expect(inputField._split()[0]).toEqual("abcde");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000123 expect(inputField.context()).toEqual("abcde");
124 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000125
Nils Diewald7c8ced22015-04-15 19:21:00 +0000126 /*
127 it('should be correctly triggerable', function () {
128 // https://developer.mozilla.org/samples/domref/dispatchEvent.html
129 var hint = KorAP.Hint.create({ "inputField" : input });
130 emitKeyboardEvent(hint.inputField.element, "keypress", 20);
131 });
132 */
Nils Diewald19ccee92014-12-08 11:30:08 +0000133 });
134
Nils Diewald1c546922015-04-13 01:56:19 +0000135
Nils Diewald7c8ced22015-04-15 19:21:00 +0000136 describe('KorAP.ContextAnalyzer', function () {
Akronfac16472018-07-26 16:47:21 +0200137 beforeAll(beforeAllFunc);
138 afterAll(afterAllFunc);
139
Nils Diewald7c8ced22015-04-15 19:21:00 +0000140 it('should be initializable', function () {
141 var analyzer = contextClass.create(")");
142 expect(analyzer).toBe(undefined);
143 analyzer = contextClass.create(".+?");
144 expect(analyzer).not.toBe(undefined);
145 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000146
Nils Diewald7c8ced22015-04-15 19:21:00 +0000147 it('should check correctly', function () {
Akronfac16472018-07-26 16:47:21 +0200148
149 // Intialize KorAP.context
150 hintClass.create();
151
Nils Diewald7c8ced22015-04-15 19:21:00 +0000152 analyzer = contextClass.create(KorAP.context);
153 expect(analyzer.test("cnx/]cnx/c=")).toEqual("cnx/c=");
154 expect(analyzer.test("cnx/c=")).toEqual("cnx/c=");
155 expect(analyzer.test("cnx/c=np mate/m=mood:")).toEqual("mate/m=mood:");
156 expect(analyzer.test("impcnx/")).toEqual("impcnx/");
157 expect(analyzer.test("cnx/c=npcnx/")).toEqual("npcnx/");
158 expect(analyzer.test("mate/m=degree:pos corenlp/ne_dewac_175m_600="))
Akronfac16472018-07-26 16:47:21 +0200159 .toEqual("corenlp/ne_dewac_175m_600=");
Akron113cc1a2016-01-22 21:17:57 +0100160 expect(analyzer.test("corenlp/")).toEqual("corenlp/");
161 expect(analyzer.test("corenlp/c=")).toEqual("corenlp/c=");
162 expect(analyzer.test("corenlp/c=PP-")).toEqual("corenlp/c=PP-");
163 expect(analyzer.test("corenlp/c=XY-")).toEqual("corenlp/c=XY-");
Akroncff9bac2016-01-25 21:39:38 +0100164 expect(analyzer.test("sgbr/l=")).toEqual("sgbr/l=");
165 expect(analyzer.test("sgbr/lv=")).toEqual("sgbr/lv=");
166 expect(analyzer.test("sgbr/p=")).toEqual("sgbr/p=");
Akronee9ef4a2016-06-03 12:50:08 +0200167 expect(analyzer.test("")).toEqual(undefined);
168 expect(analyzer.test("abcdecnx/")).toEqual("abcdecnx/");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000169 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000170 });
171
Nils Diewald19ccee92014-12-08 11:30:08 +0000172
Nils Diewald7c8ced22015-04-15 19:21:00 +0000173 describe('KorAP.Hint', function () {
Akronfac16472018-07-26 16:47:21 +0200174 beforeAll(beforeAllFunc);
175 afterAll(afterAllFunc);
Nils Diewald19ccee92014-12-08 11:30:08 +0000176
Nils Diewald7c8ced22015-04-15 19:21:00 +0000177 beforeEach(function () {
178 input = document.createElement("input");
179 input.setAttribute("type", "text");
180 input.setAttribute("value", "abcdefghijklmno");
181 input.style.position = 'absolute';
182 input.style.top = "20px";
183 input.style.left = "30px";
184 input.focus();
185 input.selectionStart = 5;
186 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000187
Nils Diewald7c8ced22015-04-15 19:21:00 +0000188 it('should be initializable', function () {
Akronfac16472018-07-26 16:47:21 +0200189
Nils Diewald7c8ced22015-04-15 19:21:00 +0000190 // Supports: context, searchField
191 var hint = hintClass.create({
Akronfac16472018-07-26 16:47:21 +0200192 inputField : input
Nils Diewald7c8ced22015-04-15 19:21:00 +0000193 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000194
Nils Diewald7c8ced22015-04-15 19:21:00 +0000195 expect(hint).toBeTruthy();
196 });
Akron00cd4d12016-05-31 21:01:11 +0200197
Akron1a5a5872016-09-05 20:17:14 +0200198
Akron00cd4d12016-05-31 21:01:11 +0200199 it('should alert at char pos', function () {
200 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200201 inputField : input
Akron00cd4d12016-05-31 21:01:11 +0200202 });
203
204 expect(hint.active()).toBeFalsy();
205
206 expect(hint.alert(4, 'That does not work!')).toBeTruthy();
207
208 expect(hint.active()).toBeTruthy();
209
210 var container = hint.inputField().container();
211 expect(container.firstChild.classList.contains('hint')).toBe(true);
212 expect(container.firstChild.classList.contains('alert')).toBe(true);
213 expect(container.firstChild.textContent).toEqual('That does not work!');
214 expect(hint.inputField().mirrorValue()).toEqual('abcd');
215
216 expect(hint.alert(4, 'That does not work!')).toBeFalsy();
217
218 // Update - meaning: hide alert
219 hint.update();
220
221 expect(hint.alert().active).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200222 expect(hint.active()).toBeFalsy();
Akron1a5a5872016-09-05 20:17:14 +0200223
224 // Show again
225 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
226 expect(hint.inputField().mirrorValue()).toEqual('abcde');
227 expect(hint.alert().active).toBeTruthy();
228 expect(hint.active()).toBeTruthy();
229
230 // Show menu, hide alert!
231 hint.show(false);
232 expect(hint.active()).toBeTruthy();
233 expect(hint.inputField().mirrorValue()).toEqual('abcde');
234 expect(hint.alert().active).toBeFalsy();
235
Akronfac16472018-07-26 16:47:21 +0200236 // Show again
Akron1a5a5872016-09-05 20:17:14 +0200237 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
238 expect(hint.inputField().mirrorValue()).toEqual('abcde');
239 expect(hint.alert().active).toBeTruthy();
240 expect(hint.active()).toBeTruthy();
241
242 // Show menu, hide alert!
243 hint.show(false);
Akroncae907d2018-08-20 17:22:15 +0200244
245 // PROBLEM!
Akron1a5a5872016-09-05 20:17:14 +0200246 expect(hint.active()).toBeTruthy();
247 expect(hint.inputField().mirrorValue()).toEqual('abcde');
248 expect(hint.alert().active).toBeFalsy();
Akronee9ef4a2016-06-03 12:50:08 +0200249 });
Akron00cd4d12016-05-31 21:01:11 +0200250
Akronee9ef4a2016-06-03 12:50:08 +0200251 it('should view main menu on default', function () {
252 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200253 inputField : input
Akronee9ef4a2016-06-03 12:50:08 +0200254 });
255
256 expect(hint.active()).toBeFalsy();
257
258 hint.inputField().insert('der Baum corenlp/');
Akronee9ef4a2016-06-03 12:50:08 +0200259
Akron8eaeb2e2016-08-29 18:26:28 +0200260 var cont = hint.inputField().container();
261
262 expect(cont.getElementsByTagName('div').length).toBe(1);
263 expect(cont.getElementsByTagName('ul').length).toBe(0);
264 expect(cont.firstChild).toEqual(cont.firstChild);
265
266 // Show menu, if a relevant context exists
267 // There is a menu for corenlp/
Akron02360e42016-06-07 13:41:12 +0200268 hint.show(false);
269
Akron8eaeb2e2016-08-29 18:26:28 +0200270 expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1);
271 expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(3);
Akron65c74352016-09-02 17:23:39 +0200272
Akron8eaeb2e2016-08-29 18:26:28 +0200273 // Hide the menu and focus on the input
274 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200275
276 expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(1);
277 expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(0);
Akronee9ef4a2016-06-03 12:50:08 +0200278
Akron02360e42016-06-07 13:41:12 +0200279 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200280
Akronee9ef4a2016-06-03 12:50:08 +0200281 hint.inputField().insert(' hhhh');
Akron65c74352016-09-02 17:23:39 +0200282
Akronee9ef4a2016-06-03 12:50:08 +0200283 // show with context
Akron02360e42016-06-07 13:41:12 +0200284 hint.show(false);
Akron65c74352016-09-02 17:23:39 +0200285
Akronee9ef4a2016-06-03 12:50:08 +0200286 expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(4);
287 expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1);
Akron02360e42016-06-07 13:41:12 +0200288 expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(2);
289
Akronee9ef4a2016-06-03 12:50:08 +0200290 hint.unshow();
291 hint.inputField().insert(' aaaa/');
292
293 // show with context
294 hint.show(true);
295
Akron1a5a5872016-09-05 20:17:14 +0200296 expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(1);
297 expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(0);
298 });
299
Akron95abaf42018-04-26 15:33:22 +0200300
301 it('should open menus depending on the context', function () {
302 var hint = hintClass.create({
303 inputField : input
304 });
305 hint.inputField().reset();
306
307 expect(hint.active()).toBeFalsy();
308
309 // show with context
310 hint.show(false);
311
312 expect(hint.active()).toBeTruthy();
313
314 expect(hint.inputField().container().getElementsByTagName('li')[0].firstChild.innerText).toEqual("Base Annotation");
315
316 // Type in prefix
317 hint.active().prefix("cor").show();
318 expect(hint.active().prefix()).toEqual("cor");
319
320 // Click first step
321 expect(hint.inputField().container().getElementsByTagName('li')[0].firstChild.firstChild.innerText).toEqual("Cor");
322 hint.inputField().container().getElementsByTagName('li')[0].click();
323
324 expect(hint.active()).toBeTruthy();
325
326 // Click second step
327 expect(hint.inputField().container().getElementsByTagName('li')[0].firstChild.innerText).toEqual("Named Entity");
328 hint.inputField().container().getElementsByTagName('li')[0].click()
329
330 // Invisible menu
331 expect(hint.inputField().container().getElementsByTagName('li')[0]).toBeUndefined();
332
333 // Inactive menu
334 expect(hint.active()).toBeFalsy();
335
336 // show with context
337 hint.show(false);
338
339 // No prefix
340 expect(hint.active().prefix()).toEqual("");
341 });
342
343
Akron1a5a5872016-09-05 20:17:14 +0200344 it('should not view main menu if context is mandatory', function () {
345 var hint = hintClass.create({
346 inputField : input
347 });
348
349 expect(hint.active()).toBeFalsy();
350
351 // Fine
352 hint.inputField().insert('der Baum corenlp/');
353 hint.show(true);
354 expect(hint.active()).toBeTruthy();
355
356 // Not analyzable
357 hint.inputField().insert('jhgjughjfhgnhfcvgnhj');
358 hint.show(true);
359 expect(hint.active()).toBeFalsy();
360
361 // Not available
362 hint.inputField().insert('jhgjughjfhgnhfcvgnhj/');
363 hint.show(true);
364 expect(hint.active()).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200365 });
Akron5746ecf2018-06-23 10:57:24 +0200366
367
368 it('should show the assistant bar on blur', function () {
369 var hint = hintClass.create({
370 inputField : input
371 });
372 // Fine
373 hint.inputField().insert('der Baum corenlp/');
374 hint.show(true);
375 expect(hint.active()).toBeTruthy();
376
377 // Blur
Akrone0789112018-08-31 14:32:04 +0200378 hint.active().hide();
Akron5746ecf2018-06-23 10:57:24 +0200379 expect(hint.active()).toBeFalsy();
380 });
Akron65c74352016-09-02 17:23:39 +0200381
Akron02360e42016-06-07 13:41:12 +0200382 xit('should remove all menus on escape');
Nils Diewald19ccee92014-12-08 11:30:08 +0000383 });
384
Akron65c74352016-09-02 17:23:39 +0200385
Nils Diewald7c8ced22015-04-15 19:21:00 +0000386 describe('KorAP.HintMenuItem', function () {
Akronfac16472018-07-26 16:47:21 +0200387 beforeAll(beforeAllFunc);
388 afterAll(afterAllFunc);
389
Nils Diewald7c8ced22015-04-15 19:21:00 +0000390 it('should be initializable', function () {
391 expect(
Akronfac16472018-07-26 16:47:21 +0200392 function() { menuItemClass.create([]) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000393 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000394
Nils Diewald7c8ced22015-04-15 19:21:00 +0000395 expect(
Akronfac16472018-07-26 16:47:21 +0200396 function() { menuItemClass.create(['CoreNLP']) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000397 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000398
Nils Diewald7c8ced22015-04-15 19:21:00 +0000399 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
400 expect(menuItem.name()).toEqual('CoreNLP');
401 expect(menuItem.action()).toEqual('corenlp/');
402 expect(menuItem.desc()).toBeUndefined();
Nils Diewald19ccee92014-12-08 11:30:08 +0000403
Nils Diewald7c8ced22015-04-15 19:21:00 +0000404 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200405 ['CoreNLP', 'corenlp/', 'It\'s funny']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000406 );
407 expect(menuItem.name()).toEqual('CoreNLP');
408 expect(menuItem.action()).toEqual('corenlp/');
409 expect(menuItem.desc()).not.toBeUndefined();
410 expect(menuItem.desc()).toEqual('It\'s funny');
411 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000412
Akronfac16472018-07-26 16:47:21 +0200413
Nils Diewald7c8ced22015-04-15 19:21:00 +0000414 it('should have an element', function () {
415 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
416 expect(menuItem.element()).not.toBe(undefined);
417 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000418
Nils Diewald7c8ced22015-04-15 19:21:00 +0000419 var title = menuItem.element().firstChild;
420 expect(title.nodeName).toEqual("SPAN");
421 expect(title.firstChild.nodeType).toEqual(3);
422 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
423 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
424 expect(menuItem.element().childNodes[1]).toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000425
Nils Diewald7c8ced22015-04-15 19:21:00 +0000426 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200427 ['CoreNLP', 'corenlp/', 'my DescRiption']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000428 );
429 expect(menuItem.element()).not.toBe(undefined);
430 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000431
Nils Diewald7c8ced22015-04-15 19:21:00 +0000432 title = menuItem.element().firstChild;
433 expect(title.nodeName).toEqual("SPAN");
434 expect(title.firstChild.nodeType).toEqual(3); // TextNode
435 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
Nils Diewald19ccee92014-12-08 11:30:08 +0000436
Nils Diewald7c8ced22015-04-15 19:21:00 +0000437 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
438 expect(menuItem.element().childNodes[1]).not.toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000439
Nils Diewald7c8ced22015-04-15 19:21:00 +0000440 var desc = menuItem.element().lastChild;
441 expect(desc.nodeName).toEqual("SPAN");
442 expect(desc.firstChild.nodeType).toEqual(3); // TextNode
443 expect(desc.firstChild.nodeValue).toEqual("my DescRiption");
444 });
Nils Diewald1c546922015-04-13 01:56:19 +0000445
Nils Diewald19ccee92014-12-08 11:30:08 +0000446
Nils Diewald7c8ced22015-04-15 19:21:00 +0000447 it('should be activatable and deactivateable by class', function () {
448 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
449 expect(menuItem.active()).toBe(false);
450 expect(menuItem.element().getAttribute("class")).toBe(null);
451 menuItem.active(true);
452 expect(menuItem.active()).toBe(true);
453 expect(menuItem.element().getAttribute("class")).toEqual("active");
454 menuItem.active(false); // Is active
455 expect(menuItem.active()).toBe(false);
456 expect(menuItem.element().getAttribute("class")).toEqual("");
457 menuItem.active(true);
458 expect(menuItem.active()).toBe(true);
459 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000460
Nils Diewald7c8ced22015-04-15 19:21:00 +0000461 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
462 expect(menuItem.active()).toBe(false);
463 expect(menuItem.element().getAttribute("class")).toBe(null);
464 menuItem.active(false); // Is not active
465 expect(menuItem.active()).toBe(false);
466 expect(menuItem.element().getAttribute("class")).toBe(null);
467 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000468
Akronfac16472018-07-26 16:47:21 +0200469
Nils Diewald7c8ced22015-04-15 19:21:00 +0000470 it('should be set to boundary', function () {
471 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
472 expect(menuItem.active()).toBe(false);
473 expect(menuItem.element().getAttribute("class")).toBe(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000474
Nils Diewald7c8ced22015-04-15 19:21:00 +0000475 // Set active
476 menuItem.active(true);
477 expect(menuItem.active()).toBe(true);
478 expect(menuItem.noMore()).toBe(false);
479 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000480
Nils Diewald7c8ced22015-04-15 19:21:00 +0000481 // Set no more
482 menuItem.noMore(true);
483 expect(menuItem.active()).toBe(true);
484 expect(menuItem.noMore()).toBe(true);
485 expect(menuItem.element().getAttribute("class")).toEqual("active no-more");
Nils Diewald1c546922015-04-13 01:56:19 +0000486
Nils Diewald7c8ced22015-04-15 19:21:00 +0000487 // No no more
488 menuItem.noMore(false);
489 expect(menuItem.active()).toBe(true);
490 expect(menuItem.noMore()).toBe(false);
491 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald1c546922015-04-13 01:56:19 +0000492
Nils Diewald7c8ced22015-04-15 19:21:00 +0000493 // Set no more, deactivate
494 menuItem.noMore(true);
495 menuItem.active(false);
496 expect(menuItem.active()).toBe(false);
497 expect(menuItem.noMore()).toBe(true);
498 expect(menuItem.element().getAttribute("class")).toEqual("no-more");
Nils Diewald19ccee92014-12-08 11:30:08 +0000499
Nils Diewald7c8ced22015-04-15 19:21:00 +0000500 // Set active
501 menuItem.active(true);
502 expect(menuItem.active()).toBe(true);
503 expect(menuItem.noMore()).toBe(true);
504 expect(menuItem.element().getAttribute("class")).toEqual("no-more active");
505 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000506
Akronfac16472018-07-26 16:47:21 +0200507
Nils Diewald7c8ced22015-04-15 19:21:00 +0000508 it('should be highlightable', function () {
509 // Highlight in the middle
510 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
511 menuItem.highlight("ren");
512 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000513
Nils Diewald7c8ced22015-04-15 19:21:00 +0000514 menuItem.lowlight();
515 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000516
Nils Diewald7c8ced22015-04-15 19:21:00 +0000517 // Starting highlight
518 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
519 menuItem.highlight("cor");
520 expect(menuItem.element().innerHTML).toEqual("<span><mark>Cor</mark>eNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000521
Nils Diewald7c8ced22015-04-15 19:21:00 +0000522 menuItem.lowlight();
523 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
524
525 // Starting highlight - short
526 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
527 menuItem.highlight("c");
528 expect(menuItem.element().innerHTML).toEqual("<span><mark>C</mark>oreNLP</span>");
529
530 menuItem.lowlight();
531 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
532
533 // Highlight at the end
534 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
535 menuItem.highlight("nlp");
536 expect(menuItem.element().innerHTML).toEqual("<span>Core<mark>NLP</mark></span>");
537
538 menuItem.lowlight();
539 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
540
541 // Highlight at the end - short
542 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
543 menuItem.highlight("p");
544 expect(menuItem.element().innerHTML).toEqual("<span>CoreNL<mark>P</mark></span>");
545
546 menuItem.lowlight();
547 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
548
549 // No highlight
550 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
551 menuItem.highlight("xp");
552 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
553
554 menuItem.lowlight();
555 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
556
557 // Highlight in the middle - first
558 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
559
560 menuItem.highlight("ren");
561 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span><span class=\"desc\">This is my Example</span>");
562
563 menuItem.lowlight();
564 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
565
566 // Highlight in the middle - second
567 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
568 menuItem.highlight("ampl");
569 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Ex<mark>ampl</mark>e</span>');
570
571 menuItem.lowlight();
572 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
573
574 // Highlight in the middle - both
575 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
576
577 menuItem.highlight("e");
578 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>');
579
580 menuItem.lowlight();
581 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
582
583 // Highlight in the end - second
584 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
585 menuItem.highlight("le");
586 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Examp<mark>le</mark></span>");
587
588 menuItem.lowlight();
589 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
590
591 // Highlight at the beginning - second
592 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
593 menuItem.highlight("this");
594 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\"><mark>This</mark> is my Example</span>");
595
596 menuItem.lowlight();
597 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
598 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000599 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000600
Akronfac16472018-07-26 16:47:21 +0200601
Nils Diewald7c8ced22015-04-15 19:21:00 +0000602 describe('KorAP.HintMenu', function () {
Akronfac16472018-07-26 16:47:21 +0200603 beforeAll(beforeAllFunc);
604 afterAll(afterAllFunc);
605
Nils Diewald7c8ced22015-04-15 19:21:00 +0000606 var list = [
607 ["Constituency", "c=", "Example 1"],
608 ["Lemma", "l="],
609 ["Morphology", "m=", "Example 2"],
610 ["Part-of-Speech", "p="],
611 ["Syntax", "syn="]
612 ];
Nils Diewald19ccee92014-12-08 11:30:08 +0000613
Nils Diewald7c8ced22015-04-15 19:21:00 +0000614 it('should be initializable', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000615 var menu = menuClass.create(null, "cnx/", list);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000616 expect(menu.element().nodeName).toEqual('UL');
Akron65c74352016-09-02 17:23:39 +0200617 // expect(menu.element().style.opacity).toEqual("0");
Nils Diewald19ccee92014-12-08 11:30:08 +0000618
Nils Diewald7c8ced22015-04-15 19:21:00 +0000619 menu.limit(8);
Nils Diewald19ccee92014-12-08 11:30:08 +0000620
Nils Diewald7c8ced22015-04-15 19:21:00 +0000621 // view
622 menu.show();
Akron6ed13992016-05-23 18:06:05 +0200623 expect(menu.prefix()).toBe('');
Nils Diewald19ccee92014-12-08 11:30:08 +0000624
Nils Diewald7c8ced22015-04-15 19:21:00 +0000625 // First element in list
626 expect(menu.item(0).active()).toBe(true);
627 expect(menu.item(0).noMore()).toBe(true);
628
629 // Middle element in list
630 expect(menu.item(2).active()).toBe(false);
631 expect(menu.item(2).noMore()).toBe(false);
Nils Diewald19ccee92014-12-08 11:30:08 +0000632
Nils Diewald7c8ced22015-04-15 19:21:00 +0000633 // Last element in list
634 expect(menu.item(menu.length() - 1).active()).toBe(false);
635 expect(menu.item(menu.length() - 1).noMore()).toBe(true);
Akron6ed13992016-05-23 18:06:05 +0200636
637 expect(menu.shownItem(0).active()).toBeTruthy();
638 expect(menu.shownItem(0).lcField()).toEqual(' constituency example 1');
639 expect(menu.shownItem(1).lcField()).toEqual(' lemma');
640 expect(menu.shownItem(2).lcField()).toEqual(' morphology example 2');
641
642 menu.next();
643 expect(menu.shownItem(1).active()).toBeTruthy();
644
645 menu.next();
646 expect(menu.shownItem(2).active()).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000647 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000648 });
649});