blob: 0df614143926cd6c5b69e48ef694d53126741fe8 [file] [log] [blame]
Bri Prebilic Cole093739a2015-01-23 10:22:50 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Cole093739a2015-01-23 10:22:50 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Widget -- Table Service - Unit Tests
19 */
Matteo Scandolo812aa5a2016-04-19 18:12:45 -070020
21// NOTE TableService does not exist! It has been replaced/renamed?
22
23xdescribe('factory: fw/widget/table.js', function () {
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080024 var $log, $compile, $rootScope,
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070025 fs, ts, mast, is,
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070026 scope,
27 containerDiv,
28 headerDiv, bodyDiv,
29 header, body,
30 mockHeader,
31 mockHeaderHeight = 40;
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080032
Bri Prebilic Cole4162f012015-04-16 11:36:54 -070033 var onosFixedHeaderTags =
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070034 '<div class="summary-list" onos-fixed-header>' +
35 '<div class="table-header">' +
36 '<table>' +
37 '<tr>' +
38 '<td colId="type" class="table-icon"></td>' +
39 '<td colId="id">Host ID </td>' +
40 '<td colId="mac" sortable>MAC Address </td>' +
41 '<td colId="location" col-width="110px">Location </td>' +
42 '</tr>' +
43 '</table>' +
44 '</div>' +
45
46 '<div class="table-body">' +
47 '<table>' +
48 '<tr class="ignore-width">' +
49 '<td class="not-picked"></td>' +
50 '</tr>' +
51 '<tr>' +
52 '<td class="table-icon">Some Icon</td>' +
53 '<td>Some ID</td>' +
54 '<td>Some MAC Address</td>' +
55 '<td>Some Location</td>' +
56 '</tr>' +
57 '</table>' +
58 '</div>' +
59 '</div>',
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080060
Bri Prebilic Cole4162f012015-04-16 11:36:54 -070061 onosSortableHeaderTags =
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070062 '<div onos-sortable-header ' +
Bri Prebilic Cole4162f012015-04-16 11:36:54 -070063 'sort-callback="sortCallback(requestParams)">' +
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070064 '<table>' +
65 '<tr>' +
66 '<td colId="type"></td>' +
67 '<td colId="id" sortable>Host ID </td>' +
68 '<td colId="mac" sortable>MAC Address </td>' +
69 '<td colId="location" sortable>Location </td>' +
70 '</tr>' +
71 '</table>' +
72 '</div>';
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080073
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070074 beforeEach(module('onosWidget', 'onosUtil', 'onosMast', 'onosSvg'));
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080075
Simon Hunt31bb01f2015-03-31 16:50:41 -070076 var mockWindow = {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -070077 innerWidth: 600,
78 innerHeight: 400,
Simon Hunt31bb01f2015-03-31 16:50:41 -070079 navigator: {
80 userAgent: 'defaultUA'
81 },
82 on: function () {},
83 addEventListener: function () {}
84 };
85
86 beforeEach(function () {
87 module(function ($provide) {
88 $provide.value('$window', mockWindow);
89 });
90 });
91
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080092 beforeEach(inject(function (_$log_, _$compile_, _$rootScope_,
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070093 FnService, TableService, MastService, IconService) {
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080094 $log = _$log_;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080095 $compile = _$compile_;
96 $rootScope = _$rootScope_;
97 fs = FnService;
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070098 ts = TableService;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -070099 mast = MastService;
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800100 is = IconService;
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800101 }));
102
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800103 beforeEach(function () {
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800104 scope = $rootScope.$new();
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700105 scope.tableData = [];
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800106 });
107
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700108 // Note: dummy header so that d3 doesn't trip up.
109 // $compile has to be used on the directive tag element, so it can't
110 // be included in the tag strings declared above.
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700111 beforeEach(function () {
112 mockHeader = d3.select('body')
113 .append('h2')
Bri Prebilic Cole4162f012015-04-16 11:36:54 -0700114 .classed('tabular-header', true)
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700115 .style({
116 height: mockHeaderHeight + 'px',
117 margin: 0,
118 padding: 0
119 })
120 .text('Some Header');
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700121 });
122
Simon Huntc4ae8302015-01-26 14:22:48 -0800123 afterEach(function () {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700124 containerDiv = undefined;
125 headerDiv = undefined;
126 bodyDiv = undefined;
127 header = undefined;
128 body = undefined;
Bri Prebilic Coleb5f2b152015-04-07 14:58:09 -0700129 mockHeader.remove();
Simon Huntc4ae8302015-01-26 14:22:48 -0800130 });
131
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700132 function populateTableData() {
133 scope.tableData = [
134 {
135 type: 'endstation',
136 id: '1234',
137 mac: '00:00:03',
138 location: 'USA'
139 }
140 ];
141 }
142
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700143 it('should define TableBuilderService', function () {
144 expect(ts).toBeDefined();
145 });
146
147 it('should define api functions', function () {
148 expect(fs.areFunctions(ts, [
149 'resetSortIcons'
150 ])).toBeTruthy();
151 });
152
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700153 function compile(elem) {
154 var compiled = $compile(elem);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800155 compiled(scope);
156 scope.$digest();
157 }
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800158
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700159 function selectTables() {
160 expect(containerDiv.find('div').length).toBe(2);
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800161
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700162 headerDiv = angular.element(containerDiv[0].querySelector('.table-header'));
163 expect(headerDiv.length).toBe(1);
164
165 bodyDiv = angular.element(containerDiv[0].querySelector('.table-body'));
166 expect(bodyDiv.length).toBe(1);
167
168 header = headerDiv.find('table');
169 expect(header.length).toBe(1);
170
171 body = bodyDiv.find('table');
172 expect(body.length).toBe(1);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800173 }
174
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700175 function verifyGivenTags(dirName, div) {
176 expect(div).toBeDefined();
177 expect(div.attr(dirName)).toBe('');
178 }
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800179
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700180 function verifyDefaultSize() {
181 expect(header.css('width')).toBe('570px');
182 expect(body.css('width')).toBe('570px');
183 }
Bri Prebilic Cole4162f012015-04-16 11:36:54 -0700184
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700185 function verifyHeight() {
Bri Prebilic Cole9a5e0062015-05-11 17:20:57 -0700186 var padding = 22,
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700187 mastHeight = 36,
188 tableHeight = (mockWindow.innerHeight - mockHeaderHeight) -
189 (fs.noPx(headerDiv.css('height')) + mastHeight + padding);
190
191 expect(bodyDiv.css('height')).toBe(tableHeight + 'px');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800192 }
193
194 function verifyColWidth() {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700195 var hdrs = header.find('td'),
196 cols = body.find('td');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800197
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700198 expect(angular.element(hdrs[0]).css('width')).toBe('33px');
199 expect(angular.element(hdrs[3]).css('width')).toBe('110px');
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800200
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700201 expect(angular.element(cols[1]).css('width')).toBe('33px');
202 expect(angular.element(cols[4]).css('width')).toBe('110px');
203 }
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800204
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700205 function verifyCallbacks(h) {
206 expect(scope.sortCallback).not.toHaveBeenCalled();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800207
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700208 h[0].click();
209 expect(scope.sortCallback).not.toHaveBeenCalled();
210
211 h[1].click();
212 expect(scope.sortCallback).toHaveBeenCalledWith({
213 sortCol: 'id',
214 sortDir: 'asc'
215 });
216 h[1].click();
217 expect(scope.sortCallback).toHaveBeenCalledWith({
218 sortCol: 'id',
219 sortDir: 'desc'
220 });
221 h[1].click();
222 expect(scope.sortCallback).toHaveBeenCalledWith({
223 sortCol: 'id',
224 sortDir: 'asc'
225 });
226
227 h[2].click();
228 expect(scope.sortCallback).toHaveBeenCalledWith({
229 sortCol: 'mac',
230 sortDir: 'asc'
231 });
232 h[2].click();
233 expect(scope.sortCallback).toHaveBeenCalledWith({
234 sortCol: 'mac',
235 sortDir: 'desc'
236 });
237 h[2].click();
238 expect(scope.sortCallback).toHaveBeenCalledWith({
239 sortCol: 'mac',
240 sortDir: 'asc'
241 });
242
243 h[3].click();
244 expect(scope.sortCallback).toHaveBeenCalledWith({
245 sortCol: 'location',
246 sortDir: 'asc'
247 });
248 h[3].click();
249 expect(scope.sortCallback).toHaveBeenCalledWith({
250 sortCol: 'location',
251 sortDir: 'desc'
252 });
253 h[3].click();
254 expect(scope.sortCallback).toHaveBeenCalledWith({
255 sortCol: 'location',
256 sortDir: 'asc'
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800257 });
258 }
259
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700260 function verifyIcons(h) {
261 var currH, div;
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800262
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700263 h[1].click();
264 currH = angular.element(h[1]);
265 div = currH.find('div');
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700266 expect(div.html()).toBe(
267 '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' +
268 '50 50"><g class="icon upArrow"><rect width="50" height="50" ' +
269 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' +
270 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleUp">' +
271 '</use></g></svg>'
272 );
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700273 h[1].click();
274 div = currH.find('div');
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700275 expect(div.html()).toBe(
276 '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' +
277 '50 50"><g class="icon downArrow"><rect width="50" height="50" ' +
278 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' +
279 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleDown">' +
280 '</use></g></svg>'
281 );
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800282
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700283 h[2].click();
284 div = currH.children();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800285 // clicked on a new element, so the previous icon should have been deleted
286 expect(div.html()).toBeFalsy();
287
288 // the new element should have the ascending icon
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700289 currH = angular.element(h[2]);
290 div = currH.children();
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700291 expect(div.html()).toBe(
292 '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' +
293 '50 50"><g class="icon upArrow"><rect width="50" height="50" ' +
294 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' +
295 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleUp">' +
296 '</use></g></svg>'
297 );
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800298 }
299
300 it('should affirm that onos-fixed-header is working', function () {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700301 containerDiv = angular.element(onosFixedHeaderTags);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800302
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700303 compile(containerDiv);
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800304
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700305 verifyGivenTags('onos-fixed-header', containerDiv);
306 selectTables();
307 verifyDefaultSize();
308
309 populateTableData();
310
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800311 scope.$emit('LastElement');
312 scope.$digest();
313
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700314 verifyHeight();
315 verifyColWidth();
316
317 mockWindow.innerHeight = 300;
318 scope.$digest();
319 verifyHeight();
320
321 mockWindow.innerWidth = 500;
322 scope.$digest();
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800323 verifyColWidth();
Bri Prebilic Cole093739a2015-01-23 10:22:50 -0800324 });
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800325
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800326 it('should affirm that onos-sortable-header is working', function () {
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700327 headerDiv = angular.element(onosSortableHeaderTags);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800328
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700329 compile(headerDiv);
330 verifyGivenTags('onos-sortable-header', headerDiv);
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700331
Bri Prebilic Cole7c445752015-02-17 14:49:46 -0800332 scope.sortCallback = jasmine.createSpy('sortCallback');
333
Bri Prebilic Colee568ead2015-05-01 09:51:28 -0700334 header = headerDiv.find('td');
335 verifyCallbacks(header);
336 verifyIcons(header);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800337 });
338
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700339 // Note: testing resetSortIcons isn't feasible because due to the nature of
340 // directive compilation: they are jQuery elements, not d3 elements,
341 // so the function doesn't work.
342
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -0800343});