Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 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 -- Button Service - Unit Tests |
| 19 | */ |
| 20 | describe('factory: fw/widget/button.js', function () { |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 21 | var $log, fs, bns, d3Elem; |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 22 | |
| 23 | beforeEach(module('onosWidget', 'onosSvg')); |
| 24 | |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 25 | beforeEach(inject(function (_$log_, FnService, ButtonService) { |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 26 | $log = _$log_; |
| 27 | fs = FnService; |
| 28 | bns = ButtonService; |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 29 | })); |
| 30 | |
| 31 | beforeEach(function () { |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 32 | d3Elem = d3.select('body').append('div').attr('id', 'testDiv'); |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 33 | }); |
| 34 | |
| 35 | afterEach(function () { |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 36 | d3.select('#testDiv').remove(); |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 37 | }); |
| 38 | |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 39 | |
| 40 | // re-usable null function |
| 41 | function nullFunc () {} |
| 42 | |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 43 | it('should define ButtonService', function () { |
| 44 | expect(bns).toBeDefined(); |
| 45 | }); |
| 46 | |
| 47 | it('should define api functions', function () { |
| 48 | expect(fs.areFunctions(bns, [ |
| 49 | 'button', 'toggle', 'radioSet' |
| 50 | ])).toBeTruthy(); |
| 51 | }); |
| 52 | |
| 53 | it('should verify button glyph', function () { |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 54 | var btn = bns.button(d3Elem, 'foo-id', 'crown', nullFunc); |
| 55 | var el = d3Elem.select('#foo-id'); |
| 56 | expect(el.classed('button')).toBeTruthy(); |
| 57 | expect(el.attr('id')).toBe('foo-id'); |
| 58 | expect(el.select('svg')).toBeTruthy(); |
| 59 | expect(el.select('use')).toBeTruthy(); |
| 60 | expect(el.select('use').classed('glyph')).toBeTruthy(); |
| 61 | expect(el.select('use').attr('xlink:href')).toBe('#crown'); |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 62 | }); |
| 63 | |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 64 | |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 65 | it('should not append button to an undefined div', function () { |
| 66 | spyOn($log, 'warn'); |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 67 | expect(bns.button(null, 'id', 'gid', nullFunc)).toBeNull(); |
| 68 | expect($log.warn).toHaveBeenCalledWith('div undefined (button)'); |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 69 | }); |
| 70 | |
| 71 | it('should verify button callback', function () { |
| 72 | var count = 0; |
| 73 | function cb() { count++; } |
| 74 | var btn = bns.button(d3Elem, 'test', 'nothing', cb); |
| 75 | expect(count).toBe(0); |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 76 | d3Elem.select('#test').on('click')(); |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 77 | expect(count).toBe(1); |
| 78 | }); |
| 79 | |
| 80 | it('should ignore non-function callbacks button', function () { |
| 81 | var count = 0; |
| 82 | var btn = bns.button(d3Elem, 'test', 'nothing', 'foo'); |
| 83 | expect(count).toBe(0); |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 84 | d3Elem.select('#test').on('click')(); |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 85 | expect(count).toBe(0); |
| 86 | }); |
| 87 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 88 | it('should not append toggle to an undefined div', function () { |
| 89 | spyOn($log, 'warn'); |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 90 | expect(bns.toggle(undefined, 'id', 'gid', false, nullFunc)).toBeNull(); |
| 91 | expect($log.warn).toHaveBeenCalledWith('div undefined (toggle button)'); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 92 | }); |
| 93 | |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 94 | it('should verify toggle glyph', function () { |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 95 | var tog = bns.toggle(d3Elem, 'foo-id', 'crown', false, nullFunc); |
| 96 | var el = d3Elem.select('#foo-id'); |
| 97 | expect(el.classed('toggleButton')).toBeTruthy(); |
| 98 | expect(el.attr('id')).toBe('foo-id'); |
| 99 | expect(el.select('svg')).toBeTruthy(); |
| 100 | expect(el.select('use')).toBeTruthy(); |
| 101 | expect(el.select('use').classed('glyph')).toBeTruthy(); |
| 102 | expect(el.select('use').attr('xlink:href')).toBe('#crown'); |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 103 | }); |
| 104 | |
| 105 | it('should toggle the selected state', function () { |
| 106 | var tog = bns.toggle(d3Elem, 'test', 'nothing'); |
| 107 | expect(tog.selected()).toBe(false); |
| 108 | tog.toggle(); |
| 109 | expect(tog.selected()).toBe(true); |
| 110 | tog.toggle(); |
| 111 | expect(tog.selected()).toBe(false); |
| 112 | }); |
| 113 | |
| 114 | it('should set toggle state', function () { |
| 115 | var tog = bns.toggle(d3Elem, 'test', 'nothing'); |
| 116 | tog.toggle(true); |
| 117 | expect(tog.selected()).toBe(true); |
| 118 | tog.toggle(); |
| 119 | expect(tog.selected()).toBe(false); |
| 120 | tog.toggle('truthy string'); |
| 121 | expect(tog.selected()).toBe(true); |
| 122 | tog.toggle(null); |
| 123 | expect(tog.selected()).toBe(false); |
| 124 | tog.toggle(''); |
| 125 | expect(tog.selected()).toBe(false); |
| 126 | }); |
| 127 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 128 | it('should verity toggle initial state', function () { |
| 129 | var tog = bns.toggle(d3Elem, 'id', 'gid', true); |
| 130 | expect(tog.selected()).toBe(true); |
| 131 | tog = bns.toggle(d3Elem, 'id', 'gid', false); |
| 132 | expect(tog.selected()).toBe(false); |
| 133 | tog = bns.toggle(d3Elem, 'id', 'gid', ''); |
| 134 | expect(tog.selected()).toBe(false); |
| 135 | tog = bns.toggle(d3Elem, 'id', 'gid', 'something'); |
| 136 | expect(tog.selected()).toBe(true); |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 137 | }); |
| 138 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 139 | it('should not append radio button set to an undefined div', function () { |
| 140 | spyOn($log, 'warn'); |
| 141 | expect(bns.radioSet(undefined, 'id', [])).toBeNull(); |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 142 | expect($log.warn).toHaveBeenCalledWith('div undefined (radio button set)'); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 143 | }); |
| 144 | |
| 145 | it('should not create radio button set from a non-array', function () { |
| 146 | var rads = {test: 'test'}; |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 147 | var warning = 'invalid array (radio button set)'; |
| 148 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 149 | spyOn($log, 'warn'); |
| 150 | |
| 151 | expect(bns.radioSet(d3Elem, 'test', rads)).toBeNull(); |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 152 | expect($log.warn).toHaveBeenCalledWith(warning); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 153 | rads = 'rads'; |
| 154 | expect(bns.radioSet(d3Elem, 'test', rads)).toBeNull(); |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 155 | expect($log.warn).toHaveBeenCalledWith(warning); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 156 | rads = {arr: [1, 2, 3]}; |
| 157 | expect(bns.radioSet(d3Elem, 'test', rads)).toBeNull(); |
Simon Hunt | 7c7dd3e | 2015-02-27 11:42:18 -0800 | [diff] [blame] | 158 | expect($log.warn).toHaveBeenCalledWith(warning); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 159 | }); |
| 160 | |
| 161 | it('should not create radio button set from empty array', function () { |
| 162 | var rads = []; |
| 163 | spyOn($log, 'warn'); |
| 164 | expect(bns.radioSet(d3Elem, 'test', rads)).toBeNull(); |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 165 | expect($log.warn).toHaveBeenCalledWith('invalid array (radio button set)'); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 166 | }); |
| 167 | |
| 168 | it('should verify radio button glyph structure', function () { |
| 169 | var rads = [ |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 170 | { gid: 'crown', cb: nullFunc, tooltip: 'n/a'} |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 171 | ], rdiv; |
| 172 | |
| 173 | spyOn($log, 'warn'); |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 174 | expect(bns.radioSet(d3Elem, 'foo', rads)).toBeTruthy(); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 175 | expect($log.warn).not.toHaveBeenCalled(); |
| 176 | |
| 177 | rdiv = d3Elem.select('div'); |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 178 | expect(rdiv.classed('radioSet')).toBe(true); |
| 179 | expect(rdiv.select('div').classed('radioButton')).toBe(true); |
| 180 | expect(rdiv.select('div').attr('id')).toBe('foo-0'); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 181 | expect(rdiv.select('div').select('svg')).toBeTruthy(); |
| 182 | expect(rdiv.select('use').classed('glyph')).toBeTruthy(); |
| 183 | expect(rdiv.select('use').attr('xlink:href')).toBe('#crown'); |
| 184 | }); |
| 185 | |
| 186 | it('should verify more than one radio button glyph was added', function () { |
| 187 | var rads = [ |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 188 | { gid: 'crown', cb: nullFunc, tooltip: 'n/a'}, |
| 189 | { gid: 'router', cb: nullFunc, tooltip: 'n/a'} |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 190 | ], rdiv; |
| 191 | |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 192 | expect(bns.radioSet(d3Elem, 'foo', rads)).toBeTruthy(); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 193 | rdiv = d3Elem.select('div'); |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 194 | expect(rdiv.select('#foo-0')).toBeTruthy(); |
| 195 | expect(rdiv.select('#foo-1')).toBeTruthy(); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 196 | |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 197 | expect(rdiv.select('#foo-0') |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 198 | .select('use') |
| 199 | .classed('glyph')) |
| 200 | .toBeTruthy(); |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 201 | expect(rdiv.select('#foo-0') |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 202 | .select('use') |
| 203 | .attr('xlink:href')) |
| 204 | .toBe('#crown'); |
| 205 | |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 206 | expect(rdiv.select('#foo-1') |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 207 | .select('use') |
| 208 | .classed('glyph')) |
| 209 | .toBeTruthy(); |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 210 | expect(rdiv.select('#foo-1') |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 211 | .select('use') |
| 212 | .attr('xlink:href')) |
| 213 | .toBe('#router'); |
| 214 | }); |
| 215 | |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 216 | it('should select radio button by index', function () { |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 217 | var count0 = 0, |
| 218 | count1 = 9; |
| 219 | function cb0() { count0++; } |
| 220 | function cb1() { count1++; } |
| 221 | |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 222 | function validate(expSel, exp0, exp1) { |
| 223 | expect(rset.selected()).toBe(expSel); |
| 224 | expect(count0).toBe(exp0); |
| 225 | expect(count1).toBe(exp1); |
| 226 | } |
| 227 | |
| 228 | function checkWarn(msg, index) { |
| 229 | expect($log.warn).toHaveBeenCalledWith(msg, index); |
| 230 | } |
| 231 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 232 | var rads = [ |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 233 | { gid: 'crown', cb: cb0, tooltip: 'n/a'}, |
| 234 | { gid: 'router', cb: cb1, tooltip: 'n/a'} |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 235 | ], |
| 236 | rset = bns.radioSet(d3Elem, 'test', rads); |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 237 | spyOn($log, 'warn'); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 238 | |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 239 | validate(0, 0, 9); |
| 240 | rset.selectedIndex(0); |
| 241 | validate(0, 0, 9); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 242 | |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 243 | rset.selectedIndex(1); |
| 244 | validate(1, 0, 10); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 245 | |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 246 | rset.selectedIndex(-1); |
| 247 | checkWarn('invalid radio button index:', -1); |
| 248 | validate(1, 0, 10); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 249 | |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 250 | rset.selectedIndex(66); |
| 251 | checkWarn('invalid radio button index:', 66); |
| 252 | validate(1, 0, 10); |
| 253 | |
| 254 | rset.selectedIndex(0); |
| 255 | validate(0, 1, 10); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 256 | }); |
| 257 | |
Bri Prebilic Cole | 1848917 | 2015-02-27 17:12:00 -0800 | [diff] [blame] | 258 | it('should select radio button by key', function () { |
| 259 | var count0 = 0, |
| 260 | count1 = 9; |
| 261 | function cb0() { count0++; } |
| 262 | function cb1() { count1++; } |
| 263 | |
| 264 | function validate(expSel, exp0, exp1) { |
| 265 | expect(rset.selected()).toBe(expSel); |
| 266 | expect(count0).toBe(exp0); |
| 267 | expect(count1).toBe(exp1); |
| 268 | } |
| 269 | |
| 270 | function checkWarn(msg, index) { |
| 271 | expect($log.warn).toHaveBeenCalledWith(msg, index); |
| 272 | } |
| 273 | |
| 274 | var rads = [ |
| 275 | { key: 'foo', gid: 'crown', cb: cb0, tooltip: 'n/a'}, |
| 276 | { key: 'bar', gid: 'router', cb: cb1, tooltip: 'n/a'} |
| 277 | ], |
| 278 | rset = bns.radioSet(d3Elem, 'test', rads); |
| 279 | spyOn($log, 'warn'); |
| 280 | |
| 281 | validate('foo', 0, 9); |
| 282 | rset.selected('foo'); |
| 283 | validate('foo', 0, 9); |
| 284 | |
| 285 | rset.selected('bar'); |
| 286 | validate('bar', 0, 10); |
| 287 | |
| 288 | rset.selected('blob'); |
| 289 | checkWarn('no radio button with key:', 'blob'); |
| 290 | validate('bar', 0, 10); |
| 291 | |
| 292 | rset.selected('foo'); |
| 293 | validate('foo', 1, 10); |
| 294 | |
| 295 | rset.selected('foo'); |
| 296 | validate('foo', 1, 10); |
| 297 | checkWarn('current index already selected:', 0); |
| 298 | }); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 299 | |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 300 | }); |