1 const body = document.getElementsByTagName('body')[0];
3 suite('Dashboard', function() {
4 suite('Global Functions', function() {
10 let paginationControl;
26 message: 'testMessage',
30 content: 'exampleContent'
32 // Override the functions for testing
33 $.post = function(url, data, cb, type) {
35 return $.Deferred().resolve();
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';
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);
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');
76 test('draw_breadcrumbs', function() {
77 assert.isAbove(topPagination.childElementCount, 1);
79 test('create_step', function() {
80 assert.equal(topPagination.firstChild.innerText, 'title');
82 test('update_exit_button', function() {
83 assert.equal(cancelButton.innerText, 'Return to Parent');
85 test('update_side_buttons', function() {
86 assert(forwardButton.disabled);
87 assert(backButton.disabled);
89 test('update_description', function() {
90 update_description('title', 'description');
92 assert.equal(viewTitle.innerText, 'title');
93 assert.equal(viewDesc.innerText, 'description');
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'));
101 test('submitStepForm', function() {
102 // Empty the container so that the function changes it
103 formContainer.innerHTML = '';
105 assert.equal(formContainer.innerHTML, 'exampleContent');
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);
113 run_form_callbacks();
114 assert.isNotNull(document.getElementById('testObject'));
118 suite('MultipleSelectFilterWidget', function() {
130 // Create elements that represent these choices
131 lab1 = document.createElement('div');
133 lab2 = document.createElement('div');
135 host1 = document.createElement('div');
137 host2 = document.createElement('div');
140 // Append elements to the page
160 lab_1: ['host_1', 'host_2']
166 description: 'first host',
179 description: 'second host',
192 description: 'first lab',
205 description: 'second lab',
216 widget = new MultipleSelectFilterWidget(graph_neighbors, filter_items, {});
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'));
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'));
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'));
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'));
245 suite('NetworkStep', function() {
248 let overviewContainer;
249 let toolbarContainer;
261 description: 'description1'
265 description: 'example host1',
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';
276 body.appendChild(graphContainer);
277 body.appendChild(overviewContainer);
278 body.appendChild(toolbarContainer);
279 body.appendChild(networkList);
281 networkStep = new NetworkStep(true, '', hosts, [], [], graphContainer, overviewContainer, toolbarContainer);
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');
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');
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');
311 suite('SearchableSelectMultipleWidget', function() {
313 placeholder: 'Example placeholder',
314 results_scrollable: true,
315 selectable_limit: -1,
316 show_from_noentry: false,
321 expanded_name: 'Test User',
323 small_name: 'small Test',
324 string: 'email@test.com'
330 let scrollRestrictor;
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';
352 input.value = 'Test ';
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);
362 widget = new SearchableSelectMultipleWidget(formatVars, fieldDataset, []);
365 test('disable', function() {
367 assert.equal(userField.disabled, 'True');
368 assert.equal(dropdown.style.display, 'none');
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);
382 test('build_all_tries', function() {
385 expanded_name: 'exnamea',
386 small_name: 'smnamea',
391 expanded_name: 'exnameb',
392 small_name: 'smnameb',
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');
408 test('add_item', function() {
411 expanded_name: 'item',
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');
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');
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);
434 test('getSubtree', function() {
435 // 'email@test.com': search for 'email', next letter should be '@'
436 assert.property(widget.getSubtree('email', widget.string_trie), '@');
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);
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);
450 test('generate_element_text', function() {
456 assert.equal(widget.generate_element_text(obj), '1 (2, 3)');
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);
465 test('select_item', function() {
466 widget.select_item('1');
467 assert.lengthOf(addedList.childNodes, 1);
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);
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);