blob: 883899880250d70e58b28312bbb83989c4db2a36 [file] [log] [blame]
Simon Huntf3069722014-12-16 18:15:37 -08001/*
Simon Hunt8ead3a22015-01-06 11:00:15 -08002 * Copyright 2014,2015 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:
Simon Hunt48e61672015-01-30 14:48:25 -0800212 it('should define api functions', function () {
213 expect(fs.areFunctions(fs, [
214 'isF', 'isA', 'isS', 'isO', 'contains',
Simon Hunt31bb01f2015-03-31 16:50:41 -0700215 'areFunctions', 'areFunctionsNonStrict', 'windowSize', 'isMobile',
216 'find', 'inArray', 'removeFromArray', 'isEmptyObject', 'cap'
Simon Hunt48e61672015-01-30 14:48:25 -0800217 ])).toBeTruthy();
218 });
219
220
Simon Hunta11b4eb2015-01-28 16:20:50 -0800221 // === Tests for windowSize()
222 it('windowSize(): noargs', function () {
223 var dim = fs.windowSize();
224 expect(dim.width).toEqual(400);
225 expect(dim.height).toEqual(200);
226 });
227
228 it('windowSize(): adjust height', function () {
229 var dim = fs.windowSize(50);
230 expect(dim.width).toEqual(400);
231 expect(dim.height).toEqual(150);
232 });
233
234 it('windowSize(): adjust width', function () {
235 var dim = fs.windowSize(0, 50);
236 expect(dim.width).toEqual(350);
237 expect(dim.height).toEqual(200);
238 });
239
240 it('windowSize(): adjust width and height', function () {
241 var dim = fs.windowSize(101, 201);
242 expect(dim.width).toEqual(199);
243 expect(dim.height).toEqual(99);
244 });
Simon Hunt48e61672015-01-30 14:48:25 -0800245
Simon Hunt31bb01f2015-03-31 16:50:41 -0700246 // === Tests for isMobile()
247 var uaMap = {
248 chrome: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) " +
249 "AppleWebKit/537.36 (KHTML, like Gecko) " +
250 "Chrome/41.0.2272.89 Safari/537.36",
251
252 iPad: "Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) " +
253 "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " +
254 "Mobile/11A465 Safari/9537.53",
255
256 iPhone: "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) " +
257 "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " +
258 "Mobile/11A465 Safari/9537.53"
259 };
260
261 function setUa(key) {
262 var str = uaMap[key];
263 expect(str).toBeTruthy();
264 mockWindow.navigator.userAgent = str;
265 }
266
267 it('isMobile(): should be false for Chrome on Mac OS X', function () {
268 setUa('chrome');
269 expect(fs.isMobile()).toBe(false);
270 });
271 it('isMobile(): should be true for Safari on iPad', function () {
272 setUa('iPad');
273 expect(fs.isMobile()).toBe(true);
274 });
275 it('isMobile(): should be true for Safari on iPhone', function () {
276 setUa('iPhone');
277 expect(fs.isMobile()).toBe(true);
278 });
Simon Hunt48e61672015-01-30 14:48:25 -0800279
Simon Hunt36c936b2015-01-30 16:07:18 -0800280 // === Tests for find()
281 var dataset = [
282 { id: 'foo', name: 'Furby'},
283 { id: 'bar', name: 'Barbi'},
284 { id: 'baz', name: 'Basil'},
285 { id: 'goo', name: 'Gabby'},
286 { id: 'zoo', name: 'Zevvv'}
287 ];
288
289 it('should not find ooo', function () {
290 expect(fs.find('ooo', dataset)).toEqual(-1);
291 });
292 it('should find foo', function () {
293 expect(fs.find('foo', dataset)).toEqual(0);
294 });
295 it('should find zoo', function () {
296 expect(fs.find('zoo', dataset)).toEqual(4);
297 });
298
299 it('should not find Simon', function () {
300 expect(fs.find('Simon', dataset, 'name')).toEqual(-1);
301 });
302 it('should find Furby', function () {
303 expect(fs.find('Furby', dataset, 'name')).toEqual(0);
304 });
305 it('should find Zevvv', function () {
306 expect(fs.find('Zevvv', dataset, 'name')).toEqual(4);
307 });
Simon Hunt205099e2015-02-07 13:12:01 -0800308
309
310 // === Tests for inArray()
311 var objRef = { x:1, y:2 },
312 array = [1, 3.14, 'hey', objRef, 'there', true],
313 array2 = ['b', 'a', 'd', 'a', 's', 's'];
314
315 it('should return -1 on non-arrays', function () {
316 expect(fs.inArray(1, {x:1})).toEqual(-1);
317 });
318 it('should not find HOO', function () {
319 expect(fs.inArray('HOO', array)).toEqual(-1);
320 });
321 it('should find 1', function () {
322 expect(fs.inArray(1, array)).toEqual(0);
323 });
324 it('should find pi', function () {
325 expect(fs.inArray(3.14, array)).toEqual(1);
326 });
327 it('should find hey', function () {
328 expect(fs.inArray('hey', array)).toEqual(2);
329 });
330 it('should find the object', function () {
331 expect(fs.inArray(objRef, array)).toEqual(3);
332 });
333 it('should find there', function () {
334 expect(fs.inArray('there', array)).toEqual(4);
335 });
336 it('should find true', function () {
337 expect(fs.inArray(true, array)).toEqual(5);
338 });
339
340 it('should find the first occurrence A', function () {
341 expect(fs.inArray('a', array2)).toEqual(1);
342 });
343 it('should find the first occurrence S', function () {
344 expect(fs.inArray('s', array2)).toEqual(4);
345 });
346 it('should not find X', function () {
347 expect(fs.inArray('x', array2)).toEqual(-1);
348 });
349
350 // === Tests for removeFromArray()
351 it('should ignore non-arrays', function () {
352 expect(fs.removeFromArray(1, {x:1})).toBe(false);
353 });
354 it('should keep the array the same, for non-match', function () {
355 var array = [1, 2, 3];
356 expect(fs.removeFromArray(4, array)).toBe(false);
357 expect(array).toEqual([1, 2, 3]);
358 });
359 it('should remove a value', function () {
360 var array = [1, 2, 3];
361 expect(fs.removeFromArray(2, array)).toBe(true);
362 expect(array).toEqual([1, 3]);
363 });
364 it('should remove the first occurrence', function () {
365 var array = ['x', 'y', 'z', 'z', 'y'];
366 expect(fs.removeFromArray('y', array)).toBe(true);
367 expect(array).toEqual(['x', 'z', 'z', 'y']);
368 expect(fs.removeFromArray('x', array)).toBe(true);
369 expect(array).toEqual(['z', 'z', 'y']);
370 });
371
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700372 // === Tests for isEmptyObject()
373 it('should return true if an object is empty', function () {
374 expect(fs.isEmptyObject({})).toBe(true);
375 });
376 it('should return false if an object is not empty', function () {
377 expect(fs.isEmptyObject({foo: 'bar'})).toBe(false);
378 });
379
Simon Hunt27a5cc82015-02-19 14:49:55 -0800380 // === Tests for cap()
381 it('should ignore non-alpha', function () {
382 expect(fs.cap('123')).toEqual('123');
383 });
384 it('should capitalize first char', function () {
385 expect(fs.cap('Foo')).toEqual('Foo');
386 expect(fs.cap('foo')).toEqual('Foo');
387 expect(fs.cap('foo bar')).toEqual('Foo bar');
388 });
389
Simon Huntf3069722014-12-16 18:15:37 -0800390});