GUI -- Reworked ButtonService unit tests (fixed logic error).
Change-Id: I793c02a327885b746ccf2345156f7a5fd1007bd2
Showing
3 changed files
with
102 additions
and
63 deletions
| ... | @@ -203,7 +203,7 @@ | ... | @@ -203,7 +203,7 @@ |
| 203 | } else { | 203 | } else { |
| 204 | idx = idxByKey[x]; | 204 | idx = idxByKey[x]; |
| 205 | if (idx === undefined) { | 205 | if (idx === undefined) { |
| 206 | - $log.warn('no radio button with key "' + x + '"'); | 206 | + $log.warn('no radio button with key:', x); |
| 207 | } else { | 207 | } else { |
| 208 | selectedIndex(idx); | 208 | selectedIndex(idx); |
| 209 | } | 209 | } |
| ... | @@ -215,10 +215,14 @@ | ... | @@ -215,10 +215,14 @@ |
| 215 | return currIdx; | 215 | return currIdx; |
| 216 | } else { | 216 | } else { |
| 217 | if (x >= 0 && x < rads.length) { | 217 | if (x >= 0 && x < rads.length) { |
| 218 | - currIdx = x; | 218 | + if (currIdx !== x) { |
| 219 | - invokeCurrent(); | 219 | + currIdx = x; |
| 220 | + invokeCurrent(); | ||
| 221 | + } else { | ||
| 222 | + $log.warn('current index already selected:', x); | ||
| 223 | + } | ||
| 220 | } else { | 224 | } else { |
| 221 | - $log.warn('invalid radio button index', x); | 225 | + $log.warn('invalid radio button index:', x); |
| 222 | } | 226 | } |
| 223 | } | 227 | } |
| 224 | } | 228 | } | ... | ... |
| ... | @@ -158,34 +158,26 @@ describe('factory: fw/widget/button.js', function () { | ... | @@ -158,34 +158,26 @@ describe('factory: fw/widget/button.js', function () { |
| 158 | expect($log.warn).toHaveBeenCalledWith(warning); | 158 | expect($log.warn).toHaveBeenCalledWith(warning); |
| 159 | }); | 159 | }); |
| 160 | 160 | ||
| 161 | - // =================================================================== | ||
| 162 | - // =================================================================== | ||
| 163 | - // =================================================================== | ||
| 164 | - | ||
| 165 | - | ||
| 166 | - | ||
| 167 | it('should not create radio button set from empty array', function () { | 161 | it('should not create radio button set from empty array', function () { |
| 168 | var rads = []; | 162 | var rads = []; |
| 169 | spyOn($log, 'warn'); | 163 | spyOn($log, 'warn'); |
| 170 | expect(bns.radioSet(d3Elem, 'test', rads)).toBeNull(); | 164 | expect(bns.radioSet(d3Elem, 'test', rads)).toBeNull(); |
| 171 | - expect($log.warn).toHaveBeenCalledWith('Cannot create radio button ' + | 165 | + expect($log.warn).toHaveBeenCalledWith('invalid array (radio button set)'); |
| 172 | - 'set from empty array'); | ||
| 173 | }); | 166 | }); |
| 174 | 167 | ||
| 175 | it('should verify radio button glyph structure', function () { | 168 | it('should verify radio button glyph structure', function () { |
| 176 | var rads = [ | 169 | var rads = [ |
| 177 | - { gid: 'crown', cb: function () {}, tooltip: 'n/a'} | 170 | + { gid: 'crown', cb: nullFunc, tooltip: 'n/a'} |
| 178 | ], rdiv; | 171 | ], rdiv; |
| 179 | 172 | ||
| 180 | spyOn($log, 'warn'); | 173 | spyOn($log, 'warn'); |
| 181 | - expect(bns.radioSet(d3Elem, 'test', rads)).toBeTruthy(); | 174 | + expect(bns.radioSet(d3Elem, 'foo', rads)).toBeTruthy(); |
| 182 | expect($log.warn).not.toHaveBeenCalled(); | 175 | expect($log.warn).not.toHaveBeenCalled(); |
| 183 | 176 | ||
| 184 | rdiv = d3Elem.select('div'); | 177 | rdiv = d3Elem.select('div'); |
| 185 | - expect(rdiv.classed('rset')).toBe(true); | 178 | + expect(rdiv.classed('radioSet')).toBe(true); |
| 186 | - expect(rdiv.select('div').classed('rad')).toBe(true); | 179 | + expect(rdiv.select('div').classed('radioButton')).toBe(true); |
| 187 | - expect(rdiv.select('div').classed('tog')).toBe(false); | 180 | + expect(rdiv.select('div').attr('id')).toBe('foo-0'); |
| 188 | - expect(rdiv.select('div').attr('id')).toBe('test-0'); | ||
| 189 | expect(rdiv.select('div').select('svg')).toBeTruthy(); | 181 | expect(rdiv.select('div').select('svg')).toBeTruthy(); |
| 190 | expect(rdiv.select('use').classed('glyph')).toBeTruthy(); | 182 | expect(rdiv.select('use').classed('glyph')).toBeTruthy(); |
| 191 | expect(rdiv.select('use').attr('xlink:href')).toBe('#crown'); | 183 | expect(rdiv.select('use').attr('xlink:href')).toBe('#crown'); |
| ... | @@ -193,75 +185,116 @@ describe('factory: fw/widget/button.js', function () { | ... | @@ -193,75 +185,116 @@ describe('factory: fw/widget/button.js', function () { |
| 193 | 185 | ||
| 194 | it('should verify more than one radio button glyph was added', function () { | 186 | it('should verify more than one radio button glyph was added', function () { |
| 195 | var rads = [ | 187 | var rads = [ |
| 196 | - { gid: 'crown', cb: function () {}, tooltip: 'n/a'}, | 188 | + { gid: 'crown', cb: nullFunc, tooltip: 'n/a'}, |
| 197 | - { gid: 'router', cb: function () {}, tooltip: 'n/a'} | 189 | + { gid: 'router', cb: nullFunc, tooltip: 'n/a'} |
| 198 | ], rdiv; | 190 | ], rdiv; |
| 199 | 191 | ||
| 200 | - expect(bns.radioSet(d3Elem, 'test', rads)).toBeTruthy(); | 192 | + expect(bns.radioSet(d3Elem, 'foo', rads)).toBeTruthy(); |
| 201 | rdiv = d3Elem.select('div'); | 193 | rdiv = d3Elem.select('div'); |
| 202 | - expect(rdiv.select('#test-0')).toBeTruthy(); | 194 | + expect(rdiv.select('#foo-0')).toBeTruthy(); |
| 203 | - expect(rdiv.select('#test-1')).toBeTruthy(); | 195 | + expect(rdiv.select('#foo-1')).toBeTruthy(); |
| 204 | 196 | ||
| 205 | - expect(rdiv.select('#test-0') | 197 | + expect(rdiv.select('#foo-0') |
| 206 | .select('use') | 198 | .select('use') |
| 207 | .classed('glyph')) | 199 | .classed('glyph')) |
| 208 | .toBeTruthy(); | 200 | .toBeTruthy(); |
| 209 | - expect(rdiv.select('#test-0') | 201 | + expect(rdiv.select('#foo-0') |
| 210 | .select('use') | 202 | .select('use') |
| 211 | .attr('xlink:href')) | 203 | .attr('xlink:href')) |
| 212 | .toBe('#crown'); | 204 | .toBe('#crown'); |
| 213 | 205 | ||
| 214 | - expect(rdiv.select('#test-1') | 206 | + expect(rdiv.select('#foo-1') |
| 215 | .select('use') | 207 | .select('use') |
| 216 | .classed('glyph')) | 208 | .classed('glyph')) |
| 217 | .toBeTruthy(); | 209 | .toBeTruthy(); |
| 218 | - expect(rdiv.select('#test-1') | 210 | + expect(rdiv.select('#foo-1') |
| 219 | .select('use') | 211 | .select('use') |
| 220 | .attr('xlink:href')) | 212 | .attr('xlink:href')) |
| 221 | .toBe('#router'); | 213 | .toBe('#router'); |
| 222 | }); | 214 | }); |
| 223 | 215 | ||
| 224 | - it('should select the correct radio button', function () { | 216 | + it('should select radio button by index', function () { |
| 225 | var count0 = 0, | 217 | var count0 = 0, |
| 226 | count1 = 9; | 218 | count1 = 9; |
| 227 | function cb0() { count0++; } | 219 | function cb0() { count0++; } |
| 228 | function cb1() { count1++; } | 220 | function cb1() { count1++; } |
| 229 | 221 | ||
| 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 | + | ||
| 230 | var rads = [ | 232 | var rads = [ |
| 231 | - { gid: 'crown', cb: cb0, tooltip: 'n/a'}, | 233 | + { gid: 'crown', cb: cb0, tooltip: 'n/a'}, |
| 232 | - { gid: 'router', cb: cb1, tooltip: 'n/a'} | 234 | + { gid: 'router', cb: cb1, tooltip: 'n/a'} |
| 233 | ], | 235 | ], |
| 234 | rset = bns.radioSet(d3Elem, 'test', rads); | 236 | rset = bns.radioSet(d3Elem, 'test', rads); |
| 235 | - spyOn($log, 'error'); | 237 | + spyOn($log, 'warn'); |
| 236 | - | 238 | + |
| 237 | - expect(rset.selected()).toBe(0); | 239 | + validate(0, 0, 9); |
| 238 | - expect(count0).toBe(0); | 240 | + rset.selectedIndex(0); |
| 239 | - expect(count1).toBe(9); | 241 | + validate(0, 0, 9); |
| 240 | - rset.selected(0); | 242 | + |
| 241 | - expect(rset.selected()).toBe(0); | 243 | + rset.selectedIndex(1); |
| 242 | - expect(count0).toBe(0); | 244 | + validate(1, 0, 10); |
| 243 | - expect(count1).toBe(9); | 245 | + |
| 244 | - | 246 | + rset.selectedIndex(-1); |
| 245 | - rset.selected(1); | 247 | + checkWarn('invalid radio button index:', -1); |
| 246 | - expect(rset.selected()).toBe(1); | 248 | + validate(1, 0, 10); |
| 247 | - expect(count0).toBe(1); | 249 | + |
| 248 | - expect(count1).toBe(10); | 250 | + rset.selectedIndex(66); |
| 249 | - | 251 | + checkWarn('invalid radio button index:', 66); |
| 250 | - rset.selected(-1); | 252 | + validate(1, 0, 10); |
| 251 | - expect($log.error).toHaveBeenCalledWith('Cannot select radio button ' + | 253 | + |
| 252 | - 'of index -1'); | 254 | + rset.selectedIndex(0); |
| 253 | - expect(rset.selected()).toBe(1); | 255 | + validate(0, 1, 10); |
| 254 | - expect(count0).toBe(1); | ||
| 255 | - expect(count1).toBe(10); | ||
| 256 | - | ||
| 257 | - rset.selected(66); | ||
| 258 | - expect($log.error).toHaveBeenCalledWith('Cannot select radio button ' + | ||
| 259 | - 'of index 66'); | ||
| 260 | - expect(rset.selected()).toBe(1); | ||
| 261 | - expect(count0).toBe(1); | ||
| 262 | - expect(count1).toBe(10); | ||
| 263 | }); | 256 | }); |
| 264 | 257 | ||
| 265 | - // TODO: figure out how to trigger d3 onclick for buttons and toggles | 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 | + }); | ||
| 266 | 299 | ||
| 267 | }); | 300 | }); | ... | ... |
| ... | @@ -36,13 +36,13 @@ describe('factory: fw/widget/toolbar.js', function () { | ... | @@ -36,13 +36,13 @@ describe('factory: fw/widget/toolbar.js', function () { |
| 36 | // TODO: figure out solution for calling tests with new info instead of calling init | 36 | // TODO: figure out solution for calling tests with new info instead of calling init |
| 37 | 37 | ||
| 38 | beforeEach(function () { | 38 | beforeEach(function () { |
| 39 | - d3Elem = d3.select('body').append('div').attr('id', 'floatpanels'); | 39 | + d3Elem = d3.select('body').append('div').attr('id', 'testToolbar'); |
| 40 | tbs.init(); | 40 | tbs.init(); |
| 41 | ps.init(); | 41 | ps.init(); |
| 42 | }); | 42 | }); |
| 43 | 43 | ||
| 44 | afterEach(function () { | 44 | afterEach(function () { |
| 45 | - d3.select('#floatpanels').remove(); | 45 | + d3.select('#testToolbar').remove(); |
| 46 | tbs.init(); | 46 | tbs.init(); |
| 47 | ps.init(); | 47 | ps.init(); |
| 48 | }); | 48 | }); |
| ... | @@ -61,11 +61,13 @@ describe('factory: fw/widget/toolbar.js', function () { | ... | @@ -61,11 +61,13 @@ describe('factory: fw/widget/toolbar.js', function () { |
| 61 | it('should warn if createToolbar id is invalid', function () { | 61 | it('should warn if createToolbar id is invalid', function () { |
| 62 | spyOn($log, 'warn'); | 62 | spyOn($log, 'warn'); |
| 63 | expect(tbs.createToolbar()).toBeNull(); | 63 | expect(tbs.createToolbar()).toBeNull(); |
| 64 | - expect($log.warn).toHaveBeenCalledWith('createToolbar: no ID given'); | 64 | + expect($log.warn).toHaveBeenCalledWith('createToolbar: ' + |
| 65 | + 'no ID given: [undefined]'); | ||
| 65 | 66 | ||
| 66 | expect(tbs.createToolbar('test')).toBeTruthy(); | 67 | expect(tbs.createToolbar('test')).toBeTruthy(); |
| 67 | expect(tbs.createToolbar('test')).toBeNull(); | 68 | expect(tbs.createToolbar('test')).toBeNull(); |
| 68 | - expect($log.warn).toHaveBeenCalledWith('createToolbar: ID already exists'); | 69 | + expect($log.warn).toHaveBeenCalledWith('createToolbar: ' + |
| 70 | + 'duplicate ID given: [undefined]'); | ||
| 69 | }); | 71 | }); |
| 70 | 72 | ||
| 71 | it('should create an unpopulated toolbar', function () { | 73 | it('should create an unpopulated toolbar', function () { | ... | ... |
-
Please register or login to post a comment