blob: efd9c9158083185b93104f7e44604a5e009b72b8 [file] [log] [blame]
Simon Huntf3069722014-12-16 18:15:37 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Simon Huntf3069722014-12-16 18:15:37 -08003 *
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/*
Simon Huntdc6362a2014-12-18 19:55:23 -080018 ONOS GUI -- Util -- General Purpose Functions - Unit Tests
Simon Huntf3069722014-12-16 18:15:37 -080019 */
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080020describe('factory: fw/util/fn.js', function() {
Simon Hunta11b4eb2015-01-28 16:20:50 -080021 var $window,
22 fs,
Simon Huntf3069722014-12-16 18:15:37 -080023 someFunction = function () {},
24 someArray = [1, 2, 3],
25 someObject = { foo: 'bar'},
26 someNumber = 42,
27 someString = 'xyyzy',
Simon Hunt59df0b22014-12-17 10:32:25 -080028 someDate = new Date(),
29 stringArray = ['foo', 'bar'];
Simon Huntf3069722014-12-16 18:15:37 -080030
Simon Huntdc6362a2014-12-18 19:55:23 -080031 beforeEach(module('onosUtil'));
Simon Huntf3069722014-12-16 18:15:37 -080032
Simon Hunt31bb01f2015-03-31 16:50:41 -070033 var mockWindow = {
34 innerWidth: 400,
35 innerHeight: 200,
36 navigator: {
37 userAgent: 'defaultUA'
38 }
39 };
40
41 beforeEach(function () {
42 module(function ($provide) {
43 $provide.value('$window', mockWindow);
44 });
45 });
46
Simon Hunta11b4eb2015-01-28 16:20:50 -080047 beforeEach(inject(function (_$window_, FnService) {
48 $window = _$window_;
Simon Huntf3069722014-12-16 18:15:37 -080049 fs = FnService;
50 }));
51
Simon Huntf3069722014-12-16 18:15:37 -080052 // === Tests for isF()
53 it('isF(): null for undefined', function () {
54 expect(fs.isF(undefined)).toBeNull();
55 });
56 it('isF(): null for null', function () {
57 expect(fs.isF(null)).toBeNull();
58 });
59 it('isF(): the reference for function', function () {
60 expect(fs.isF(someFunction)).toBe(someFunction);
61 });
62 it('isF(): null for string', function () {
63 expect(fs.isF(someString)).toBeNull();
64 });
65 it('isF(): null for number', function () {
66 expect(fs.isF(someNumber)).toBeNull();
67 });
68 it('isF(): null for Date', function () {
69 expect(fs.isF(someDate)).toBeNull();
70 });
71 it('isF(): null for array', function () {
72 expect(fs.isF(someArray)).toBeNull();
73 });
74 it('isF(): null for object', function () {
75 expect(fs.isF(someObject)).toBeNull();
76 });
77
78
79 // === Tests for isA()
80 it('isA(): null for undefined', function () {
81 expect(fs.isA(undefined)).toBeNull();
82 });
83 it('isA(): null for null', function () {
84 expect(fs.isA(null)).toBeNull();
85 });
86 it('isA(): null for function', function () {
87 expect(fs.isA(someFunction)).toBeNull();
88 });
89 it('isA(): null for string', function () {
90 expect(fs.isA(someString)).toBeNull();
91 });
92 it('isA(): null for number', function () {
93 expect(fs.isA(someNumber)).toBeNull();
94 });
95 it('isA(): null for Date', function () {
96 expect(fs.isA(someDate)).toBeNull();
97 });
98 it('isA(): the reference for array', function () {
99 expect(fs.isA(someArray)).toBe(someArray);
100 });
101 it('isA(): null for object', function () {
102 expect(fs.isA(someObject)).toBeNull();
103 });
104
105
106 // === Tests for isS()
107 it('isS(): null for undefined', function () {
108 expect(fs.isS(undefined)).toBeNull();
109 });
110 it('isS(): null for null', function () {
111 expect(fs.isS(null)).toBeNull();
112 });
113 it('isS(): null for function', function () {
114 expect(fs.isS(someFunction)).toBeNull();
115 });
116 it('isS(): the reference for string', function () {
117 expect(fs.isS(someString)).toBe(someString);
118 });
119 it('isS(): null for number', function () {
120 expect(fs.isS(someNumber)).toBeNull();
121 });
122 it('isS(): null for Date', function () {
123 expect(fs.isS(someDate)).toBeNull();
124 });
125 it('isS(): null for array', function () {
126 expect(fs.isS(someArray)).toBeNull();
127 });
128 it('isS(): null for object', function () {
129 expect(fs.isS(someObject)).toBeNull();
130 });
131
132
133 // === Tests for isO()
134 it('isO(): null for undefined', function () {
135 expect(fs.isO(undefined)).toBeNull();
136 });
137 it('isO(): null for null', function () {
138 expect(fs.isO(null)).toBeNull();
139 });
140 it('isO(): null for function', function () {
141 expect(fs.isO(someFunction)).toBeNull();
142 });
143 it('isO(): null for string', function () {
144 expect(fs.isO(someString)).toBeNull();
145 });
146 it('isO(): null for number', function () {
147 expect(fs.isO(someNumber)).toBeNull();
148 });
149 it('isO(): null for Date', function () {
150 expect(fs.isO(someDate)).toBeNull();
151 });
152 it('isO(): null for array', function () {
153 expect(fs.isO(someArray)).toBeNull();
154 });
155 it('isO(): the reference for object', function () {
156 expect(fs.isO(someObject)).toBe(someObject);
157 });
Simon Hunt59df0b22014-12-17 10:32:25 -0800158
159 // === Tests for contains()
160 it('contains(): false for improper args', function () {
161 expect(fs.contains()).toBeFalsy();
162 });
163 it('contains(): false for non-array', function () {
164 expect(fs.contains(null, 1)).toBeFalsy();
165 });
Simon Huntdc6362a2014-12-18 19:55:23 -0800166 it('contains(): true for contained item', function () {
Simon Hunt59df0b22014-12-17 10:32:25 -0800167 expect(fs.contains(someArray, 1)).toBeTruthy();
168 expect(fs.contains(stringArray, 'bar')).toBeTruthy();
169 });
Simon Huntdc6362a2014-12-18 19:55:23 -0800170 it('contains(): false for non-contained item', function () {
Simon Hunt59df0b22014-12-17 10:32:25 -0800171 expect(fs.contains(someArray, 109)).toBeFalsy();
172 expect(fs.contains(stringArray, 'zonko')).toBeFalsy();
173 });
Simon Hunt51fc40b2015-01-06 13:56:12 -0800174
175 // === Tests for areFunctions()
176 it('areFunctions(): false for non-array', function () {
177 expect(fs.areFunctions({}, 'not-an-array')).toBeFalsy();
178 });
179 it('areFunctions(): true for empty-array', function () {
180 expect(fs.areFunctions({}, [])).toBeTruthy();
181 });
182 it('areFunctions(): true for some api', function () {
183 expect(fs.areFunctions({
184 a: function () {},
185 b: function () {}
186 }, ['b', 'a'])).toBeTruthy();
187 });
188 it('areFunctions(): false for some other api', function () {
189 expect(fs.areFunctions({
190 a: function () {},
191 b: 'not-a-function'
192 }, ['b', 'a'])).toBeFalsy();
193 });
Simon Hunt48e61672015-01-30 14:48:25 -0800194 it('areFunctions(): extraneous stuff NOT ignored', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -0800195 expect(fs.areFunctions({
196 a: function () {},
197 b: function () {},
198 c: 1,
199 d: 'foo'
Simon Hunt48e61672015-01-30 14:48:25 -0800200 }, ['a', 'b'])).toBeFalsy();
201 });
202 it('areFunctions(): extraneous stuff ignored (alternate fn)', function () {
203 expect(fs.areFunctionsNonStrict({
204 a: function () {},
205 b: function () {},
206 c: 1,
207 d: 'foo'
Simon Hunt51fc40b2015-01-06 13:56:12 -0800208 }, ['a', 'b'])).toBeTruthy();
209 });
210
Simon Hunt36c936b2015-01-30 16:07:18 -0800211 // == use the now-tested areFunctions() on our own api:
Matteo Scandolo812aa5a2016-04-19 18:12:45 -0700212 xit('should define api functions', function () {
Simon Hunt48e61672015-01-30 14:48:25 -0800213 expect(fs.areFunctions(fs, [
214 'isF', 'isA', 'isS', 'isO', 'contains',
Simon Hunt31bb01f2015-03-31 16:50:41 -0700215 'areFunctions', 'areFunctionsNonStrict', 'windowSize', 'isMobile',
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700216 'find', 'inArray', 'removeFromArray', 'isEmptyObject', 'cap',
Simon Hunt96641372015-06-02 15:53:25 -0700217 'noPx', 'noPxStyle', 'endsWith', 'parseBitRate'
Simon Hunt48e61672015-01-30 14:48:25 -0800218 ])).toBeTruthy();
219 });
220
221
Simon Hunta11b4eb2015-01-28 16:20:50 -0800222 // === Tests for windowSize()
223 it('windowSize(): noargs', function () {
224 var dim = fs.windowSize();
225 expect(dim.width).toEqual(400);
226 expect(dim.height).toEqual(200);
227 });
228
229 it('windowSize(): adjust height', function () {
230 var dim = fs.windowSize(50);
231 expect(dim.width).toEqual(400);
232 expect(dim.height).toEqual(150);
233 });
234
235 it('windowSize(): adjust width', function () {
236 var dim = fs.windowSize(0, 50);
237 expect(dim.width).toEqual(350);
238 expect(dim.height).toEqual(200);
239 });
240
241 it('windowSize(): adjust width and height', function () {
242 var dim = fs.windowSize(101, 201);
243 expect(dim.width).toEqual(199);
244 expect(dim.height).toEqual(99);
245 });
Simon Hunt48e61672015-01-30 14:48:25 -0800246
Simon Hunt31bb01f2015-03-31 16:50:41 -0700247 // === Tests for isMobile()
248 var uaMap = {
249 chrome: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) " +
250 "AppleWebKit/537.36 (KHTML, like Gecko) " +
251 "Chrome/41.0.2272.89 Safari/537.36",
252
253 iPad: "Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) " +
254 "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " +
255 "Mobile/11A465 Safari/9537.53",
256
257 iPhone: "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) " +
258 "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " +
259 "Mobile/11A465 Safari/9537.53"
260 };
261
262 function setUa(key) {
263 var str = uaMap[key];
264 expect(str).toBeTruthy();
265 mockWindow.navigator.userAgent = str;
266 }
267
268 it('isMobile(): should be false for Chrome on Mac OS X', function () {
269 setUa('chrome');
270 expect(fs.isMobile()).toBe(false);
271 });
272 it('isMobile(): should be true for Safari on iPad', function () {
273 setUa('iPad');
274 expect(fs.isMobile()).toBe(true);
275 });
276 it('isMobile(): should be true for Safari on iPhone', function () {
277 setUa('iPhone');
278 expect(fs.isMobile()).toBe(true);
279 });
Simon Hunt48e61672015-01-30 14:48:25 -0800280
Simon Hunt36c936b2015-01-30 16:07:18 -0800281 // === Tests for find()
282 var dataset = [
283 { id: 'foo', name: 'Furby'},
284 { id: 'bar', name: 'Barbi'},
285 { id: 'baz', name: 'Basil'},
286 { id: 'goo', name: 'Gabby'},
287 { id: 'zoo', name: 'Zevvv'}
288 ];
289
290 it('should not find ooo', function () {
291 expect(fs.find('ooo', dataset)).toEqual(-1);
292 });
293 it('should find foo', function () {
294 expect(fs.find('foo', dataset)).toEqual(0);
295 });
296 it('should find zoo', function () {
297 expect(fs.find('zoo', dataset)).toEqual(4);
298 });
299
300 it('should not find Simon', function () {
301 expect(fs.find('Simon', dataset, 'name')).toEqual(-1);
302 });
303 it('should find Furby', function () {
304 expect(fs.find('Furby', dataset, 'name')).toEqual(0);
305 });
306 it('should find Zevvv', function () {
307 expect(fs.find('Zevvv', dataset, 'name')).toEqual(4);
308 });
Simon Hunt205099e2015-02-07 13:12:01 -0800309
310
311 // === Tests for inArray()
312 var objRef = { x:1, y:2 },
313 array = [1, 3.14, 'hey', objRef, 'there', true],
314 array2 = ['b', 'a', 'd', 'a', 's', 's'];
315
316 it('should return -1 on non-arrays', function () {
317 expect(fs.inArray(1, {x:1})).toEqual(-1);
318 });
319 it('should not find HOO', function () {
320 expect(fs.inArray('HOO', array)).toEqual(-1);
321 });
322 it('should find 1', function () {
323 expect(fs.inArray(1, array)).toEqual(0);
324 });
325 it('should find pi', function () {
326 expect(fs.inArray(3.14, array)).toEqual(1);
327 });
328 it('should find hey', function () {
329 expect(fs.inArray('hey', array)).toEqual(2);
330 });
331 it('should find the object', function () {
332 expect(fs.inArray(objRef, array)).toEqual(3);
333 });
334 it('should find there', function () {
335 expect(fs.inArray('there', array)).toEqual(4);
336 });
337 it('should find true', function () {
338 expect(fs.inArray(true, array)).toEqual(5);
339 });
340
341 it('should find the first occurrence A', function () {
342 expect(fs.inArray('a', array2)).toEqual(1);
343 });
344 it('should find the first occurrence S', function () {
345 expect(fs.inArray('s', array2)).toEqual(4);
346 });
347 it('should not find X', function () {
348 expect(fs.inArray('x', array2)).toEqual(-1);
349 });
350
351 // === Tests for removeFromArray()
352 it('should ignore non-arrays', function () {
353 expect(fs.removeFromArray(1, {x:1})).toBe(false);
354 });
355 it('should keep the array the same, for non-match', function () {
356 var array = [1, 2, 3];
357 expect(fs.removeFromArray(4, array)).toBe(false);
358 expect(array).toEqual([1, 2, 3]);
359 });
360 it('should remove a value', function () {
361 var array = [1, 2, 3];
362 expect(fs.removeFromArray(2, array)).toBe(true);
363 expect(array).toEqual([1, 3]);
364 });
365 it('should remove the first occurrence', function () {
366 var array = ['x', 'y', 'z', 'z', 'y'];
367 expect(fs.removeFromArray('y', array)).toBe(true);
368 expect(array).toEqual(['x', 'z', 'z', 'y']);
369 expect(fs.removeFromArray('x', array)).toBe(true);
370 expect(array).toEqual(['z', 'z', 'y']);
371 });
372
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700373 // === Tests for isEmptyObject()
374 it('should return true if an object is empty', function () {
375 expect(fs.isEmptyObject({})).toBe(true);
376 });
377 it('should return false if an object is not empty', function () {
378 expect(fs.isEmptyObject({foo: 'bar'})).toBe(false);
379 });
380
Simon Hunt27a5cc82015-02-19 14:49:55 -0800381 // === Tests for cap()
382 it('should ignore non-alpha', function () {
383 expect(fs.cap('123')).toEqual('123');
384 });
Matteo Scandolo812aa5a2016-04-19 18:12:45 -0700385 xit('should capitalize first char', function () {
Simon Hunt27a5cc82015-02-19 14:49:55 -0800386 expect(fs.cap('Foo')).toEqual('Foo');
387 expect(fs.cap('foo')).toEqual('Foo');
388 expect(fs.cap('foo bar')).toEqual('Foo bar');
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700389 expect(fs.cap('FOO BAR')).toEqual('Foo bar');
390 expect(fs.cap('foo Bar')).toEqual('Foo bar');
Simon Hunt27a5cc82015-02-19 14:49:55 -0800391 });
392
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700393 // === Tests for noPx()
394 it('should return the value without px suffix', function () {
395 expect(fs.noPx('10px')).toBe(10);
396 expect(fs.noPx('500px')).toBe(500);
397 expect(fs.noPx('-80px')).toBe(-80);
398 });
399
400 // === Tests for noPxStyle()
401 it("should give a style's property without px suffix", function () {
402 var d3Elem = d3.select('body')
403 .append('div')
404 .attr('id', 'fooElem')
405 .style({
406 width: '500px',
407 height: '200px',
408 'font-size': '12px'
409 });
410 expect(fs.noPxStyle(d3Elem, 'width')).toBe(500);
411 expect(fs.noPxStyle(d3Elem, 'height')).toBe(200);
412 expect(fs.noPxStyle(d3Elem, 'font-size')).toBe(12);
413 d3.select('#fooElem').remove();
414 });
415
Simon Hunt96641372015-06-02 15:53:25 -0700416 // === Tests for endsWith()
417 it('should return true if string ends with foo', function () {
418 expect(fs.endsWith("barfoo", "foo")).toBe(true);
419 });
420
421 it('should return false if string doesnt end with foo', function () {
422 expect(fs.endsWith("barfood", "foo")).toBe(false);
423 });
424
425 // === Tests for parseBitRate()
426 it('should return 5 - a', function () {
427 expect(fs.parseBitRate('5.47 KBps')).toBe(5);
428 });
429
430 it('should return 5 - b', function () {
431 expect(fs.parseBitRate('5. KBps')).toBe(5);
432 });
433
434 it('should return 5 - c', function () {
435 expect(fs.parseBitRate('5 KBps')).toBe(5);
436 });
437
438 it('should return 5 - d', function () {
439 expect(fs.parseBitRate('5 Kbps')).toBe(5);
440 });
441
442 it('should return 2001', function () {
443 expect(fs.parseBitRate('2,001.59 Gbps')).toBe(2001);
444 });
Simon Huntf3069722014-12-16 18:15:37 -0800445});
Simon Hunt96641372015-06-02 15:53:25 -0700446