Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -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 -- Button Service - Unit Tests |
| 19 | */ |
| 20 | describe('factory: fw/widget/button.js', function () { |
| 21 | var $log, fs, bns, gs, |
| 22 | d3Elem; |
| 23 | |
| 24 | beforeEach(module('onosWidget', 'onosSvg')); |
| 25 | |
| 26 | beforeEach(inject(function (_$log_, FnService, |
| 27 | ButtonService, GlyphService) { |
| 28 | $log = _$log_; |
| 29 | fs = FnService; |
| 30 | bns = ButtonService; |
| 31 | gs = GlyphService; |
| 32 | })); |
| 33 | |
| 34 | beforeEach(function () { |
| 35 | gs.init(); |
| 36 | d3Elem = d3.select('body').append('div').attr('id', 'testToolbar'); |
| 37 | }); |
| 38 | |
| 39 | afterEach(function () { |
| 40 | d3.select('#testToolbar').remove(); |
| 41 | }); |
| 42 | |
| 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 () { |
| 54 | var btn = bns.button(d3Elem, 'tbar0-btn-0', 'crown', function () {}); |
| 55 | expect((btn.el).classed('btn')).toBeTruthy(); |
| 56 | expect((btn.el).attr('id')).toBe('tbar0-btn-0'); |
| 57 | expect((btn.el).select('svg')).toBeTruthy(); |
| 58 | expect((btn.el).select('use')).toBeTruthy(); |
| 59 | expect((btn.el).select('use').classed('glyph')).toBeTruthy(); |
| 60 | expect((btn.el).select('use').attr('xlink:href')).toBe('#crown'); |
| 61 | }); |
| 62 | |
| 63 | it('should not append button to an undefined div', function () { |
| 64 | spyOn($log, 'warn'); |
| 65 | expect(bns.button(undefined, 'id', 'gid', function () {})).toBeNull(); |
| 66 | expect($log.warn).toHaveBeenCalledWith('Button cannot append to div'); |
| 67 | }); |
| 68 | |
| 69 | it('should verify button callback', function () { |
| 70 | var count = 0; |
| 71 | function cb() { count++; } |
| 72 | var btn = bns.button(d3Elem, 'test', 'nothing', cb); |
| 73 | expect(count).toBe(0); |
| 74 | btn.click(); |
| 75 | expect(count).toBe(1); |
| 76 | }); |
| 77 | |
| 78 | it('should ignore non-function callbacks button', function () { |
| 79 | var count = 0; |
| 80 | var btn = bns.button(d3Elem, 'test', 'nothing', 'foo'); |
| 81 | expect(count).toBe(0); |
| 82 | btn.click(); |
| 83 | expect(count).toBe(0); |
| 84 | }); |
| 85 | |
| 86 | it('should verify toggle glyph', function () { |
| 87 | var tog = bns.toggle(d3Elem, 'tbar0-tog-0', 'crown', function () {}); |
| 88 | expect((tog.el).classed('tog')).toBeTruthy(); |
| 89 | expect((tog.el).attr('id')).toBe('tbar0-tog-0'); |
| 90 | expect((tog.el).select('svg')).toBeTruthy(); |
| 91 | expect((tog.el).select('use')).toBeTruthy(); |
| 92 | expect((tog.el).select('use').classed('glyph')).toBeTruthy(); |
| 93 | expect((tog.el).select('use').attr('xlink:href')).toBe('#crown'); |
| 94 | }); |
| 95 | |
| 96 | it('should toggle the selected state', function () { |
| 97 | var tog = bns.toggle(d3Elem, 'test', 'nothing'); |
| 98 | expect(tog.selected()).toBe(false); |
| 99 | tog.toggle(); |
| 100 | expect(tog.selected()).toBe(true); |
| 101 | tog.toggle(); |
| 102 | expect(tog.selected()).toBe(false); |
| 103 | }); |
| 104 | |
| 105 | it('should set toggle state', function () { |
| 106 | var tog = bns.toggle(d3Elem, 'test', 'nothing'); |
| 107 | tog.toggle(true); |
| 108 | expect(tog.selected()).toBe(true); |
| 109 | tog.toggle(); |
| 110 | expect(tog.selected()).toBe(false); |
| 111 | tog.toggle('truthy string'); |
| 112 | expect(tog.selected()).toBe(true); |
| 113 | tog.toggle(null); |
| 114 | expect(tog.selected()).toBe(false); |
| 115 | tog.toggle(''); |
| 116 | expect(tog.selected()).toBe(false); |
| 117 | }); |
| 118 | |
| 119 | it('should not append toggle to an undefined div', function () { |
| 120 | spyOn($log, 'warn'); |
| 121 | expect(bns.toggle(undefined, 'id', 'gid', function () {})).toBeNull(); |
| 122 | expect($log.warn).toHaveBeenCalledWith('Toggle cannot append to div'); |
| 123 | }); |
| 124 | |
| 125 | }); |