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