Akron | b46d8e3 | 2017-06-29 14:26:14 +0200 | [diff] [blame] | 1 | function matchInfoFactory () { |
| 2 | var info = document.createElement('div'); |
| 3 | info.className = 'matchinfo'; |
| 4 | info.innerHTML = |
| 5 | " <div class=\"matchtable\">" + |
| 6 | " <table>" + |
| 7 | " <thead>" + |
| 8 | " <tr>" + |
| 9 | " <th>Foundry</th>" + |
| 10 | " <th>Layer</th>" + |
| 11 | " <th>Der</th>" + |
| 12 | " <th>älteste</th>" + |
| 13 | " <th>lebende</th>" + |
| 14 | " <th>Baum</th>" + |
| 15 | " </tr>" + |
| 16 | " </thead>" + |
| 17 | " <tbody>" + |
| 18 | " <tr tabindex=\"0\">" + |
| 19 | " <th>corenlp</th>" + |
| 20 | " <th>p</th>" + |
| 21 | " <td>ART</td>" + |
| 22 | " <td>ADJA</td>" + |
| 23 | " <td>ADJA<br>ADJD</td>" + |
| 24 | " <td>NN</td>" + |
| 25 | " </tr>" + |
| 26 | " <tr tabindex=\"0\">" + |
| 27 | " <th>opennlp</th>" + |
| 28 | " <th>p</th>" + |
| 29 | " <td>ART</td>" + |
| 30 | " <td>ADJA</td>" + |
| 31 | " <td></td>" + |
| 32 | " <td>NN</td>" + |
| 33 | " </tr>" + |
| 34 | " </tbody>" + |
| 35 | " </table>" + |
| 36 | " </div>" + |
| 37 | "</div>"; |
| 38 | |
| 39 | return info; |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | define(['match/querycreator'], function (qcClass) { |
| 44 | |
| 45 | describe('KorAP.QueryCreator', function () { |
| 46 | |
| 47 | it('should be initializable', function () { |
| 48 | expect( |
| 49 | function() { |
| 50 | qcClass.create() |
| 51 | } |
| 52 | ).toThrow(new Error("Missing parameters")); |
| 53 | |
| 54 | expect( |
| 55 | function() { |
| 56 | qcClass.create("Test") |
| 57 | } |
| 58 | ).toThrow(new Error("Requires element")); |
| 59 | |
| 60 | expect( |
| 61 | function() { |
| 62 | var minfo = document.createElement('div'); |
| 63 | qcClass.create(minfo); |
| 64 | } |
| 65 | ).toThrow(new Error("Element contains no match table")); |
| 66 | |
| 67 | expect(qcClass.create(matchInfoFactory()).toString()).toEqual(""); |
| 68 | }); |
| 69 | |
| 70 | it('should listen to click events', function () { |
| 71 | |
| 72 | var matchInfo = matchInfoFactory(); |
| 73 | var qc = qcClass.create(matchInfo); |
| 74 | |
| 75 | // Nothing to show |
| 76 | expect(qc.toString()).toEqual(""); |
| 77 | expect(qc.shown()).toBe(false); |
| 78 | qc.show(); |
| 79 | expect(qc.shown()).toBe(false); |
| 80 | expect(qc.element().className).toEqual("queryfragment"); |
| 81 | |
| 82 | // Click on cell 0:0 "Foundry" |
| 83 | var cell = matchInfo.querySelector("thead > tr > th:first-child"); |
| 84 | expect(cell.innerText).toEqual("Foundry"); |
| 85 | cell.click(); |
| 86 | expect(cell.classList.contains("chosen")).toBe(false); |
| 87 | expect(qc.toString()).toEqual(""); |
| 88 | expect(qc.shown()).toBe(false); |
| 89 | |
| 90 | // Click on cell 0:2 "Der" |
| 91 | cell = matchInfo.querySelector("thead > tr > th:nth-child(3)") |
| 92 | expect(cell.innerText).toEqual("Der"); |
| 93 | cell.click(); |
| 94 | expect(cell.classList.contains("chosen")).toBeTruthy(); |
| 95 | expect(qc.toString()).toEqual("[orth=Der]"); |
| 96 | expect(qc.shown()).toBeTruthy(); |
| 97 | |
| 98 | // Click on cell 0:2 "Der" again - to hide |
| 99 | cell = matchInfo.querySelector("thead > tr > th:nth-child(3)") |
| 100 | expect(cell.innerText).toEqual("Der"); |
| 101 | cell.click(); |
| 102 | expect(cell.classList.contains("chosen")).toBe(false); |
| 103 | expect(qc.toString()).toEqual(""); |
| 104 | expect(qc.shown()).toBe(false); |
| 105 | }); |
| 106 | |
| 107 | it('should create tokens in arbitrary order', function () { |
| 108 | var matchInfo = matchInfoFactory(); |
| 109 | var qc = qcClass.create(matchInfo); |
| 110 | |
| 111 | var cell = matchInfo.querySelector("tbody > tr:nth-child(2) > td:nth-child(4)"); |
| 112 | expect(cell.innerText).toEqual("ADJA"); |
| 113 | cell.click(); |
| 114 | expect(qc.toString()).toEqual("[opennlp/p=ADJA]"); |
| 115 | |
| 116 | cell = matchInfo.querySelector("thead > tr > th:nth-child(4)"); |
| 117 | expect(cell.innerText).toEqual("älteste"); |
| 118 | cell.click(); |
| 119 | expect(qc.toString()).toEqual("[opennlp/p=ADJA & orth=älteste]"); |
| 120 | |
| 121 | cell = matchInfo.querySelector("tbody > tr > td:nth-child(4)"); |
| 122 | expect(cell.innerText).toEqual("ADJA"); |
| 123 | cell.click(); |
| 124 | expect(qc.toString()).toEqual("[corenlp/p=ADJA & opennlp/p=ADJA & orth=älteste]"); |
| 125 | }); |
| 126 | |
| 127 | it('should create token sequences in arbitrary order', function () { |
| 128 | var matchInfo = matchInfoFactory(); |
| 129 | var qc = qcClass.create(matchInfo); |
| 130 | |
| 131 | var cell = matchInfo.querySelector("thead > tr > th:nth-child(5)"); |
| 132 | expect(cell.innerText).toEqual("lebende"); |
| 133 | cell.click(); |
| 134 | expect(qc.toString()).toEqual("[orth=lebende]"); |
| 135 | |
| 136 | cell = matchInfo.querySelector("tbody > tr:nth-child(2) > td:nth-child(3)"); |
| 137 | expect(cell.innerText).toEqual("ART"); |
| 138 | cell.click(); |
| 139 | expect(qc.toString()).toEqual("[opennlp/p=ART][orth=lebende]"); |
| 140 | |
| 141 | cell = matchInfo.querySelector("tbody > tr > td:nth-child(4)"); |
| 142 | expect(cell.innerText).toEqual("ADJA"); |
| 143 | cell.click(); |
| 144 | expect(qc.toString()).toEqual("[opennlp/p=ART][corenlp/p=ADJA][orth=lebende]"); |
| 145 | }); |
| 146 | |
| 147 | it('should remove chosen elements again', function () { |
| 148 | var matchInfo = matchInfoFactory(); |
| 149 | var qc = qcClass.create(matchInfo); |
| 150 | |
| 151 | var cell = matchInfo.querySelector("tbody > tr:nth-child(2) > td:nth-child(4)"); |
| 152 | expect(cell.innerText).toEqual("ADJA"); |
| 153 | cell.click(); |
| 154 | expect(qc.toString()).toEqual("[opennlp/p=ADJA]"); |
| 155 | var cell1 = cell; |
| 156 | |
| 157 | cell = matchInfo.querySelector("thead > tr > th:nth-child(4)"); |
| 158 | expect(cell.innerText).toEqual("älteste"); |
| 159 | cell.click(); |
| 160 | expect(qc.toString()).toEqual("[opennlp/p=ADJA & orth=älteste]"); |
| 161 | var cell2 = cell; |
| 162 | |
| 163 | cell = matchInfo.querySelector("tbody > tr > td:nth-child(3)"); |
| 164 | expect(cell.innerText).toEqual("ART"); |
| 165 | cell.click(); |
| 166 | expect(qc.toString()).toEqual("[corenlp/p=ART][opennlp/p=ADJA & orth=älteste]"); |
| 167 | var cell3 = cell; |
| 168 | |
| 169 | cell = matchInfo.querySelector("thead > tr > th:nth-child(6)"); |
| 170 | expect(cell.innerText).toEqual("Baum"); |
| 171 | cell.click(); |
| 172 | expect(qc.toString()).toEqual("[corenlp/p=ART][opennlp/p=ADJA & orth=älteste][orth=Baum]"); |
| 173 | var cell4 = cell; |
| 174 | |
| 175 | cell = matchInfo.querySelector("thead > tr > th:nth-child(5)"); |
| 176 | expect(cell.innerText).toEqual("lebende"); |
| 177 | cell.click(); |
| 178 | expect(qc.toString()).toEqual("[corenlp/p=ART][opennlp/p=ADJA & orth=älteste][orth=lebende][orth=Baum]"); |
| 179 | var cell5 = cell; |
| 180 | |
| 181 | |
| 182 | // Remove annotations again |
| 183 | expect(cell5.classList.contains("chosen")).toBeTruthy(); |
| 184 | cell5.click() |
| 185 | expect(cell5.classList.contains("chosen")).toBe(false); |
| 186 | expect(qc.toString()).toEqual("[corenlp/p=ART][opennlp/p=ADJA & orth=älteste][orth=Baum]"); |
| 187 | |
| 188 | expect(cell2.classList.contains("chosen")).toBeTruthy(); |
| 189 | cell2.click() |
| 190 | expect(cell2.classList.contains("chosen")).toBe(false); |
| 191 | expect(qc.toString()).toEqual("[corenlp/p=ART][opennlp/p=ADJA][orth=Baum]"); |
| 192 | |
| 193 | expect(cell1.classList.contains("chosen")).toBeTruthy(); |
| 194 | cell1.click() |
| 195 | expect(cell1.classList.contains("chosen")).toBe(false); |
| 196 | expect(qc.toString()).toEqual("[corenlp/p=ART][orth=Baum]"); |
| 197 | |
| 198 | // Re-add first cell at the same position |
| 199 | expect(cell1.classList.contains("chosen")).toBe(false); |
| 200 | cell1.click() |
| 201 | expect(cell1.classList.contains("chosen")).toBeTruthy(); |
| 202 | expect(qc.toString()).toEqual("[corenlp/p=ART][opennlp/p=ADJA][orth=Baum]"); |
| 203 | |
| 204 | expect(cell3.classList.contains("chosen")).toBeTruthy(); |
| 205 | cell3.click() |
| 206 | expect(cell3.classList.contains("chosen")).toBe(false); |
| 207 | expect(qc.toString()).toEqual("[opennlp/p=ADJA][orth=Baum]"); |
| 208 | |
| 209 | expect(cell4.classList.contains("chosen")).toBeTruthy(); |
| 210 | cell4.click() |
| 211 | expect(cell4.classList.contains("chosen")).toBe(false); |
| 212 | expect(qc.toString()).toEqual("[opennlp/p=ADJA]"); |
| 213 | |
| 214 | // Remove last token |
| 215 | expect(qc.shown()).toBeTruthy(); |
| 216 | expect(qc.element().innerHTML).toEqual("<span>New Query:</span><span>[opennlp/p=ADJA]</span>"); |
| 217 | expect(cell1.classList.contains("chosen")).toBeTruthy(); |
| 218 | cell1.click() |
| 219 | expect(cell1.classList.contains("chosen")).toBe(false); |
| 220 | expect(qc.toString()).toEqual(""); |
| 221 | expect(qc.shown()).toBe(false); |
| 222 | |
| 223 | // Re-add token |
| 224 | expect(cell4.classList.contains("chosen")).toBe(false); |
| 225 | cell4.click() |
| 226 | expect(cell4.classList.contains("chosen")).toBeTruthy(); |
| 227 | expect(qc.toString()).toEqual("[orth=Baum]"); |
| 228 | expect(qc.shown()).toBeTruthy(); |
| 229 | expect(qc.element().innerHTML).toEqual("<span>New Query:</span><span>[orth=Baum]</span>"); |
| 230 | }); |
| 231 | |
Akron | c8461bf | 2017-06-29 15:05:24 +0200 | [diff] [blame] | 232 | it('should ignore empty terms', function () { |
| 233 | var matchInfo = matchInfoFactory(); |
| 234 | var qc = qcClass.create(matchInfo); |
Akron | b46d8e3 | 2017-06-29 14:26:14 +0200 | [diff] [blame] | 235 | |
Akron | c8461bf | 2017-06-29 15:05:24 +0200 | [diff] [blame] | 236 | // Nothing to show |
| 237 | expect(qc.toString()).toEqual(""); |
| 238 | expect(qc.shown()).toBe(false); |
| 239 | qc.show(); |
| 240 | expect(qc.shown()).toBe(false); |
| 241 | expect(qc.element().className).toEqual("queryfragment"); |
| 242 | |
| 243 | var cell = matchInfo.querySelector("tbody > tr:nth-child(2) > td:nth-child(4)"); |
| 244 | expect(cell.innerText).toEqual("ADJA"); |
| 245 | expect(cell.classList.contains("chosen")).toBe(false); |
| 246 | cell.click(); |
| 247 | expect(cell.classList.contains("chosen")).toBeTruthy(); |
| 248 | expect(qc.toString()).toEqual("[opennlp/p=ADJA]"); |
| 249 | |
| 250 | cell = matchInfo.querySelector("tbody > tr:nth-child(2) > td:nth-child(5)"); |
| 251 | expect(cell.innerText).toEqual(""); |
| 252 | expect(cell.classList.contains("chosen")).toBe(false); |
| 253 | cell.click(); |
| 254 | expect(cell.classList.contains("chosen")).toBe(false); |
| 255 | expect(qc.toString()).toEqual("[opennlp/p=ADJA]"); |
| 256 | |
| 257 | }); |
| 258 | |
| 259 | it('should create groups for multiple terms', function () { |
| 260 | var matchInfo = matchInfoFactory(); |
| 261 | var qc = qcClass.create(matchInfo); |
| 262 | |
| 263 | // Nothing to show |
| 264 | expect(qc.toString()).toEqual(""); |
| 265 | expect(qc.shown()).toBe(false); |
| 266 | qc.show(); |
| 267 | expect(qc.shown()).toBe(false); |
| 268 | expect(qc.element().className).toEqual("queryfragment"); |
| 269 | |
| 270 | var cell = matchInfo.querySelector("thead > tr > th:nth-child(5)"); |
| 271 | expect(cell.innerText).toEqual("lebende"); |
| 272 | expect(cell.classList.contains("chosen")).toBe(false); |
| 273 | cell.click(); |
| 274 | expect(cell.classList.contains("chosen")).toBeTruthy(); |
| 275 | expect(qc.toString()).toEqual("[orth=lebende]"); |
| 276 | |
| 277 | cell = matchInfo.querySelector("tbody > tr:nth-child(1) > td:nth-child(5)"); |
| 278 | expect(cell.innerText).toEqual("ADJAADJD"); |
| 279 | expect(cell.classList.contains("chosen")).toBe(false); |
| 280 | cell.click(); |
| 281 | expect(cell.classList.contains("chosen")).toBeTruthy(); |
| 282 | expect(qc.toString()).toEqual("[(corenlp/p=ADJA | corenlp/p=ADJD) & orth=lebende]"); |
| 283 | |
| 284 | // Remove or group again |
| 285 | expect(cell.classList.contains("chosen")).toBeTruthy(); |
| 286 | cell.click(); |
| 287 | expect(cell.classList.contains("chosen")).toBe(false); |
| 288 | expect(qc.toString()).toEqual("[orth=lebende]"); |
| 289 | }); |
| 290 | |
| 291 | |
| 292 | it('should add whole rows', function () { |
| 293 | var matchInfo = matchInfoFactory(); |
| 294 | var qc = qcClass.create(matchInfo); |
| 295 | |
| 296 | // Nothing to show |
| 297 | expect(qc.toString()).toEqual(""); |
| 298 | expect(qc.shown()).toBe(false); |
| 299 | qc.show(); |
| 300 | expect(qc.shown()).toBe(false); |
| 301 | expect(qc.element().className).toEqual("queryfragment"); |
| 302 | |
| 303 | var corenlpRow = matchInfo.querySelector("tbody > tr:nth-child(1)"); |
| 304 | |
| 305 | var cell = corenlpRow.querySelector("td:nth-child(4)"); |
| 306 | expect(cell.innerText).toEqual("ADJA"); |
| 307 | expect(cell.classList.contains("chosen")).toBe(false); |
| 308 | cell.click(); |
| 309 | expect(cell.classList.contains("chosen")).toBeTruthy(); |
| 310 | |
| 311 | // Activate another cell in another row |
| 312 | cell = matchInfo.querySelector("tbody > tr:nth-child(2) td:nth-child(3)"); |
| 313 | expect(cell.innerText).toEqual("ART"); |
| 314 | expect(cell.classList.contains("chosen")).toBe(false); |
| 315 | cell.click(); |
| 316 | expect(cell.classList.contains("chosen")).toBeTruthy(); |
| 317 | |
| 318 | expect(corenlpRow.querySelector("td:nth-child(3).chosen")).toBeNull(); |
| 319 | expect(corenlpRow.querySelector("td:nth-child(4).chosen")).toBeTruthy(); |
| 320 | expect(corenlpRow.querySelector("td:nth-child(5).chosen")).toBeNull(); |
| 321 | expect(corenlpRow.querySelector("td:nth-child(6).chosen")).toBeNull(); |
| 322 | |
| 323 | expect(qc.toString()).toEqual("[opennlp/p=ART][corenlp/p=ADJA]"); |
| 324 | |
| 325 | // Mark all corenlp lists |
| 326 | cell = corenlpRow.querySelector("th:nth-child(1)"); |
| 327 | expect(cell.innerText).toEqual("corenlp"); |
| 328 | expect(cell.classList.contains("chosen")).toBe(false); |
| 329 | cell.click(); |
| 330 | expect(cell.classList.contains("chosen")).toBe(false); |
| 331 | |
| 332 | expect(corenlpRow.querySelector("td:nth-child(3).chosen")).toBeTruthy(); |
| 333 | expect(corenlpRow.querySelector("td:nth-child(4).chosen")).toBeTruthy(); |
| 334 | expect(corenlpRow.querySelector("td:nth-child(5).chosen")).toBeTruthy(); |
| 335 | expect(corenlpRow.querySelector("td:nth-child(6).chosen")).toBeTruthy(); |
| 336 | |
| 337 | // Replay the choice without any effect |
| 338 | cell = corenlpRow.querySelector("th:nth-child(1)"); |
| 339 | expect(cell.innerText).toEqual("corenlp"); |
| 340 | expect(cell.classList.contains("chosen")).toBe(false); |
| 341 | cell.click(); |
| 342 | expect(cell.classList.contains("chosen")).toBe(false); |
| 343 | |
| 344 | expect(corenlpRow.querySelector("td:nth-child(3).chosen")).toBeTruthy(); |
| 345 | expect(corenlpRow.querySelector("td:nth-child(4).chosen")).toBeTruthy(); |
| 346 | expect(corenlpRow.querySelector("td:nth-child(5).chosen")).toBeTruthy(); |
| 347 | expect(corenlpRow.querySelector("td:nth-child(6).chosen")).toBeTruthy(); |
| 348 | |
| 349 | expect(qc.toString()).toEqual("[corenlp/p=ART & opennlp/p=ART][corenlp/p=ADJA][(corenlp/p=ADJA | corenlp/p=ADJD)][corenlp/p=NN]"); |
| 350 | |
| 351 | // Remove one of the cells again |
| 352 | cell = corenlpRow.querySelector("td:nth-child(5).chosen"); |
| 353 | expect(cell.innerText).toEqual("ADJAADJD"); |
| 354 | expect(cell.classList.contains("chosen")).toBeTruthy(); |
| 355 | cell.click(); |
| 356 | expect(cell.classList.contains("chosen")).toBe(false); |
| 357 | expect(qc.toString()).toEqual("[corenlp/p=ART & opennlp/p=ART][corenlp/p=ADJA][corenlp/p=NN]"); |
| 358 | |
| 359 | }); |
Akron | b46d8e3 | 2017-06-29 14:26:14 +0200 | [diff] [blame] | 360 | }); |
| 361 | }); |