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', |
| 204 | 'areFunctions', 'areFunctionsNonStrict', 'windowSize', 'find' |
| 205 | ])).toBeTruthy(); |
| 206 | }); |
| 207 | |
| 208 | |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 209 | // === Tests for windowSize() |
| 210 | it('windowSize(): noargs', function () { |
| 211 | var dim = fs.windowSize(); |
| 212 | expect(dim.width).toEqual(400); |
| 213 | expect(dim.height).toEqual(200); |
| 214 | }); |
| 215 | |
| 216 | it('windowSize(): adjust height', function () { |
| 217 | var dim = fs.windowSize(50); |
| 218 | expect(dim.width).toEqual(400); |
| 219 | expect(dim.height).toEqual(150); |
| 220 | }); |
| 221 | |
| 222 | it('windowSize(): adjust width', function () { |
| 223 | var dim = fs.windowSize(0, 50); |
| 224 | expect(dim.width).toEqual(350); |
| 225 | expect(dim.height).toEqual(200); |
| 226 | }); |
| 227 | |
| 228 | it('windowSize(): adjust width and height', function () { |
| 229 | var dim = fs.windowSize(101, 201); |
| 230 | expect(dim.width).toEqual(199); |
| 231 | expect(dim.height).toEqual(99); |
| 232 | }); |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 233 | |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 234 | |
Simon Hunt | 36c936b | 2015-01-30 16:07:18 -0800 | [diff] [blame^] | 235 | // === Tests for find() |
| 236 | var dataset = [ |
| 237 | { id: 'foo', name: 'Furby'}, |
| 238 | { id: 'bar', name: 'Barbi'}, |
| 239 | { id: 'baz', name: 'Basil'}, |
| 240 | { id: 'goo', name: 'Gabby'}, |
| 241 | { id: 'zoo', name: 'Zevvv'} |
| 242 | ]; |
| 243 | |
| 244 | it('should not find ooo', function () { |
| 245 | expect(fs.find('ooo', dataset)).toEqual(-1); |
| 246 | }); |
| 247 | it('should find foo', function () { |
| 248 | expect(fs.find('foo', dataset)).toEqual(0); |
| 249 | }); |
| 250 | it('should find zoo', function () { |
| 251 | expect(fs.find('zoo', dataset)).toEqual(4); |
| 252 | }); |
| 253 | |
| 254 | it('should not find Simon', function () { |
| 255 | expect(fs.find('Simon', dataset, 'name')).toEqual(-1); |
| 256 | }); |
| 257 | it('should find Furby', function () { |
| 258 | expect(fs.find('Furby', dataset, 'name')).toEqual(0); |
| 259 | }); |
| 260 | it('should find Zevvv', function () { |
| 261 | expect(fs.find('Zevvv', dataset, 'name')).toEqual(4); |
| 262 | }); |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 263 | }); |