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