blob: 570f3f4f4ab4d2f548042f90335d78d4f006f3cd [file] [log] [blame]
Akron22598cd2019-12-09 14:59:03 +01001define(['plugin/server','plugin/widget','panel', 'panel/query', 'panel/result', 'plugin/service'], function (pluginServerClass, widgetClass, panelClass, queryPanelClass, resultPanelClass, serviceClass) {
Akronb43c8c62018-07-04 18:27:28 +02002
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();
Akron4a703872018-07-26 10:59:41 +020013 var panel = panelClass.create();
14 var id = manager.addWidget(panel, 'Example 1', 'about:blank');
Akronb43c8c62018-07-04 18:27:28 +020015 expect(id).toMatch(/^id-/);
Akron4a703872018-07-26 10:59:41 +020016
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
Akronb43c8c62018-07-04 18:27:28 +020027 manager.destroy();
28 });
Akron10a47962018-07-12 21:17:10 +020029
Akron22598cd2019-12-09 14:59:03 +010030 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
Akron4a703872018-07-26 10:59:41 +020053 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
Akron22598cd2019-12-09 14:59:03 +010065 var widget = manager.service(id);
66 expect(widget.isWidget).toBeTruthy();
Akron4a703872018-07-26 10:59:41 +020067 widget.close();
68
69 expect(panelE.getElementsByClassName('view').length).toEqual(0);
Akron4a703872018-07-26 10:59:41 +020070 manager.destroy();
71 });
72
73
74 it('should fail on invalid registrations', function () {
Akron10a47962018-07-12 21:17:10 +020075 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"));
Akron2d0d96d2019-11-18 19:49:50 +010096 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();
Akron10a47962018-07-12 21:17:10 +0200133 });
hebasta043e96f2019-11-28 12:33:00 +0100134
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 });
153
Akronb43c8c62018-07-04 18:27:28 +0200154 });
Akron2d0d96d2019-11-18 19:49:50 +0100155
Akronb43c8c62018-07-04 18:27:28 +0200156 describe('KorAP.Plugin.Widget', function () {
157 it('should be initializable', function () {
Akron22598cd2019-12-09 14:59:03 +0100158 expect(function () { widgetClass.create() }).toThrow(new Error("Service not well defined"));
Akron56a11af2018-07-27 18:28:45 +0200159
160 widget = widgetClass.create("Test", "https://example", 56);
Akronb43c8c62018-07-04 18:27:28 +0200161 expect(widget).toBeTruthy();
Akron56a11af2018-07-27 18:28:45 +0200162 expect(widget.id).toEqual(56);
163 expect(widget.name).toEqual("Test");
164 expect(widget.src).toEqual("https://example");
165 });
166
167 it('should create a view element', function () {
168 var widget = widgetClass.create("Test", "https://example", 56);
169 var we = widget.element();
170
171 expect(we.tagName).toEqual("DIV");
172 expect(we.classList.contains('view')).toBeTruthy();
173 expect(we.classList.contains('widget')).toBeTruthy();
174
175 var iframe = we.firstChild;
176 expect(iframe.tagName).toEqual("IFRAME");
hebasta78913242020-03-30 13:39:20 +0200177 expect(iframe.getAttribute("sandbox")).toEqual("allow-scripts allow-forms");
Akron56a11af2018-07-27 18:28:45 +0200178 expect(iframe.getAttribute("src")).toEqual("https://example");
179 expect(iframe.getAttribute("name")).toEqual("56");
180
181 var btn = we.lastChild;
182 expect(btn.classList.contains("button-group")).toBeTruthy();
183 expect(btn.classList.contains("button-view")).toBeTruthy();
184 expect(btn.classList.contains("widget")).toBeTruthy();
185
186 expect(btn.firstChild.tagName).toEqual("SPAN");
187 expect(btn.firstChild.classList.contains("button-icon")).toBeTruthy();
188 expect(btn.firstChild.classList.contains("close")).toBeTruthy();
189 expect(btn.firstChild.firstChild.tagName).toEqual("SPAN");
190
191 expect(btn.lastChild.tagName).toEqual("SPAN");
192 expect(btn.lastChild.classList.contains("button-icon")).toBeTruthy();
193 expect(btn.lastChild.classList.contains("plugin")).toBeTruthy();
194 expect(btn.lastChild.firstChild.tagName).toEqual("SPAN");
195 expect(btn.lastChild.textContent).toEqual("Test");
196 })
197
198 it('should be resizable', function () {
199 var widget = widgetClass.create("Test", "https://example", 56);
200 var iframe = widget.show();
201 expect(iframe.style.height).toEqual('0px');
202 widget.resize({ height : 9 });
203 expect(iframe.style.height).toEqual('9px');
Akronb43c8c62018-07-04 18:27:28 +0200204 });
205 });
Akron2d0d96d2019-11-18 19:49:50 +0100206
Akron22598cd2019-12-09 14:59:03 +0100207 describe('KorAP.Plugin.Service', function () {
208 it('should be initializable', function () {
209 expect(function () { serviceClass.create() }).toThrow(new Error("Service not well defined"));
210
211 let service = serviceClass.create("Test", "https://example", 56);
212 expect(service).toBeTruthy();
213 expect(service.id).toEqual(56);
214 expect(service.name).toEqual("Test");
215 expect(service.src).toEqual("https://example");
216 });
217
218 it('should be loadable', function () {
219 let service = serviceClass.create("Test", "https://example", 56);
220 expect(service).toBeTruthy();
221
222 let i = service.load();
223 expect(i.tagName).toEqual("IFRAME");
224 expect(i.getAttribute("allowTransparency")).toEqual("true");
225 expect(i.getAttribute("frameborder")).toEqual(''+0);
226 expect(i.getAttribute("name")).toEqual(''+service.id);
227 expect(i.getAttribute("src")).toEqual(service.src);
228 });
229 });
230
Akron2d0d96d2019-11-18 19:49:50 +0100231 describe('KorAP.Plugin.QueryPanel', function () {
232 it('should establish a query plugin', function () {
233 var queryPanel = queryPanelClass.create();
234
235 var div = document.createElement('div');
236
237 div.appendChild(queryPanel.element());
238 KorAP.Panel = KorAP.Panel || {};
239 KorAP.Panel['query'] = queryPanel;
240
241 // Register plugin afterwards
242 var manager = pluginServerClass.create();
243
244 manager.register({
245 name : 'Check',
246 embed : [{
247 panel : 'query',
248 title : 'Translate',
249 onClick : {
250 template : 'test'
251 }
252 }]
253 });
254
255 expect(manager.buttonGroup('query').length).toEqual(0);
256
257 // Clean up
258 KorAP.Panel['query'] = undefined;
259 manager.destroy();
260 });
hebastaf6adf8d2019-11-26 14:04:10 +0100261
262 it('Plugin buttons should be cleared after adding to panel', function () {
263
264 // Register plugin first
265 KorAP.Plugin = pluginServerClass.create();
266
267 KorAP.Plugin.register({
268 name : 'Check',
269 embed : [{
270 panel : 'query',
271 title : 'Translate',
272 onClick : {
273 template : 'test'
274 }
275 }]
276 });
277
278
279 var queryPanel = queryPanelClass.create();
280 var div = document.createElement('div');
281
282 div.appendChild(queryPanel.element());
283 KorAP.Panel = KorAP.Panel || {};
284 KorAP.Panel['query'] = queryPanel;
285 expect(KorAP.Plugin.buttonGroup('query').length).toEqual(0);
286
287 // Clean up
288 KorAP.Panel['query'] = undefined;
289 KorAP.Plugin.destroy();
290 KorAP.Plugin = undefined;
291 });
Akron2d0d96d2019-11-18 19:49:50 +0100292 });
hebasta043e96f2019-11-28 12:33:00 +0100293
294 describe('KorAP.Plugin.ResultPanel', function () {
295
296 it('Plugin is registered second: buttons should be added to panel', function () {
297
298 var resultPanel = resultPanelClass.create();
299 resultPanel.addAlignAction();
300 var div = document.createElement('div');
301
302 div.appendChild(resultPanel.element());
303 KorAP.Panel = KorAP.Panel || {};
304 KorAP.Panel['result'] = resultPanel;
305
306 // Register plugin afterwards
307 var manager = pluginServerClass.create();
308
309 manager.register({
310 name : 'ResultPlugin',
311 embed : [{
312 panel : 'result',
313 title : 'Dosomething',
314 onClick : {
315 template : 'test'
316 }
317 }]
318 });
319
320 expect(manager.buttonGroup('result').length).toEqual(0);
321 expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething');
322
323 // Clean up
324 KorAP.Panel['result'] = undefined;
325 manager.destroy();
326 });
327
328 it('Plugin is registered first: Buttons should be added to panel and cleared', function () {
329
330 // Register plugin first
331 KorAP.Plugin = pluginServerClass.create();
332
333 KorAP.Plugin.register({
334 name : 'ResultPlugin',
335 embed : [{
336 panel : 'result',
337 title : 'Dosomething',
338 onClick : {
339 template : 'test'
340 }
341 }]
342 });
343
344 expect(KorAP.Plugin.buttonGroup('result').length).toEqual(1);
345
346 var resultPanel = resultPanelClass.create();
347 var div = document.createElement('div');
348 div.appendChild(resultPanel.element());
349 KorAP.Panel = KorAP.Panel || {};
350 KorAP.Panel['result'] = resultPanel;
351 expect(KorAP.Plugin.buttonGroup('result').length).toEqual(0);
352 expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething');
353
354 // Clean up
355 KorAP.Panel['result'] = undefined;
356 KorAP.Plugin.destroy();
357 KorAP.Plugin = undefined;
358 });
359 });
360
Akronb43c8c62018-07-04 18:27:28 +0200361});