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