Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 | */ |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 20 | describe('factory: fw/widget/table.js', function () { |
| 21 | var $log, $compile, $rootScope, |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame^] | 22 | fs, ts, mast, is, |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 23 | scope, compiled, |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 24 | table, thead, tbody, mockHeader, |
Bri Prebilic Cole | 4162f01 | 2015-04-16 11:36:54 -0700 | [diff] [blame] | 25 | mockH2Height = 20, |
| 26 | mockMastHeight = 20, |
| 27 | mockHeaderHeight = mockH2Height + mockMastHeight, |
Simon Hunt | 3074fb2 | 2015-03-31 15:06:25 -0700 | [diff] [blame] | 28 | tableIconTdSize = 100, |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 29 | numTestElems = 4; |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 30 | |
Bri Prebilic Cole | 4162f01 | 2015-04-16 11:36:54 -0700 | [diff] [blame] | 31 | var onosFixedHeaderTags = |
| 32 | '<table onos-fixed-header>' + |
| 33 | '<thead style="height:27px;">' + |
| 34 | '<tr>' + |
| 35 | '<th></th>' + |
| 36 | '<th>Device ID </th>' + |
| 37 | '<th col-width="100px">H/W Version </th>' + |
| 38 | '<th>S/W Version </th>' + |
| 39 | '</tr>' + |
| 40 | '</thead>' + |
| 41 | '<tbody>' + |
| 42 | '<tr>' + |
| 43 | '<td colspan="4">' + |
| 44 | 'No Devices found' + |
| 45 | '</td>' + |
| 46 | '</tr>' + |
| 47 | '<tr>' + |
| 48 | '<td class="table-icon">' + |
| 49 | '<div icon icon-id="{{dev._iconid_available}}">' + |
| 50 | '</div>' + |
| 51 | '</td>' + |
| 52 | '<td>Some ID</td>' + |
| 53 | '<td>Some HW</td>' + |
| 54 | '<td>Some Software</td>' + |
| 55 | '</tr>' + |
| 56 | '</tbody>' + |
| 57 | '</table>', |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 58 | |
Bri Prebilic Cole | 4162f01 | 2015-04-16 11:36:54 -0700 | [diff] [blame] | 59 | onosSortableHeaderTags = |
| 60 | '<table onos-sortable-header ' + |
| 61 | 'sort-callback="sortCallback(requestParams)">' + |
| 62 | '<thead>' + |
| 63 | '<tr>' + |
| 64 | '<th colId="available"></th>' + |
| 65 | '<th colId="id" sortable>Device ID </th>' + |
| 66 | '<th colId="hw" sortable>H/W Version </th>' + |
| 67 | '<th colId="sw" sortable>S/W Version </th>' + |
| 68 | '</tr>' + |
| 69 | '</thead>' + |
| 70 | '<tbody>' + |
| 71 | '<tr>' + |
| 72 | '<td>' + |
| 73 | '<div icon icon-id="{{dev._iconid_available}}">' + |
| 74 | '</div>' + |
| 75 | '</td>' + |
| 76 | '<td>Some ID</td>' + |
| 77 | '<td>Some HW</td>' + |
| 78 | '<td>Some Software</td>' + |
| 79 | '</tr>' + |
| 80 | '</tbody>' + |
| 81 | '</table>'; |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 82 | |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 83 | beforeEach(module('onosWidget', 'onosUtil', 'onosMast', 'onosSvg')); |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 84 | |
Simon Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 85 | var mockWindow = { |
| 86 | innerWidth: 400, |
| 87 | innerHeight: 200, |
| 88 | navigator: { |
| 89 | userAgent: 'defaultUA' |
| 90 | }, |
| 91 | on: function () {}, |
| 92 | addEventListener: function () {} |
| 93 | }; |
| 94 | |
| 95 | beforeEach(function () { |
| 96 | module(function ($provide) { |
| 97 | $provide.value('$window', mockWindow); |
| 98 | }); |
| 99 | }); |
| 100 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 101 | beforeEach(inject(function (_$log_, _$compile_, _$rootScope_, |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame^] | 102 | FnService, TableService, MastService, IconService) { |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 103 | $log = _$log_; |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 104 | $compile = _$compile_; |
| 105 | $rootScope = _$rootScope_; |
| 106 | fs = FnService; |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame^] | 107 | ts = TableService; |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 108 | mast = MastService; |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 109 | is = IconService; |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 110 | })); |
| 111 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 112 | beforeEach(function () { |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 113 | scope = $rootScope.$new(); |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 114 | }); |
| 115 | |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 116 | beforeEach(function () { |
| 117 | mockHeader = d3.select('body') |
| 118 | .append('h2') |
Bri Prebilic Cole | 4162f01 | 2015-04-16 11:36:54 -0700 | [diff] [blame] | 119 | .classed('tabular-header', true) |
| 120 | .style('height', mockHeaderHeight + 'px') |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 121 | .html('Some Header'); |
| 122 | }); |
| 123 | |
Simon Hunt | c4ae830 | 2015-01-26 14:22:48 -0800 | [diff] [blame] | 124 | afterEach(function () { |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 125 | table = null; |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 126 | thead = null; |
| 127 | tbody = null; |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 128 | mockHeader.remove(); |
Simon Hunt | c4ae830 | 2015-01-26 14:22:48 -0800 | [diff] [blame] | 129 | }); |
| 130 | |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame^] | 131 | it('should define TableBuilderService', function () { |
| 132 | expect(ts).toBeDefined(); |
| 133 | }); |
| 134 | |
| 135 | it('should define api functions', function () { |
| 136 | expect(fs.areFunctions(ts, [ |
| 137 | 'resetSortIcons' |
| 138 | ])).toBeTruthy(); |
| 139 | }); |
| 140 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 141 | function compileTable() { |
| 142 | compiled = $compile(table); |
| 143 | compiled(scope); |
| 144 | scope.$digest(); |
| 145 | } |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 146 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 147 | function verifyGivenTags(dirName) { |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 148 | expect(table).toBeDefined(); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 149 | expect(table.attr(dirName)).toBe(''); |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 150 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 151 | thead = table.find('thead'); |
| 152 | expect(thead).toBeDefined(); |
| 153 | tbody = table.find('tbody'); |
| 154 | expect(tbody).toBeDefined(); |
| 155 | } |
| 156 | |
| 157 | function verifyCssDisplay() { |
Bri Prebilic Cole | 4162f01 | 2015-04-16 11:36:54 -0700 | [diff] [blame] | 158 | var padding = 21, // bottom table constant 12 + mastPadding(?) 9 |
| 159 | tableHeight = fs.windowSize(mockHeaderHeight).height - |
| 160 | (fs.noPx(table.find('thead').css('height')) + padding); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 161 | |
| 162 | expect(thead.css('display')).toBe('block'); |
| 163 | expect(tbody.css('display')).toBe('block'); |
Bri Prebilic Cole | b5f2b15 | 2015-04-07 14:58:09 -0700 | [diff] [blame] | 164 | expect(tbody.css('height')).toBe(tableHeight + 'px'); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 165 | expect(tbody.css('overflow')).toBe('auto'); |
Bri Prebilic Cole | 4162f01 | 2015-04-16 11:36:54 -0700 | [diff] [blame] | 166 | |
| 167 | // TODO: investigate why math for calculating the height works better |
| 168 | // in the browser window (thead height is 0 in this test?) |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | function verifyColWidth() { |
| 172 | var winWidth = fs.windowSize().width, |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 173 | colWidth, thElems, tr, tdElem; |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 174 | |
| 175 | colWidth = Math.floor(winWidth / numTestElems); |
| 176 | |
| 177 | thElems = thead.find('th'); |
| 178 | |
| 179 | angular.forEach(thElems, function (thElem, i) { |
| 180 | thElem = angular.element(thElems[i]); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 181 | tr = angular.element(tbody.find('tr').eq(1)); |
| 182 | tdElem = angular.element(tr.find('td').eq(i)); |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 183 | var custWidth = thElem.attr('col-width'); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 184 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 185 | if (custWidth) { |
| 186 | expect(thElem.css('width')).toBe(custWidth); |
| 187 | expect(tdElem.css('width')).toBe(custWidth); |
| 188 | } else if (tdElem.attr('class') === 'table-icon') { |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 189 | expect(thElem.css('width')).toBe(tableIconTdSize + 'px'); |
| 190 | expect(tdElem.css('width')).toBe(tableIconTdSize + 'px'); |
| 191 | } else { |
| 192 | expect(thElem.css('width')).toBe(colWidth + 'px'); |
| 193 | expect(tdElem.css('width')).toBe(colWidth + 'px'); |
| 194 | } |
| 195 | }); |
| 196 | } |
| 197 | |
| 198 | function verifyCallbacks(thElems) { |
| 199 | expect(scope.sortCallback).not.toHaveBeenCalled(); |
| 200 | |
| 201 | // first test header has no 'sortable' attr |
| 202 | thElems[0].click(); |
| 203 | expect(scope.sortCallback).not.toHaveBeenCalled(); |
| 204 | |
| 205 | // the other headers have 'sortable' |
| 206 | for(var i = 1; i < numTestElems; i += 1) { |
| 207 | thElems[i].click(); |
| 208 | expect(scope.sortCallback).toHaveBeenCalled(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | function verifyIcons(thElems) { |
| 213 | var currentTh, div; |
| 214 | // make sure it has the correct icon after clicking |
| 215 | thElems[1].click(); |
| 216 | currentTh = angular.element(thElems[1]); |
| 217 | div = currentTh.find('div'); |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame^] | 218 | expect(div.html()).toBe( |
| 219 | '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' + |
| 220 | '50 50"><g class="icon upArrow"><rect width="50" height="50" ' + |
| 221 | 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' + |
| 222 | 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleUp">' + |
| 223 | '</use></g></svg>' |
| 224 | ); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 225 | thElems[1].click(); |
| 226 | div = currentTh.find('div'); |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame^] | 227 | expect(div.html()).toBe( |
| 228 | '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' + |
| 229 | '50 50"><g class="icon downArrow"><rect width="50" height="50" ' + |
| 230 | 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' + |
| 231 | 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleDown">' + |
| 232 | '</use></g></svg>' |
| 233 | ); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 234 | |
| 235 | thElems[2].click(); |
| 236 | div = currentTh.children(); |
| 237 | // clicked on a new element, so the previous icon should have been deleted |
| 238 | expect(div.html()).toBeFalsy(); |
| 239 | |
| 240 | // the new element should have the ascending icon |
| 241 | currentTh = angular.element(thElems[2]); |
| 242 | div = currentTh.children(); |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame^] | 243 | expect(div.html()).toBe( |
| 244 | '<svg class="embeddedIcon" width="10" height="10" viewBox="0 0 ' + |
| 245 | '50 50"><g class="icon upArrow"><rect width="50" height="50" ' + |
| 246 | 'rx="5"></rect><use width="50" height="50" class="glyph" xmlns:' + |
| 247 | 'xlink="http://www.w3.org/1999/xlink" xlink:href="#triangleUp">' + |
| 248 | '</use></g></svg>' |
| 249 | ); |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | it('should affirm that onos-fixed-header is working', function () { |
| 253 | table = angular.element(onosFixedHeaderTags); |
| 254 | |
| 255 | compileTable(); |
| 256 | verifyGivenTags('onos-fixed-header'); |
| 257 | |
| 258 | // table will not be fixed unless it receives the 'LastElement' event |
| 259 | scope.$emit('LastElement'); |
| 260 | scope.$digest(); |
| 261 | |
| 262 | verifyCssDisplay(); |
| 263 | verifyColWidth(); |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 264 | }); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 265 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 266 | it('should affirm that onos-sortable-header is working', function () { |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 267 | var thElems; |
| 268 | table = angular.element(onosSortableHeaderTags); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 269 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 270 | compileTable(); |
| 271 | verifyGivenTags('onos-sortable-header'); |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame^] | 272 | |
Bri Prebilic Cole | 7c44575 | 2015-02-17 14:49:46 -0800 | [diff] [blame] | 273 | scope.sortCallback = jasmine.createSpy('sortCallback'); |
| 274 | |
| 275 | thElems = thead.find('th'); |
| 276 | verifyCallbacks(thElems); |
| 277 | verifyIcons(thElems); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 278 | }); |
| 279 | |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame^] | 280 | // Note: testing resetSortIcons isn't feasible because due to the nature of |
| 281 | // directive compilation: they are jQuery elements, not d3 elements, |
| 282 | // so the function doesn't work. |
| 283 | |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 284 | }); |