Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame^] | 1 | define(['plugin/server','plugin/widget','panel', 'panel/query'], function (pluginServerClass, widgetClass, panelClass, queryPanelClass) { |
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 | }); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 110 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame^] | 111 | |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 112 | describe('KorAP.Plugin.Widget', function () { |
| 113 | it('should be initializable', function () { |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 114 | expect(function () { widgetClass.create() }).toThrow(new Error("Widget not well defined")); |
| 115 | |
| 116 | widget = widgetClass.create("Test", "https://example", 56); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 117 | expect(widget).toBeTruthy(); |
Akron | 56a11af | 2018-07-27 18:28:45 +0200 | [diff] [blame] | 118 | expect(widget.id).toEqual(56); |
| 119 | expect(widget.name).toEqual("Test"); |
| 120 | expect(widget.src).toEqual("https://example"); |
| 121 | }); |
| 122 | |
| 123 | it('should create a view element', function () { |
| 124 | var widget = widgetClass.create("Test", "https://example", 56); |
| 125 | var we = widget.element(); |
| 126 | |
| 127 | expect(we.tagName).toEqual("DIV"); |
| 128 | expect(we.classList.contains('view')).toBeTruthy(); |
| 129 | expect(we.classList.contains('widget')).toBeTruthy(); |
| 130 | |
| 131 | var iframe = we.firstChild; |
| 132 | expect(iframe.tagName).toEqual("IFRAME"); |
| 133 | expect(iframe.getAttribute("sandbox")).toEqual("allow-scripts"); |
| 134 | expect(iframe.getAttribute("src")).toEqual("https://example"); |
| 135 | expect(iframe.getAttribute("name")).toEqual("56"); |
| 136 | |
| 137 | var btn = we.lastChild; |
| 138 | expect(btn.classList.contains("button-group")).toBeTruthy(); |
| 139 | expect(btn.classList.contains("button-view")).toBeTruthy(); |
| 140 | expect(btn.classList.contains("widget")).toBeTruthy(); |
| 141 | |
| 142 | expect(btn.firstChild.tagName).toEqual("SPAN"); |
| 143 | expect(btn.firstChild.classList.contains("button-icon")).toBeTruthy(); |
| 144 | expect(btn.firstChild.classList.contains("close")).toBeTruthy(); |
| 145 | expect(btn.firstChild.firstChild.tagName).toEqual("SPAN"); |
| 146 | |
| 147 | expect(btn.lastChild.tagName).toEqual("SPAN"); |
| 148 | expect(btn.lastChild.classList.contains("button-icon")).toBeTruthy(); |
| 149 | expect(btn.lastChild.classList.contains("plugin")).toBeTruthy(); |
| 150 | expect(btn.lastChild.firstChild.tagName).toEqual("SPAN"); |
| 151 | expect(btn.lastChild.textContent).toEqual("Test"); |
| 152 | }) |
| 153 | |
| 154 | it('should be resizable', function () { |
| 155 | var widget = widgetClass.create("Test", "https://example", 56); |
| 156 | var iframe = widget.show(); |
| 157 | expect(iframe.style.height).toEqual('0px'); |
| 158 | widget.resize({ height : 9 }); |
| 159 | expect(iframe.style.height).toEqual('9px'); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 160 | }); |
| 161 | }); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame^] | 162 | |
| 163 | describe('KorAP.Plugin.QueryPanel', function () { |
| 164 | it('should establish a query plugin', function () { |
| 165 | var queryPanel = queryPanelClass.create(); |
| 166 | |
| 167 | var div = document.createElement('div'); |
| 168 | |
| 169 | div.appendChild(queryPanel.element()); |
| 170 | KorAP.Panel = KorAP.Panel || {}; |
| 171 | KorAP.Panel['query'] = queryPanel; |
| 172 | |
| 173 | // Register plugin afterwards |
| 174 | var manager = pluginServerClass.create(); |
| 175 | |
| 176 | manager.register({ |
| 177 | name : 'Check', |
| 178 | embed : [{ |
| 179 | panel : 'query', |
| 180 | title : 'Translate', |
| 181 | onClick : { |
| 182 | template : 'test' |
| 183 | } |
| 184 | }] |
| 185 | }); |
| 186 | |
| 187 | expect(manager.buttonGroup('query').length).toEqual(0); |
| 188 | |
| 189 | // Clean up |
| 190 | KorAP.Panel['query'] = undefined; |
| 191 | manager.destroy(); |
| 192 | }); |
| 193 | }); |
Akron | b43c8c6 | 2018-07-04 18:27:28 +0200 | [diff] [blame] | 194 | }); |