Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 1 | function 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 8 | keyboardEvent[initMethod]( |
| 9 | type, |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 10 | true, // bubbles |
| 11 | true, // cancelable |
| 12 | window, // viewArg: should be window |
| 13 | false, // ctrlKeyArg |
| 14 | false, // altKeyArg |
| 15 | false, // shiftKeyArg |
| 16 | false, // metaKeyArg |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 17 | keyCode, // keyCodeArg : unsigned long the virtual key code, else 0 |
Akron | 308db38 | 2016-05-30 22:34:07 +0200 | [diff] [blame] | 18 | 0 // charCodeArgs : unsigned long the Unicode character |
| 19 | // associated with the depressed key, else 0 |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 20 | ); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 21 | element.dispatchEvent(keyboardEvent); |
| 22 | }; |
| 23 | |
| 24 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 25 | define(['hint'], function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 26 | 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 Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 31 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 32 | describe('KorAP.InputField', function () { |
| 33 | var input; |
Nils Diewald | 1c54692 | 2015-04-13 01:56:19 +0000 | [diff] [blame] | 34 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 35 | 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 Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 45 | }); |
| 46 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 47 | afterEach(function () { |
| 48 | document.getElementsByTagName("body")[0].removeChild( |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 49 | input |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 50 | ); |
| 51 | }); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 52 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 53 | afterAll(function () { |
| 54 | try { |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 55 | var mirrors = document.querySelectorAll(".hint.mirror"); |
| 56 | for (var i in mirrors) { |
| 57 | mirrors[i].parentNode.removeChild(mirrors[i]) |
| 58 | }; |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 59 | } |
| 60 | catch (e) {}; |
| 61 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 62 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 63 | it('should be initializable', function () { |
| 64 | // Supports: context, searchField |
| 65 | var inputField = inputClass.create(input); |
| 66 | expect(inputField._element).not.toBe(undefined); |
| 67 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 68 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 69 | it('should have text', function () { |
| 70 | expect(input.value).toEqual('abcdefghijklmno'); |
| 71 | var inputField = inputClass.create(input); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 72 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 73 | expect(inputField.value()).toEqual("abcdefghijklmno"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 74 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 75 | expect(input.selectionStart).toEqual(5); |
| 76 | expect(inputField.element().selectionStart).toEqual(5); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 77 | expect(inputField._split()[0]).toEqual("abcde"); |
| 78 | expect(inputField._split()[1]).toEqual("fghijklmno"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 79 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 80 | inputField.insert("xyz"); |
| 81 | expect(inputField.value()).toEqual('abcdexyzfghijklmno'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 82 | expect(inputField._split()[0]).toEqual("abcdexyz"); |
| 83 | expect(inputField._split()[1]).toEqual("fghijklmno"); |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 84 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 85 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 86 | 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 95 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 96 | 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 Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 101 | expect(inputField._split()[0]).toEqual("abcde"); |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 102 | expect(inputField.context()).toEqual("abcde"); |
| 103 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 104 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 105 | /* |
| 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 112 | }); |
| 113 | |
Nils Diewald | 1c54692 | 2015-04-13 01:56:19 +0000 | [diff] [blame] | 114 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 115 | 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 122 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 123 | 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=")) |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 131 | .toEqual("corenlp/ne_dewac_175m_600="); |
Akron | 113cc1a | 2016-01-22 21:17:57 +0100 | [diff] [blame] | 132 | 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-"); |
Akron | cff9bac | 2016-01-25 21:39:38 +0100 | [diff] [blame] | 136 | 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="); |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 139 | expect(analyzer.test("")).toEqual(undefined); |
| 140 | expect(analyzer.test("abcdecnx/")).toEqual("abcdecnx/"); |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 141 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 142 | }); |
| 143 | |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 144 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 145 | describe('KorAP.Hint', function () { |
| 146 | KorAP.hintArray = { |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 147 | "-" : [ |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 148 | ["Base Annotation", "base/s=", "Structure"], |
| 149 | ["CoreNLP", "corenlp/", "Constituency, Named Entities, Part-of-Speech"] |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 150 | ], |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 151 | "corenlp/" : [ |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 152 | ["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 Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 155 | ] |
| 156 | }; |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 157 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 158 | 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 168 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 169 | it('should be initializable', function () { |
| 170 | // Supports: context, searchField |
| 171 | var hint = hintClass.create({ |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 172 | inputField : input |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 173 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 174 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 175 | expect(hint).toBeTruthy(); |
| 176 | }); |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 177 | |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 178 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 179 | it('should alert at char pos', function () { |
| 180 | var hint = hintClass.create({ |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 181 | inputField : input |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 182 | }); |
| 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(); |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 202 | expect(hint.active()).toBeFalsy(); |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 203 | |
| 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(); |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 227 | }); |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 228 | |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 229 | |
| 230 | |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 231 | it('should view main menu on default', function () { |
| 232 | var hint = hintClass.create({ |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 233 | inputField : input |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 234 | }); |
| 235 | |
| 236 | expect(hint.active()).toBeFalsy(); |
| 237 | |
| 238 | hint.inputField().insert('der Baum corenlp/'); |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 239 | |
Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 240 | 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/ |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 248 | hint.show(false); |
| 249 | |
Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 250 | expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1); |
| 251 | expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(3); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 252 | |
Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 253 | // Hide the menu and focus on the input |
| 254 | hint.unshow(); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 255 | |
| 256 | expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(1); |
| 257 | expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(0); |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 258 | |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 259 | hint.unshow(); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 260 | |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 261 | hint.inputField().insert(' hhhh'); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 262 | |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 263 | // show with context |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 264 | hint.show(false); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 265 | |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 266 | expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(4); |
| 267 | expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1); |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 268 | expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(2); |
| 269 | |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 270 | hint.unshow(); |
| 271 | hint.inputField().insert(' aaaa/'); |
| 272 | |
| 273 | // show with context |
| 274 | hint.show(true); |
| 275 | |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 276 | 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(); |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 301 | }); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 302 | |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 303 | xit('should remove all menus on escape'); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 304 | }); |
| 305 | |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 306 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 307 | describe('KorAP.HintMenuItem', function () { |
| 308 | it('should be initializable', function () { |
| 309 | expect( |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 310 | function() { menuItemClass.create([]) } |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 311 | ).toThrow(new Error("Missing parameters")); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 312 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 313 | expect( |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 314 | function() { menuItemClass.create(['CoreNLP']) } |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 315 | ).toThrow(new Error("Missing parameters")); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 316 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 317 | var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']); |
| 318 | expect(menuItem.name()).toEqual('CoreNLP'); |
| 319 | expect(menuItem.action()).toEqual('corenlp/'); |
| 320 | expect(menuItem.desc()).toBeUndefined(); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 321 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 322 | menuItem = menuItemClass.create( |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 323 | ['CoreNLP', 'corenlp/', 'It\'s funny'] |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 324 | ); |
| 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 330 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 331 | 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 335 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 336 | 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 342 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 343 | menuItem = menuItemClass.create( |
Akron | 1a5a587 | 2016-09-05 20:17:14 +0200 | [diff] [blame^] | 344 | ['CoreNLP', 'corenlp/', 'my DescRiption'] |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 345 | ); |
| 346 | expect(menuItem.element()).not.toBe(undefined); |
| 347 | expect(menuItem.element().nodeName).toEqual("LI"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 348 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 349 | 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 353 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 354 | expect(menuItem.element().childNodes[0]).not.toBe(undefined); |
| 355 | expect(menuItem.element().childNodes[1]).not.toBe(undefined); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 356 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 357 | 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 Diewald | 1c54692 | 2015-04-13 01:56:19 +0000 | [diff] [blame] | 362 | |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 363 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 364 | 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 377 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 378 | 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 385 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 386 | 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 390 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 391 | // 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 396 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 397 | // 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 Diewald | 1c54692 | 2015-04-13 01:56:19 +0000 | [diff] [blame] | 402 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 403 | // 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 Diewald | 1c54692 | 2015-04-13 01:56:19 +0000 | [diff] [blame] | 408 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 409 | // 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 415 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 416 | // 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 422 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 423 | 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 428 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 429 | menuItem.lowlight(); |
| 430 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 431 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 432 | // 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 436 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 437 | 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>"); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 513 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 514 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 515 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 516 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 517 | describe('KorAP.HintMenu', function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 518 | 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 525 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 526 | it('should be initializable', function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 527 | var menu = menuClass.create(null, "cnx/", list); |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 528 | expect(menu.element().nodeName).toEqual('UL'); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame] | 529 | // expect(menu.element().style.opacity).toEqual("0"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 530 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 531 | menu.limit(8); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 532 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 533 | // view |
| 534 | menu.show(); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 535 | expect(menu.prefix()).toBe(''); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 536 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 537 | // 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 Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 544 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 545 | // Last element in list |
| 546 | expect(menu.item(menu.length() - 1).active()).toBe(false); |
| 547 | expect(menu.item(menu.length() - 1).noMore()).toBe(true); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 548 | |
| 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 Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 559 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 560 | }); |
| 561 | }); |