Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 1 | define(['plugin/server','plugin/widget','panel', 'panel/query', 'panel/result', 'plugin/service', 'pipe', 'vc','util'], function (pluginServerClass, widgetClass, panelClass, queryPanelClass, resultPanelClass, serviceClass, pipeClass, vcClass) { |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 2 | |
| 3 | describe('KorAP.Plugin.Server', function () { |
| 4 | |
| 5 | it('should be initializable', function () { |
| 6 | var manager = pluginServerClass.create(); |
| 7 | expect(manager).toBeTruthy(); |
| 8 | manager.destroy(); |
| 9 | }); |
| 10 | |
| 11 | it('should add a widget', function () { |
| 12 | var manager = pluginServerClass.create(); |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 13 | var panel = panelClass.create(); |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 14 | var id = manager.addWidget(panel, {"name": 'Example 1', "src": 'about:blank'}); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 15 | expect(id).toMatch(/^id-/); |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 16 | |
| 17 | var panelE = panel.element(); |
| 18 | var widgetE = panelE.firstChild.firstChild; |
| 19 | expect(widgetE.classList.contains('widget')).toBeTruthy(); |
| 20 | expect(widgetE.firstChild.tagName).toEqual("IFRAME"); |
| 21 | var iframe = widgetE.firstChild; |
| 22 | expect(iframe.getAttribute("src")).toEqual("about:blank"); |
| 23 | |
| 24 | expect(widgetE.lastChild.firstChild.textContent).toEqual("Close"); |
| 25 | expect(widgetE.lastChild.lastChild.textContent).toEqual("Example 1"); |
| 26 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 27 | manager.destroy(); |
| 28 | }); |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 29 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 30 | it('should add a service', function () { |
| 31 | var manager = pluginServerClass.create(); |
| 32 | |
| 33 | var e = manager.element(); |
| 34 | |
| 35 | document.body.appendChild(e); |
| 36 | |
| 37 | expect(document.getElementById("services")).toBeTruthy(); |
| 38 | |
| 39 | expect(e.getAttribute("id")).toBe("services"); |
| 40 | expect(e.children.length).toBe(0); |
| 41 | |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 42 | var id = manager.addService({"name":'Example 1', "src":'about:blank'}); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 43 | expect(id).toMatch(/^id-/); |
| 44 | |
| 45 | expect(e.children.length).toBe(1); |
| 46 | |
| 47 | manager.destroy(); |
| 48 | |
| 49 | expect(document.getElementById("services")).toBeFalsy(); |
| 50 | |
| 51 | }); |
| 52 | |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 53 | it('should close a widget', function () { |
| 54 | var manager = pluginServerClass.create(); |
| 55 | var panel = panelClass.create(); |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 56 | var id = manager.addWidget(panel, {"name":'Example 2', "src":'about:blank'}); |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 57 | expect(id).toMatch(/^id-/); |
| 58 | |
| 59 | var panelE = panel.element(); |
| 60 | var widgetE = panelE.firstChild.firstChild; |
| 61 | expect(widgetE.classList.contains('widget')).toBeTruthy(); |
| 62 | |
| 63 | expect(panelE.getElementsByClassName('view').length).toEqual(1); |
| 64 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 65 | var widget = manager.service(id); |
| 66 | expect(widget.isWidget).toBeTruthy(); |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 67 | widget.close(); |
| 68 | |
| 69 | expect(panelE.getElementsByClassName('view').length).toEqual(0); |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 70 | manager.destroy(); |
| 71 | }); |
| 72 | |
| 73 | |
| 74 | it('should fail on invalid registrations', function () { |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 75 | var manager = pluginServerClass.create(); |
| 76 | |
| 77 | expect( |
| 78 | function() { manager.register({}) } |
| 79 | ).toThrow(new Error("Missing name of plugin")); |
| 80 | |
| 81 | expect( |
| 82 | function() { manager.register({ |
| 83 | name : 'Example', |
| 84 | embed : '' |
| 85 | })} |
| 86 | ).toThrow(new Error("Embedding of plugin is no list")); |
| 87 | |
| 88 | expect( |
| 89 | function() { manager.register({ |
| 90 | name : 'Example', |
| 91 | embed : [{ |
| 92 | panel : '' |
| 93 | }] |
| 94 | })} |
| 95 | ).toThrow(new Error("Panel for plugin is invalid")); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 96 | manager.destroy(); |
| 97 | }); |
| 98 | |
| 99 | it('should accept valid registrations for matches', function () { |
| 100 | var manager = pluginServerClass.create(); |
| 101 | |
| 102 | manager.register({ |
| 103 | name : 'Check', |
| 104 | embed : [{ |
| 105 | panel : 'match', |
| 106 | title : 'Translate', |
| 107 | onClick : { |
| 108 | template : 'test' |
| 109 | } |
| 110 | }] |
| 111 | }); |
| 112 | |
| 113 | expect(manager.buttonGroup('match').length).toEqual(1); |
| 114 | manager.destroy(); |
| 115 | }); |
Akron | 3d01380 | 2020-10-07 15:03:38 +0200 | [diff] [blame] | 116 | |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 117 | it('should accept valid registrations for query temporary', function () { |
| 118 | var manager = pluginServerClass.create(); |
| 119 | |
| 120 | manager.register({ |
| 121 | name : 'Check', |
| 122 | embed : [{ |
| 123 | panel : 'query', |
| 124 | title : 'Translate', |
| 125 | onClick : { |
| 126 | template : 'test' |
| 127 | } |
| 128 | }] |
| 129 | }); |
| 130 | |
| 131 | expect(manager.buttonGroup('query').length).toEqual(1); |
| 132 | manager.destroy(); |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 133 | }); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 134 | |
| 135 | |
| 136 | it('should accept valid registrations for result', function () { |
| 137 | var manager = pluginServerClass.create(); |
| 138 | |
| 139 | manager.register({ |
| 140 | name : 'Check', |
| 141 | embed : [{ |
| 142 | panel : 'result', |
| 143 | title : 'Translate', |
| 144 | onClick : { |
| 145 | template : 'test' |
| 146 | } |
| 147 | }] |
| 148 | }); |
| 149 | |
| 150 | expect(manager.buttonGroup('result').length).toEqual(1); |
| 151 | manager.destroy(); |
| 152 | }); |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 153 | |
| 154 | it('should accept valid registrations for addWidget', function () { |
| 155 | let p = KorAP.Panel["result"] = panelClass.create(); |
| 156 | |
| 157 | let manager = pluginServerClass.create(); |
| 158 | |
| 159 | manager.register({ |
| 160 | name : 'Check', |
| 161 | embed : [{ |
| 162 | panel : 'result', |
| 163 | title : 'Add', |
| 164 | onClick : { |
| 165 | template : 'about:blank', |
| 166 | action : 'addWidget' |
| 167 | } |
| 168 | }] |
| 169 | }); |
| 170 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 171 | let b = p.actions().element().firstChild; |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 172 | expect(b.hasAttribute("data-icon")).toBeFalsy(); |
| 173 | expect(b.hasAttribute("cls")).toBeFalsy(); |
| 174 | expect(b.getAttribute("title")).toEqual("Add"); |
| 175 | |
| 176 | expect(p.element().querySelectorAll("iframe").length).toEqual(0); |
| 177 | |
| 178 | b.click(); |
| 179 | |
| 180 | expect(p.element().querySelectorAll("iframe").length).toEqual(1); |
| 181 | expect(p.element().querySelectorAll("div.view.widget").length).toEqual(1); |
| 182 | expect(p.element().querySelectorAll("div.view.show.widget").length).toEqual(1); |
| 183 | |
| 184 | b.click(); |
| 185 | |
| 186 | expect(p.element().querySelectorAll("iframe").length).toEqual(2); |
| 187 | expect(p.element().querySelectorAll("div.view.widget").length).toEqual(2); |
| 188 | expect(p.element().querySelectorAll("div.view.show.widget").length).toEqual(2); |
| 189 | |
| 190 | manager.destroy(); |
| 191 | |
| 192 | KorAP.Panel["result"] = undefined; |
| 193 | }); |
| 194 | |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 195 | it('should accept valid registrations for setWidget', function () { |
| 196 | let p = KorAP.Panel["result"] = panelClass.create(); |
| 197 | |
| 198 | let manager = pluginServerClass.create(); |
| 199 | |
| 200 | manager.register({ |
| 201 | name : 'Check', |
| 202 | embed : [{ |
| 203 | panel : 'result', |
| 204 | title : 'Add', |
| 205 | onClick : { |
| 206 | template : 'about:blank', |
| 207 | action : 'setWidget' |
| 208 | } |
| 209 | }] |
| 210 | }); |
| 211 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 212 | let b = p.actions().element().firstChild; |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 213 | expect(b.hasAttribute("data-icon")).toBeFalsy(); |
| 214 | expect(b.hasAttribute("cls")).toBeFalsy(); |
| 215 | expect(b.getAttribute("title")).toEqual("Add"); |
| 216 | |
| 217 | expect(p.element().querySelectorAll("iframe").length).toEqual(0); |
| 218 | |
| 219 | b.click(); |
| 220 | |
| 221 | expect(p.element().querySelectorAll("iframe").length).toEqual(1); |
| 222 | expect(p.element().querySelectorAll("div.view.show.widget").length).toEqual(1); |
| 223 | |
| 224 | b.click(); |
| 225 | |
| 226 | expect(p.element().querySelectorAll("iframe").length).toEqual(1); |
| 227 | expect(p.element().querySelectorAll("div.view.widget").length).toEqual(1); |
| 228 | expect(p.element().querySelectorAll("div.view.show.widget").length).toEqual(0); |
Akron | fcf89db | 2020-10-01 17:40:20 +0200 | [diff] [blame] | 229 | |
| 230 | p.element().querySelector("span.close").click(); |
| 231 | |
| 232 | expect(p.element().querySelectorAll("iframe").length).toEqual(0); |
| 233 | |
| 234 | b.click(); |
| 235 | |
| 236 | expect(p.element().querySelectorAll("iframe").length).toEqual(1); |
| 237 | expect(p.element().querySelectorAll("div.view.widget").length).toEqual(1); |
| 238 | expect(p.element().querySelectorAll("div.view.show.widget").length).toEqual(1); |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 239 | expect(p.element().querySelector("iframe").getAttribute('sandbox')).toEqual(''); |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 240 | |
| 241 | manager.destroy(); |
| 242 | |
| 243 | KorAP.Panel["result"] = undefined; |
| 244 | }); |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 245 | |
Akron | a70b689 | 2021-11-04 14:23:24 +0100 | [diff] [blame] | 246 | it('should accept valid registrations for toggle', function () { |
| 247 | let p = KorAP.Panel["result"] = panelClass.create(); |
| 248 | |
| 249 | let manager = pluginServerClass.create(); |
| 250 | |
| 251 | manager.register({ |
| 252 | name : 'Check', |
| 253 | embed : [{ |
| 254 | panel : 'result', |
| 255 | title : 'Glemm', |
| 256 | onClick : { |
| 257 | template : 'about:blank', |
| 258 | action : 'toggle', |
| 259 | 'default' : false |
| 260 | } |
| 261 | }] |
| 262 | }); |
| 263 | |
| 264 | let b = p.actions().element().firstChild; |
| 265 | expect(b.hasAttribute("data-icon")).toBeFalsy(); |
| 266 | expect(b.hasAttribute("cls")).toBeFalsy(); |
| 267 | expect(b.getAttribute("title")).toEqual("Glemm"); |
| 268 | expect(b.firstChild.classList.contains('button-icon')).toBeTruthy(); |
| 269 | expect(b.firstChild.classList.contains('check')).toBeTruthy(); |
| 270 | expect(b.firstChild.classList.contains('checked')).toBeFalsy(); |
| 271 | |
| 272 | expect(p.element().querySelectorAll("iframe").length).toEqual(0); |
| 273 | |
| 274 | b.click(); |
| 275 | |
| 276 | expect(b.getAttribute("title")).toEqual("Glemm"); |
| 277 | expect(b.firstChild.classList.contains('button-icon')).toBeTruthy(); |
| 278 | expect(b.firstChild.classList.contains('check')).toBeTruthy(); |
| 279 | expect(b.firstChild.classList.contains('checked')).toBeTruthy(); |
| 280 | |
| 281 | // Check with true default |
| 282 | p = KorAP.Panel["result"] = panelClass.create(); |
| 283 | |
| 284 | manager = pluginServerClass.create(); |
| 285 | |
| 286 | manager.register({ |
| 287 | name : 'Check', |
| 288 | embed : [{ |
| 289 | panel : 'result', |
| 290 | title : 'Glemm', |
| 291 | onClick : { |
| 292 | template : 'about:blank', |
| 293 | action : 'toggle', |
| 294 | 'default' : true |
| 295 | } |
| 296 | }] |
| 297 | }); |
| 298 | |
| 299 | b = p.actions().element().firstChild; |
| 300 | expect(b.hasAttribute("data-icon")).toBeFalsy(); |
| 301 | expect(b.hasAttribute("cls")).toBeFalsy(); |
| 302 | expect(b.getAttribute("title")).toEqual("Glemm"); |
| 303 | expect(b.firstChild.classList.contains('button-icon')).toBeTruthy(); |
| 304 | expect(b.firstChild.classList.contains('check')).toBeTruthy(); |
| 305 | expect(b.firstChild.classList.contains('checked')).toBeTruthy(); |
| 306 | |
| 307 | manager.destroy(); |
| 308 | KorAP.Panel["result"] = undefined; |
| 309 | }); |
| 310 | |
| 311 | |
Akron | 3d01380 | 2020-10-07 15:03:38 +0200 | [diff] [blame] | 312 | it('should alert on plugin info (1)', function () { |
| 313 | |
| 314 | let alertMsg; |
| 315 | spyOn(window, 'alert').and.callFake(function(msg) { |
| 316 | alertMsg = msg; |
| 317 | }); |
| 318 | |
| 319 | let p = KorAP.Panel["result"] = panelClass.create(); |
| 320 | |
| 321 | let manager = pluginServerClass.create(); |
| 322 | |
| 323 | manager.register({ |
| 324 | name : 'Check', |
| 325 | embed : [{ |
| 326 | panel : 'result', |
| 327 | title : 'Add', |
| 328 | onClick : { |
| 329 | template : 'about:blank', |
| 330 | action : 'setWidget' |
| 331 | } |
| 332 | }] |
| 333 | }); |
| 334 | |
| 335 | expect(alertMsg).toBeUndefined(); |
| 336 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 337 | let b = p.actions().element().firstChild; |
Akron | 3d01380 | 2020-10-07 15:03:38 +0200 | [diff] [blame] | 338 | b.click(); |
| 339 | |
| 340 | // This may only be temporary and should open the plugin window instead |
| 341 | p.element().querySelector("span.plugin").click(); |
| 342 | |
| 343 | expect(alertMsg).toEqual('Check'); |
| 344 | |
| 345 | manager.destroy(); |
| 346 | KorAP.Panel["result"] = undefined; |
| 347 | }); |
| 348 | |
| 349 | |
| 350 | it('should alert on plugin info (2)', function () { |
| 351 | |
| 352 | let alertMsg; |
| 353 | spyOn(window, 'alert').and.callFake(function(msg) { |
| 354 | alertMsg = msg; |
| 355 | }); |
| 356 | |
| 357 | let p = KorAP.Panel["result"] = panelClass.create(); |
| 358 | |
| 359 | let manager = pluginServerClass.create(); |
| 360 | |
| 361 | manager.register({ |
| 362 | name : 'Check', |
| 363 | desc : 'Simple Check plugin', |
| 364 | embed : [{ |
| 365 | panel : 'result', |
| 366 | title : 'Add', |
| 367 | onClick : { |
| 368 | template : 'about:blank', |
| 369 | action : 'setWidget' |
| 370 | } |
| 371 | }] |
| 372 | }); |
| 373 | |
| 374 | expect(alertMsg).toBeUndefined(); |
| 375 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 376 | let b = p.actions().element().firstChild; |
Akron | 3d01380 | 2020-10-07 15:03:38 +0200 | [diff] [blame] | 377 | b.click(); |
| 378 | |
| 379 | // This may only be temporary and should open the plugin window instead |
| 380 | p.element().querySelector("span.plugin").click(); |
| 381 | |
| 382 | expect(alertMsg).toEqual("Check\n\nSimple Check plugin"); |
| 383 | |
| 384 | manager.destroy(); |
| 385 | KorAP.Panel["result"] = undefined; |
| 386 | }); |
| 387 | |
| 388 | |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 389 | it('should accept widget permissions', function () { |
| 390 | let p = KorAP.Panel["result"] = panelClass.create(); |
| 391 | |
| 392 | let manager = pluginServerClass.create(); |
| 393 | |
| 394 | manager.register({ |
| 395 | name : 'Check', |
| 396 | embed : [{ |
| 397 | panel : 'result', |
| 398 | title : 'Add', |
| 399 | onClick : { |
| 400 | template : 'about:blank', |
| 401 | action : 'addWidget', |
Akron | ce0d882 | 2020-10-05 16:25:40 +0200 | [diff] [blame] | 402 | permissions: ['scripts', 'forms', 'all'] |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 403 | } |
| 404 | }] |
| 405 | }); |
| 406 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 407 | let b = p.actions().element().firstChild; |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 408 | b.click(); |
| 409 | expect(p.element().querySelectorAll("iframe").length).toEqual(1); |
| 410 | expect(p.element().querySelector("iframe").getAttribute('sandbox')).toEqual('allow-forms allow-scripts'); |
| 411 | }); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 412 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 413 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 414 | describe('KorAP.Plugin.Widget', function () { |
| 415 | it('should be initializable', function () { |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 416 | expect(function () { widgetClass.create() }).toThrow(new Error("Service not well defined")); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 417 | |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 418 | widget = widgetClass.create({"name" : "Test", "src":"https://example", "id":56}); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 419 | expect(widget).toBeTruthy(); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 420 | expect(widget.id).toEqual(56); |
| 421 | expect(widget.name).toEqual("Test"); |
| 422 | expect(widget.src).toEqual("https://example"); |
| 423 | }); |
| 424 | |
| 425 | it('should create a view element', function () { |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 426 | var widget = widgetClass.create({ |
| 427 | "name":"Test", |
| 428 | "src":"https://example", |
| 429 | "id":56, |
Akron | ce0d882 | 2020-10-05 16:25:40 +0200 | [diff] [blame] | 430 | "permissions":["scripts","forms"] |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 431 | }); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 432 | var we = widget.element(); |
| 433 | |
| 434 | expect(we.tagName).toEqual("DIV"); |
| 435 | expect(we.classList.contains('view')).toBeTruthy(); |
| 436 | expect(we.classList.contains('widget')).toBeTruthy(); |
| 437 | |
| 438 | var iframe = we.firstChild; |
| 439 | expect(iframe.tagName).toEqual("IFRAME"); |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 440 | expect(iframe.getAttribute("sandbox")).toEqual("allow-forms allow-scripts"); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 441 | expect(iframe.getAttribute("src")).toEqual("https://example"); |
| 442 | expect(iframe.getAttribute("name")).toEqual("56"); |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 443 | |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 444 | var btn = we.lastChild; |
| 445 | expect(btn.classList.contains("button-group")).toBeTruthy(); |
| 446 | expect(btn.classList.contains("button-view")).toBeTruthy(); |
| 447 | expect(btn.classList.contains("widget")).toBeTruthy(); |
| 448 | |
| 449 | expect(btn.firstChild.tagName).toEqual("SPAN"); |
| 450 | expect(btn.firstChild.classList.contains("button-icon")).toBeTruthy(); |
| 451 | expect(btn.firstChild.classList.contains("close")).toBeTruthy(); |
| 452 | expect(btn.firstChild.firstChild.tagName).toEqual("SPAN"); |
| 453 | |
| 454 | expect(btn.lastChild.tagName).toEqual("SPAN"); |
| 455 | expect(btn.lastChild.classList.contains("button-icon")).toBeTruthy(); |
| 456 | expect(btn.lastChild.classList.contains("plugin")).toBeTruthy(); |
| 457 | expect(btn.lastChild.firstChild.tagName).toEqual("SPAN"); |
| 458 | expect(btn.lastChild.textContent).toEqual("Test"); |
| 459 | }) |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 460 | |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 461 | it('should be resizable', function () { |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 462 | var widget = widgetClass.create({"name":"Test", "src":"https://example", "id":56}); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 463 | var iframe = widget.show(); |
| 464 | expect(iframe.style.height).toEqual('0px'); |
| 465 | widget.resize({ height : 9 }); |
| 466 | expect(iframe.style.height).toEqual('9px'); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 467 | }); |
Akron | 3d9ce5e | 2020-10-01 15:18:36 +0200 | [diff] [blame] | 468 | |
| 469 | it('should be minimizable', function () { |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 470 | var widget = widgetClass.create({"name":"Test", "src":"https://example", "id":56}); |
Akron | 3d9ce5e | 2020-10-01 15:18:36 +0200 | [diff] [blame] | 471 | var we = widget.element(); |
| 472 | expect(we.classList.contains('show')).toBeTruthy(); |
| 473 | widget.minimize(); |
| 474 | expect(we.classList.contains('show')).toBeFalsy(); |
| 475 | }); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 476 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 477 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 478 | describe('KorAP.Plugin.Service', function () { |
| 479 | it('should be initializable', function () { |
| 480 | expect(function () { serviceClass.create() }).toThrow(new Error("Service not well defined")); |
| 481 | |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 482 | let service = serviceClass.create({"name":"Test", "src":"https://example", "id":56}); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 483 | expect(service).toBeTruthy(); |
| 484 | expect(service.id).toEqual(56); |
| 485 | expect(service.name).toEqual("Test"); |
| 486 | expect(service.src).toEqual("https://example"); |
| 487 | }); |
| 488 | |
| 489 | it('should be loadable', function () { |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 490 | let service = serviceClass.create({"name":"Test", "src":"https://example", "id":56}); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 491 | expect(service).toBeTruthy(); |
| 492 | |
| 493 | let i = service.load(); |
| 494 | expect(i.tagName).toEqual("IFRAME"); |
| 495 | expect(i.getAttribute("allowTransparency")).toEqual("true"); |
| 496 | expect(i.getAttribute("frameborder")).toEqual(''+0); |
| 497 | expect(i.getAttribute("name")).toEqual(''+service.id); |
| 498 | expect(i.getAttribute("src")).toEqual(service.src); |
| 499 | }); |
| 500 | }); |
| 501 | |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 502 | describe('KorAP.Plugin.QueryPanel', function () { |
| 503 | it('should establish a query plugin', function () { |
| 504 | var queryPanel = queryPanelClass.create(); |
| 505 | |
| 506 | var div = document.createElement('div'); |
| 507 | |
| 508 | div.appendChild(queryPanel.element()); |
| 509 | KorAP.Panel = KorAP.Panel || {}; |
| 510 | KorAP.Panel['query'] = queryPanel; |
| 511 | |
| 512 | // Register plugin afterwards |
| 513 | var manager = pluginServerClass.create(); |
| 514 | |
| 515 | manager.register({ |
| 516 | name : 'Check', |
| 517 | embed : [{ |
| 518 | panel : 'query', |
| 519 | title : 'Translate', |
| 520 | onClick : { |
| 521 | template : 'test' |
| 522 | } |
| 523 | }] |
| 524 | }); |
| 525 | |
| 526 | expect(manager.buttonGroup('query').length).toEqual(0); |
| 527 | |
| 528 | // Clean up |
| 529 | KorAP.Panel['query'] = undefined; |
| 530 | manager.destroy(); |
| 531 | }); |
hebasta | f6adf8d | 2019-11-26 14:04:10 +0100 | [diff] [blame] | 532 | |
| 533 | it('Plugin buttons should be cleared after adding to panel', function () { |
| 534 | |
| 535 | // Register plugin first |
| 536 | KorAP.Plugin = pluginServerClass.create(); |
| 537 | |
| 538 | KorAP.Plugin.register({ |
| 539 | name : 'Check', |
| 540 | embed : [{ |
| 541 | panel : 'query', |
| 542 | title : 'Translate', |
| 543 | onClick : { |
| 544 | template : 'test' |
| 545 | } |
| 546 | }] |
| 547 | }); |
| 548 | |
| 549 | |
| 550 | var queryPanel = queryPanelClass.create(); |
| 551 | var div = document.createElement('div'); |
| 552 | |
| 553 | div.appendChild(queryPanel.element()); |
| 554 | KorAP.Panel = KorAP.Panel || {}; |
| 555 | KorAP.Panel['query'] = queryPanel; |
| 556 | expect(KorAP.Plugin.buttonGroup('query').length).toEqual(0); |
| 557 | |
| 558 | // Clean up |
| 559 | KorAP.Panel['query'] = undefined; |
| 560 | KorAP.Plugin.destroy(); |
| 561 | KorAP.Plugin = undefined; |
| 562 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 563 | }); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 564 | |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 565 | describe('KorAP.Plugin.ResultPanel', function () { |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 566 | it('Plugin is registered second: buttons should be added to panel', function () { |
| 567 | |
| 568 | var resultPanel = resultPanelClass.create(); |
| 569 | resultPanel.addAlignAction(); |
| 570 | var div = document.createElement('div'); |
| 571 | |
| 572 | div.appendChild(resultPanel.element()); |
| 573 | KorAP.Panel = KorAP.Panel || {}; |
| 574 | KorAP.Panel['result'] = resultPanel; |
| 575 | |
| 576 | // Register plugin afterwards |
| 577 | var manager = pluginServerClass.create(); |
| 578 | |
| 579 | manager.register({ |
| 580 | name : 'ResultPlugin', |
| 581 | embed : [{ |
| 582 | panel : 'result', |
| 583 | title : 'Dosomething', |
| 584 | onClick : { |
| 585 | template : 'test' |
| 586 | } |
| 587 | }] |
| 588 | }); |
| 589 | |
| 590 | expect(manager.buttonGroup('result').length).toEqual(0); |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 591 | expect(KorAP.Panel['result'].actions().element().innerHTML).toContain('Dosomething'); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 592 | |
| 593 | // Clean up |
| 594 | KorAP.Panel['result'] = undefined; |
| 595 | manager.destroy(); |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 596 | }); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 597 | |
| 598 | it('Plugin is registered first: Buttons should be added to panel and cleared', function () { |
| 599 | |
| 600 | // Register plugin first |
| 601 | KorAP.Plugin = pluginServerClass.create(); |
| 602 | |
| 603 | KorAP.Plugin.register({ |
| 604 | name : 'ResultPlugin', |
| 605 | embed : [{ |
| 606 | panel : 'result', |
| 607 | title : 'Dosomething', |
| 608 | onClick : { |
| 609 | template : 'test' |
| 610 | } |
| 611 | }] |
| 612 | }); |
| 613 | |
| 614 | expect(KorAP.Plugin.buttonGroup('result').length).toEqual(1); |
| 615 | |
| 616 | var resultPanel = resultPanelClass.create(); |
| 617 | var div = document.createElement('div'); |
| 618 | div.appendChild(resultPanel.element()); |
| 619 | KorAP.Panel = KorAP.Panel || {}; |
| 620 | KorAP.Panel['result'] = resultPanel; |
| 621 | expect(KorAP.Plugin.buttonGroup('result').length).toEqual(0); |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 622 | expect(KorAP.Panel['result'].actions().element().innerHTML).toContain('Dosomething'); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 623 | |
| 624 | // Clean up |
| 625 | KorAP.Panel['result'] = undefined; |
| 626 | KorAP.Plugin.destroy(); |
| 627 | KorAP.Plugin = undefined; |
| 628 | }); |
| 629 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 630 | |
| 631 | describe('KorAP.Plugin communications', function () { |
| 632 | it('should receive messages', function () { |
| 633 | var manager = pluginServerClass.create(); |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 634 | var id = manager.addService({"name":'Example 1', "src":'about:blank'}); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 635 | expect(id).toMatch(/^id-/); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 636 | var temp = KorAP.koralQuery; |
| 637 | KorAP.koralQuery = { "@type" : "koral:test" }; |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 638 | let data = { |
| 639 | "originID" : id, |
| 640 | "action" : "get", |
| 641 | "key" : "KQ" |
| 642 | }; |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 643 | manager._receiveMsg({ |
| 644 | "data" : data |
| 645 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 646 | manager.destroy(); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 647 | expect(data.value["@type"]).toEqual("koral:test"); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 648 | // Recreate initial state |
| 649 | KorAP.koralQuery = temp; |
| 650 | }); |
| 651 | |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 652 | it('should add to pipe', function () { |
| 653 | var manager = pluginServerClass.create(); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 654 | var temp = KorAP.Pipe; |
| 655 | KorAP.Pipe = pipeClass.create(); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 656 | expect(KorAP.Pipe.toString()).toEqual(""); |
| 657 | |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 658 | var id = manager.addService({"name":'Example 2', "src":'about:blank'}); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 659 | expect(id).toMatch(/^id-/); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 660 | manager._receiveMsg({ |
| 661 | "data" : { |
| 662 | "originID" : id, |
| 663 | "action" : "pipe", |
| 664 | "job" : "add", |
| 665 | "service" : "https://pipe-service.de" |
| 666 | } |
| 667 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 668 | expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de"); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 669 | manager._receiveMsg({ |
| 670 | "data" : { |
| 671 | "originID" : id, |
| 672 | "action" : "pipe", |
| 673 | "job" : "add", |
| 674 | "service" : "https://pipe-service-2.de" |
| 675 | } |
| 676 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 677 | expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de,https://pipe-service-2.de"); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 678 | manager._receiveMsg({ |
| 679 | "data" : { |
| 680 | "originID" : id, |
| 681 | "action" : "pipe", |
| 682 | "job" : "del", |
| 683 | "service" : "https://pipe-service.de" |
| 684 | } |
| 685 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 686 | expect(KorAP.Pipe.toString()).toEqual("https://pipe-service-2.de"); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 687 | manager.destroy(); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 688 | // Recreate initial state |
| 689 | KorAP.Pipe = temp; |
| 690 | }); |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 691 | |
Akron | 26d57f2 | 2021-09-10 16:48:57 +0200 | [diff] [blame] | 692 | it('should reply to query information requests (queryform)', function () { |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 693 | var manager = pluginServerClass.create(); |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 694 | var id = manager.addService({"name":'Service', "src":'about:blank'}); |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 695 | expect(id).toMatch(/^id-/); |
| 696 | var temp = KorAP.vc; |
| 697 | // Create form for query form information |
| 698 | let f = document.createElement('form'); |
| 699 | var qfield = f.addE('input'); |
| 700 | qfield.setAttribute("id", "q-field"); |
| 701 | qfield.value = "[orth=Baum]"; |
| 702 | var qlfield = f.addE('select'); |
| 703 | qlfield.setAttribute("id", "ql-field"); |
| 704 | qlfield.addE('option').setAttribute('value', 'cosmas-2'); |
| 705 | qlfield.addE('option').setAttribute('value', 'poliqarp'); |
| 706 | qlfield.selectedIndex = 1; |
| 707 | |
| 708 | KorAP.vc = vcClass.create().fromJson({ |
| 709 | "key" : "title", |
| 710 | "type" : "type:regex", |
| 711 | "value" : "[^b]ee.+?", |
| 712 | "@type" : "koral:doc" |
| 713 | }); |
| 714 | // console.log(KorAP.vc.toQuery()); |
| 715 | |
| 716 | document.body.appendChild(f); |
| 717 | let data = { |
| 718 | "originID" : id, |
| 719 | "action" : "get", |
| 720 | "key" : "QueryForm" |
| 721 | }; |
| 722 | manager._receiveMsg({ |
| 723 | "data" : data |
| 724 | }); |
| 725 | manager.destroy(); |
| 726 | expect(data.value["q"]).toEqual("[orth=Baum]"); |
| 727 | expect(data.value["ql"]).toEqual("poliqarp"); |
| 728 | expect(data.value["cq"]).toEqual("title = /[^b]ee.+?/"); |
Akron | 26d57f2 | 2021-09-10 16:48:57 +0200 | [diff] [blame] | 729 | |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 730 | // Recreate initial state |
| 731 | KorAP.vc = temp; |
| 732 | document.body.removeChild(f); |
| 733 | }); |
Akron | 26d57f2 | 2021-09-10 16:48:57 +0200 | [diff] [blame] | 734 | |
| 735 | it('should reply to query information requests (pagination)', function () { |
| 736 | var manager = pluginServerClass.create(); |
| 737 | var id = manager.addService({"name":'Service', "src":'about:blank'}); |
| 738 | expect(id).toMatch(/^id-/); |
| 739 | |
| 740 | // Create pagination element for pagination information |
| 741 | let p = document.createElement('div'); |
| 742 | p.setAttribute('id', 'pagination') |
| 743 | p.setAttribute('data-page',3); |
| 744 | p.setAttribute('data-total',30); |
| 745 | p.setAttribute('data-count',25); |
| 746 | |
| 747 | document.body.appendChild(p); |
| 748 | |
| 749 | let data = { |
| 750 | "originID" : id, |
| 751 | "action" : "get", |
| 752 | "key" : "Pagination" |
| 753 | }; |
| 754 | manager._receiveMsg({ |
| 755 | "data" : data |
| 756 | }); |
| 757 | manager.destroy(); |
| 758 | expect(data.value["count"]).toEqual(25); |
| 759 | expect(data.value["page"]).toEqual(3); |
| 760 | expect(data.value["total"]).toEqual(30); |
| 761 | |
| 762 | // Recreate initial state |
| 763 | document.body.removeChild(p); |
| 764 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 765 | }); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 766 | }); |