blob: 42c9c51c2453f72d38019d00b4f318f9eba44d3d [file] [log] [blame]
Akronb46d8e32017-06-29 14:26:14 +02001function 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>" +
Akrone9be11d2017-06-29 20:10:58 +020015 " <th>hier</th>" +
Akronb46d8e32017-06-29 14:26:14 +020016 " </tr>" +
17 " </thead>" +
18 " <tbody>" +
19 " <tr tabindex=\"0\">" +
20 " <th>corenlp</th>" +
21 " <th>p</th>" +
22 " <td>ART</td>" +
23 " <td>ADJA</td>" +
24 " <td>ADJA<br>ADJD</td>" +
25 " <td>NN</td>" +
Akrone9be11d2017-06-29 20:10:58 +020026 " <td>ADV</td>" +
Akronb46d8e32017-06-29 14:26:14 +020027 " </tr>" +
28 " <tr tabindex=\"0\">" +
29 " <th>opennlp</th>" +
30 " <th>p</th>" +
31 " <td>ART</td>" +
32 " <td>ADJA</td>" +
33 " <td></td>" +
34 " <td>NN</td>" +
Akrone9be11d2017-06-29 20:10:58 +020035 " <td>ADV</td>" +
Akronb46d8e32017-06-29 14:26:14 +020036 " </tr>" +
37 " </tbody>" +
38 " </table>" +
39 " </div>" +
40 "</div>";
41
42 return info;
43};
44
45
46define(['match/querycreator'], function (qcClass) {
47
48 describe('KorAP.QueryCreator', function () {
49
50 it('should be initializable', function () {
51 expect(
52 function() {
53 qcClass.create()
54 }
55 ).toThrow(new Error("Missing parameters"));
56
57 expect(
58 function() {
59 qcClass.create("Test")
60 }
61 ).toThrow(new Error("Requires element"));
62
63 expect(
64 function() {
65 var minfo = document.createElement('div');
66 qcClass.create(minfo);
67 }
68 ).toThrow(new Error("Element contains no match table"));
69
70 expect(qcClass.create(matchInfoFactory()).toString()).toEqual("");
71 });
72
73 it('should listen to click events', function () {
74
75 var matchInfo = matchInfoFactory();
76 var qc = qcClass.create(matchInfo);
77
78 // Nothing to show
79 expect(qc.toString()).toEqual("");
80 expect(qc.shown()).toBe(false);
81 qc.show();
82 expect(qc.shown()).toBe(false);
83 expect(qc.element().className).toEqual("queryfragment");
84
85 // Click on cell 0:0 "Foundry"
86 var cell = matchInfo.querySelector("thead > tr > th:first-child");
87 expect(cell.innerText).toEqual("Foundry");
88 cell.click();
89 expect(cell.classList.contains("chosen")).toBe(false);
90 expect(qc.toString()).toEqual("");
91 expect(qc.shown()).toBe(false);
92
93 // Click on cell 0:2 "Der"
94 cell = matchInfo.querySelector("thead > tr > th:nth-child(3)")
95 expect(cell.innerText).toEqual("Der");
96 cell.click();
97 expect(cell.classList.contains("chosen")).toBeTruthy();
98 expect(qc.toString()).toEqual("[orth=Der]");
99 expect(qc.shown()).toBeTruthy();
100
101 // Click on cell 0:2 "Der" again - to hide
102 cell = matchInfo.querySelector("thead > tr > th:nth-child(3)")
103 expect(cell.innerText).toEqual("Der");
104 cell.click();
105 expect(cell.classList.contains("chosen")).toBe(false);
106 expect(qc.toString()).toEqual("");
107 expect(qc.shown()).toBe(false);
108 });
109
110 it('should create tokens in arbitrary order', function () {
111 var matchInfo = matchInfoFactory();
112 var qc = qcClass.create(matchInfo);
113
114 var cell = matchInfo.querySelector("tbody > tr:nth-child(2) > td:nth-child(4)");
115 expect(cell.innerText).toEqual("ADJA");
116 cell.click();
117 expect(qc.toString()).toEqual("[opennlp/p=ADJA]");
118
119 cell = matchInfo.querySelector("thead > tr > th:nth-child(4)");
120 expect(cell.innerText).toEqual("älteste");
121 cell.click();
122 expect(qc.toString()).toEqual("[opennlp/p=ADJA & orth=älteste]");
123
124 cell = matchInfo.querySelector("tbody > tr > td:nth-child(4)");
125 expect(cell.innerText).toEqual("ADJA");
126 cell.click();
127 expect(qc.toString()).toEqual("[corenlp/p=ADJA & opennlp/p=ADJA & orth=älteste]");
128 });
129
130 it('should create token sequences in arbitrary order', function () {
131 var matchInfo = matchInfoFactory();
132 var qc = qcClass.create(matchInfo);
133
134 var cell = matchInfo.querySelector("thead > tr > th:nth-child(5)");
135 expect(cell.innerText).toEqual("lebende");
136 cell.click();
137 expect(qc.toString()).toEqual("[orth=lebende]");
138
139 cell = matchInfo.querySelector("tbody > tr:nth-child(2) > td:nth-child(3)");
140 expect(cell.innerText).toEqual("ART");
141 cell.click();
Akrone9be11d2017-06-29 20:10:58 +0200142 expect(qc.toString()).toEqual("[opennlp/p=ART][][orth=lebende]");
Akronb46d8e32017-06-29 14:26:14 +0200143
144 cell = matchInfo.querySelector("tbody > tr > td:nth-child(4)");
145 expect(cell.innerText).toEqual("ADJA");
146 cell.click();
147 expect(qc.toString()).toEqual("[opennlp/p=ART][corenlp/p=ADJA][orth=lebende]");
148 });
149
150 it('should remove chosen elements again', function () {
151 var matchInfo = matchInfoFactory();
152 var qc = qcClass.create(matchInfo);
153
154 var cell = matchInfo.querySelector("tbody > tr:nth-child(2) > td:nth-child(4)");
155 expect(cell.innerText).toEqual("ADJA");
156 cell.click();
157 expect(qc.toString()).toEqual("[opennlp/p=ADJA]");
158 var cell1 = cell;
159
160 cell = matchInfo.querySelector("thead > tr > th:nth-child(4)");
161 expect(cell.innerText).toEqual("älteste");
162 cell.click();
163 expect(qc.toString()).toEqual("[opennlp/p=ADJA & orth=älteste]");
164 var cell2 = cell;
165
166 cell = matchInfo.querySelector("tbody > tr > td:nth-child(3)");
167 expect(cell.innerText).toEqual("ART");
168 cell.click();
169 expect(qc.toString()).toEqual("[corenlp/p=ART][opennlp/p=ADJA & orth=älteste]");
170 var cell3 = cell;
171
172 cell = matchInfo.querySelector("thead > tr > th:nth-child(6)");
173 expect(cell.innerText).toEqual("Baum");
174 cell.click();
Akrone9be11d2017-06-29 20:10:58 +0200175 expect(qc.toString()).toEqual("[corenlp/p=ART][opennlp/p=ADJA & orth=älteste][][orth=Baum]");
Akronb46d8e32017-06-29 14:26:14 +0200176 var cell4 = cell;
177
178 cell = matchInfo.querySelector("thead > tr > th:nth-child(5)");
179 expect(cell.innerText).toEqual("lebende");
180 cell.click();
181 expect(qc.toString()).toEqual("[corenlp/p=ART][opennlp/p=ADJA & orth=älteste][orth=lebende][orth=Baum]");
182 var cell5 = cell;
183
184
185 // Remove annotations again
186 expect(cell5.classList.contains("chosen")).toBeTruthy();
187 cell5.click()
188 expect(cell5.classList.contains("chosen")).toBe(false);
Akrone9be11d2017-06-29 20:10:58 +0200189 expect(qc.toString()).toEqual("[corenlp/p=ART][opennlp/p=ADJA & orth=älteste][][orth=Baum]");
Akronb46d8e32017-06-29 14:26:14 +0200190
191 expect(cell2.classList.contains("chosen")).toBeTruthy();
192 cell2.click()
193 expect(cell2.classList.contains("chosen")).toBe(false);
Akrone9be11d2017-06-29 20:10:58 +0200194 expect(qc.toString()).toEqual("[corenlp/p=ART][opennlp/p=ADJA][][orth=Baum]");
Akronb46d8e32017-06-29 14:26:14 +0200195
196 expect(cell1.classList.contains("chosen")).toBeTruthy();
197 cell1.click()
198 expect(cell1.classList.contains("chosen")).toBe(false);
Akrone9be11d2017-06-29 20:10:58 +0200199 expect(qc.toString()).toEqual("[corenlp/p=ART][]{2}[orth=Baum]");
Akronb46d8e32017-06-29 14:26:14 +0200200
201 // Re-add first cell at the same position
202 expect(cell1.classList.contains("chosen")).toBe(false);
203 cell1.click()
204 expect(cell1.classList.contains("chosen")).toBeTruthy();
Akrone9be11d2017-06-29 20:10:58 +0200205 expect(qc.toString()).toEqual("[corenlp/p=ART][opennlp/p=ADJA][][orth=Baum]");
Akronb46d8e32017-06-29 14:26:14 +0200206
207 expect(cell3.classList.contains("chosen")).toBeTruthy();
208 cell3.click()
209 expect(cell3.classList.contains("chosen")).toBe(false);
Akrone9be11d2017-06-29 20:10:58 +0200210 expect(qc.toString()).toEqual("[opennlp/p=ADJA][][orth=Baum]");
Akronb46d8e32017-06-29 14:26:14 +0200211
212 expect(cell4.classList.contains("chosen")).toBeTruthy();
213 cell4.click()
214 expect(cell4.classList.contains("chosen")).toBe(false);
215 expect(qc.toString()).toEqual("[opennlp/p=ADJA]");
216
217 // Remove last token
218 expect(qc.shown()).toBeTruthy();
219 expect(qc.element().innerHTML).toEqual("<span>New Query:</span><span>[opennlp/p=ADJA]</span>");
220 expect(cell1.classList.contains("chosen")).toBeTruthy();
221 cell1.click()
222 expect(cell1.classList.contains("chosen")).toBe(false);
223 expect(qc.toString()).toEqual("");
224 expect(qc.shown()).toBe(false);
225
226 // Re-add token
227 expect(cell4.classList.contains("chosen")).toBe(false);
228 cell4.click()
229 expect(cell4.classList.contains("chosen")).toBeTruthy();
230 expect(qc.toString()).toEqual("[orth=Baum]");
231 expect(qc.shown()).toBeTruthy();
232 expect(qc.element().innerHTML).toEqual("<span>New Query:</span><span>[orth=Baum]</span>");
233 });
234
Akronc8461bf2017-06-29 15:05:24 +0200235 it('should ignore empty terms', function () {
236 var matchInfo = matchInfoFactory();
237 var qc = qcClass.create(matchInfo);
Akronb46d8e32017-06-29 14:26:14 +0200238
Akronc8461bf2017-06-29 15:05:24 +0200239 // Nothing to show
240 expect(qc.toString()).toEqual("");
241 expect(qc.shown()).toBe(false);
242 qc.show();
243 expect(qc.shown()).toBe(false);
244 expect(qc.element().className).toEqual("queryfragment");
245
246 var cell = matchInfo.querySelector("tbody > tr:nth-child(2) > td:nth-child(4)");
247 expect(cell.innerText).toEqual("ADJA");
248 expect(cell.classList.contains("chosen")).toBe(false);
249 cell.click();
250 expect(cell.classList.contains("chosen")).toBeTruthy();
251 expect(qc.toString()).toEqual("[opennlp/p=ADJA]");
252
253 cell = matchInfo.querySelector("tbody > tr:nth-child(2) > td:nth-child(5)");
254 expect(cell.innerText).toEqual("");
255 expect(cell.classList.contains("chosen")).toBe(false);
256 cell.click();
257 expect(cell.classList.contains("chosen")).toBe(false);
258 expect(qc.toString()).toEqual("[opennlp/p=ADJA]");
259
260 });
261
262 it('should create groups for multiple terms', function () {
263 var matchInfo = matchInfoFactory();
264 var qc = qcClass.create(matchInfo);
265
266 // Nothing to show
267 expect(qc.toString()).toEqual("");
268 expect(qc.shown()).toBe(false);
269 qc.show();
270 expect(qc.shown()).toBe(false);
271 expect(qc.element().className).toEqual("queryfragment");
272
273 var cell = matchInfo.querySelector("thead > tr > th:nth-child(5)");
274 expect(cell.innerText).toEqual("lebende");
275 expect(cell.classList.contains("chosen")).toBe(false);
276 cell.click();
277 expect(cell.classList.contains("chosen")).toBeTruthy();
278 expect(qc.toString()).toEqual("[orth=lebende]");
279
280 cell = matchInfo.querySelector("tbody > tr:nth-child(1) > td:nth-child(5)");
281 expect(cell.innerText).toEqual("ADJAADJD");
282 expect(cell.classList.contains("chosen")).toBe(false);
283 cell.click();
284 expect(cell.classList.contains("chosen")).toBeTruthy();
285 expect(qc.toString()).toEqual("[(corenlp/p=ADJA | corenlp/p=ADJD) & orth=lebende]");
286
287 // Remove or group again
288 expect(cell.classList.contains("chosen")).toBeTruthy();
289 cell.click();
290 expect(cell.classList.contains("chosen")).toBe(false);
291 expect(qc.toString()).toEqual("[orth=lebende]");
292 });
293
294
295 it('should add whole rows', function () {
296 var matchInfo = matchInfoFactory();
297 var qc = qcClass.create(matchInfo);
298
299 // Nothing to show
300 expect(qc.toString()).toEqual("");
301 expect(qc.shown()).toBe(false);
302 qc.show();
303 expect(qc.shown()).toBe(false);
304 expect(qc.element().className).toEqual("queryfragment");
305
306 var corenlpRow = matchInfo.querySelector("tbody > tr:nth-child(1)");
307
308 var cell = corenlpRow.querySelector("td:nth-child(4)");
309 expect(cell.innerText).toEqual("ADJA");
310 expect(cell.classList.contains("chosen")).toBe(false);
311 cell.click();
312 expect(cell.classList.contains("chosen")).toBeTruthy();
313
314 // Activate another cell in another row
315 cell = matchInfo.querySelector("tbody > tr:nth-child(2) td:nth-child(3)");
316 expect(cell.innerText).toEqual("ART");
317 expect(cell.classList.contains("chosen")).toBe(false);
318 cell.click();
319 expect(cell.classList.contains("chosen")).toBeTruthy();
320
321 expect(corenlpRow.querySelector("td:nth-child(3).chosen")).toBeNull();
322 expect(corenlpRow.querySelector("td:nth-child(4).chosen")).toBeTruthy();
323 expect(corenlpRow.querySelector("td:nth-child(5).chosen")).toBeNull();
324 expect(corenlpRow.querySelector("td:nth-child(6).chosen")).toBeNull();
325
326 expect(qc.toString()).toEqual("[opennlp/p=ART][corenlp/p=ADJA]");
327
328 // Mark all corenlp lists
329 cell = corenlpRow.querySelector("th:nth-child(1)");
330 expect(cell.innerText).toEqual("corenlp");
331 expect(cell.classList.contains("chosen")).toBe(false);
332 cell.click();
333 expect(cell.classList.contains("chosen")).toBe(false);
334
335 expect(corenlpRow.querySelector("td:nth-child(3).chosen")).toBeTruthy();
336 expect(corenlpRow.querySelector("td:nth-child(4).chosen")).toBeTruthy();
337 expect(corenlpRow.querySelector("td:nth-child(5).chosen")).toBeTruthy();
338 expect(corenlpRow.querySelector("td:nth-child(6).chosen")).toBeTruthy();
339
340 // Replay the choice without any effect
341 cell = corenlpRow.querySelector("th:nth-child(1)");
342 expect(cell.innerText).toEqual("corenlp");
343 expect(cell.classList.contains("chosen")).toBe(false);
344 cell.click();
345 expect(cell.classList.contains("chosen")).toBe(false);
346
347 expect(corenlpRow.querySelector("td:nth-child(3).chosen")).toBeTruthy();
348 expect(corenlpRow.querySelector("td:nth-child(4).chosen")).toBeTruthy();
349 expect(corenlpRow.querySelector("td:nth-child(5).chosen")).toBeTruthy();
350 expect(corenlpRow.querySelector("td:nth-child(6).chosen")).toBeTruthy();
Akrone9be11d2017-06-29 20:10:58 +0200351 expect(corenlpRow.querySelector("td:nth-child(7).chosen")).toBeTruthy();
Akronc8461bf2017-06-29 15:05:24 +0200352
Akrone9be11d2017-06-29 20:10:58 +0200353 expect(qc.toString()).toEqual("[corenlp/p=ART & opennlp/p=ART][corenlp/p=ADJA][(corenlp/p=ADJA | corenlp/p=ADJD)][corenlp/p=NN][corenlp/p=ADV]");
Akronc8461bf2017-06-29 15:05:24 +0200354
355 // Remove one of the cells again
356 cell = corenlpRow.querySelector("td:nth-child(5).chosen");
357 expect(cell.innerText).toEqual("ADJAADJD");
358 expect(cell.classList.contains("chosen")).toBeTruthy();
359 cell.click();
360 expect(cell.classList.contains("chosen")).toBe(false);
Akrone9be11d2017-06-29 20:10:58 +0200361 expect(qc.toString()).toEqual("[corenlp/p=ART & opennlp/p=ART][corenlp/p=ADJA][][corenlp/p=NN][corenlp/p=ADV]");
Akronc8461bf2017-06-29 15:05:24 +0200362
363 });
Akron8084cb52017-06-29 17:11:14 +0200364
365 it('should ignore empty terms in whole rows', function () {
366 var matchInfo = matchInfoFactory();
367 var qc = qcClass.create(matchInfo);
368 expect(qc.toString()).toEqual("");
369
370 var opennlpRow = matchInfo.querySelector("tbody > tr:nth-child(2)");
371
372 expect(opennlpRow.querySelector("td:nth-child(3).chosen")).toBeNull();
373 expect(opennlpRow.querySelector("td:nth-child(4).chosen")).toBeNull();
374 expect(opennlpRow.querySelector("td:nth-child(5).chosen")).toBeNull();
375 expect(opennlpRow.querySelector("td:nth-child(6).chosen")).toBeNull();
376
377 expect(qc.toString()).toEqual("");
378
379 // Mark all opennlp lists
380 cell = opennlpRow.querySelector("th:nth-child(1)");
381 expect(cell.innerText).toEqual("opennlp");
382 expect(cell.classList.contains("chosen")).toBe(false);
383 cell.click();
384 expect(cell.classList.contains("chosen")).toBe(false);
385
386 expect(opennlpRow.querySelector("td:nth-child(3).chosen")).toBeTruthy();
387 expect(opennlpRow.querySelector("td:nth-child(4).chosen")).toBeTruthy();
388 expect(opennlpRow.querySelector("td:nth-child(5).chosen")).toBeNull();
389 expect(opennlpRow.querySelector("td:nth-child(6).chosen")).toBeTruthy();
390 });
Akrone9be11d2017-06-29 20:10:58 +0200391
392 it('should support multiple distances', function () {
393 var matchInfo = matchInfoFactory();
394 var qc = qcClass.create(matchInfo);
395
396 var cell = matchInfo.querySelector("thead > tr > th:nth-child(3)");
397 expect(cell.innerText).toEqual("Der");
398 cell.click();
399 expect(qc.toString()).toEqual("[orth=Der]");
400
401 cell = matchInfo.querySelector("thead > tr > th:nth-child(7)");
402 expect(cell.innerText).toEqual("hier");
403 cell.click();
404 expect(qc.toString()).toEqual("[orth=Der][]{3}[orth=hier]");
405
406 cell = matchInfo.querySelector("thead > tr > th:nth-child(5)");
407 expect(cell.innerText).toEqual("lebende");
408 cell.click();
409 expect(qc.toString()).toEqual("[orth=Der][][orth=lebende][][orth=hier]");
410 });
Akronb46d8e32017-06-29 14:26:14 +0200411 });
412});