hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 1 | define(['plugin/server','plugin/widget','panel', 'panel/query', 'panel/result'], function (pluginServerClass, widgetClass, panelClass, queryPanelClass, resultPanelClass) { |
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 | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 30 | it('should close a widget', function () { |
| 31 | var manager = pluginServerClass.create(); |
| 32 | var panel = panelClass.create(); |
| 33 | var id = manager.addWidget(panel, 'Example 2', 'about:blank'); |
| 34 | expect(id).toMatch(/^id-/); |
| 35 | |
| 36 | var panelE = panel.element(); |
| 37 | var widgetE = panelE.firstChild.firstChild; |
| 38 | expect(widgetE.classList.contains('widget')).toBeTruthy(); |
| 39 | |
| 40 | expect(panelE.getElementsByClassName('view').length).toEqual(1); |
| 41 | |
| 42 | var widget = manager.widget(id); |
| 43 | widget.close(); |
| 44 | |
| 45 | expect(panelE.getElementsByClassName('view').length).toEqual(0); |
Akron | 4a70387 | 2018-07-26 10:59:41 +0200 | [diff] [blame] | 46 | manager.destroy(); |
| 47 | }); |
| 48 | |
| 49 | |
| 50 | it('should fail on invalid registrations', function () { |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 51 | var manager = pluginServerClass.create(); |
| 52 | |
| 53 | expect( |
| 54 | function() { manager.register({}) } |
| 55 | ).toThrow(new Error("Missing name of plugin")); |
| 56 | |
| 57 | expect( |
| 58 | function() { manager.register({ |
| 59 | name : 'Example', |
| 60 | embed : '' |
| 61 | })} |
| 62 | ).toThrow(new Error("Embedding of plugin is no list")); |
| 63 | |
| 64 | expect( |
| 65 | function() { manager.register({ |
| 66 | name : 'Example', |
| 67 | embed : [{ |
| 68 | panel : '' |
| 69 | }] |
| 70 | })} |
| 71 | ).toThrow(new Error("Panel for plugin is invalid")); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 72 | manager.destroy(); |
| 73 | }); |
| 74 | |
| 75 | it('should accept valid registrations for matches', function () { |
| 76 | var manager = pluginServerClass.create(); |
| 77 | |
| 78 | manager.register({ |
| 79 | name : 'Check', |
| 80 | embed : [{ |
| 81 | panel : 'match', |
| 82 | title : 'Translate', |
| 83 | onClick : { |
| 84 | template : 'test' |
| 85 | } |
| 86 | }] |
| 87 | }); |
| 88 | |
| 89 | expect(manager.buttonGroup('match').length).toEqual(1); |
| 90 | manager.destroy(); |
| 91 | }); |
| 92 | |
| 93 | it('should accept valid registrations for query temporary', function () { |
| 94 | var manager = pluginServerClass.create(); |
| 95 | |
| 96 | manager.register({ |
| 97 | name : 'Check', |
| 98 | embed : [{ |
| 99 | panel : 'query', |
| 100 | title : 'Translate', |
| 101 | onClick : { |
| 102 | template : 'test' |
| 103 | } |
| 104 | }] |
| 105 | }); |
| 106 | |
| 107 | expect(manager.buttonGroup('query').length).toEqual(1); |
| 108 | manager.destroy(); |
Akron | 10a4796 | 2018-07-12 21:17:10 +0200 | [diff] [blame] | 109 | }); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 110 | |
| 111 | |
| 112 | it('should accept valid registrations for result', function () { |
| 113 | var manager = pluginServerClass.create(); |
| 114 | |
| 115 | manager.register({ |
| 116 | name : 'Check', |
| 117 | embed : [{ |
| 118 | panel : 'result', |
| 119 | title : 'Translate', |
| 120 | onClick : { |
| 121 | template : 'test' |
| 122 | } |
| 123 | }] |
| 124 | }); |
| 125 | |
| 126 | expect(manager.buttonGroup('result').length).toEqual(1); |
| 127 | manager.destroy(); |
| 128 | }); |
| 129 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 130 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 131 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 132 | describe('KorAP.Plugin.Widget', function () { |
| 133 | it('should be initializable', function () { |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 134 | expect(function () { widgetClass.create() }).toThrow(new Error("Widget not well defined")); |
| 135 | |
| 136 | widget = widgetClass.create("Test", "https://example", 56); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 137 | expect(widget).toBeTruthy(); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 138 | expect(widget.id).toEqual(56); |
| 139 | expect(widget.name).toEqual("Test"); |
| 140 | expect(widget.src).toEqual("https://example"); |
| 141 | }); |
| 142 | |
| 143 | it('should create a view element', function () { |
| 144 | var widget = widgetClass.create("Test", "https://example", 56); |
| 145 | var we = widget.element(); |
| 146 | |
| 147 | expect(we.tagName).toEqual("DIV"); |
| 148 | expect(we.classList.contains('view')).toBeTruthy(); |
| 149 | expect(we.classList.contains('widget')).toBeTruthy(); |
| 150 | |
| 151 | var iframe = we.firstChild; |
| 152 | expect(iframe.tagName).toEqual("IFRAME"); |
| 153 | expect(iframe.getAttribute("sandbox")).toEqual("allow-scripts"); |
| 154 | expect(iframe.getAttribute("src")).toEqual("https://example"); |
| 155 | expect(iframe.getAttribute("name")).toEqual("56"); |
| 156 | |
| 157 | var btn = we.lastChild; |
| 158 | expect(btn.classList.contains("button-group")).toBeTruthy(); |
| 159 | expect(btn.classList.contains("button-view")).toBeTruthy(); |
| 160 | expect(btn.classList.contains("widget")).toBeTruthy(); |
| 161 | |
| 162 | expect(btn.firstChild.tagName).toEqual("SPAN"); |
| 163 | expect(btn.firstChild.classList.contains("button-icon")).toBeTruthy(); |
| 164 | expect(btn.firstChild.classList.contains("close")).toBeTruthy(); |
| 165 | expect(btn.firstChild.firstChild.tagName).toEqual("SPAN"); |
| 166 | |
| 167 | expect(btn.lastChild.tagName).toEqual("SPAN"); |
| 168 | expect(btn.lastChild.classList.contains("button-icon")).toBeTruthy(); |
| 169 | expect(btn.lastChild.classList.contains("plugin")).toBeTruthy(); |
| 170 | expect(btn.lastChild.firstChild.tagName).toEqual("SPAN"); |
| 171 | expect(btn.lastChild.textContent).toEqual("Test"); |
| 172 | }) |
| 173 | |
| 174 | it('should be resizable', function () { |
| 175 | var widget = widgetClass.create("Test", "https://example", 56); |
| 176 | var iframe = widget.show(); |
| 177 | expect(iframe.style.height).toEqual('0px'); |
| 178 | widget.resize({ height : 9 }); |
| 179 | expect(iframe.style.height).toEqual('9px'); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 180 | }); |
| 181 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 182 | |
| 183 | describe('KorAP.Plugin.QueryPanel', function () { |
| 184 | it('should establish a query plugin', function () { |
| 185 | var queryPanel = queryPanelClass.create(); |
| 186 | |
| 187 | var div = document.createElement('div'); |
| 188 | |
| 189 | div.appendChild(queryPanel.element()); |
| 190 | KorAP.Panel = KorAP.Panel || {}; |
| 191 | KorAP.Panel['query'] = queryPanel; |
| 192 | |
| 193 | // Register plugin afterwards |
| 194 | var manager = pluginServerClass.create(); |
| 195 | |
| 196 | manager.register({ |
| 197 | name : 'Check', |
| 198 | embed : [{ |
| 199 | panel : 'query', |
| 200 | title : 'Translate', |
| 201 | onClick : { |
| 202 | template : 'test' |
| 203 | } |
| 204 | }] |
| 205 | }); |
| 206 | |
| 207 | expect(manager.buttonGroup('query').length).toEqual(0); |
| 208 | |
| 209 | // Clean up |
| 210 | KorAP.Panel['query'] = undefined; |
| 211 | manager.destroy(); |
| 212 | }); |
hebasta | f6adf8d | 2019-11-26 14:04:10 +0100 | [diff] [blame] | 213 | |
| 214 | it('Plugin buttons should be cleared after adding to panel', function () { |
| 215 | |
| 216 | // Register plugin first |
| 217 | KorAP.Plugin = pluginServerClass.create(); |
| 218 | |
| 219 | KorAP.Plugin.register({ |
| 220 | name : 'Check', |
| 221 | embed : [{ |
| 222 | panel : 'query', |
| 223 | title : 'Translate', |
| 224 | onClick : { |
| 225 | template : 'test' |
| 226 | } |
| 227 | }] |
| 228 | }); |
| 229 | |
| 230 | |
| 231 | var queryPanel = queryPanelClass.create(); |
| 232 | var div = document.createElement('div'); |
| 233 | |
| 234 | div.appendChild(queryPanel.element()); |
| 235 | KorAP.Panel = KorAP.Panel || {}; |
| 236 | KorAP.Panel['query'] = queryPanel; |
| 237 | expect(KorAP.Plugin.buttonGroup('query').length).toEqual(0); |
| 238 | |
| 239 | // Clean up |
| 240 | KorAP.Panel['query'] = undefined; |
| 241 | KorAP.Plugin.destroy(); |
| 242 | KorAP.Plugin = undefined; |
| 243 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 244 | }); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 245 | |
| 246 | describe('KorAP.Plugin.ResultPanel', function () { |
| 247 | |
| 248 | it('Plugin is registered second: buttons should be added to panel', function () { |
| 249 | |
| 250 | var resultPanel = resultPanelClass.create(); |
| 251 | resultPanel.addAlignAction(); |
| 252 | var div = document.createElement('div'); |
| 253 | |
| 254 | div.appendChild(resultPanel.element()); |
| 255 | KorAP.Panel = KorAP.Panel || {}; |
| 256 | KorAP.Panel['result'] = resultPanel; |
| 257 | |
| 258 | // Register plugin afterwards |
| 259 | var manager = pluginServerClass.create(); |
| 260 | |
| 261 | manager.register({ |
| 262 | name : 'ResultPlugin', |
| 263 | embed : [{ |
| 264 | panel : 'result', |
| 265 | title : 'Dosomething', |
| 266 | onClick : { |
| 267 | template : 'test' |
| 268 | } |
| 269 | }] |
| 270 | }); |
| 271 | |
| 272 | expect(manager.buttonGroup('result').length).toEqual(0); |
| 273 | expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething'); |
| 274 | |
| 275 | // Clean up |
| 276 | KorAP.Panel['result'] = undefined; |
| 277 | manager.destroy(); |
| 278 | }); |
| 279 | |
| 280 | it('Plugin is registered first: Buttons should be added to panel and cleared', function () { |
| 281 | |
| 282 | // Register plugin first |
| 283 | KorAP.Plugin = pluginServerClass.create(); |
| 284 | |
| 285 | KorAP.Plugin.register({ |
| 286 | name : 'ResultPlugin', |
| 287 | embed : [{ |
| 288 | panel : 'result', |
| 289 | title : 'Dosomething', |
| 290 | onClick : { |
| 291 | template : 'test' |
| 292 | } |
| 293 | }] |
| 294 | }); |
| 295 | |
| 296 | expect(KorAP.Plugin.buttonGroup('result').length).toEqual(1); |
| 297 | |
| 298 | var resultPanel = resultPanelClass.create(); |
| 299 | var div = document.createElement('div'); |
| 300 | div.appendChild(resultPanel.element()); |
| 301 | KorAP.Panel = KorAP.Panel || {}; |
| 302 | KorAP.Panel['result'] = resultPanel; |
| 303 | expect(KorAP.Plugin.buttonGroup('result').length).toEqual(0); |
| 304 | expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething'); |
| 305 | |
| 306 | // Clean up |
| 307 | KorAP.Panel['result'] = undefined; |
| 308 | KorAP.Plugin.destroy(); |
| 309 | KorAP.Plugin = undefined; |
| 310 | }); |
| 311 | }); |
| 312 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 313 | }); |