Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame^] | 1 | define(['plugin/server','plugin/widget','panel', 'panel/query', 'panel/result', 'plugin/service', 'pipe'], function (pluginServerClass, widgetClass, panelClass, queryPanelClass, resultPanelClass, serviceClass, pipeClass) { |
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(); |
| 14 | var id = manager.addWidget(panel, 'Example 1', '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 | |
| 42 | var id = manager.addService('Example 1', 'about:blank'); |
| 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(); |
| 56 | var id = manager.addWidget(panel, 'Example 2', 'about:blank'); |
| 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 | }); |
| 116 | |
| 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 | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 153 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 154 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 155 | describe('KorAP.Plugin.Widget', function () { |
| 156 | it('should be initializable', function () { |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 157 | expect(function () { widgetClass.create() }).toThrow(new Error("Service not well defined")); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 158 | |
| 159 | widget = widgetClass.create("Test", "https://example", 56); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 160 | expect(widget).toBeTruthy(); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 161 | expect(widget.id).toEqual(56); |
| 162 | expect(widget.name).toEqual("Test"); |
| 163 | expect(widget.src).toEqual("https://example"); |
| 164 | }); |
| 165 | |
| 166 | it('should create a view element', function () { |
| 167 | var widget = widgetClass.create("Test", "https://example", 56); |
| 168 | var we = widget.element(); |
| 169 | |
| 170 | expect(we.tagName).toEqual("DIV"); |
| 171 | expect(we.classList.contains('view')).toBeTruthy(); |
| 172 | expect(we.classList.contains('widget')).toBeTruthy(); |
| 173 | |
| 174 | var iframe = we.firstChild; |
| 175 | expect(iframe.tagName).toEqual("IFRAME"); |
hebasta | 7891324 | 2020-03-30 13:39:20 +0200 | [diff] [blame] | 176 | expect(iframe.getAttribute("sandbox")).toEqual("allow-scripts allow-forms"); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 177 | expect(iframe.getAttribute("src")).toEqual("https://example"); |
| 178 | expect(iframe.getAttribute("name")).toEqual("56"); |
| 179 | |
| 180 | var btn = we.lastChild; |
| 181 | expect(btn.classList.contains("button-group")).toBeTruthy(); |
| 182 | expect(btn.classList.contains("button-view")).toBeTruthy(); |
| 183 | expect(btn.classList.contains("widget")).toBeTruthy(); |
| 184 | |
| 185 | expect(btn.firstChild.tagName).toEqual("SPAN"); |
| 186 | expect(btn.firstChild.classList.contains("button-icon")).toBeTruthy(); |
| 187 | expect(btn.firstChild.classList.contains("close")).toBeTruthy(); |
| 188 | expect(btn.firstChild.firstChild.tagName).toEqual("SPAN"); |
| 189 | |
| 190 | expect(btn.lastChild.tagName).toEqual("SPAN"); |
| 191 | expect(btn.lastChild.classList.contains("button-icon")).toBeTruthy(); |
| 192 | expect(btn.lastChild.classList.contains("plugin")).toBeTruthy(); |
| 193 | expect(btn.lastChild.firstChild.tagName).toEqual("SPAN"); |
| 194 | expect(btn.lastChild.textContent).toEqual("Test"); |
| 195 | }) |
| 196 | |
| 197 | it('should be resizable', function () { |
| 198 | var widget = widgetClass.create("Test", "https://example", 56); |
| 199 | var iframe = widget.show(); |
| 200 | expect(iframe.style.height).toEqual('0px'); |
| 201 | widget.resize({ height : 9 }); |
| 202 | expect(iframe.style.height).toEqual('9px'); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 203 | }); |
| 204 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 205 | |
Akron | 22598cd | 2019-12-09 14:59:03 +0100 | [diff] [blame] | 206 | describe('KorAP.Plugin.Service', function () { |
| 207 | it('should be initializable', function () { |
| 208 | expect(function () { serviceClass.create() }).toThrow(new Error("Service not well defined")); |
| 209 | |
| 210 | let service = serviceClass.create("Test", "https://example", 56); |
| 211 | expect(service).toBeTruthy(); |
| 212 | expect(service.id).toEqual(56); |
| 213 | expect(service.name).toEqual("Test"); |
| 214 | expect(service.src).toEqual("https://example"); |
| 215 | }); |
| 216 | |
| 217 | it('should be loadable', function () { |
| 218 | let service = serviceClass.create("Test", "https://example", 56); |
| 219 | expect(service).toBeTruthy(); |
| 220 | |
| 221 | let i = service.load(); |
| 222 | expect(i.tagName).toEqual("IFRAME"); |
| 223 | expect(i.getAttribute("allowTransparency")).toEqual("true"); |
| 224 | expect(i.getAttribute("frameborder")).toEqual(''+0); |
| 225 | expect(i.getAttribute("name")).toEqual(''+service.id); |
| 226 | expect(i.getAttribute("src")).toEqual(service.src); |
| 227 | }); |
| 228 | }); |
| 229 | |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 230 | describe('KorAP.Plugin.QueryPanel', function () { |
| 231 | it('should establish a query plugin', function () { |
| 232 | var queryPanel = queryPanelClass.create(); |
| 233 | |
| 234 | var div = document.createElement('div'); |
| 235 | |
| 236 | div.appendChild(queryPanel.element()); |
| 237 | KorAP.Panel = KorAP.Panel || {}; |
| 238 | KorAP.Panel['query'] = queryPanel; |
| 239 | |
| 240 | // Register plugin afterwards |
| 241 | var manager = pluginServerClass.create(); |
| 242 | |
| 243 | manager.register({ |
| 244 | name : 'Check', |
| 245 | embed : [{ |
| 246 | panel : 'query', |
| 247 | title : 'Translate', |
| 248 | onClick : { |
| 249 | template : 'test' |
| 250 | } |
| 251 | }] |
| 252 | }); |
| 253 | |
| 254 | expect(manager.buttonGroup('query').length).toEqual(0); |
| 255 | |
| 256 | // Clean up |
| 257 | KorAP.Panel['query'] = undefined; |
| 258 | manager.destroy(); |
| 259 | }); |
hebasta | f6adf8d | 2019-11-26 14:04:10 +0100 | [diff] [blame] | 260 | |
| 261 | it('Plugin buttons should be cleared after adding to panel', function () { |
| 262 | |
| 263 | // Register plugin first |
| 264 | KorAP.Plugin = pluginServerClass.create(); |
| 265 | |
| 266 | KorAP.Plugin.register({ |
| 267 | name : 'Check', |
| 268 | embed : [{ |
| 269 | panel : 'query', |
| 270 | title : 'Translate', |
| 271 | onClick : { |
| 272 | template : 'test' |
| 273 | } |
| 274 | }] |
| 275 | }); |
| 276 | |
| 277 | |
| 278 | var queryPanel = queryPanelClass.create(); |
| 279 | var div = document.createElement('div'); |
| 280 | |
| 281 | div.appendChild(queryPanel.element()); |
| 282 | KorAP.Panel = KorAP.Panel || {}; |
| 283 | KorAP.Panel['query'] = queryPanel; |
| 284 | expect(KorAP.Plugin.buttonGroup('query').length).toEqual(0); |
| 285 | |
| 286 | // Clean up |
| 287 | KorAP.Panel['query'] = undefined; |
| 288 | KorAP.Plugin.destroy(); |
| 289 | KorAP.Plugin = undefined; |
| 290 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 291 | }); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 292 | |
| 293 | describe('KorAP.Plugin.ResultPanel', function () { |
| 294 | |
| 295 | it('Plugin is registered second: buttons should be added to panel', function () { |
| 296 | |
| 297 | var resultPanel = resultPanelClass.create(); |
| 298 | resultPanel.addAlignAction(); |
| 299 | var div = document.createElement('div'); |
| 300 | |
| 301 | div.appendChild(resultPanel.element()); |
| 302 | KorAP.Panel = KorAP.Panel || {}; |
| 303 | KorAP.Panel['result'] = resultPanel; |
| 304 | |
| 305 | // Register plugin afterwards |
| 306 | var manager = pluginServerClass.create(); |
| 307 | |
| 308 | manager.register({ |
| 309 | name : 'ResultPlugin', |
| 310 | embed : [{ |
| 311 | panel : 'result', |
| 312 | title : 'Dosomething', |
| 313 | onClick : { |
| 314 | template : 'test' |
| 315 | } |
| 316 | }] |
| 317 | }); |
| 318 | |
| 319 | expect(manager.buttonGroup('result').length).toEqual(0); |
| 320 | expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething'); |
| 321 | |
| 322 | // Clean up |
| 323 | KorAP.Panel['result'] = undefined; |
| 324 | manager.destroy(); |
| 325 | }); |
| 326 | |
| 327 | it('Plugin is registered first: Buttons should be added to panel and cleared', function () { |
| 328 | |
| 329 | // Register plugin first |
| 330 | KorAP.Plugin = pluginServerClass.create(); |
| 331 | |
| 332 | KorAP.Plugin.register({ |
| 333 | name : 'ResultPlugin', |
| 334 | embed : [{ |
| 335 | panel : 'result', |
| 336 | title : 'Dosomething', |
| 337 | onClick : { |
| 338 | template : 'test' |
| 339 | } |
| 340 | }] |
| 341 | }); |
| 342 | |
| 343 | expect(KorAP.Plugin.buttonGroup('result').length).toEqual(1); |
| 344 | |
| 345 | var resultPanel = resultPanelClass.create(); |
| 346 | var div = document.createElement('div'); |
| 347 | div.appendChild(resultPanel.element()); |
| 348 | KorAP.Panel = KorAP.Panel || {}; |
| 349 | KorAP.Panel['result'] = resultPanel; |
| 350 | expect(KorAP.Plugin.buttonGroup('result').length).toEqual(0); |
| 351 | expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething'); |
| 352 | |
| 353 | // Clean up |
| 354 | KorAP.Panel['result'] = undefined; |
| 355 | KorAP.Plugin.destroy(); |
| 356 | KorAP.Plugin = undefined; |
| 357 | }); |
| 358 | }); |
Akron | fec66a3 | 2020-08-28 13:01:14 +0200 | [diff] [blame^] | 359 | |
| 360 | describe('KorAP.Plugin communications', function () { |
| 361 | it('should receive messages', function () { |
| 362 | var manager = pluginServerClass.create(); |
| 363 | |
| 364 | var id = manager.addService('Example 1', 'about:blank'); |
| 365 | expect(id).toMatch(/^id-/); |
| 366 | |
| 367 | var temp = KorAP.koralQuery; |
| 368 | KorAP.koralQuery = { "@type" : "koral:test" }; |
| 369 | |
| 370 | let data = { |
| 371 | "originID" : id, |
| 372 | "action" : "get", |
| 373 | "key" : "KQ" |
| 374 | }; |
| 375 | |
| 376 | manager._receiveMsg({ |
| 377 | "data" : data |
| 378 | }); |
| 379 | |
| 380 | manager.destroy(); |
| 381 | |
| 382 | expect(data.value["@type"]).toEqual("koral:test"); |
| 383 | |
| 384 | // Recreate initial state |
| 385 | KorAP.koralQuery = temp; |
| 386 | }); |
| 387 | |
| 388 | |
| 389 | it('should add to pipe', function () { |
| 390 | var manager = pluginServerClass.create(); |
| 391 | |
| 392 | var temp = KorAP.Pipe; |
| 393 | KorAP.Pipe = pipeClass.create(); |
| 394 | |
| 395 | expect(KorAP.Pipe.toString()).toEqual(""); |
| 396 | |
| 397 | var id = manager.addService('Example 2', 'about:blank'); |
| 398 | expect(id).toMatch(/^id-/); |
| 399 | |
| 400 | manager._receiveMsg({ |
| 401 | "data" : { |
| 402 | "originID" : id, |
| 403 | "action" : "pipe", |
| 404 | "job" : "add", |
| 405 | "service" : "https://pipe-service.de" |
| 406 | } |
| 407 | }); |
| 408 | |
| 409 | expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de"); |
| 410 | |
| 411 | manager._receiveMsg({ |
| 412 | "data" : { |
| 413 | "originID" : id, |
| 414 | "action" : "pipe", |
| 415 | "job" : "add", |
| 416 | "service" : "https://pipe-service-2.de" |
| 417 | } |
| 418 | }); |
| 419 | |
| 420 | expect(KorAP.Pipe.toString()).toEqual("https://pipe-service.de,https://pipe-service-2.de"); |
| 421 | |
| 422 | manager._receiveMsg({ |
| 423 | "data" : { |
| 424 | "originID" : id, |
| 425 | "action" : "pipe", |
| 426 | "job" : "del", |
| 427 | "service" : "https://pipe-service.de" |
| 428 | } |
| 429 | }); |
| 430 | |
| 431 | expect(KorAP.Pipe.toString()).toEqual("https://pipe-service-2.de"); |
| 432 | |
| 433 | manager.destroy(); |
| 434 | |
| 435 | // Recreate initial state |
| 436 | KorAP.Pipe = temp; |
| 437 | }); |
| 438 | }); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 439 | }); |