blob: 5df65fd07170990c8dcd1dca39c2bda379c675dd [file] [log] [blame]
Nils Diewald19ccee92014-12-08 11:30:08 +00001function emitKeyboardEvent (element, type, keyCode) {
2 // event type : keydown, keyup, keypress
3 // http://stackoverflow.com/questions/596481/simulate-javascript-key-events
4 // http://stackoverflow.com/questions/961532/firing-a-keyboard-event-in-javascript
5 var keyboardEvent = document.createEvent("KeyboardEvent");
6 var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ?
7 "initKeyboardEvent" : "initKeyEvent";
Nils Diewald19ccee92014-12-08 11:30:08 +00008 keyboardEvent[initMethod](
9 type,
Akron308db382016-05-30 22:34:07 +020010 true, // bubbles
11 true, // cancelable
12 window, // viewArg: should be window
13 false, // ctrlKeyArg
14 false, // altKeyArg
15 false, // shiftKeyArg
16 false, // metaKeyArg
Nils Diewald19ccee92014-12-08 11:30:08 +000017 keyCode, // keyCodeArg : unsigned long the virtual key code, else 0
Akron308db382016-05-30 22:34:07 +020018 0 // charCodeArgs : unsigned long the Unicode character
19 // associated with the depressed key, else 0
Nils Diewald19ccee92014-12-08 11:30:08 +000020 );
Nils Diewald19ccee92014-12-08 11:30:08 +000021 element.dispatchEvent(keyboardEvent);
22};
23
24
Nils Diewald7c8ced22015-04-15 19:21:00 +000025define(['hint'], function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +000026 var hintClass = require("hint");
27 var inputClass = require("hint/input");
28 var contextClass = require("hint/contextanalyzer");
29 var menuClass = require("hint/menu");
30 var menuItemClass = require("hint/item");
Nils Diewald5c5a7472015-04-02 22:13:38 +000031
Nils Diewald7c8ced22015-04-15 19:21:00 +000032 describe('KorAP.InputField', function () {
33 var input;
Nils Diewald1c546922015-04-13 01:56:19 +000034
Nils Diewald7c8ced22015-04-15 19:21:00 +000035 beforeEach(function () {
36 input = document.createElement("input");
37 input.setAttribute('type', "text");
38 input.setAttribute("value", "abcdefghijklmno");
39 input.style.position = 'absolute';
40 document.getElementsByTagName('body')[0].appendChild(input);
41 input.style.top = "20px";
42 input.style.left = "30px";
43 input.focus();
44 input.selectionStart = 5;
Nils Diewald5c5a7472015-04-02 22:13:38 +000045 });
46
Nils Diewald7c8ced22015-04-15 19:21:00 +000047 afterEach(function () {
48 document.getElementsByTagName("body")[0].removeChild(
Akron1a5a5872016-09-05 20:17:14 +020049 input
Nils Diewald7c8ced22015-04-15 19:21:00 +000050 );
51 });
Nils Diewald5c5a7472015-04-02 22:13:38 +000052
Nils Diewald7c8ced22015-04-15 19:21:00 +000053 afterAll(function () {
54 try {
Akron1a5a5872016-09-05 20:17:14 +020055 var mirrors = document.querySelectorAll(".hint.mirror");
56 for (var i in mirrors) {
57 mirrors[i].parentNode.removeChild(mirrors[i])
58 };
Nils Diewald7c8ced22015-04-15 19:21:00 +000059 }
60 catch (e) {};
61 });
Nils Diewald19ccee92014-12-08 11:30:08 +000062
Nils Diewald7c8ced22015-04-15 19:21:00 +000063 it('should be initializable', function () {
64 // Supports: context, searchField
65 var inputField = inputClass.create(input);
66 expect(inputField._element).not.toBe(undefined);
67 });
Nils Diewald19ccee92014-12-08 11:30:08 +000068
Nils Diewald7c8ced22015-04-15 19:21:00 +000069 it('should have text', function () {
70 expect(input.value).toEqual('abcdefghijklmno');
71 var inputField = inputClass.create(input);
Nils Diewald19ccee92014-12-08 11:30:08 +000072
Nils Diewald7c8ced22015-04-15 19:21:00 +000073 expect(inputField.value()).toEqual("abcdefghijklmno");
Nils Diewald19ccee92014-12-08 11:30:08 +000074
Nils Diewald7c8ced22015-04-15 19:21:00 +000075 expect(input.selectionStart).toEqual(5);
76 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +000077 expect(inputField._split()[0]).toEqual("abcde");
78 expect(inputField._split()[1]).toEqual("fghijklmno");
Nils Diewald19ccee92014-12-08 11:30:08 +000079
Nils Diewald7c8ced22015-04-15 19:21:00 +000080 inputField.insert("xyz");
81 expect(inputField.value()).toEqual('abcdexyzfghijklmno');
Nils Diewald7148c6f2015-05-04 15:07:53 +000082 expect(inputField._split()[0]).toEqual("abcdexyz");
83 expect(inputField._split()[1]).toEqual("fghijklmno");
Nils Diewald7c8ced22015-04-15 19:21:00 +000084 });
Nils Diewald19ccee92014-12-08 11:30:08 +000085
Nils Diewald7c8ced22015-04-15 19:21:00 +000086 it('should be correctly positioned', function () {
87 expect(input.value).toEqual('abcdefghijklmno');
88 var inputField = inputClass.create(input);
89 document.getElementsByTagName("body")[0].appendChild(input);
90 inputField.reposition();
91 expect(input.style.left).toEqual("30px");
92 expect(inputField.mirror().style.left.match(/^(\d+)px$/)[1]).toBeGreaterThan(29);
93 expect(inputField.mirror().style.top.match(/^(\d+)px$/)[1]).toBeGreaterThan(20);
94 });
Nils Diewald19ccee92014-12-08 11:30:08 +000095
Nils Diewald7c8ced22015-04-15 19:21:00 +000096 it('should have a correct context', function () {
97 expect(input.value).toEqual('abcdefghijklmno');
98 var inputField = inputClass.create(input);
99 expect(inputField.value()).toEqual("abcdefghijklmno");
100 expect(inputField.element().selectionStart).toEqual(5);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000101 expect(inputField._split()[0]).toEqual("abcde");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000102 expect(inputField.context()).toEqual("abcde");
103 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000104
Nils Diewald7c8ced22015-04-15 19:21:00 +0000105 /*
106 it('should be correctly triggerable', function () {
107 // https://developer.mozilla.org/samples/domref/dispatchEvent.html
108 var hint = KorAP.Hint.create({ "inputField" : input });
109 emitKeyboardEvent(hint.inputField.element, "keypress", 20);
110 });
111 */
Nils Diewald19ccee92014-12-08 11:30:08 +0000112 });
113
Nils Diewald1c546922015-04-13 01:56:19 +0000114
Nils Diewald7c8ced22015-04-15 19:21:00 +0000115 describe('KorAP.ContextAnalyzer', function () {
116 it('should be initializable', function () {
117 var analyzer = contextClass.create(")");
118 expect(analyzer).toBe(undefined);
119 analyzer = contextClass.create(".+?");
120 expect(analyzer).not.toBe(undefined);
121 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000122
Nils Diewald7c8ced22015-04-15 19:21:00 +0000123 it('should check correctly', function () {
124 analyzer = contextClass.create(KorAP.context);
125 expect(analyzer.test("cnx/]cnx/c=")).toEqual("cnx/c=");
126 expect(analyzer.test("cnx/c=")).toEqual("cnx/c=");
127 expect(analyzer.test("cnx/c=np mate/m=mood:")).toEqual("mate/m=mood:");
128 expect(analyzer.test("impcnx/")).toEqual("impcnx/");
129 expect(analyzer.test("cnx/c=npcnx/")).toEqual("npcnx/");
130 expect(analyzer.test("mate/m=degree:pos corenlp/ne_dewac_175m_600="))
Akron1a5a5872016-09-05 20:17:14 +0200131 .toEqual("corenlp/ne_dewac_175m_600=");
Akron113cc1a2016-01-22 21:17:57 +0100132 expect(analyzer.test("corenlp/")).toEqual("corenlp/");
133 expect(analyzer.test("corenlp/c=")).toEqual("corenlp/c=");
134 expect(analyzer.test("corenlp/c=PP-")).toEqual("corenlp/c=PP-");
135 expect(analyzer.test("corenlp/c=XY-")).toEqual("corenlp/c=XY-");
Akroncff9bac2016-01-25 21:39:38 +0100136 expect(analyzer.test("sgbr/l=")).toEqual("sgbr/l=");
137 expect(analyzer.test("sgbr/lv=")).toEqual("sgbr/lv=");
138 expect(analyzer.test("sgbr/p=")).toEqual("sgbr/p=");
Akronee9ef4a2016-06-03 12:50:08 +0200139 expect(analyzer.test("")).toEqual(undefined);
140 expect(analyzer.test("abcdecnx/")).toEqual("abcdecnx/");
Nils Diewald7c8ced22015-04-15 19:21:00 +0000141 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000142 });
143
Nils Diewald19ccee92014-12-08 11:30:08 +0000144
Nils Diewald7c8ced22015-04-15 19:21:00 +0000145 describe('KorAP.Hint', function () {
146 KorAP.hintArray = {
Akron02360e42016-06-07 13:41:12 +0200147 "-" : [
Akron1a5a5872016-09-05 20:17:14 +0200148 ["Base Annotation", "base/s=", "Structure"],
149 ["CoreNLP", "corenlp/", "Constituency, Named Entities, Part-of-Speech"]
Akron02360e42016-06-07 13:41:12 +0200150 ],
Nils Diewald7c8ced22015-04-15 19:21:00 +0000151 "corenlp/" : [
Akron1a5a5872016-09-05 20:17:14 +0200152 ["Named Entity", "ne=" , "Combined"],
153 ["Named Entity", "ne_dewac_175m_600=" , "ne_dewac_175m_600"],
154 ["Named Entity", "ne_hgc_175m_600=", "ne_hgc_175m_600"]
Nils Diewald7c8ced22015-04-15 19:21:00 +0000155 ]
156 };
Nils Diewald19ccee92014-12-08 11:30:08 +0000157
Nils Diewald7c8ced22015-04-15 19:21:00 +0000158 beforeEach(function () {
159 input = document.createElement("input");
160 input.setAttribute("type", "text");
161 input.setAttribute("value", "abcdefghijklmno");
162 input.style.position = 'absolute';
163 input.style.top = "20px";
164 input.style.left = "30px";
165 input.focus();
166 input.selectionStart = 5;
167 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000168
Nils Diewald7c8ced22015-04-15 19:21:00 +0000169 it('should be initializable', function () {
170 // Supports: context, searchField
171 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200172 inputField : input
Nils Diewald7c8ced22015-04-15 19:21:00 +0000173 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000174
Nils Diewald7c8ced22015-04-15 19:21:00 +0000175 expect(hint).toBeTruthy();
176 });
Akron00cd4d12016-05-31 21:01:11 +0200177
Akron1a5a5872016-09-05 20:17:14 +0200178
Akron00cd4d12016-05-31 21:01:11 +0200179 it('should alert at char pos', function () {
180 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200181 inputField : input
Akron00cd4d12016-05-31 21:01:11 +0200182 });
183
184 expect(hint.active()).toBeFalsy();
185
186 expect(hint.alert(4, 'That does not work!')).toBeTruthy();
187
188 expect(hint.active()).toBeTruthy();
189
190 var container = hint.inputField().container();
191 expect(container.firstChild.classList.contains('hint')).toBe(true);
192 expect(container.firstChild.classList.contains('alert')).toBe(true);
193 expect(container.firstChild.textContent).toEqual('That does not work!');
194 expect(hint.inputField().mirrorValue()).toEqual('abcd');
195
196 expect(hint.alert(4, 'That does not work!')).toBeFalsy();
197
198 // Update - meaning: hide alert
199 hint.update();
200
201 expect(hint.alert().active).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200202 expect(hint.active()).toBeFalsy();
Akron1a5a5872016-09-05 20:17:14 +0200203
204 // Show again
205 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
206 expect(hint.inputField().mirrorValue()).toEqual('abcde');
207 expect(hint.alert().active).toBeTruthy();
208 expect(hint.active()).toBeTruthy();
209
210 // Show menu, hide alert!
211 hint.show(false);
212 expect(hint.active()).toBeTruthy();
213 expect(hint.inputField().mirrorValue()).toEqual('abcde');
214 expect(hint.alert().active).toBeFalsy();
215
216 // Show again
217 expect(hint.alert(5, 'That does not work!')).toBeTruthy();
218 expect(hint.inputField().mirrorValue()).toEqual('abcde');
219 expect(hint.alert().active).toBeTruthy();
220 expect(hint.active()).toBeTruthy();
221
222 // Show menu, hide alert!
223 hint.show(false);
224 expect(hint.active()).toBeTruthy();
225 expect(hint.inputField().mirrorValue()).toEqual('abcde');
226 expect(hint.alert().active).toBeFalsy();
Akronee9ef4a2016-06-03 12:50:08 +0200227 });
Akron00cd4d12016-05-31 21:01:11 +0200228
Akron1a5a5872016-09-05 20:17:14 +0200229
230
Akronee9ef4a2016-06-03 12:50:08 +0200231 it('should view main menu on default', function () {
232 var hint = hintClass.create({
Akron1a5a5872016-09-05 20:17:14 +0200233 inputField : input
Akronee9ef4a2016-06-03 12:50:08 +0200234 });
235
236 expect(hint.active()).toBeFalsy();
237
238 hint.inputField().insert('der Baum corenlp/');
Akronee9ef4a2016-06-03 12:50:08 +0200239
Akron8eaeb2e2016-08-29 18:26:28 +0200240 var cont = hint.inputField().container();
241
242 expect(cont.getElementsByTagName('div').length).toBe(1);
243 expect(cont.getElementsByTagName('ul').length).toBe(0);
244 expect(cont.firstChild).toEqual(cont.firstChild);
245
246 // Show menu, if a relevant context exists
247 // There is a menu for corenlp/
Akron02360e42016-06-07 13:41:12 +0200248 hint.show(false);
249
Akron8eaeb2e2016-08-29 18:26:28 +0200250 expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1);
251 expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(3);
Akron65c74352016-09-02 17:23:39 +0200252
Akron8eaeb2e2016-08-29 18:26:28 +0200253 // Hide the menu and focus on the input
254 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200255
256 expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(1);
257 expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(0);
Akronee9ef4a2016-06-03 12:50:08 +0200258
Akron02360e42016-06-07 13:41:12 +0200259 hint.unshow();
Akron65c74352016-09-02 17:23:39 +0200260
Akronee9ef4a2016-06-03 12:50:08 +0200261 hint.inputField().insert(' hhhh');
Akron65c74352016-09-02 17:23:39 +0200262
Akronee9ef4a2016-06-03 12:50:08 +0200263 // show with context
Akron02360e42016-06-07 13:41:12 +0200264 hint.show(false);
Akron65c74352016-09-02 17:23:39 +0200265
Akronee9ef4a2016-06-03 12:50:08 +0200266 expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(4);
267 expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1);
Akron02360e42016-06-07 13:41:12 +0200268 expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(2);
269
Akronee9ef4a2016-06-03 12:50:08 +0200270 hint.unshow();
271 hint.inputField().insert(' aaaa/');
272
273 // show with context
274 hint.show(true);
275
Akron1a5a5872016-09-05 20:17:14 +0200276 expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(1);
277 expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(0);
278 });
279
280 it('should not view main menu if context is mandatory', function () {
281 var hint = hintClass.create({
282 inputField : input
283 });
284
285 expect(hint.active()).toBeFalsy();
286
287 // Fine
288 hint.inputField().insert('der Baum corenlp/');
289 hint.show(true);
290 expect(hint.active()).toBeTruthy();
291
292 // Not analyzable
293 hint.inputField().insert('jhgjughjfhgnhfcvgnhj');
294 hint.show(true);
295 expect(hint.active()).toBeFalsy();
296
297 // Not available
298 hint.inputField().insert('jhgjughjfhgnhfcvgnhj/');
299 hint.show(true);
300 expect(hint.active()).toBeFalsy();
Akron00cd4d12016-05-31 21:01:11 +0200301 });
Akron65c74352016-09-02 17:23:39 +0200302
Akron02360e42016-06-07 13:41:12 +0200303 xit('should remove all menus on escape');
Nils Diewald19ccee92014-12-08 11:30:08 +0000304 });
305
Akron65c74352016-09-02 17:23:39 +0200306
Nils Diewald7c8ced22015-04-15 19:21:00 +0000307 describe('KorAP.HintMenuItem', function () {
308 it('should be initializable', function () {
309 expect(
Akron1a5a5872016-09-05 20:17:14 +0200310 function() { menuItemClass.create([]) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000311 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000312
Nils Diewald7c8ced22015-04-15 19:21:00 +0000313 expect(
Akron1a5a5872016-09-05 20:17:14 +0200314 function() { menuItemClass.create(['CoreNLP']) }
Nils Diewald7c8ced22015-04-15 19:21:00 +0000315 ).toThrow(new Error("Missing parameters"));
Nils Diewald19ccee92014-12-08 11:30:08 +0000316
Nils Diewald7c8ced22015-04-15 19:21:00 +0000317 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
318 expect(menuItem.name()).toEqual('CoreNLP');
319 expect(menuItem.action()).toEqual('corenlp/');
320 expect(menuItem.desc()).toBeUndefined();
Nils Diewald19ccee92014-12-08 11:30:08 +0000321
Nils Diewald7c8ced22015-04-15 19:21:00 +0000322 menuItem = menuItemClass.create(
Akron1a5a5872016-09-05 20:17:14 +0200323 ['CoreNLP', 'corenlp/', 'It\'s funny']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000324 );
325 expect(menuItem.name()).toEqual('CoreNLP');
326 expect(menuItem.action()).toEqual('corenlp/');
327 expect(menuItem.desc()).not.toBeUndefined();
328 expect(menuItem.desc()).toEqual('It\'s funny');
329 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000330
Nils Diewald7c8ced22015-04-15 19:21:00 +0000331 it('should have an element', function () {
332 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
333 expect(menuItem.element()).not.toBe(undefined);
334 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000335
Nils Diewald7c8ced22015-04-15 19:21:00 +0000336 var title = menuItem.element().firstChild;
337 expect(title.nodeName).toEqual("SPAN");
338 expect(title.firstChild.nodeType).toEqual(3);
339 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
340 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
341 expect(menuItem.element().childNodes[1]).toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000342
Nils Diewald7c8ced22015-04-15 19:21:00 +0000343 menuItem = menuItemClass.create(
Akron1a5a5872016-09-05 20:17:14 +0200344 ['CoreNLP', 'corenlp/', 'my DescRiption']
Nils Diewald7c8ced22015-04-15 19:21:00 +0000345 );
346 expect(menuItem.element()).not.toBe(undefined);
347 expect(menuItem.element().nodeName).toEqual("LI");
Nils Diewald19ccee92014-12-08 11:30:08 +0000348
Nils Diewald7c8ced22015-04-15 19:21:00 +0000349 title = menuItem.element().firstChild;
350 expect(title.nodeName).toEqual("SPAN");
351 expect(title.firstChild.nodeType).toEqual(3); // TextNode
352 expect(title.firstChild.nodeValue).toEqual("CoreNLP");
Nils Diewald19ccee92014-12-08 11:30:08 +0000353
Nils Diewald7c8ced22015-04-15 19:21:00 +0000354 expect(menuItem.element().childNodes[0]).not.toBe(undefined);
355 expect(menuItem.element().childNodes[1]).not.toBe(undefined);
Nils Diewald19ccee92014-12-08 11:30:08 +0000356
Nils Diewald7c8ced22015-04-15 19:21:00 +0000357 var desc = menuItem.element().lastChild;
358 expect(desc.nodeName).toEqual("SPAN");
359 expect(desc.firstChild.nodeType).toEqual(3); // TextNode
360 expect(desc.firstChild.nodeValue).toEqual("my DescRiption");
361 });
Nils Diewald1c546922015-04-13 01:56:19 +0000362
Nils Diewald19ccee92014-12-08 11:30:08 +0000363
Nils Diewald7c8ced22015-04-15 19:21:00 +0000364 it('should be activatable and deactivateable by class', function () {
365 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
366 expect(menuItem.active()).toBe(false);
367 expect(menuItem.element().getAttribute("class")).toBe(null);
368 menuItem.active(true);
369 expect(menuItem.active()).toBe(true);
370 expect(menuItem.element().getAttribute("class")).toEqual("active");
371 menuItem.active(false); // Is active
372 expect(menuItem.active()).toBe(false);
373 expect(menuItem.element().getAttribute("class")).toEqual("");
374 menuItem.active(true);
375 expect(menuItem.active()).toBe(true);
376 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000377
Nils Diewald7c8ced22015-04-15 19:21:00 +0000378 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
379 expect(menuItem.active()).toBe(false);
380 expect(menuItem.element().getAttribute("class")).toBe(null);
381 menuItem.active(false); // Is not active
382 expect(menuItem.active()).toBe(false);
383 expect(menuItem.element().getAttribute("class")).toBe(null);
384 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000385
Nils Diewald7c8ced22015-04-15 19:21:00 +0000386 it('should be set to boundary', function () {
387 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
388 expect(menuItem.active()).toBe(false);
389 expect(menuItem.element().getAttribute("class")).toBe(null);
Nils Diewald19ccee92014-12-08 11:30:08 +0000390
Nils Diewald7c8ced22015-04-15 19:21:00 +0000391 // Set active
392 menuItem.active(true);
393 expect(menuItem.active()).toBe(true);
394 expect(menuItem.noMore()).toBe(false);
395 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald19ccee92014-12-08 11:30:08 +0000396
Nils Diewald7c8ced22015-04-15 19:21:00 +0000397 // Set no more
398 menuItem.noMore(true);
399 expect(menuItem.active()).toBe(true);
400 expect(menuItem.noMore()).toBe(true);
401 expect(menuItem.element().getAttribute("class")).toEqual("active no-more");
Nils Diewald1c546922015-04-13 01:56:19 +0000402
Nils Diewald7c8ced22015-04-15 19:21:00 +0000403 // No no more
404 menuItem.noMore(false);
405 expect(menuItem.active()).toBe(true);
406 expect(menuItem.noMore()).toBe(false);
407 expect(menuItem.element().getAttribute("class")).toEqual("active");
Nils Diewald1c546922015-04-13 01:56:19 +0000408
Nils Diewald7c8ced22015-04-15 19:21:00 +0000409 // Set no more, deactivate
410 menuItem.noMore(true);
411 menuItem.active(false);
412 expect(menuItem.active()).toBe(false);
413 expect(menuItem.noMore()).toBe(true);
414 expect(menuItem.element().getAttribute("class")).toEqual("no-more");
Nils Diewald19ccee92014-12-08 11:30:08 +0000415
Nils Diewald7c8ced22015-04-15 19:21:00 +0000416 // Set active
417 menuItem.active(true);
418 expect(menuItem.active()).toBe(true);
419 expect(menuItem.noMore()).toBe(true);
420 expect(menuItem.element().getAttribute("class")).toEqual("no-more active");
421 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000422
Nils Diewald7c8ced22015-04-15 19:21:00 +0000423 it('should be highlightable', function () {
424 // Highlight in the middle
425 var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
426 menuItem.highlight("ren");
427 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000428
Nils Diewald7c8ced22015-04-15 19:21:00 +0000429 menuItem.lowlight();
430 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000431
Nils Diewald7c8ced22015-04-15 19:21:00 +0000432 // Starting highlight
433 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
434 menuItem.highlight("cor");
435 expect(menuItem.element().innerHTML).toEqual("<span><mark>Cor</mark>eNLP</span>");
Nils Diewald19ccee92014-12-08 11:30:08 +0000436
Nils Diewald7c8ced22015-04-15 19:21:00 +0000437 menuItem.lowlight();
438 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
439
440 // Starting highlight - short
441 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
442 menuItem.highlight("c");
443 expect(menuItem.element().innerHTML).toEqual("<span><mark>C</mark>oreNLP</span>");
444
445 menuItem.lowlight();
446 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
447
448 // Highlight at the end
449 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
450 menuItem.highlight("nlp");
451 expect(menuItem.element().innerHTML).toEqual("<span>Core<mark>NLP</mark></span>");
452
453 menuItem.lowlight();
454 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
455
456 // Highlight at the end - short
457 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
458 menuItem.highlight("p");
459 expect(menuItem.element().innerHTML).toEqual("<span>CoreNL<mark>P</mark></span>");
460
461 menuItem.lowlight();
462 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
463
464 // No highlight
465 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']);
466 menuItem.highlight("xp");
467 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
468
469 menuItem.lowlight();
470 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>");
471
472 // Highlight in the middle - first
473 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
474
475 menuItem.highlight("ren");
476 expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span><span class=\"desc\">This is my Example</span>");
477
478 menuItem.lowlight();
479 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
480
481 // Highlight in the middle - second
482 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
483 menuItem.highlight("ampl");
484 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Ex<mark>ampl</mark>e</span>');
485
486 menuItem.lowlight();
487 expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>');
488
489 // Highlight in the middle - both
490 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
491
492 menuItem.highlight("e");
493 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>');
494
495 menuItem.lowlight();
496 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
497
498 // Highlight in the end - second
499 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
500 menuItem.highlight("le");
501 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Examp<mark>le</mark></span>");
502
503 menuItem.lowlight();
504 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
505
506 // Highlight at the beginning - second
507 menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']);
508 menuItem.highlight("this");
509 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\"><mark>This</mark> is my Example</span>");
510
511 menuItem.lowlight();
512 expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>");
Akron6ed13992016-05-23 18:06:05 +0200513
Nils Diewald7c8ced22015-04-15 19:21:00 +0000514 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000515 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000516
Nils Diewald7c8ced22015-04-15 19:21:00 +0000517 describe('KorAP.HintMenu', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000518 var list = [
519 ["Constituency", "c=", "Example 1"],
520 ["Lemma", "l="],
521 ["Morphology", "m=", "Example 2"],
522 ["Part-of-Speech", "p="],
523 ["Syntax", "syn="]
524 ];
Nils Diewald19ccee92014-12-08 11:30:08 +0000525
Nils Diewald7c8ced22015-04-15 19:21:00 +0000526 it('should be initializable', function () {
Nils Diewald7c8ced22015-04-15 19:21:00 +0000527 var menu = menuClass.create(null, "cnx/", list);
Nils Diewald7c8ced22015-04-15 19:21:00 +0000528 expect(menu.element().nodeName).toEqual('UL');
Akron65c74352016-09-02 17:23:39 +0200529 // expect(menu.element().style.opacity).toEqual("0");
Nils Diewald19ccee92014-12-08 11:30:08 +0000530
Nils Diewald7c8ced22015-04-15 19:21:00 +0000531 menu.limit(8);
Nils Diewald19ccee92014-12-08 11:30:08 +0000532
Nils Diewald7c8ced22015-04-15 19:21:00 +0000533 // view
534 menu.show();
Akron6ed13992016-05-23 18:06:05 +0200535 expect(menu.prefix()).toBe('');
Nils Diewald19ccee92014-12-08 11:30:08 +0000536
Nils Diewald7c8ced22015-04-15 19:21:00 +0000537 // First element in list
538 expect(menu.item(0).active()).toBe(true);
539 expect(menu.item(0).noMore()).toBe(true);
540
541 // Middle element in list
542 expect(menu.item(2).active()).toBe(false);
543 expect(menu.item(2).noMore()).toBe(false);
Nils Diewald19ccee92014-12-08 11:30:08 +0000544
Nils Diewald7c8ced22015-04-15 19:21:00 +0000545 // Last element in list
546 expect(menu.item(menu.length() - 1).active()).toBe(false);
547 expect(menu.item(menu.length() - 1).noMore()).toBe(true);
Akron6ed13992016-05-23 18:06:05 +0200548
549 expect(menu.shownItem(0).active()).toBeTruthy();
550 expect(menu.shownItem(0).lcField()).toEqual(' constituency example 1');
551 expect(menu.shownItem(1).lcField()).toEqual(' lemma');
552 expect(menu.shownItem(2).lcField()).toEqual(' morphology example 2');
553
554 menu.next();
555 expect(menu.shownItem(1).active()).toBeTruthy();
556
557 menu.next();
558 expect(menu.shownItem(2).active()).toBeTruthy();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000559 });
Nils Diewald19ccee92014-12-08 11:30:08 +0000560 });
561});