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); |
Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 233 | |
Akron | fcf89db | 2020-10-01 17:40:20 +0200 | [diff] [blame] | 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 | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 240 | |
Akron | ba09ed2 | 2020-10-01 16:01:45 +0200 | [diff] [blame] | 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 : { |
Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 257 | state : 'check', |
Akron | a70b689 | 2021-11-04 14:23:24 +0100 | [diff] [blame] | 258 | template : 'about:blank', |
| 259 | action : 'toggle', |
| 260 | 'default' : false |
| 261 | } |
| 262 | }] |
| 263 | }); |
| 264 | |
| 265 | let b = p.actions().element().firstChild; |
| 266 | expect(b.hasAttribute("data-icon")).toBeFalsy(); |
| 267 | expect(b.hasAttribute("cls")).toBeFalsy(); |
| 268 | expect(b.getAttribute("title")).toEqual("Glemm"); |
| 269 | expect(b.firstChild.classList.contains('button-icon')).toBeTruthy(); |
| 270 | expect(b.firstChild.classList.contains('check')).toBeTruthy(); |
| 271 | expect(b.firstChild.classList.contains('checked')).toBeFalsy(); |
| 272 | |
| 273 | expect(p.element().querySelectorAll("iframe").length).toEqual(0); |
| 274 | |
Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 275 | expect(manager.states().toString()).toEqual(""); |
| 276 | |
Akron | a70b689 | 2021-11-04 14:23:24 +0100 | [diff] [blame] | 277 | b.click(); |
| 278 | |
Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 279 | expect(manager.states().toString()).toEqual("\"check\":true"); |
| 280 | |
Akron | a70b689 | 2021-11-04 14:23:24 +0100 | [diff] [blame] | 281 | expect(b.getAttribute("title")).toEqual("Glemm"); |
| 282 | expect(b.firstChild.classList.contains('button-icon')).toBeTruthy(); |
| 283 | expect(b.firstChild.classList.contains('check')).toBeTruthy(); |
| 284 | expect(b.firstChild.classList.contains('checked')).toBeTruthy(); |
| 285 | |
| 286 | // Check with true default |
| 287 | p = KorAP.Panel["result"] = panelClass.create(); |
| 288 | |
| 289 | manager = pluginServerClass.create(); |
| 290 | |
| 291 | manager.register({ |
| 292 | name : 'Check', |
| 293 | embed : [{ |
| 294 | panel : 'result', |
| 295 | title : 'Glemm', |
| 296 | onClick : { |
| 297 | template : 'about:blank', |
| 298 | action : 'toggle', |
| 299 | 'default' : true |
| 300 | } |
| 301 | }] |
| 302 | }); |
| 303 | |
| 304 | b = p.actions().element().firstChild; |
| 305 | expect(b.hasAttribute("data-icon")).toBeFalsy(); |
| 306 | expect(b.hasAttribute("cls")).toBeFalsy(); |
| 307 | expect(b.getAttribute("title")).toEqual("Glemm"); |
| 308 | expect(b.firstChild.classList.contains('button-icon')).toBeTruthy(); |
| 309 | expect(b.firstChild.classList.contains('check')).toBeTruthy(); |
| 310 | expect(b.firstChild.classList.contains('checked')).toBeTruthy(); |
| 311 | |
| 312 | manager.destroy(); |
| 313 | KorAP.Panel["result"] = undefined; |
| 314 | }); |
| 315 | |
| 316 | |
Akron | 3d01380 | 2020-10-07 15:03:38 +0200 | [diff] [blame] | 317 | it('should alert on plugin info (1)', function () { |
| 318 | |
| 319 | let alertMsg; |
| 320 | spyOn(window, 'alert').and.callFake(function(msg) { |
| 321 | alertMsg = msg; |
| 322 | }); |
| 323 | |
| 324 | let p = KorAP.Panel["result"] = panelClass.create(); |
| 325 | |
| 326 | let manager = pluginServerClass.create(); |
| 327 | |
| 328 | manager.register({ |
| 329 | name : 'Check', |
| 330 | embed : [{ |
| 331 | panel : 'result', |
| 332 | title : 'Add', |
| 333 | onClick : { |
| 334 | template : 'about:blank', |
| 335 | action : 'setWidget' |
| 336 | } |
| 337 | }] |
| 338 | }); |
| 339 | |
| 340 | expect(alertMsg).toBeUndefined(); |
| 341 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 342 | let b = p.actions().element().firstChild; |
Akron | 3d01380 | 2020-10-07 15:03:38 +0200 | [diff] [blame] | 343 | b.click(); |
| 344 | |
| 345 | // This may only be temporary and should open the plugin window instead |
| 346 | p.element().querySelector("span.plugin").click(); |
| 347 | |
| 348 | expect(alertMsg).toEqual('Check'); |
| 349 | |
| 350 | manager.destroy(); |
| 351 | KorAP.Panel["result"] = undefined; |
| 352 | }); |
| 353 | |
| 354 | |
| 355 | it('should alert on plugin info (2)', function () { |
| 356 | |
| 357 | let alertMsg; |
| 358 | spyOn(window, 'alert').and.callFake(function(msg) { |
| 359 | alertMsg = msg; |
| 360 | }); |
| 361 | |
| 362 | let p = KorAP.Panel["result"] = panelClass.create(); |
| 363 | |
| 364 | let manager = pluginServerClass.create(); |
| 365 | |
| 366 | manager.register({ |
| 367 | name : 'Check', |
| 368 | desc : 'Simple Check plugin', |
| 369 | embed : [{ |
| 370 | panel : 'result', |
| 371 | title : 'Add', |
| 372 | onClick : { |
| 373 | template : 'about:blank', |
| 374 | action : 'setWidget' |
| 375 | } |
| 376 | }] |
| 377 | }); |
| 378 | |
| 379 | expect(alertMsg).toBeUndefined(); |
| 380 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 381 | let b = p.actions().element().firstChild; |
Akron | 3d01380 | 2020-10-07 15:03:38 +0200 | [diff] [blame] | 382 | b.click(); |
| 383 | |
| 384 | // This may only be temporary and should open the plugin window instead |
| 385 | p.element().querySelector("span.plugin").click(); |
| 386 | |
| 387 | expect(alertMsg).toEqual("Check\n\nSimple Check plugin"); |
| 388 | |
| 389 | manager.destroy(); |
| 390 | KorAP.Panel["result"] = undefined; |
| 391 | }); |
| 392 | |
| 393 | |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 394 | it('should accept widget permissions', function () { |
| 395 | let p = KorAP.Panel["result"] = panelClass.create(); |
| 396 | |
| 397 | let manager = pluginServerClass.create(); |
| 398 | |
| 399 | manager.register({ |
| 400 | name : 'Check', |
| 401 | embed : [{ |
| 402 | panel : 'result', |
| 403 | title : 'Add', |
| 404 | onClick : { |
| 405 | template : 'about:blank', |
| 406 | action : 'addWidget', |
Akron | ce0d882 | 2020-10-05 16:25:40 +0200 | [diff] [blame] | 407 | permissions: ['scripts', 'forms', 'all'] |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 408 | } |
| 409 | }] |
| 410 | }); |
| 411 | |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 412 | let b = p.actions().element().firstChild; |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 413 | b.click(); |
| 414 | expect(p.element().querySelectorAll("iframe").length).toEqual(1); |
| 415 | expect(p.element().querySelector("iframe").getAttribute('sandbox')).toEqual('allow-forms allow-scripts'); |
| 416 | }); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 417 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 418 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 419 | describe('KorAP.Plugin.Widget', function () { |
| 420 | it('should be initializable', function () { |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 421 | expect(function () { widgetClass.create() }).toThrow(new Error("Service not well defined")); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 422 | |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 423 | widget = widgetClass.create({"name" : "Test", "src":"https://example", "id":56}); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 424 | expect(widget).toBeTruthy(); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 425 | expect(widget.id).toEqual(56); |
| 426 | expect(widget.name).toEqual("Test"); |
| 427 | expect(widget.src).toEqual("https://example"); |
| 428 | }); |
| 429 | |
| 430 | it('should create a view element', function () { |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 431 | var widget = widgetClass.create({ |
| 432 | "name":"Test", |
| 433 | "src":"https://example", |
| 434 | "id":56, |
Akron | ce0d882 | 2020-10-05 16:25:40 +0200 | [diff] [blame] | 435 | "permissions":["scripts","forms"] |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 436 | }); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 437 | var we = widget.element(); |
| 438 | |
| 439 | expect(we.tagName).toEqual("DIV"); |
| 440 | expect(we.classList.contains('view')).toBeTruthy(); |
| 441 | expect(we.classList.contains('widget')).toBeTruthy(); |
| 442 | |
| 443 | var iframe = we.firstChild; |
| 444 | expect(iframe.tagName).toEqual("IFRAME"); |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 445 | expect(iframe.getAttribute("sandbox")).toEqual("allow-forms allow-scripts"); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 446 | expect(iframe.getAttribute("src")).toEqual("https://example"); |
| 447 | expect(iframe.getAttribute("name")).toEqual("56"); |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 448 | |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 449 | var btn = we.lastChild; |
| 450 | expect(btn.classList.contains("button-group")).toBeTruthy(); |
| 451 | expect(btn.classList.contains("button-view")).toBeTruthy(); |
| 452 | expect(btn.classList.contains("widget")).toBeTruthy(); |
| 453 | |
| 454 | expect(btn.firstChild.tagName).toEqual("SPAN"); |
| 455 | expect(btn.firstChild.classList.contains("button-icon")).toBeTruthy(); |
| 456 | expect(btn.firstChild.classList.contains("close")).toBeTruthy(); |
| 457 | expect(btn.firstChild.firstChild.tagName).toEqual("SPAN"); |
| 458 | |
| 459 | expect(btn.lastChild.tagName).toEqual("SPAN"); |
| 460 | expect(btn.lastChild.classList.contains("button-icon")).toBeTruthy(); |
| 461 | expect(btn.lastChild.classList.contains("plugin")).toBeTruthy(); |
| 462 | expect(btn.lastChild.firstChild.tagName).toEqual("SPAN"); |
| 463 | expect(btn.lastChild.textContent).toEqual("Test"); |
| 464 | }) |
Akron | fb11a96 | 2020-10-05 12:12:55 +0200 | [diff] [blame] | 465 | |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 466 | it('should be resizable', function () { |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 467 | var widget = widgetClass.create({"name":"Test", "src":"https://example", "id":56}); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 468 | var iframe = widget.show(); |
| 469 | expect(iframe.style.height).toEqual('0px'); |
| 470 | widget.resize({ height : 9 }); |
| 471 | expect(iframe.style.height).toEqual('9px'); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 472 | }); |
Akron | 3d9ce5e | 2020-10-01 15:18:36 +0200 | [diff] [blame] | 473 | |
| 474 | it('should be minimizable', function () { |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 475 | var widget = widgetClass.create({"name":"Test", "src":"https://example", "id":56}); |
Akron | 3d9ce5e | 2020-10-01 15:18:36 +0200 | [diff] [blame] | 476 | var we = widget.element(); |
| 477 | expect(we.classList.contains('show')).toBeTruthy(); |
| 478 | widget.minimize(); |
| 479 | expect(we.classList.contains('show')).toBeFalsy(); |
| 480 | }); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 481 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 482 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 483 | describe('KorAP.Plugin.Service', function () { |
| 484 | it('should be initializable', function () { |
| 485 | expect(function () { serviceClass.create() }).toThrow(new Error("Service not well defined")); |
| 486 | |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 487 | let service = serviceClass.create({"name":"Test", "src":"https://example", "id":56}); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 488 | expect(service).toBeTruthy(); |
| 489 | expect(service.id).toEqual(56); |
| 490 | expect(service.name).toEqual("Test"); |
| 491 | expect(service.src).toEqual("https://example"); |
| 492 | }); |
| 493 | |
| 494 | it('should be loadable', function () { |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 495 | let service = serviceClass.create({"name":"Test", "src":"https://example", "id":56}); |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 496 | expect(service).toBeTruthy(); |
| 497 | |
| 498 | let i = service.load(); |
| 499 | expect(i.tagName).toEqual("IFRAME"); |
| 500 | expect(i.getAttribute("allowTransparency")).toEqual("true"); |
| 501 | expect(i.getAttribute("frameborder")).toEqual(''+0); |
| 502 | expect(i.getAttribute("name")).toEqual(''+service.id); |
| 503 | expect(i.getAttribute("src")).toEqual(service.src); |
| 504 | }); |
| 505 | }); |
| 506 | |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 507 | describe('KorAP.Plugin.QueryPanel', function () { |
| 508 | it('should establish a query plugin', function () { |
| 509 | var queryPanel = queryPanelClass.create(); |
| 510 | |
| 511 | var div = document.createElement('div'); |
| 512 | |
| 513 | div.appendChild(queryPanel.element()); |
| 514 | KorAP.Panel = KorAP.Panel || {}; |
| 515 | KorAP.Panel['query'] = queryPanel; |
| 516 | |
| 517 | // Register plugin afterwards |
| 518 | var manager = pluginServerClass.create(); |
| 519 | |
| 520 | manager.register({ |
| 521 | name : 'Check', |
| 522 | embed : [{ |
| 523 | panel : 'query', |
| 524 | title : 'Translate', |
| 525 | onClick : { |
| 526 | template : 'test' |
| 527 | } |
| 528 | }] |
| 529 | }); |
| 530 | |
| 531 | expect(manager.buttonGroup('query').length).toEqual(0); |
| 532 | |
| 533 | // Clean up |
| 534 | KorAP.Panel['query'] = undefined; |
| 535 | manager.destroy(); |
| 536 | }); |
hebasta | f6adf8d | 2019-11-26 14:04:10 +0100 | [diff] [blame] | 537 | |
| 538 | it('Plugin buttons should be cleared after adding to panel', function () { |
| 539 | |
| 540 | // Register plugin first |
| 541 | KorAP.Plugin = pluginServerClass.create(); |
| 542 | |
| 543 | KorAP.Plugin.register({ |
| 544 | name : 'Check', |
| 545 | embed : [{ |
| 546 | panel : 'query', |
| 547 | title : 'Translate', |
| 548 | onClick : { |
| 549 | template : 'test' |
| 550 | } |
| 551 | }] |
| 552 | }); |
| 553 | |
| 554 | |
| 555 | var queryPanel = queryPanelClass.create(); |
| 556 | var div = document.createElement('div'); |
| 557 | |
| 558 | div.appendChild(queryPanel.element()); |
| 559 | KorAP.Panel = KorAP.Panel || {}; |
| 560 | KorAP.Panel['query'] = queryPanel; |
| 561 | expect(KorAP.Plugin.buttonGroup('query').length).toEqual(0); |
| 562 | |
| 563 | // Clean up |
| 564 | KorAP.Panel['query'] = undefined; |
| 565 | KorAP.Plugin.destroy(); |
| 566 | KorAP.Plugin = undefined; |
| 567 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 568 | }); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 569 | |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 570 | describe('KorAP.Plugin.ResultPanel', function () { |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 571 | it('Plugin is registered second: buttons should be added to panel', function () { |
| 572 | |
| 573 | var resultPanel = resultPanelClass.create(); |
| 574 | resultPanel.addAlignAction(); |
| 575 | var div = document.createElement('div'); |
| 576 | |
| 577 | div.appendChild(resultPanel.element()); |
| 578 | KorAP.Panel = KorAP.Panel || {}; |
| 579 | KorAP.Panel['result'] = resultPanel; |
| 580 | |
| 581 | // Register plugin afterwards |
| 582 | var manager = pluginServerClass.create(); |
| 583 | |
| 584 | manager.register({ |
| 585 | name : 'ResultPlugin', |
| 586 | embed : [{ |
| 587 | panel : 'result', |
| 588 | title : 'Dosomething', |
| 589 | onClick : { |
| 590 | template : 'test' |
| 591 | } |
| 592 | }] |
| 593 | }); |
| 594 | |
| 595 | expect(manager.buttonGroup('result').length).toEqual(0); |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 596 | expect(KorAP.Panel['result'].actions().element().innerHTML).toContain('Dosomething'); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 597 | |
| 598 | // Clean up |
| 599 | KorAP.Panel['result'] = undefined; |
| 600 | manager.destroy(); |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 601 | }); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 602 | |
| 603 | it('Plugin is registered first: Buttons should be added to panel and cleared', function () { |
| 604 | |
| 605 | // Register plugin first |
| 606 | KorAP.Plugin = pluginServerClass.create(); |
| 607 | |
| 608 | KorAP.Plugin.register({ |
| 609 | name : 'ResultPlugin', |
| 610 | embed : [{ |
| 611 | panel : 'result', |
| 612 | title : 'Dosomething', |
| 613 | onClick : { |
| 614 | template : 'test' |
| 615 | } |
| 616 | }] |
| 617 | }); |
| 618 | |
| 619 | expect(KorAP.Plugin.buttonGroup('result').length).toEqual(1); |
| 620 | |
| 621 | var resultPanel = resultPanelClass.create(); |
| 622 | var div = document.createElement('div'); |
| 623 | div.appendChild(resultPanel.element()); |
| 624 | KorAP.Panel = KorAP.Panel || {}; |
| 625 | KorAP.Panel['result'] = resultPanel; |
| 626 | expect(KorAP.Plugin.buttonGroup('result').length).toEqual(0); |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 627 | expect(KorAP.Panel['result'].actions().element().innerHTML).toContain('Dosomething'); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 628 | |
| 629 | // Clean up |
| 630 | KorAP.Panel['result'] = undefined; |
| 631 | KorAP.Plugin.destroy(); |
| 632 | KorAP.Plugin = undefined; |
| 633 | }); |
| 634 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 635 | |
| 636 | describe('KorAP.Plugin communications', function () { |
| 637 | it('should receive messages', function () { |
| 638 | var manager = pluginServerClass.create(); |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 639 | var id = manager.addService({"name":'Example 1', "src":'about:blank'}); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 640 | expect(id).toMatch(/^id-/); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 641 | var temp = KorAP.koralQuery; |
| 642 | KorAP.koralQuery = { "@type" : "koral:test" }; |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 643 | let data = { |
| 644 | "originID" : id, |
| 645 | "action" : "get", |
| 646 | "key" : "KQ" |
| 647 | }; |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 648 | manager._receiveMsg({ |
| 649 | "data" : data |
| 650 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 651 | manager.destroy(); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 652 | expect(data.value["@type"]).toEqual("koral:test"); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 653 | // Recreate initial state |
| 654 | KorAP.koralQuery = temp; |
| 655 | }); |
| 656 | |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 657 | it('should add to pipe', function () { |
| 658 | var manager = pluginServerClass.create(); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 659 | var temp = KorAP.Pipe; |
| 660 | KorAP.Pipe = pipeClass.create(); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 661 | expect(KorAP.Pipe.toString()).toEqual(""); |
| 662 | |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 663 | var id = manager.addService({"name":'Example 2', "src":'about:blank'}); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 664 | expect(id).toMatch(/^id-/); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 665 | manager._receiveMsg({ |
| 666 | "data" : { |
| 667 | "originID" : id, |
| 668 | "action" : "pipe", |
| 669 | "job" : "add", |
| 670 | "service" : "https://pipe-service.de" |
| 671 | } |
| 672 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 673 | expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de"); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 674 | manager._receiveMsg({ |
| 675 | "data" : { |
| 676 | "originID" : id, |
| 677 | "action" : "pipe", |
| 678 | "job" : "add", |
| 679 | "service" : "https://pipe-service-2.de" |
| 680 | } |
| 681 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 682 | 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] | 683 | manager._receiveMsg({ |
| 684 | "data" : { |
| 685 | "originID" : id, |
| 686 | "action" : "pipe", |
| 687 | "job" : "del", |
| 688 | "service" : "https://pipe-service.de" |
| 689 | } |
| 690 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 691 | expect(KorAP.Pipe.toString()).toEqual("https://pipe-service-2.de"); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 692 | manager.destroy(); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 693 | // Recreate initial state |
| 694 | KorAP.Pipe = temp; |
| 695 | }); |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 696 | |
Akron | 26d57f2 | 2021-09-10 16:48:57 +0200 | [diff] [blame] | 697 | it('should reply to query information requests (queryform)', function () { |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 698 | var manager = pluginServerClass.create(); |
Akron | bb89198 | 2020-10-05 16:07:18 +0200 | [diff] [blame] | 699 | var id = manager.addService({"name":'Service', "src":'about:blank'}); |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 700 | expect(id).toMatch(/^id-/); |
| 701 | var temp = KorAP.vc; |
| 702 | // Create form for query form information |
| 703 | let f = document.createElement('form'); |
| 704 | var qfield = f.addE('input'); |
| 705 | qfield.setAttribute("id", "q-field"); |
| 706 | qfield.value = "[orth=Baum]"; |
| 707 | var qlfield = f.addE('select'); |
| 708 | qlfield.setAttribute("id", "ql-field"); |
| 709 | qlfield.addE('option').setAttribute('value', 'cosmas-2'); |
| 710 | qlfield.addE('option').setAttribute('value', 'poliqarp'); |
| 711 | qlfield.selectedIndex = 1; |
| 712 | |
| 713 | KorAP.vc = vcClass.create().fromJson({ |
| 714 | "key" : "title", |
| 715 | "type" : "type:regex", |
| 716 | "value" : "[^b]ee.+?", |
| 717 | "@type" : "koral:doc" |
| 718 | }); |
| 719 | // console.log(KorAP.vc.toQuery()); |
| 720 | |
| 721 | document.body.appendChild(f); |
| 722 | let data = { |
| 723 | "originID" : id, |
| 724 | "action" : "get", |
| 725 | "key" : "QueryForm" |
| 726 | }; |
| 727 | manager._receiveMsg({ |
| 728 | "data" : data |
| 729 | }); |
| 730 | manager.destroy(); |
| 731 | expect(data.value["q"]).toEqual("[orth=Baum]"); |
| 732 | expect(data.value["ql"]).toEqual("poliqarp"); |
| 733 | expect(data.value["cq"]).toEqual("title = /[^b]ee.+?/"); |
Akron | 26d57f2 | 2021-09-10 16:48:57 +0200 | [diff] [blame] | 734 | |
Akron | 45308ce | 2020-08-28 14:10:23 +0200 | [diff] [blame] | 735 | // Recreate initial state |
| 736 | KorAP.vc = temp; |
| 737 | document.body.removeChild(f); |
| 738 | }); |
Akron | 26d57f2 | 2021-09-10 16:48:57 +0200 | [diff] [blame] | 739 | |
| 740 | it('should reply to query information requests (pagination)', function () { |
| 741 | var manager = pluginServerClass.create(); |
| 742 | var id = manager.addService({"name":'Service', "src":'about:blank'}); |
| 743 | expect(id).toMatch(/^id-/); |
| 744 | |
| 745 | // Create pagination element for pagination information |
| 746 | let p = document.createElement('div'); |
| 747 | p.setAttribute('id', 'pagination') |
| 748 | p.setAttribute('data-page',3); |
| 749 | p.setAttribute('data-total',30); |
| 750 | p.setAttribute('data-count',25); |
| 751 | |
| 752 | document.body.appendChild(p); |
| 753 | |
| 754 | let data = { |
| 755 | "originID" : id, |
| 756 | "action" : "get", |
| 757 | "key" : "Pagination" |
| 758 | }; |
| 759 | manager._receiveMsg({ |
| 760 | "data" : data |
| 761 | }); |
| 762 | manager.destroy(); |
| 763 | expect(data.value["count"]).toEqual(25); |
| 764 | expect(data.value["page"]).toEqual(3); |
| 765 | expect(data.value["total"]).toEqual(30); |
| 766 | |
| 767 | // Recreate initial state |
| 768 | document.body.removeChild(p); |
| 769 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame] | 770 | }); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 771 | }); |