blob: a78a8355d259bece64bc114e65d497f283bc9029 [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);
Akron24aa0052020-11-10 11:00:34 +010087 expect(inputField._el).not.toBe(undefined);
Nils Diewald7c8ced22015-04-15 19:21:00 +000088 });
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);
Akronee9ef4a2016-06-03 12:50:08 +0200244 });
Akron00cd4d12016-05-31 21:01:11 +0200245
Akronda5bd3a2020-10-16 17:37:49 +0200246 it('should work both in Chrome and Firefox', function () {
247 var hint = hintClass.create({
248 inputField : input
249 });
250 hint.show(false);
251 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
252
253 // Show menu, hide alert!
254 hint.show(false);
255
256 expect(hint.active()).toBeFalsy();
257 });
258
259
Akronee9ef4a2016-06-03 12:50:08 +0200260 it('should view main menu on default', function () {
261 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200262 inputField : input
Akronee9ef4a2016-06-03 12:50:08 +0200263 });
264
265 expect(hint.active()).toBeFalsy();
266
267 hint.inputField().insert('der Baum corenlp/');
Akronee9ef4a2016-06-03 12:50:08 +0200268
Akron8eaeb2e2016-08-29 18:26:28 +0200269 var cont = hint.inputField().container();
270
271 expect(cont.getElementsByTagName('div').length).toBe(1);
272 expect(cont.getElementsByTagName('ul').length).toBe(0);
273 expect(cont.firstChild).toEqual(cont.firstChild);
274
275 // Show menu, if a relevant context exists
276 // There is a menu for corenlp/
Akron02360e42016-06-07 13:41:12 +0200277 hint.show(false);
278
Akron8eaeb2e2016-08-29 18:26:28 +0200279 expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1);
280 expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(3);
Akron65c74352016-09-02 17:23:39 +0200281
Akron8eaeb2e2016-08-29 18:26:28 +0200282 // Hide the menu and focus on the input
283 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200284
285 expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(1);
286 expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(0);
Akronee9ef4a2016-06-03 12:50:08 +0200287
Akron02360e42016-06-07 13:41:12 +0200288 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200289
Akronee9ef4a2016-06-03 12:50:08 +0200290 hint.inputField().insert(' hhhh');
Akron65c74352016-09-02 17:23:39 +0200291
Akronee9ef4a2016-06-03 12:50:08 +0200292 // show with context
Akron02360e42016-06-07 13:41:12 +0200293 hint.show(false);
Akron65c74352016-09-02 17:23:39 +0200294
Akronee9ef4a2016-06-03 12:50:08 +0200295 expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(4);
296 expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1);
Akron02360e42016-06-07 13:41:12 +0200297 expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(2);
298
Akronee9ef4a2016-06-03 12:50:08 +0200299 hint.unshow();
300 hint.inputField().insert(' aaaa/');
301
302 // show with context
303 hint.show(true);
304
Akron1a5a5872016-09-05 20:17:14 +0200305 expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(1);
306 expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(0);
307 });
308
Akron95abaf42018-04-26 15:33:22 +0200309
310 it('should open menus depending on the context', function () {
311 var hint = hintClass.create({
312 inputField : input
313 });
314 hint.inputField().reset();
315
316 expect(hint.active()).toBeFalsy();
317
318 // show with context
319 hint.show(false);
320
321 expect(hint.active()).toBeTruthy();
322
323 expect(hint.inputField().container().getElementsByTagName('li')[0].firstChild.innerText).toEqual("Base Annotation");
324
325 // Type in prefix
326 hint.active().prefix("cor").show();
327 expect(hint.active().prefix()).toEqual("cor");
328
329 // Click first step
330 expect(hint.inputField().container().getElementsByTagName('li')[0].firstChild.firstChild.innerText).toEqual("Cor");
331 hint.inputField().container().getElementsByTagName('li')[0].click();
332
333 expect(hint.active()).toBeTruthy();
334
335 // Click second step
336 expect(hint.inputField().container().getElementsByTagName('li')[0].firstChild.innerText).toEqual("Named Entity");
337 hint.inputField().container().getElementsByTagName('li')[0].click()
338
339 // Invisible menu
340 expect(hint.inputField().container().getElementsByTagName('li')[0]).toBeUndefined();
341
342 // Inactive menu
343 expect(hint.active()).toBeFalsy();
344
345 // show with context
346 hint.show(false);
347
348 // No prefix
349 expect(hint.active().prefix()).toEqual("");
350 });
351
352
Akron1a5a5872016-09-05 20:17:14 +0200353 it('should not view main menu if context is mandatory', function () {
354 var hint = hintClass.create({
355 inputField : input
356 });
357
358 expect(hint.active()).toBeFalsy();
359
360 // Fine
361 hint.inputField().insert('der Baum corenlp/');
362 hint.show(true);
363 expect(hint.active()).toBeTruthy();
364
365 // Not analyzable
366 hint.inputField().insert('jhgjughjfhgnhfcvgnhj');
367 hint.show(true);
368 expect(hint.active()).toBeFalsy();
369
370 // Not available
371 hint.inputField().insert('jhgjughjfhgnhfcvgnhj/');
372 hint.show(true);
373 expect(hint.active()).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200374 });
Akron5746ecf2018-06-23 10:57:24 +0200375
376
377 it('should show the assistant bar on blur', function () {
378 var hint = hintClass.create({
379 inputField : input
380 });
381 // Fine
382 hint.inputField().insert('der Baum corenlp/');
383 hint.show(true);
384 expect(hint.active()).toBeTruthy();
385
386 // Blur
Akrone0789112018-08-31 14:32:04 +0200387 hint.active().hide();
Akron5746ecf2018-06-23 10:57:24 +0200388 expect(hint.active()).toBeFalsy();
389 });
Akron65c74352016-09-02 17:23:39 +0200390
Akron02360e42016-06-07 13:41:12 +0200391 xit('should remove all menus on escape');
Nils Diewald19ccee92014-12-08 11:30:08 +0000392 });
393
Akron65c74352016-09-02 17:23:39 +0200394
Nils Diewald7c8ced22015-04-15 19:21:00 +0000395 describe('KorAP.HintMenuItem', function () {
Akronfac16472018-07-26 16:47:21 +0200396 beforeAll(beforeAllFunc);
397 afterAll(afterAllFunc);
398
Nils Diewald7c8ced22015-04-15 19:21:00 +0000399 it('should be initializable', function () {
400 expect(
Akronfac16472018-07-26 16:47:21 +0200401 function() { menuItemClass.create([]) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000402 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000403
Nils Diewald7c8ced22015-04-15 19:21:00 +0000404 expect(
Akronfac16472018-07-26 16:47:21 +0200405 function() { menuItemClass.create(['CoreNLP']) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000406 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000407
Nils Diewald7c8ced22015-04-15 19:21:00 +0000408 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
409 expect(menuItem.name()).toEqual('CoreNLP');
410 expect(menuItem.action()).toEqual('corenlp/');
411 expect(menuItem.desc()).toBeUndefined();
Nils Diewald19ccee92014-12-08 11:30:08 +0000412
Nils Diewald7c8ced22015-04-15 19:21:00 +0000413 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200414 ['CoreNLP', 'corenlp/', 'It\'s funny']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000415 );
416 expect(menuItem.name()).toEqual('CoreNLP');
417 expect(menuItem.action()).toEqual('corenlp/');
418 expect(menuItem.desc()).not.toBeUndefined();
419 expect(menuItem.desc()).toEqual('It\'s funny');
420 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000421
Akronfac16472018-07-26 16:47:21 +0200422
Nils Diewald7c8ced22015-04-15 19:21:00 +0000423 it('should have an element', function () {
424 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
425 expect(menuItem.element()).not.toBe(undefined);
426 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000427
Nils Diewald7c8ced22015-04-15 19:21:00 +0000428 var title = menuItem.element().firstChild;
429 expect(title.nodeName).toEqual("SPAN");
430 expect(title.firstChild.nodeType).toEqual(3);
431 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
432 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
433 expect(menuItem.element().childNodes[1]).toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000434
Nils Diewald7c8ced22015-04-15 19:21:00 +0000435 menuItem = menuItemClass.create(
Akronfac16472018-07-26 16:47:21 +0200436 ['CoreNLP', 'corenlp/', 'my DescRiption']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000437 );
438 expect(menuItem.element()).not.toBe(undefined);
439 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000440
Nils Diewald7c8ced22015-04-15 19:21:00 +0000441 title = menuItem.element().firstChild;
442 expect(title.nodeName).toEqual("SPAN");
443 expect(title.firstChild.nodeType).toEqual(3); // TextNode
444 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
Nils Diewald19ccee92014-12-08 11:30:08 +0000445
Nils Diewald7c8ced22015-04-15 19:21:00 +0000446 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
447 expect(menuItem.element().childNodes[1]).not.toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000448
Nils Diewald7c8ced22015-04-15 19:21:00 +0000449 var desc = menuItem.element().lastChild;
450 expect(desc.nodeName).toEqual("SPAN");
451 expect(desc.firstChild.nodeType).toEqual(3); // TextNode
452 expect(desc.firstChild.nodeValue).toEqual("my DescRiption");
453 });
Nils Diewald1c546922015-04-13 01:56:19 +0000454
Nils Diewald19ccee92014-12-08 11:30:08 +0000455
Nils Diewald7c8ced22015-04-15 19:21:00 +0000456 it('should be activatable and deactivateable by class', function () {
457 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
458 expect(menuItem.active()).toBe(false);
459 expect(menuItem.element().getAttribute("class")).toBe(null);
460 menuItem.active(true);
461 expect(menuItem.active()).toBe(true);
462 expect(menuItem.element().getAttribute("class")).toEqual("active");
463 menuItem.active(false); // Is active
464 expect(menuItem.active()).toBe(false);
465 expect(menuItem.element().getAttribute("class")).toEqual("");
466 menuItem.active(true);
467 expect(menuItem.active()).toBe(true);
468 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000469
Nils Diewald7c8ced22015-04-15 19:21:00 +0000470 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
471 expect(menuItem.active()).toBe(false);
472 expect(menuItem.element().getAttribute("class")).toBe(null);
473 menuItem.active(false); // Is not active
474 expect(menuItem.active()).toBe(false);
475 expect(menuItem.element().getAttribute("class")).toBe(null);
476 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000477
Akronfac16472018-07-26 16:47:21 +0200478
Nils Diewald7c8ced22015-04-15 19:21:00 +0000479 it('should be set to boundary', function () {
480 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
481 expect(menuItem.active()).toBe(false);
482 expect(menuItem.element().getAttribute("class")).toBe(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000483
Nils Diewald7c8ced22015-04-15 19:21:00 +0000484 // Set active
485 menuItem.active(true);
486 expect(menuItem.active()).toBe(true);
487 expect(menuItem.noMore()).toBe(false);
488 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000489
Nils Diewald7c8ced22015-04-15 19:21:00 +0000490 // Set no more
491 menuItem.noMore(true);
492 expect(menuItem.active()).toBe(true);
493 expect(menuItem.noMore()).toBe(true);
494 expect(menuItem.element().getAttribute("class")).toEqual("active no-more");
Nils Diewald1c546922015-04-13 01:56:19 +0000495
Nils Diewald7c8ced22015-04-15 19:21:00 +0000496 // No no more
497 menuItem.noMore(false);
498 expect(menuItem.active()).toBe(true);
499 expect(menuItem.noMore()).toBe(false);
500 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald1c546922015-04-13 01:56:19 +0000501
Nils Diewald7c8ced22015-04-15 19:21:00 +0000502 // Set no more, deactivate
503 menuItem.noMore(true);
504 menuItem.active(false);
505 expect(menuItem.active()).toBe(false);
506 expect(menuItem.noMore()).toBe(true);
507 expect(menuItem.element().getAttribute("class")).toEqual("no-more");
Nils Diewald19ccee92014-12-08 11:30:08 +0000508
Nils Diewald7c8ced22015-04-15 19:21:00 +0000509 // Set active
510 menuItem.active(true);
511 expect(menuItem.active()).toBe(true);
512 expect(menuItem.noMore()).toBe(true);
513 expect(menuItem.element().getAttribute("class")).toEqual("no-more active");
514 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000515
Akronfac16472018-07-26 16:47:21 +0200516
Nils Diewald7c8ced22015-04-15 19:21:00 +0000517 it('should be highlightable', function () {
518 // Highlight in the middle
519 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
520 menuItem.highlight("ren");
521 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000522
Nils Diewald7c8ced22015-04-15 19:21:00 +0000523 menuItem.lowlight();
524 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000525
Nils Diewald7c8ced22015-04-15 19:21:00 +0000526 // Starting highlight
527 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
528 menuItem.highlight("cor");
529 expect(menuItem.element().innerHTML).toEqual("<span><mark>Cor</mark>eNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000530
Nils Diewald7c8ced22015-04-15 19:21:00 +0000531 menuItem.lowlight();
532 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
533
534 // Starting highlight - short
535 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
536 menuItem.highlight("c");
537 expect(menuItem.element().innerHTML).toEqual("<span><mark>C</mark>oreNLP</span>");
538
539 menuItem.lowlight();
540 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
541
542 // Highlight at the end
543 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
544 menuItem.highlight("nlp");
545 expect(menuItem.element().innerHTML).toEqual("<span>Core<mark>NLP</mark></span>");
546
547 menuItem.lowlight();
548 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
549
550 // Highlight at the end - short
551 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
552 menuItem.highlight("p");
553 expect(menuItem.element().innerHTML).toEqual("<span>CoreNL<mark>P</mark></span>");
554
555 menuItem.lowlight();
556 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
557
558 // No highlight
559 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
560 menuItem.highlight("xp");
561 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
562
563 menuItem.lowlight();
564 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
565
566 // Highlight in the middle - first
567 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
568
569 menuItem.highlight("ren");
570 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span><span class=\"desc\">This is my Example</span>");
571
572 menuItem.lowlight();
573 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
574
575 // Highlight in the middle - second
576 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
577 menuItem.highlight("ampl");
578 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Ex<mark>ampl</mark>e</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 middle - both
584 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
585
586 menuItem.highlight("e");
587 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>');
588
589 menuItem.lowlight();
590 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
591
592 // Highlight in the end - second
593 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
594 menuItem.highlight("le");
595 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Examp<mark>le</mark></span>");
596
597 menuItem.lowlight();
598 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
599
600 // Highlight at the beginning - second
601 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
602 menuItem.highlight("this");
603 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\"><mark>This</mark> is my Example</span>");
604
605 menuItem.lowlight();
606 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
607 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000608 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000609
Akronfac16472018-07-26 16:47:21 +0200610
Nils Diewald7c8ced22015-04-15 19:21:00 +0000611 describe('KorAP.HintMenu', function () {
Akronfac16472018-07-26 16:47:21 +0200612 beforeAll(beforeAllFunc);
613 afterAll(afterAllFunc);
614
Nils Diewald7c8ced22015-04-15 19:21:00 +0000615 var list = [
616 ["Constituency", "c=", "Example 1"],
617 ["Lemma", "l="],
618 ["Morphology", "m=", "Example 2"],
619 ["Part-of-Speech", "p="],
620 ["Syntax", "syn="]
621 ];
Nils Diewald19ccee92014-12-08 11:30:08 +0000622
Nils Diewald7c8ced22015-04-15 19:21:00 +0000623 it('should be initializable', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000624 var menu = menuClass.create(null, "cnx/", list);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000625 expect(menu.element().nodeName).toEqual('UL');
Akron65c74352016-09-02 17:23:39 +0200626 // expect(menu.element().style.opacity).toEqual("0");
Nils Diewald19ccee92014-12-08 11:30:08 +0000627
Nils Diewald7c8ced22015-04-15 19:21:00 +0000628 menu.limit(8);
Nils Diewald19ccee92014-12-08 11:30:08 +0000629
Nils Diewald7c8ced22015-04-15 19:21:00 +0000630 // view
631 menu.show();
Akron6ed13992016-05-23 18:06:05 +0200632 expect(menu.prefix()).toBe('');
Nils Diewald19ccee92014-12-08 11:30:08 +0000633
Nils Diewald7c8ced22015-04-15 19:21:00 +0000634 // First element in list
635 expect(menu.item(0).active()).toBe(true);
636 expect(menu.item(0).noMore()).toBe(true);
637
638 // Middle element in list
639 expect(menu.item(2).active()).toBe(false);
640 expect(menu.item(2).noMore()).toBe(false);
Nils Diewald19ccee92014-12-08 11:30:08 +0000641
Nils Diewald7c8ced22015-04-15 19:21:00 +0000642 // Last element in list
643 expect(menu.item(menu.length() - 1).active()).toBe(false);
644 expect(menu.item(menu.length() - 1).noMore()).toBe(true);
Akron6ed13992016-05-23 18:06:05 +0200645
646 expect(menu.shownItem(0).active()).toBeTruthy();
647 expect(menu.shownItem(0).lcField()).toEqual(' constituency example 1');
648 expect(menu.shownItem(1).lcField()).toEqual(' lemma');
649 expect(menu.shownItem(2).lcField()).toEqual(' morphology example 2');
650
651 menu.next();
652 expect(menu.shownItem(1).active()).toBeTruthy();
653
654 menu.next();
655 expect(menu.shownItem(2).active()).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000656 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000657 });
658});