toolbar-spec.js
9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
ONOS GUI -- Widget -- Toolbar Service - Unit Tests
*/
describe('factory: fw/widget/toolbar.js', function () {
var $log, fs, tbs, ps,
d3Elem;
beforeEach(module('onosWidget', 'onosUtil', 'onosLayer'));
beforeEach(inject(function (_$log_, FnService,
ToolbarService, PanelService) {
$log = _$log_;
fs = FnService;
tbs = ToolbarService;
ps = PanelService;
}));
beforeEach(function () {
d3Elem = d3.select('body').append('div').attr('id', 'floatpanels');
tbs.init();
ps.init();
});
afterEach(function () {
d3.select('#floatpanels').remove();
tbs.init();
ps.init();
});
it('should define ToolbarService', function () {
expect(tbs).toBeDefined();
});
it('should define api functions', function () {
expect(fs.areFunctions(tbs, [
'init', 'makeButton', 'makeToggle', 'makeRadio', 'separator',
'createToolbar'
])).toBeTruthy();
});
function toolbarSelection() {
return d3Elem.selectAll('.toolbar');
}
it('should have no toolbars to start', function () {
expect(toolbarSelection().size()).toBe(0);
});
it('should log a warning if no ID is given', function () {
spyOn($log, 'warn');
var tbar = tbs.createToolbar();
expect(tbar).toBeNull();
expect($log.warn).toHaveBeenCalledWith('createToolbar: no ID given');
expect(toolbarSelection().size()).toBe(0);
});
it('should log a warning if no tools are given', function () {
spyOn($log, 'warn');
var tbar = tbs.createToolbar(true);
expect(tbar).toBeNull();
expect($log.warn).toHaveBeenCalledWith('createToolbar: no tools given');
expect(toolbarSelection().size()).toBe(0);
});
it('should log a warning if tools are not an array', function () {
spyOn($log, 'warn');
var tbar = tbs.createToolbar(true, {});
expect(tbar).toBeNull();
expect($log.warn).toHaveBeenCalledWith('createToolbar: no tools given');
expect(toolbarSelection().size()).toBe(0);
});
it('should log a warning if tools array is empty', function () {
spyOn($log, 'warn');
var tbar = tbs.createToolbar(true, []);
expect(tbar).toBeNull();
expect($log.warn).toHaveBeenCalledWith('createToolbar: no tools given');
expect(toolbarSelection().size()).toBe(0);
});
it("should verify makeButton's returned object", function () {
var button = tbs.makeButton('foo', 'glyph-bar', function () {});
expect(button.t).toBe('btn');
expect(button.id).toBe('foo');
expect(button.gid).toBe('glyph-bar');
expect(fs.isF(button.cb)).toBeTruthy();
});
it("should verify makeToggle's returned object", function () {
var toggle = tbs.makeToggle('foo', 'glyph-bar', function () {});
expect(toggle.t).toBe('tog');
expect(toggle.id).toBe('foo');
expect(toggle.gid).toBe('glyph-bar');
expect(fs.isF(toggle.cb)).toBeTruthy();
});
it("should verify makeRadio's returned object", function () {
var rFoo0 = tbs.makeRadio('foo', 'glyph-foo0', function () {});
var rFoo1 = tbs.makeRadio('foo', 'glyph-foo1', function () {});
var rFoo2 = tbs.makeRadio('foo', 'glyph-foo2', function () {});
var rBar0 = tbs.makeRadio('bar', 'glyph-bar0', function () {});
var rBar1 = tbs.makeRadio('bar', 'glyph-bar1', function () {});
var rFoo3 = tbs.makeRadio('foo', 'glyph-foo3', function () {});
expect(rFoo0.t).toBe('rad');
expect(rFoo0.id).toBe('foo-0');
expect(rFoo0.rid).toBe('0');
expect(rFoo0.gid).toBe('glyph-foo0');
expect(fs.isF(rFoo0.cb)).toBeTruthy();
expect(rFoo1.t).toBe('rad');
expect(rFoo1.id).toBe('foo-1');
expect(rFoo1.rid).toBe('1');
expect(rFoo1.gid).toBe('glyph-foo1');
expect(fs.isF(rFoo1.cb)).toBeTruthy();
expect(rFoo2.t).toBe('rad');
expect(rFoo2.id).toBe('foo-2');
expect(rFoo2.rid).toBe('2');
expect(rFoo2.gid).toBe('glyph-foo2');
expect(fs.isF(rFoo2.cb)).toBeTruthy();
expect(rFoo3.t).toBe('rad');
expect(rFoo3.id).toBe('foo-3');
expect(rFoo3.rid).toBe('3');
expect(rFoo3.gid).toBe('glyph-foo3');
expect(fs.isF(rFoo3.cb)).toBeTruthy();
expect(rBar0.t).toBe('rad');
expect(rBar0.id).toBe('bar-0');
expect(rBar0.rid).toBe('0');
expect(rBar0.gid).toBe('glyph-bar0');
expect(fs.isF(rBar0.cb)).toBeTruthy();
expect(rBar1.t).toBe('rad');
expect(rBar1.id).toBe('bar-1');
expect(rBar1.rid).toBe('1');
expect(rBar1.gid).toBe('glyph-bar1');
expect(fs.isF(rBar1.cb)).toBeTruthy();
});
it("should verify separator's returned object", function () {
var separator = tbs.separator();
expect(separator.t).toBe('sep');
});
it('should log a warning if btn id is already in use', function () {
var tools = [
tbs.makeButton('id0', 'glyph-id0', function () {}),
tbs.makeButton('id0', 'glyph-id0', function () {})
];
spyOn($log, 'warn');
var tbar = tbs.createToolbar('someId', tools);
expect(tbar).toBeNull();
expect($log.warn).toHaveBeenCalledWith('createToolbar: item with id ' +
'id0 already exists');
expect(toolbarSelection().size()).toBe(0);
});
it('should log a warning if tog id is already in use', function () {
var tools = [
tbs.makeToggle('id0', 'glyph-id0', function () {}),
tbs.makeToggle('id1', 'glyph-id1', function () {}),
tbs.makeToggle('id0', 'glyph-id0', function () {})
];
spyOn($log, 'warn');
var tbar = tbs.createToolbar('someId', tools);
expect(tbar).toBeNull();
expect($log.warn).toHaveBeenCalledWith('createToolbar: item with id ' +
'id0 already exists');
expect(toolbarSelection().size()).toBe(0);
});
it('should create a toolbar', function () {
// need to create a button so it does not throw errors
var tools = [
tbs.makeButton('btn0', 'glyph0', function () {})
];
spyOn($log, 'warn');
var tbar = tbs.createToolbar('test', tools);
expect($log.warn).not.toHaveBeenCalled();
expect(toolbarSelection().size()).toBe(1);
});
it('should test multiple separators in a row', function () {
var tools = [
tbs.separator(),
tbs.separator(),
tbs.separator()
];
spyOn($log, 'warn');
var tbar = tbs.createToolbar('test', tools);
expect($log.warn).not.toHaveBeenCalled();
expect(toolbarSelection().size()).toBe(1);
});
it('should create a button div', function () {
var tools = [
tbs.makeButton('btn0', 'glyph0', function () {})
];
spyOn($log, 'warn');
var tbar = tbs.createToolbar('test', tools);
expect($log.warn).not.toHaveBeenCalled();
expect(toolbarSelection().size()).toBe(1);
expect(d3Elem.select('.btn')).toBeTruthy();
});
it('should create a toggle div', function () {
var tools = [
tbs.makeToggle('tog0', 'glyph0', function () {})
];
spyOn($log, 'warn');
var tbar = tbs.createToolbar('test', tools);
expect($log.warn).not.toHaveBeenCalled();
expect(toolbarSelection().size()).toBe(1);
expect(d3Elem.select('.tog')).toBeTruthy();
});
it('should create a radio btn div', function () {
var tools = [
tbs.makeRadio('rad0', 'glyph0', function () {})
];
spyOn($log, 'warn');
var tbar = tbs.createToolbar('test', tools);
expect($log.warn).not.toHaveBeenCalled();
expect(toolbarSelection().size()).toBe(1);
expect(d3Elem.select('.rad')).toBeTruthy();
});
it('should create a separator div', function () {
var tools = [
tbs.separator()
];
tbs.createToolbar('test', tools);
var sepDiv = d3Elem.select('.sep');
expect(sepDiv).toBeTruthy();
expect(sepDiv.style('width')).toBe('2px');
expect(sepDiv.style('border-width')).toBe('1px');
expect(sepDiv.style('border-style')).toBe('solid');
});
});