93830ebae53de2f78c8ed2fb9243e4927c81b097
[laas.git] / tests / tests / test.js
1 const body = document.getElementsByTagName('body')[0];
2
3 suite('Dashboard', function() {
4     suite('Global Functions', function() {
5         let formContainer;
6         let cancelButton;
7         let backButton;
8         let forwardButton;
9         let viewMessage;
10         let paginationControl;
11         let topPagination;
12         let viewTitle;
13         let viewDesc;
14         let response;
15
16         setup(function() {
17             body.innerHTML = '';
18             response = {
19                 redirect: false,
20                 meta: {
21                     workflow_count: 0,
22                     active: 0,
23                     steps: [{
24                         title: 'title',
25                         valid: '299',
26                         message: 'testMessage',
27                         enabled: false
28                     }]
29                 },
30                 content: 'exampleContent'
31             };
32             // Override the functions for testing
33             $.post = function(url, data, cb, type) {
34                 cb(response);
35                 return $.Deferred().resolve();
36             }
37             // Create all elements needed for the global functions
38             formContainer = document.createElement('div');
39             formContainer.id = 'formContainer';
40             cancelButton = document.createElement('button');
41             cancelButton.id = 'cancel_btn';
42             backButton = document.createElement('button');
43             backButton.id = 'gob';
44             forwardButton = document.createElement('button');
45             forwardButton.id = 'gof';
46             viewMessage = document.createElement('p');
47             viewMessage.id = 'view_message';
48             paginationControl = document.createElement('li');
49             paginationControl.classList.add('page-control');
50             topPagination = document.createElement('ul');
51             topPagination.id = 'topPagination';
52             topPagination.appendChild(paginationControl);
53             viewTitle = document.createElement('span');
54             viewTitle.id = 'view_title';
55             viewDesc = document.createElement('span');
56             viewDesc.id = 'view_desc';
57
58             // Update the elements on the page
59             body.appendChild(formContainer);
60             body.appendChild(backButton);
61             body.appendChild(forwardButton);
62             body.appendChild(viewMessage);
63             body.appendChild(cancelButton);
64             body.appendChild(topPagination);
65             body.appendChild(viewTitle);
66             body.appendChild(viewDesc);
67             update_page(response);
68         });
69
70         // Testing all of these because they are all required to run
71         // when running update_page(), and removing parts of it will break
72         // document.body is different outside and inside the test() callback.
73         test('update_page', function() {
74             assert.equal(formContainer.innerHTML, 'exampleContent');
75         });
76         test('draw_breadcrumbs', function() {
77             assert.isAbove(topPagination.childElementCount, 1);
78         })
79         test('create_step', function() {
80             assert.equal(topPagination.firstChild.innerText, 'title');
81         });
82         test('update_exit_button', function() {
83             assert.equal(cancelButton.innerText, 'Return to Parent');
84         });
85         test('update_side_buttons', function() {
86             assert(forwardButton.disabled);
87             assert(backButton.disabled);
88         });
89         test('update_description', function() {
90             update_description('title', 'description');
91
92             assert.equal(viewTitle.innerText, 'title');
93             assert.equal(viewDesc.innerText, 'description');
94         });
95         test('update_message', function() {
96             update_message('message', 999);
97             assert.equal(viewMessage.innerText, 'message');
98             assert(viewMessage.classList.contains('step_message'));
99             assert(viewMessage.classList.contains('message_999'));
100         });
101         test('submitStepForm', function() {
102             // Empty the container so that the function changes it
103             formContainer.innerHTML = '';
104             submitStepForm();
105             assert.equal(formContainer.innerHTML, 'exampleContent');
106         });
107         test('run_form_callbacks', function() {
108             form_submission_callbacks.push(function() {
109                 let testObject = document.createElement('span');
110                 testObject.id = 'testObject';
111                 body.appendChild(testObject);
112             });
113             run_form_callbacks();
114             assert.isNotNull(document.getElementById('testObject'));
115         });
116     });
117
118     suite('MultipleSelectFilterWidget', function() {
119         let widget;
120         let initialData;
121         let lab1;
122         let lab2;
123         let host1;
124         let host2;
125         let graph_neighbors;
126         let filter_items;
127
128         setup(function() {
129             body.innerHTML = '';
130             // Create elements that represent these choices
131             lab1 = document.createElement('div');
132             lab1.id = 'lab_1';
133             lab2 = document.createElement('div');
134             lab2.id = 'lab_2';
135             host1 = document.createElement('div');
136             host1.id = 'host_1';
137             host2 = document.createElement('div');
138             host2.id = 'host_2';
139
140             // Append elements to the page
141             body.append(lab1);
142             body.append(lab2);
143             body.append(host1);
144             body.append(host2);
145             initialData = {
146                 host: {
147                     host_1: {
148                         selected: true
149                     }
150                 },
151                 lab: {
152                     lab_1: {
153                         selected: true
154                     }
155                 }
156             };
157             graph_neighbors = {
158                 host_1: ['lab_1'],
159                 host_2: ['lab_1'],
160                 lab_1: ['host_1', 'host_2']
161             };
162             filter_items = {
163                 host_1: {
164                     name: 'host 1',
165                     class: 'host',
166                     description: 'first host',
167                     id: 'host_1',
168                     form: {
169                         type: 'text',
170                         name: 'textInput'
171                     },
172                     selectable: true,
173                     multiple: false,
174                     selected: false
175                 },
176                 host_2: {
177                     name: 'host 2',
178                     class: 'host',
179                     description: 'second host',
180                     id: 'host_2',
181                     form: {
182                         type: 'text',
183                         name: 'textInput'
184                     },
185                     selectable: true,
186                     multiple: false,
187                     selected: false
188                 },
189                 lab_1: {
190                     name: 'lab 1',
191                     class: 'lab',
192                     description: 'first lab',
193                     id: 'lab_1',
194                     form: {
195                         type: 'text',
196                         name: 'textInput'
197                     },
198                     selectable: true,
199                     multiple: false,
200                     selected: false
201                 },
202                 lab_2: {
203                     name: 'lab 2',
204                     class: 'lab',
205                     description: 'second lab',
206                     id: 'lab_2',
207                     form: {
208                         type: 'text',
209                         name: 'textInput'
210                     },
211                     selectable: true,
212                     multiple: false,
213                     selected: false
214                 }
215             };
216             widget = new MultipleSelectFilterWidget(graph_neighbors, filter_items, {});
217         });
218
219         test('make_selection', function() {
220             widget.make_selection(initialData);
221             assert.isTrue(lab1.classList.contains('selected_node'));
222             assert.isTrue(host1.classList.contains('selected_node'));
223         });
224
225         test('multiple selected items', function() {
226             widget.processClick('lab_1');
227             widget.processClick('host_1');
228             assert.isTrue(lab1.classList.contains('selected_node'));
229             assert.isTrue(host1.classList.contains('selected_node'));
230
231             // Make sure clicking multiple hosts/labs doesn't work
232             widget.processClick('host_2');
233             widget.processClick('lab_2');
234             assert.isFalse(host2.classList.contains('selected_node'));
235             assert.isFalse(lab2.classList.contains('selected_node'));
236
237             // Unselect host1 then try host2 again
238             widget.processClick('host_1');
239             widget.processClick('host_2');
240             assert.isFalse(host1.classList.contains('selected_node'));
241             assert.isTrue(host2.classList.contains('selected_node'));
242         });
243     });
244
245     suite('NetworkStep', function() {
246         let hosts;
247         let graphContainer;
248         let overviewContainer;
249         let toolbarContainer;
250         let networkList;
251         let networkStep;
252
253         setup(function() {
254             body.innerHTML = '';
255             hosts = [
256                 {
257                     id: 'host1',
258                     interfaces: [
259                         {
260                             name: 'interface1',
261                             description: 'description1'
262                         }
263                     ],
264                     value: {
265                         description: 'example host1',
266                         name: 'host1'
267                     }
268                 }
269             ];
270             graphContainer = document.createElement('div');
271             overviewContainer = document.createElement('div');
272             toolbarContainer = document.createElement('div');
273             networkList = document.createElement('div');
274             networkList.id = 'network_list';
275
276             body.appendChild(graphContainer);
277             body.appendChild(overviewContainer);
278             body.appendChild(toolbarContainer);
279             body.appendChild(networkList);
280
281             networkStep = new NetworkStep(true, '', hosts, [], [], graphContainer, overviewContainer, toolbarContainer);
282         });
283
284         test('public network creation', function() {
285             // Network list's first child should be the 'public' network div,
286             // Public div has two children: the colored circle and the label
287             // It does not have a delete button.
288             assert.equal(networkList.childNodes[0].childNodes.length, 2);
289             assert.equal(networkList.childNodes[0].childNodes[1].innerText, 'public');
290         });
291
292         test('duplicate network name', function() {
293             networkStep.newNetworkWindow();
294             let netInput = document.querySelector('input[name="net_name"]');
295             netInput.value = 'public'
296             document.querySelector('.mxWindowPane div button:first-of-type').click();
297             let windowErrors = document.getElementById('current_window_errors');
298             assert.equal(windowErrors.innerText, 'All network names must be unique');
299         });
300
301
302         test('new network creation', function() {
303             networkStep.newNetworkWindow();
304             let netInput = document.querySelector('input[name="net_name"]');
305             netInput.value = 'testNetwork';
306             document.querySelector('.mxWindowPane div button:first-of-type').click();
307             assert.equal(networkList.childNodes[1].childNodes[1].innerText, 'testNetwork');
308         });
309     });
310
311     suite('SearchableSelectMultipleWidget', function() {
312         let formatVars = {
313             placeholder: 'Example placeholder',
314             results_scrollable: true,
315             selectable_limit: -1,
316             show_from_noentry: false,
317             show_x_results: 5
318         };
319         let fieldDataset = {
320             '1': {
321                 expanded_name: 'Test User',
322                 id: 1,
323                 small_name: 'small Test',
324                 string: 'email@test.com'
325             }
326         };
327         let widget;
328         let input;
329         let dropdown;
330         let scrollRestrictor;
331         let userField;
332         let addedNumber;
333         let selector;
334         let addedList;
335
336         setup(function() {
337             body.innerHTML = '';
338             input = document.createElement('input');
339             dropdown = document.createElement('div');
340             scrollRestrictor = document.createElement('div');
341             addedList = document.createElement('div');
342             addedList.id = 'added_list';
343             selector = document.createElement('div');
344             selector.id = 'selector';
345             addedNumber = document.createElement('div');
346             addedNumber.id = 'added_number';
347             userField = document.createElement('div');
348             userField.id = 'user_field';
349             scrollRestrictor.id = 'scroll_restrictor';
350             dropdown.id = 'drop_results';
351             input.type = 'text';
352             input.value = 'Test ';
353
354             body.appendChild(scrollRestrictor);
355             body.appendChild(dropdown);
356             body.appendChild(input);
357             body.appendChild(userField);
358             body.appendChild(addedNumber);
359             body.appendChild(selector);
360             body.appendChild(addedList);
361
362             widget = new SearchableSelectMultipleWidget(formatVars, fieldDataset, []);
363         });
364
365         test('disable', function() {
366             widget.disable();
367             assert.equal(userField.disabled, 'True');
368             assert.equal(dropdown.style.display, 'none');
369         });
370
371         test('search_field_init', function() {
372             assert.isEmpty(addedNumber.innerText);
373             assert.isUndefined(selector.value);
374             assert.isEmpty(addedList.childNodes);
375             widget.initial = [1];
376             widget.search_field_init();
377             assert.equal(addedNumber.innerText, '1');
378             assert.equal(selector.value, '[1]');
379             assert.equal(addedList.childNodes.length, 1);
380         });
381
382         test('build_all_tries', function() {
383             let dict = {
384                 '1': {
385                     expanded_name: 'exnamea',
386                     small_name: 'smnamea',
387                     string: 'stra',
388                     id: 1
389                 },
390                 '2': {
391                     expanded_name: 'exnameb',
392                     small_name: 'smnameb',
393                     string: 'strb',
394                     id: 2
395                 }
396             };
397             widget.build_all_tries(dict);
398             // since each subtree has keys of 1 character each, look for a nested
399             // property using '.' after each letter for nesting
400             assert.nestedProperty(widget.expanded_name_trie, 'e.x.n.a.m.e.a');
401             assert.nestedProperty(widget.small_name_trie, 's.m.n.a.m.e.a');
402             assert.nestedProperty(widget.string_trie, 's.t.r.a');
403             assert.nestedProperty(widget.expanded_name_trie, 'e.x.n.a.m.e.b');
404             assert.nestedProperty(widget.small_name_trie, 's.m.n.a.m.e.b');
405             assert.nestedProperty(widget.string_trie, 's.t.r.b');
406         });
407
408         test('add_item', function() {
409             let item = {
410                 id: 1,
411                 expanded_name: 'item',
412                 small_name: 'item',
413                 string: 'item'
414             };
415             widget.add_item(item);
416             assert.nestedProperty(widget.expanded_name_trie, 'i.t.e.m');
417             assert.nestedProperty(widget.small_name_trie, 'i.t.e.m');
418             assert.nestedProperty(widget.string_trie, 'i.t.e.m');
419         });
420
421         test('add_to_tree', function() {
422             widget.add_to_tree('addtotree', 0, widget.string_trie);
423             assert.nestedProperty(widget.string_trie, 'a.d.d.t.o.t.r.e.e');
424         });
425
426         test('search', function() {
427             widget.search('Test ');
428             assert.equal(dropdown.childNodes[0].title, 'Test User (small Test, email@test.com)');
429             // Search some random text that shouldn't resolve to any user
430             widget.search('Empty');
431             assert.equal(dropdown.childElementCount, 0);
432         });
433
434         test('getSubtree', function() {
435             // 'email@test.com': search for 'email', next letter should be '@'
436             assert.property(widget.getSubtree('email', widget.string_trie), '@');
437         });
438
439         test('serialize', function() {
440             // object in string_trie has id 1, check if the array contains 1
441             assert.include(widget.serialize(widget.string_trie), 1);
442         });
443
444         test('collate', function() {
445             let trees = [widget.string_trie, widget.small_name_trie, widget.expanded_name_trie];
446             let result = widget.collate(trees);
447             assert.lengthOf(result, 2);
448         });
449
450         test('generate_element_text', function() {
451             let obj = {
452                 expanded_name: '1',
453                 small_name: '2',
454                 string: '3'
455             }
456             assert.equal(widget.generate_element_text(obj), '1 (2, 3)');
457         });
458
459         test('dropdown', function() {
460             // use undefined since dropdown doesn't use values, only keys
461             widget.dropdown({'1': undefined});
462             assert.lengthOf(dropdown.childNodes, 1);
463         });
464
465         test('select_item', function() {
466             widget.select_item('1');
467             assert.lengthOf(addedList.childNodes, 1);
468         });
469
470         test('remove_item', function() {
471             widget.select_item('1');
472             assert.isNotEmpty(addedList.childNodes);
473             widget.remove_item('1');
474             assert.isEmpty(addedList.childNodes);
475         });
476
477         test('update_selected_list', function() {
478             widget.added_items.add('1');
479             assert.lengthOf(addedList.childNodes, 0);
480             widget.update_selected_list();
481             assert.lengthOf(addedList.childNodes, 1);
482         });
483     });
484 });