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( |
| 49 | input |
| 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 | 00cd4d1 | 2016-05-31 21:01:11 +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=")) |
| 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 | "-" : [ |
| 148 | ["Base Annotation", "base/s=", "Structure"], |
| 149 | ["CoreNLP", "corenlp/", "Constituency, Named Entities, Part-of-Speech"] |
| 150 | ], |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 151 | "corenlp/" : [ |
| 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"] |
| 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({ |
| 172 | inputField : input |
| 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 | |
| 178 | it('should alert at char pos', function () { |
| 179 | var hint = hintClass.create({ |
| 180 | inputField : input |
| 181 | }); |
| 182 | |
| 183 | expect(hint.active()).toBeFalsy(); |
| 184 | |
| 185 | expect(hint.alert(4, 'That does not work!')).toBeTruthy(); |
| 186 | |
| 187 | expect(hint.active()).toBeTruthy(); |
| 188 | |
| 189 | var container = hint.inputField().container(); |
| 190 | expect(container.firstChild.classList.contains('hint')).toBe(true); |
| 191 | expect(container.firstChild.classList.contains('alert')).toBe(true); |
| 192 | expect(container.firstChild.textContent).toEqual('That does not work!'); |
| 193 | expect(hint.inputField().mirrorValue()).toEqual('abcd'); |
| 194 | |
| 195 | expect(hint.alert(4, 'That does not work!')).toBeFalsy(); |
| 196 | |
| 197 | // Update - meaning: hide alert |
| 198 | hint.update(); |
| 199 | |
| 200 | expect(hint.alert().active).toBeFalsy(); |
| 201 | |
| 202 | expect(hint.active()).toBeFalsy(); |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 203 | }); |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 204 | |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 205 | it('should view main menu on default', function () { |
| 206 | var hint = hintClass.create({ |
Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 207 | inputField : input |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 208 | }); |
| 209 | |
| 210 | expect(hint.active()).toBeFalsy(); |
| 211 | |
| 212 | hint.inputField().insert('der Baum corenlp/'); |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 213 | |
Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 214 | var cont = hint.inputField().container(); |
| 215 | |
| 216 | expect(cont.getElementsByTagName('div').length).toBe(1); |
| 217 | expect(cont.getElementsByTagName('ul').length).toBe(0); |
| 218 | expect(cont.firstChild).toEqual(cont.firstChild); |
| 219 | |
| 220 | // Show menu, if a relevant context exists |
| 221 | // There is a menu for corenlp/ |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 222 | hint.show(false); |
| 223 | |
Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 224 | expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1); |
| 225 | expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(3); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame^] | 226 | |
Akron | 8eaeb2e | 2016-08-29 18:26:28 +0200 | [diff] [blame] | 227 | // Hide the menu and focus on the input |
| 228 | hint.unshow(); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame^] | 229 | |
| 230 | expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(1); |
| 231 | expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(0); |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 232 | |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 233 | hint.unshow(); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame^] | 234 | |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 235 | hint.inputField().insert(' hhhh'); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame^] | 236 | |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 237 | // show with context |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 238 | hint.show(false); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame^] | 239 | |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 240 | expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(4); |
| 241 | expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1); |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 242 | expect(hint.inputField().container().getElementsByTagName('li').length).toEqual(2); |
| 243 | |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 244 | hint.unshow(); |
| 245 | hint.inputField().insert(' aaaa/'); |
| 246 | |
| 247 | // show with context |
| 248 | hint.show(true); |
| 249 | |
Akron | ee9ef4a | 2016-06-03 12:50:08 +0200 | [diff] [blame] | 250 | expect(hint.inputField().container().getElementsByTagName('div').length).toEqual(4); |
| 251 | expect(hint.inputField().container().getElementsByTagName('ul').length).toEqual(1); |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 252 | }); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame^] | 253 | |
Akron | 02360e4 | 2016-06-07 13:41:12 +0200 | [diff] [blame] | 254 | xit('should remove all menus on escape'); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 255 | }); |
| 256 | |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame^] | 257 | |
| 258 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 259 | describe('KorAP.HintMenuItem', function () { |
| 260 | it('should be initializable', function () { |
| 261 | expect( |
| 262 | function() { menuItemClass.create([]) } |
| 263 | ).toThrow(new Error("Missing parameters")); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 264 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 265 | expect( |
| 266 | function() { menuItemClass.create(['CoreNLP']) } |
| 267 | ).toThrow(new Error("Missing parameters")); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 268 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 269 | var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']); |
| 270 | expect(menuItem.name()).toEqual('CoreNLP'); |
| 271 | expect(menuItem.action()).toEqual('corenlp/'); |
| 272 | expect(menuItem.desc()).toBeUndefined(); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 273 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 274 | menuItem = menuItemClass.create( |
| 275 | ['CoreNLP', 'corenlp/', 'It\'s funny'] |
| 276 | ); |
| 277 | expect(menuItem.name()).toEqual('CoreNLP'); |
| 278 | expect(menuItem.action()).toEqual('corenlp/'); |
| 279 | expect(menuItem.desc()).not.toBeUndefined(); |
| 280 | expect(menuItem.desc()).toEqual('It\'s funny'); |
| 281 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 282 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 283 | it('should have an element', function () { |
| 284 | var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']); |
| 285 | expect(menuItem.element()).not.toBe(undefined); |
| 286 | expect(menuItem.element().nodeName).toEqual("LI"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 287 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 288 | var title = menuItem.element().firstChild; |
| 289 | expect(title.nodeName).toEqual("SPAN"); |
| 290 | expect(title.firstChild.nodeType).toEqual(3); |
| 291 | expect(title.firstChild.nodeValue).toEqual("CoreNLP"); |
| 292 | expect(menuItem.element().childNodes[0]).not.toBe(undefined); |
| 293 | expect(menuItem.element().childNodes[1]).toBe(undefined); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 294 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 295 | menuItem = menuItemClass.create( |
| 296 | ['CoreNLP', 'corenlp/', 'my DescRiption'] |
| 297 | ); |
| 298 | expect(menuItem.element()).not.toBe(undefined); |
| 299 | expect(menuItem.element().nodeName).toEqual("LI"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 300 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 301 | title = menuItem.element().firstChild; |
| 302 | expect(title.nodeName).toEqual("SPAN"); |
| 303 | expect(title.firstChild.nodeType).toEqual(3); // TextNode |
| 304 | expect(title.firstChild.nodeValue).toEqual("CoreNLP"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 305 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 306 | expect(menuItem.element().childNodes[0]).not.toBe(undefined); |
| 307 | expect(menuItem.element().childNodes[1]).not.toBe(undefined); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 308 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 309 | var desc = menuItem.element().lastChild; |
| 310 | expect(desc.nodeName).toEqual("SPAN"); |
| 311 | expect(desc.firstChild.nodeType).toEqual(3); // TextNode |
| 312 | expect(desc.firstChild.nodeValue).toEqual("my DescRiption"); |
| 313 | }); |
Nils Diewald | 1c54692 | 2015-04-13 01:56:19 +0000 | [diff] [blame] | 314 | |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 315 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 316 | it('should be activatable and deactivateable by class', function () { |
| 317 | var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']); |
| 318 | expect(menuItem.active()).toBe(false); |
| 319 | expect(menuItem.element().getAttribute("class")).toBe(null); |
| 320 | menuItem.active(true); |
| 321 | expect(menuItem.active()).toBe(true); |
| 322 | expect(menuItem.element().getAttribute("class")).toEqual("active"); |
| 323 | menuItem.active(false); // Is active |
| 324 | expect(menuItem.active()).toBe(false); |
| 325 | expect(menuItem.element().getAttribute("class")).toEqual(""); |
| 326 | menuItem.active(true); |
| 327 | expect(menuItem.active()).toBe(true); |
| 328 | expect(menuItem.element().getAttribute("class")).toEqual("active"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 329 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 330 | menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']); |
| 331 | expect(menuItem.active()).toBe(false); |
| 332 | expect(menuItem.element().getAttribute("class")).toBe(null); |
| 333 | menuItem.active(false); // Is not active |
| 334 | expect(menuItem.active()).toBe(false); |
| 335 | expect(menuItem.element().getAttribute("class")).toBe(null); |
| 336 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 337 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 338 | it('should be set to boundary', function () { |
| 339 | var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']); |
| 340 | expect(menuItem.active()).toBe(false); |
| 341 | expect(menuItem.element().getAttribute("class")).toBe(null); |
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 | // Set active |
| 344 | menuItem.active(true); |
| 345 | expect(menuItem.active()).toBe(true); |
| 346 | expect(menuItem.noMore()).toBe(false); |
| 347 | expect(menuItem.element().getAttribute("class")).toEqual("active"); |
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 | // Set no more |
| 350 | menuItem.noMore(true); |
| 351 | expect(menuItem.active()).toBe(true); |
| 352 | expect(menuItem.noMore()).toBe(true); |
| 353 | expect(menuItem.element().getAttribute("class")).toEqual("active no-more"); |
Nils Diewald | 1c54692 | 2015-04-13 01:56:19 +0000 | [diff] [blame] | 354 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 355 | // No no more |
| 356 | menuItem.noMore(false); |
| 357 | expect(menuItem.active()).toBe(true); |
| 358 | expect(menuItem.noMore()).toBe(false); |
| 359 | expect(menuItem.element().getAttribute("class")).toEqual("active"); |
Nils Diewald | 1c54692 | 2015-04-13 01:56:19 +0000 | [diff] [blame] | 360 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 361 | // Set no more, deactivate |
| 362 | menuItem.noMore(true); |
| 363 | menuItem.active(false); |
| 364 | expect(menuItem.active()).toBe(false); |
| 365 | expect(menuItem.noMore()).toBe(true); |
| 366 | expect(menuItem.element().getAttribute("class")).toEqual("no-more"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 367 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 368 | // Set active |
| 369 | menuItem.active(true); |
| 370 | expect(menuItem.active()).toBe(true); |
| 371 | expect(menuItem.noMore()).toBe(true); |
| 372 | expect(menuItem.element().getAttribute("class")).toEqual("no-more active"); |
| 373 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 374 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 375 | it('should be highlightable', function () { |
| 376 | // Highlight in the middle |
| 377 | var menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']); |
| 378 | menuItem.highlight("ren"); |
| 379 | expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span>"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 380 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 381 | menuItem.lowlight(); |
| 382 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 383 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 384 | // Starting highlight |
| 385 | menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']); |
| 386 | menuItem.highlight("cor"); |
| 387 | expect(menuItem.element().innerHTML).toEqual("<span><mark>Cor</mark>eNLP</span>"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 388 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 389 | menuItem.lowlight(); |
| 390 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>"); |
| 391 | |
| 392 | // Starting highlight - short |
| 393 | menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']); |
| 394 | menuItem.highlight("c"); |
| 395 | expect(menuItem.element().innerHTML).toEqual("<span><mark>C</mark>oreNLP</span>"); |
| 396 | |
| 397 | menuItem.lowlight(); |
| 398 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>"); |
| 399 | |
| 400 | // Highlight at the end |
| 401 | menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']); |
| 402 | menuItem.highlight("nlp"); |
| 403 | expect(menuItem.element().innerHTML).toEqual("<span>Core<mark>NLP</mark></span>"); |
| 404 | |
| 405 | menuItem.lowlight(); |
| 406 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>"); |
| 407 | |
| 408 | // Highlight at the end - short |
| 409 | menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']); |
| 410 | menuItem.highlight("p"); |
| 411 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNL<mark>P</mark></span>"); |
| 412 | |
| 413 | menuItem.lowlight(); |
| 414 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>"); |
| 415 | |
| 416 | // No highlight |
| 417 | menuItem = menuItemClass.create(['CoreNLP', 'corenlp/']); |
| 418 | menuItem.highlight("xp"); |
| 419 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>"); |
| 420 | |
| 421 | menuItem.lowlight(); |
| 422 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span>"); |
| 423 | |
| 424 | // Highlight in the middle - first |
| 425 | menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']); |
| 426 | |
| 427 | menuItem.highlight("ren"); |
| 428 | expect(menuItem.element().innerHTML).toEqual("<span>Co<mark>reN</mark>LP</span><span class=\"desc\">This is my Example</span>"); |
| 429 | |
| 430 | menuItem.lowlight(); |
| 431 | expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>'); |
| 432 | |
| 433 | // Highlight in the middle - second |
| 434 | menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']); |
| 435 | menuItem.highlight("ampl"); |
| 436 | expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Ex<mark>ampl</mark>e</span>'); |
| 437 | |
| 438 | menuItem.lowlight(); |
| 439 | expect(menuItem.element().innerHTML).toEqual('<span>CoreNLP</span><span class="desc">This is my Example</span>'); |
| 440 | |
| 441 | // Highlight in the middle - both |
| 442 | menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']); |
| 443 | |
| 444 | menuItem.highlight("e"); |
| 445 | 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>'); |
| 446 | |
| 447 | menuItem.lowlight(); |
| 448 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>"); |
| 449 | |
| 450 | // Highlight in the end - second |
| 451 | menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']); |
| 452 | menuItem.highlight("le"); |
| 453 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Examp<mark>le</mark></span>"); |
| 454 | |
| 455 | menuItem.lowlight(); |
| 456 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\">This is my Example</span>"); |
| 457 | |
| 458 | // Highlight at the beginning - second |
| 459 | menuItem = menuItemClass.create(['CoreNLP', 'corenlp/', 'This is my Example']); |
| 460 | menuItem.highlight("this"); |
| 461 | expect(menuItem.element().innerHTML).toEqual("<span>CoreNLP</span><span class=\"desc\"><mark>This</mark> is my Example</span>"); |
| 462 | |
| 463 | menuItem.lowlight(); |
| 464 | 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] | 465 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 466 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 467 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 468 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 469 | describe('KorAP.HintMenu', function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 470 | var list = [ |
| 471 | ["Constituency", "c=", "Example 1"], |
| 472 | ["Lemma", "l="], |
| 473 | ["Morphology", "m=", "Example 2"], |
| 474 | ["Part-of-Speech", "p="], |
| 475 | ["Syntax", "syn="] |
| 476 | ]; |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 477 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 478 | it('should be initializable', function () { |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 479 | var menu = menuClass.create(null, "cnx/", list); |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 480 | expect(menu.element().nodeName).toEqual('UL'); |
Akron | 65c7435 | 2016-09-02 17:23:39 +0200 | [diff] [blame^] | 481 | // expect(menu.element().style.opacity).toEqual("0"); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 482 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 483 | menu.limit(8); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 484 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 485 | // view |
| 486 | menu.show(); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 487 | expect(menu.prefix()).toBe(''); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 488 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 489 | // First element in list |
| 490 | expect(menu.item(0).active()).toBe(true); |
| 491 | expect(menu.item(0).noMore()).toBe(true); |
| 492 | |
| 493 | // Middle element in list |
| 494 | expect(menu.item(2).active()).toBe(false); |
| 495 | expect(menu.item(2).noMore()).toBe(false); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 496 | |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 497 | // Last element in list |
| 498 | expect(menu.item(menu.length() - 1).active()).toBe(false); |
| 499 | expect(menu.item(menu.length() - 1).noMore()).toBe(true); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 500 | |
| 501 | expect(menu.shownItem(0).active()).toBeTruthy(); |
| 502 | expect(menu.shownItem(0).lcField()).toEqual(' constituency example 1'); |
| 503 | expect(menu.shownItem(1).lcField()).toEqual(' lemma'); |
| 504 | expect(menu.shownItem(2).lcField()).toEqual(' morphology example 2'); |
| 505 | |
| 506 | menu.next(); |
| 507 | expect(menu.shownItem(1).active()).toBeTruthy(); |
| 508 | |
| 509 | menu.next(); |
| 510 | expect(menu.shownItem(2).active()).toBeTruthy(); |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 511 | }); |
Nils Diewald | 19ccee9 | 2014-12-08 11:30:08 +0000 | [diff] [blame] | 512 | }); |
| 513 | }); |