Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 1 | /* |
Simon Hunt | 8ead3a2 | 2015-01-06 11:00:15 -0800 | [diff] [blame] | 2 | * Copyright 2014,2015 Open Networking Laboratory |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 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 | /* |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 18 | ONOS GUI -- Util -- General Purpose Functions - Unit Tests |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 19 | */ |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 20 | describe('factory: fw/util/fn.js', function() { |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 21 | var $window, |
| 22 | fs, |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 23 | someFunction = function () {}, |
| 24 | someArray = [1, 2, 3], |
| 25 | someObject = { foo: 'bar'}, |
| 26 | someNumber = 42, |
| 27 | someString = 'xyyzy', |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 28 | someDate = new Date(), |
| 29 | stringArray = ['foo', 'bar']; |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 30 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 31 | beforeEach(module('onosUtil')); |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 32 | |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 33 | beforeEach(inject(function (_$window_, FnService) { |
| 34 | $window = _$window_; |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 35 | fs = FnService; |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 36 | |
| 37 | $window.innerWidth = 400; |
| 38 | $window.innerHeight = 200; |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 39 | })); |
| 40 | |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 41 | // === Tests for isF() |
| 42 | it('isF(): null for undefined', function () { |
| 43 | expect(fs.isF(undefined)).toBeNull(); |
| 44 | }); |
| 45 | it('isF(): null for null', function () { |
| 46 | expect(fs.isF(null)).toBeNull(); |
| 47 | }); |
| 48 | it('isF(): the reference for function', function () { |
| 49 | expect(fs.isF(someFunction)).toBe(someFunction); |
| 50 | }); |
| 51 | it('isF(): null for string', function () { |
| 52 | expect(fs.isF(someString)).toBeNull(); |
| 53 | }); |
| 54 | it('isF(): null for number', function () { |
| 55 | expect(fs.isF(someNumber)).toBeNull(); |
| 56 | }); |
| 57 | it('isF(): null for Date', function () { |
| 58 | expect(fs.isF(someDate)).toBeNull(); |
| 59 | }); |
| 60 | it('isF(): null for array', function () { |
| 61 | expect(fs.isF(someArray)).toBeNull(); |
| 62 | }); |
| 63 | it('isF(): null for object', function () { |
| 64 | expect(fs.isF(someObject)).toBeNull(); |
| 65 | }); |
| 66 | |
| 67 | |
| 68 | // === Tests for isA() |
| 69 | it('isA(): null for undefined', function () { |
| 70 | expect(fs.isA(undefined)).toBeNull(); |
| 71 | }); |
| 72 | it('isA(): null for null', function () { |
| 73 | expect(fs.isA(null)).toBeNull(); |
| 74 | }); |
| 75 | it('isA(): null for function', function () { |
| 76 | expect(fs.isA(someFunction)).toBeNull(); |
| 77 | }); |
| 78 | it('isA(): null for string', function () { |
| 79 | expect(fs.isA(someString)).toBeNull(); |
| 80 | }); |
| 81 | it('isA(): null for number', function () { |
| 82 | expect(fs.isA(someNumber)).toBeNull(); |
| 83 | }); |
| 84 | it('isA(): null for Date', function () { |
| 85 | expect(fs.isA(someDate)).toBeNull(); |
| 86 | }); |
| 87 | it('isA(): the reference for array', function () { |
| 88 | expect(fs.isA(someArray)).toBe(someArray); |
| 89 | }); |
| 90 | it('isA(): null for object', function () { |
| 91 | expect(fs.isA(someObject)).toBeNull(); |
| 92 | }); |
| 93 | |
| 94 | |
| 95 | // === Tests for isS() |
| 96 | it('isS(): null for undefined', function () { |
| 97 | expect(fs.isS(undefined)).toBeNull(); |
| 98 | }); |
| 99 | it('isS(): null for null', function () { |
| 100 | expect(fs.isS(null)).toBeNull(); |
| 101 | }); |
| 102 | it('isS(): null for function', function () { |
| 103 | expect(fs.isS(someFunction)).toBeNull(); |
| 104 | }); |
| 105 | it('isS(): the reference for string', function () { |
| 106 | expect(fs.isS(someString)).toBe(someString); |
| 107 | }); |
| 108 | it('isS(): null for number', function () { |
| 109 | expect(fs.isS(someNumber)).toBeNull(); |
| 110 | }); |
| 111 | it('isS(): null for Date', function () { |
| 112 | expect(fs.isS(someDate)).toBeNull(); |
| 113 | }); |
| 114 | it('isS(): null for array', function () { |
| 115 | expect(fs.isS(someArray)).toBeNull(); |
| 116 | }); |
| 117 | it('isS(): null for object', function () { |
| 118 | expect(fs.isS(someObject)).toBeNull(); |
| 119 | }); |
| 120 | |
| 121 | |
| 122 | // === Tests for isO() |
| 123 | it('isO(): null for undefined', function () { |
| 124 | expect(fs.isO(undefined)).toBeNull(); |
| 125 | }); |
| 126 | it('isO(): null for null', function () { |
| 127 | expect(fs.isO(null)).toBeNull(); |
| 128 | }); |
| 129 | it('isO(): null for function', function () { |
| 130 | expect(fs.isO(someFunction)).toBeNull(); |
| 131 | }); |
| 132 | it('isO(): null for string', function () { |
| 133 | expect(fs.isO(someString)).toBeNull(); |
| 134 | }); |
| 135 | it('isO(): null for number', function () { |
| 136 | expect(fs.isO(someNumber)).toBeNull(); |
| 137 | }); |
| 138 | it('isO(): null for Date', function () { |
| 139 | expect(fs.isO(someDate)).toBeNull(); |
| 140 | }); |
| 141 | it('isO(): null for array', function () { |
| 142 | expect(fs.isO(someArray)).toBeNull(); |
| 143 | }); |
| 144 | it('isO(): the reference for object', function () { |
| 145 | expect(fs.isO(someObject)).toBe(someObject); |
| 146 | }); |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 147 | |
| 148 | // === Tests for contains() |
| 149 | it('contains(): false for improper args', function () { |
| 150 | expect(fs.contains()).toBeFalsy(); |
| 151 | }); |
| 152 | it('contains(): false for non-array', function () { |
| 153 | expect(fs.contains(null, 1)).toBeFalsy(); |
| 154 | }); |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 155 | it('contains(): true for contained item', function () { |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 156 | expect(fs.contains(someArray, 1)).toBeTruthy(); |
| 157 | expect(fs.contains(stringArray, 'bar')).toBeTruthy(); |
| 158 | }); |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 159 | it('contains(): false for non-contained item', function () { |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 160 | expect(fs.contains(someArray, 109)).toBeFalsy(); |
| 161 | expect(fs.contains(stringArray, 'zonko')).toBeFalsy(); |
| 162 | }); |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 163 | |
| 164 | // === Tests for areFunctions() |
| 165 | it('areFunctions(): false for non-array', function () { |
| 166 | expect(fs.areFunctions({}, 'not-an-array')).toBeFalsy(); |
| 167 | }); |
| 168 | it('areFunctions(): true for empty-array', function () { |
| 169 | expect(fs.areFunctions({}, [])).toBeTruthy(); |
| 170 | }); |
| 171 | it('areFunctions(): true for some api', function () { |
| 172 | expect(fs.areFunctions({ |
| 173 | a: function () {}, |
| 174 | b: function () {} |
| 175 | }, ['b', 'a'])).toBeTruthy(); |
| 176 | }); |
| 177 | it('areFunctions(): false for some other api', function () { |
| 178 | expect(fs.areFunctions({ |
| 179 | a: function () {}, |
| 180 | b: 'not-a-function' |
| 181 | }, ['b', 'a'])).toBeFalsy(); |
| 182 | }); |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 183 | it('areFunctions(): extraneous stuff NOT ignored', function () { |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 184 | expect(fs.areFunctions({ |
| 185 | a: function () {}, |
| 186 | b: function () {}, |
| 187 | c: 1, |
| 188 | d: 'foo' |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 189 | }, ['a', 'b'])).toBeFalsy(); |
| 190 | }); |
| 191 | it('areFunctions(): extraneous stuff ignored (alternate fn)', function () { |
| 192 | expect(fs.areFunctionsNonStrict({ |
| 193 | a: function () {}, |
| 194 | b: function () {}, |
| 195 | c: 1, |
| 196 | d: 'foo' |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 197 | }, ['a', 'b'])).toBeTruthy(); |
| 198 | }); |
| 199 | |
Simon Hunt | 36c936b | 2015-01-30 16:07:18 -0800 | [diff] [blame] | 200 | // == use the now-tested areFunctions() on our own api: |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 201 | it('should define api functions', function () { |
| 202 | expect(fs.areFunctions(fs, [ |
| 203 | 'isF', 'isA', 'isS', 'isO', 'contains', |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 204 | 'areFunctions', 'areFunctionsNonStrict', 'windowSize', 'find', |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 205 | 'inArray', 'removeFromArray', 'isEmptyObject', 'cap' |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 206 | ])).toBeTruthy(); |
| 207 | }); |
| 208 | |
| 209 | |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 210 | // === Tests for windowSize() |
| 211 | it('windowSize(): noargs', function () { |
| 212 | var dim = fs.windowSize(); |
| 213 | expect(dim.width).toEqual(400); |
| 214 | expect(dim.height).toEqual(200); |
| 215 | }); |
| 216 | |
| 217 | it('windowSize(): adjust height', function () { |
| 218 | var dim = fs.windowSize(50); |
| 219 | expect(dim.width).toEqual(400); |
| 220 | expect(dim.height).toEqual(150); |
| 221 | }); |
| 222 | |
| 223 | it('windowSize(): adjust width', function () { |
| 224 | var dim = fs.windowSize(0, 50); |
| 225 | expect(dim.width).toEqual(350); |
| 226 | expect(dim.height).toEqual(200); |
| 227 | }); |
| 228 | |
| 229 | it('windowSize(): adjust width and height', function () { |
| 230 | var dim = fs.windowSize(101, 201); |
| 231 | expect(dim.width).toEqual(199); |
| 232 | expect(dim.height).toEqual(99); |
| 233 | }); |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 234 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 235 | |
Simon Hunt | 36c936b | 2015-01-30 16:07:18 -0800 | [diff] [blame] | 236 | // === Tests for find() |
| 237 | var dataset = [ |
| 238 | { id: 'foo', name: 'Furby'}, |
| 239 | { id: 'bar', name: 'Barbi'}, |
| 240 | { id: 'baz', name: 'Basil'}, |
| 241 | { id: 'goo', name: 'Gabby'}, |
| 242 | { id: 'zoo', name: 'Zevvv'} |
| 243 | ]; |
| 244 | |
| 245 | it('should not find ooo', function () { |
| 246 | expect(fs.find('ooo', dataset)).toEqual(-1); |
| 247 | }); |
| 248 | it('should find foo', function () { |
| 249 | expect(fs.find('foo', dataset)).toEqual(0); |
| 250 | }); |
| 251 | it('should find zoo', function () { |
| 252 | expect(fs.find('zoo', dataset)).toEqual(4); |
| 253 | }); |
| 254 | |
| 255 | it('should not find Simon', function () { |
| 256 | expect(fs.find('Simon', dataset, 'name')).toEqual(-1); |
| 257 | }); |
| 258 | it('should find Furby', function () { |
| 259 | expect(fs.find('Furby', dataset, 'name')).toEqual(0); |
| 260 | }); |
| 261 | it('should find Zevvv', function () { |
| 262 | expect(fs.find('Zevvv', dataset, 'name')).toEqual(4); |
| 263 | }); |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 264 | |
| 265 | |
| 266 | // === Tests for inArray() |
| 267 | var objRef = { x:1, y:2 }, |
| 268 | array = [1, 3.14, 'hey', objRef, 'there', true], |
| 269 | array2 = ['b', 'a', 'd', 'a', 's', 's']; |
| 270 | |
| 271 | it('should return -1 on non-arrays', function () { |
| 272 | expect(fs.inArray(1, {x:1})).toEqual(-1); |
| 273 | }); |
| 274 | it('should not find HOO', function () { |
| 275 | expect(fs.inArray('HOO', array)).toEqual(-1); |
| 276 | }); |
| 277 | it('should find 1', function () { |
| 278 | expect(fs.inArray(1, array)).toEqual(0); |
| 279 | }); |
| 280 | it('should find pi', function () { |
| 281 | expect(fs.inArray(3.14, array)).toEqual(1); |
| 282 | }); |
| 283 | it('should find hey', function () { |
| 284 | expect(fs.inArray('hey', array)).toEqual(2); |
| 285 | }); |
| 286 | it('should find the object', function () { |
| 287 | expect(fs.inArray(objRef, array)).toEqual(3); |
| 288 | }); |
| 289 | it('should find there', function () { |
| 290 | expect(fs.inArray('there', array)).toEqual(4); |
| 291 | }); |
| 292 | it('should find true', function () { |
| 293 | expect(fs.inArray(true, array)).toEqual(5); |
| 294 | }); |
| 295 | |
| 296 | it('should find the first occurrence A', function () { |
| 297 | expect(fs.inArray('a', array2)).toEqual(1); |
| 298 | }); |
| 299 | it('should find the first occurrence S', function () { |
| 300 | expect(fs.inArray('s', array2)).toEqual(4); |
| 301 | }); |
| 302 | it('should not find X', function () { |
| 303 | expect(fs.inArray('x', array2)).toEqual(-1); |
| 304 | }); |
| 305 | |
| 306 | // === Tests for removeFromArray() |
| 307 | it('should ignore non-arrays', function () { |
| 308 | expect(fs.removeFromArray(1, {x:1})).toBe(false); |
| 309 | }); |
| 310 | it('should keep the array the same, for non-match', function () { |
| 311 | var array = [1, 2, 3]; |
| 312 | expect(fs.removeFromArray(4, array)).toBe(false); |
| 313 | expect(array).toEqual([1, 2, 3]); |
| 314 | }); |
| 315 | it('should remove a value', function () { |
| 316 | var array = [1, 2, 3]; |
| 317 | expect(fs.removeFromArray(2, array)).toBe(true); |
| 318 | expect(array).toEqual([1, 3]); |
| 319 | }); |
| 320 | it('should remove the first occurrence', function () { |
| 321 | var array = ['x', 'y', 'z', 'z', 'y']; |
| 322 | expect(fs.removeFromArray('y', array)).toBe(true); |
| 323 | expect(array).toEqual(['x', 'z', 'z', 'y']); |
| 324 | expect(fs.removeFromArray('x', array)).toBe(true); |
| 325 | expect(array).toEqual(['z', 'z', 'y']); |
| 326 | }); |
| 327 | |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 328 | // === Tests for isEmptyObject() |
| 329 | it('should return true if an object is empty', function () { |
| 330 | expect(fs.isEmptyObject({})).toBe(true); |
| 331 | }); |
| 332 | it('should return false if an object is not empty', function () { |
| 333 | expect(fs.isEmptyObject({foo: 'bar'})).toBe(false); |
| 334 | }); |
| 335 | |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 336 | // === Tests for cap() |
| 337 | it('should ignore non-alpha', function () { |
| 338 | expect(fs.cap('123')).toEqual('123'); |
| 339 | }); |
| 340 | it('should capitalize first char', function () { |
| 341 | expect(fs.cap('Foo')).toEqual('Foo'); |
| 342 | expect(fs.cap('foo')).toEqual('Foo'); |
| 343 | expect(fs.cap('foo bar')).toEqual('Foo bar'); |
| 344 | }); |
| 345 | |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 346 | }); |