aboutsummaryrefslogtreecommitdiffstats
path: root/tests/tests/test.js
blob: 3c76789ca414021fcb3b6615112e0696fd38a9ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
const body = document.getElementsByTagName('body')[0];

suite('Dashboard', function() {
    suite('Global Functions', function() {
        let formContainer;
        let cancelButton;
        let backButton;
        let forwardButton;
        let viewMessage;
        let paginationControl;
        let topPagination;
        let viewTitle;
        let viewDesc;
        let response;

        setup(function() {
            body.innerHTML = '';
            response = {
                redirect: false,
                meta: {
                    workflow_count: 0,
                    active: 0,
                    steps: [{
                        title: 'title',
                        valid: '299',
                        message: 'testMessage',
                        enabled: false
                    }]
                },
                content: 'exampleContent'
            };
            // Override the functions for testing
            $.post = function(url, data, cb, type) {
                cb(response);
                return $.Deferred().resolve();
            }
            // Create all elements needed for the global functions
            formContainer = document.createElement('div');
            formContainer.id = 'formContainer';
            cancelButton = document.createElement('button');
            cancelButton.id = 'cancel_btn';
            backButton = document.createElement('button');
            backButton.id = 'gob';
            forwardButton = document.createElement('button');
            forwardButton.id = 'gof';
            viewMessage = document.createElement('p');
            viewMessage.id = 'view_message';
            paginationControl = document.createElement('li');
            paginationControl.classList.add('page-control');
            topPagination = document.createElement('ul');
            topPagination.id = 'topPagination';
            topPagination.appendChild(paginationControl);
            viewTitle = document.createElement('span');
            viewTitle.id = 'view_title';
            viewDesc = document.createElement('span');
            viewDesc.id = 'view_desc';

            // Update the elements on the page
            body.appendChild(formContainer);
            body.appendChild(backButton);
            body.appendChild(forwardButton);
            body.appendChild(viewMessage);
            body.appendChild(cancelButton);
            body.appendChild(topPagination);
            body.appendChild(viewTitle);
            body.appendChild(viewDesc);
            update_page(response);
        });

        // Testing all of these because they are all required to run
        // when running update_page(), and removing parts of it will break
        // document.body is different outside and inside the test() callback.
        test('update_page', function() {
            assert.equal(formContainer.innerHTML, 'exampleContent');
        });
        test('draw_breadcrumbs', function() {
            assert.isAbove(topPagination.childElementCount, 1);
        })
        test('create_step', function() {
            assert.equal(topPagination.firstChild.innerText, 'title');
        });
        test('update_exit_button', function() {
            assert.equal(cancelButton.innerText, 'Return to Parent');
        });
        test('update_side_buttons', function() {
            assert(forwardButton.disabled);
            assert(backButton.disabled);
        });
        test('update_description', function() {
            update_description('title', 'description');

            assert.equal(viewTitle.innerText, 'title');
            assert.equal(viewDesc.innerText, 'description');
        });
        test('update_message', function() {
            update_message('message', 999);
            assert.equal(viewMessage.innerText, 'message');
            assert(viewMessage.classList.contains('step_message'));
            assert(viewMessage.classList.contains('message_999'));
        });
        test('submitStepForm', function() {
            // Empty the container so that the function changes it
            formContainer.innerHTML = '';
            submitStepForm();
            assert.equal(formContainer.innerHTML, 'exampleContent');
        });
        test('run_form_callbacks', function() {
            form_submission_callbacks.push(function() {
                let testObject = document.createElement('span');
                testObject.id = 'testObject';
                body.appendChild(testObject);
            });
            run_form_callbacks();
            assert.isNotNull(document.getElementById('testObject'));
        });
    });

    suite('MultipleSelectFilterWidget', function() {
        let widget;
        let initialData;
        let lab1;
        let lab2;
        let host1;
        let host2;
        let graph_neighbors;
        let filter_items;

        setup(function() {
            body.innerHTML = '';
            // Create elements that represent these choices
            lab1 = document.createElement('div');
            lab1.id = 'lab_1';
            lab2 = document.createElement('div');
            lab2.id = 'lab_2';
            host1 = document.createElement('div');
            host1.id = 'host_1';
            host2 = document.createElement('div');
            host2.id = 'host_2';

            // Append elements to the page
            body.append(lab1);
            body.append(lab2);
            body.append(host1);
            body.append(host2);
            initialData = {
                host: {
                    host_1: {
                        selected: true
                    }
                },
                lab: {
                    lab_1: {
                        selected: true
                    }
                }
            };
            graph_neighbors = {
                host_1: ['lab_1'],
                host_2: ['lab_1'],
                lab_1: ['host_1', 'host_2']
            };
            filter_items = {
                host_1: {
                    name: 'host 1',
                    class: 'host',
                    description: 'first host',
                    id: 'host_1',
                    form: {
                        type: 'text',
                        name: 'textInput'
                    },
                    selectable: true,
                    multiple: false,
                    selected: false
                },
                host_2: {
                    name: 'host 2',
                    class: 'host',
                    description: 'second host',
                    id: 'host_2',
                    form: {
                        type: 'text',
                        name: 'textInput'
                    },
                    selectable: true,
                    multiple: false,
                    selected: false
                },
                lab_1: {
                    name: 'lab 1',
                    class: 'lab',
                    description: 'first lab',
                    id: 'lab_1',
                    form: {
                        type: 'text',
                        name: 'textInput'
                    },
                    selectable: true,
                    multiple: false,
                    selected: false
                },
                lab_2: {
                    name: 'lab 2',
                    class: 'lab',
                    description: 'second lab',
                    id: 'lab_2',
                    form: {
                        type: 'text',
                        name: 'textInput'
                    },
                    selectable: true,
                    multiple: false,
                    selected: false
                }
            };
            widget = new MultipleSelectFilterWidget(graph_neighbors, filter_items, {});
        });

        test('make_selection', function() {
            widget.make_selection(initialData);
            assert.isTrue(lab1.classList.contains('selected_node'));
            assert.isTrue(host1.classList.contains('selected_node'));
        });

        test('multiple selected items', function() {
            widget.processClick('lab_1');
            widget.processClick('host_1');
            assert.isTrue(lab1.classList.contains('selected_node'));
            assert.isTrue(host1.classList.contains('selected_node'));

            // Make sure clicking multiple hosts/labs doesn't work
            widget.processClick('host_2');
            widget.processClick('lab_2');
            assert.isFalse(host2.classList.contains('selected_node'));
            assert.isFalse(lab2.classList.contains('selected_node'));

            // Unselect host1 then try host2 again
            widget.processClick('host_1');
            widget.processClick('host_2');
            assert.isFalse(host1.classList.contains('selected_node'));
            assert.isTrue(host2.classList.contains('selected_node'));
        });
    });

    suite('NetworkStep', function() {
        let hosts;
        let graphContainer;
        let overviewContainer;
        let toolbarContainer;
        let networkList;
        let networkStep;

        setup(function() {
            body.innerHTML = '';
            hosts = [
                {
                    id: 'host1',
                    interfaces: [
                        {
                            name: 'interface1',
                            description: 'description1'
                        }
                    ],
                    value: {
                        description: 'example host1',
                        name: 'host1'
                    }
                }
            ];
            graphContainer = document.createElement('div');
            overviewContainer = document.createElement('div');
            toolbarContainer = document.createElement('div');
            networkList = document.createElement('div');
            networkList.id = 'network_list';

            body.appendChild(graphContainer);
            body.appendChild(overviewContainer);
            body.appendChild(toolbarContainer);
            body.appendChild(networkList);

            networkStep = new NetworkStep(true, '', hosts, [], [], graphContainer, overviewContainer, toolbarContainer);
        });

        test('public network creation', function() {
            // Network list's first child should be the 'public' network div,
            // Public div has two children: the colored circle and the label
            // It does not have a delete button.
            assert.equal(networkList.childNodes[0].childNodes.length, 2);
            assert.equal(networkList.childNodes[0].childNodes[1].innerText, 'public');
        });

        test('duplicate network name', function() {
            networkStep.newNetworkWindow();
            let netInput = document.querySelector('input[name="net_name"]');
            netInput.value = 'public'
            document.querySelector('.mxWindowPane div button:first-of-type').click();
            let windowErrors = document.getElementById('current_window_errors');
            assert.equal(windowErrors.innerText, 'All network names must be unique');
        });


        test('new network creation', function() {
            networkStep.newNetworkWindow();
            let netInput = document.querySelector('input[name="net_name"]');
            netInput.value = 'testNetwork';
            document.querySelector('.mxWindowPane div button:first-of-type').click();
            assert.equal(networkList.childNodes[1].childNodes[1].innerText, 'testNetwork');
        });
    });

    suite('SearchableSelectMultipleWidget', function() {
        let formatVars = {
            placeholder: 'Example placeholder',
            results_scrollable: true,
            selectable_limit: -1,
            show_from_noentry: false,
            show_x_results: 5
        };
        let fieldDataset = {
            '1': {
                expanded_name: 'Test User',
                id: 1,
                small_name: 'small Test',
                string: 'email@test.com'
            }
        };
        let widget;
        let input;
        let dropdown;
        let scrollRestrictor;

        setup(function() {
            body.innerHTML = '';
            input = document.createElement('input');
            dropdown = document.createElement('div');
            scrollRestrictor = document.createElement('div');
            scrollRestrictor.id = 'scroll_restrictor';
            dropdown.id = 'drop_results';
            input.type = 'text';
            input.value = 'Test ';

            body.appendChild(scrollRestrictor);
            body.appendChild(dropdown);
            body.appendChild(input);

            widget = new SearchableSelectMultipleWidget(formatVars, fieldDataset, []);
        });

        test('trie population', function() {
            assert.property(widget.expanded_name_trie, 'T');
            assert.property(widget.small_name_trie, 's');
            assert.property(widget.string_trie, 'e');
        });

        test('dropdown population with search', function() {
            widget.search('Test ');
            assert.equal(dropdown.childNodes[0].title, 'Test User (small Test, email@test.com)');
            // Search some random text that shouldn't resolve to any user
            widget.search('Empty');
            assert.equal(dropdown.childElementCount, 0);
        });
    });
});