Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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, |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 22 | $log, |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 23 | fs, |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 24 | someFunction = function () {}, |
| 25 | someArray = [1, 2, 3], |
| 26 | someObject = { foo: 'bar'}, |
| 27 | someNumber = 42, |
| 28 | someString = 'xyyzy', |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 29 | someDate = new Date(), |
| 30 | stringArray = ['foo', 'bar']; |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 31 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 32 | beforeEach(module('onosUtil')); |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 33 | |
Simon Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 34 | var mockWindow = { |
| 35 | innerWidth: 400, |
| 36 | innerHeight: 200, |
| 37 | navigator: { |
| 38 | userAgent: 'defaultUA' |
| 39 | } |
| 40 | }; |
| 41 | |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 42 | var mockLog = { |
| 43 | debug: function () {}, |
| 44 | info: function () {}, |
| 45 | warn: function () {}, |
| 46 | error: function () {} |
| 47 | }; |
| 48 | |
Simon Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 49 | beforeEach(function () { |
| 50 | module(function ($provide) { |
| 51 | $provide.value('$window', mockWindow); |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 52 | $provide.value('$log', mockLog); |
Simon Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 53 | }); |
| 54 | }); |
| 55 | |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 56 | beforeEach(inject(function (_$log_, _$window_, FnService) { |
| 57 | $log = _$log_; |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 58 | $window = _$window_; |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 59 | fs = FnService; |
| 60 | })); |
| 61 | |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 62 | // === Tests for isF() |
| 63 | it('isF(): null for undefined', function () { |
| 64 | expect(fs.isF(undefined)).toBeNull(); |
| 65 | }); |
| 66 | it('isF(): null for null', function () { |
| 67 | expect(fs.isF(null)).toBeNull(); |
| 68 | }); |
| 69 | it('isF(): the reference for function', function () { |
| 70 | expect(fs.isF(someFunction)).toBe(someFunction); |
| 71 | }); |
| 72 | it('isF(): null for string', function () { |
| 73 | expect(fs.isF(someString)).toBeNull(); |
| 74 | }); |
| 75 | it('isF(): null for number', function () { |
| 76 | expect(fs.isF(someNumber)).toBeNull(); |
| 77 | }); |
| 78 | it('isF(): null for Date', function () { |
| 79 | expect(fs.isF(someDate)).toBeNull(); |
| 80 | }); |
| 81 | it('isF(): null for array', function () { |
| 82 | expect(fs.isF(someArray)).toBeNull(); |
| 83 | }); |
| 84 | it('isF(): null for object', function () { |
| 85 | expect(fs.isF(someObject)).toBeNull(); |
| 86 | }); |
| 87 | |
| 88 | |
| 89 | // === Tests for isA() |
| 90 | it('isA(): null for undefined', function () { |
| 91 | expect(fs.isA(undefined)).toBeNull(); |
| 92 | }); |
| 93 | it('isA(): null for null', function () { |
| 94 | expect(fs.isA(null)).toBeNull(); |
| 95 | }); |
| 96 | it('isA(): null for function', function () { |
| 97 | expect(fs.isA(someFunction)).toBeNull(); |
| 98 | }); |
| 99 | it('isA(): null for string', function () { |
| 100 | expect(fs.isA(someString)).toBeNull(); |
| 101 | }); |
| 102 | it('isA(): null for number', function () { |
| 103 | expect(fs.isA(someNumber)).toBeNull(); |
| 104 | }); |
| 105 | it('isA(): null for Date', function () { |
| 106 | expect(fs.isA(someDate)).toBeNull(); |
| 107 | }); |
| 108 | it('isA(): the reference for array', function () { |
| 109 | expect(fs.isA(someArray)).toBe(someArray); |
| 110 | }); |
| 111 | it('isA(): null for object', function () { |
| 112 | expect(fs.isA(someObject)).toBeNull(); |
| 113 | }); |
| 114 | |
| 115 | |
| 116 | // === Tests for isS() |
| 117 | it('isS(): null for undefined', function () { |
| 118 | expect(fs.isS(undefined)).toBeNull(); |
| 119 | }); |
| 120 | it('isS(): null for null', function () { |
| 121 | expect(fs.isS(null)).toBeNull(); |
| 122 | }); |
| 123 | it('isS(): null for function', function () { |
| 124 | expect(fs.isS(someFunction)).toBeNull(); |
| 125 | }); |
| 126 | it('isS(): the reference for string', function () { |
| 127 | expect(fs.isS(someString)).toBe(someString); |
| 128 | }); |
| 129 | it('isS(): null for number', function () { |
| 130 | expect(fs.isS(someNumber)).toBeNull(); |
| 131 | }); |
| 132 | it('isS(): null for Date', function () { |
| 133 | expect(fs.isS(someDate)).toBeNull(); |
| 134 | }); |
| 135 | it('isS(): null for array', function () { |
| 136 | expect(fs.isS(someArray)).toBeNull(); |
| 137 | }); |
| 138 | it('isS(): null for object', function () { |
| 139 | expect(fs.isS(someObject)).toBeNull(); |
| 140 | }); |
| 141 | |
| 142 | |
| 143 | // === Tests for isO() |
| 144 | it('isO(): null for undefined', function () { |
| 145 | expect(fs.isO(undefined)).toBeNull(); |
| 146 | }); |
| 147 | it('isO(): null for null', function () { |
| 148 | expect(fs.isO(null)).toBeNull(); |
| 149 | }); |
| 150 | it('isO(): null for function', function () { |
| 151 | expect(fs.isO(someFunction)).toBeNull(); |
| 152 | }); |
| 153 | it('isO(): null for string', function () { |
| 154 | expect(fs.isO(someString)).toBeNull(); |
| 155 | }); |
| 156 | it('isO(): null for number', function () { |
| 157 | expect(fs.isO(someNumber)).toBeNull(); |
| 158 | }); |
| 159 | it('isO(): null for Date', function () { |
| 160 | expect(fs.isO(someDate)).toBeNull(); |
| 161 | }); |
| 162 | it('isO(): null for array', function () { |
| 163 | expect(fs.isO(someArray)).toBeNull(); |
| 164 | }); |
| 165 | it('isO(): the reference for object', function () { |
| 166 | expect(fs.isO(someObject)).toBe(someObject); |
| 167 | }); |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 168 | |
| 169 | // === Tests for contains() |
| 170 | it('contains(): false for improper args', function () { |
| 171 | expect(fs.contains()).toBeFalsy(); |
| 172 | }); |
| 173 | it('contains(): false for non-array', function () { |
| 174 | expect(fs.contains(null, 1)).toBeFalsy(); |
| 175 | }); |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 176 | it('contains(): true for contained item', function () { |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 177 | expect(fs.contains(someArray, 1)).toBeTruthy(); |
| 178 | expect(fs.contains(stringArray, 'bar')).toBeTruthy(); |
| 179 | }); |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 180 | it('contains(): false for non-contained item', function () { |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 181 | expect(fs.contains(someArray, 109)).toBeFalsy(); |
| 182 | expect(fs.contains(stringArray, 'zonko')).toBeFalsy(); |
| 183 | }); |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 184 | |
| 185 | // === Tests for areFunctions() |
| 186 | it('areFunctions(): false for non-array', function () { |
| 187 | expect(fs.areFunctions({}, 'not-an-array')).toBeFalsy(); |
| 188 | }); |
| 189 | it('areFunctions(): true for empty-array', function () { |
| 190 | expect(fs.areFunctions({}, [])).toBeTruthy(); |
| 191 | }); |
| 192 | it('areFunctions(): true for some api', function () { |
| 193 | expect(fs.areFunctions({ |
| 194 | a: function () {}, |
| 195 | b: function () {} |
| 196 | }, ['b', 'a'])).toBeTruthy(); |
| 197 | }); |
| 198 | it('areFunctions(): false for some other api', function () { |
| 199 | expect(fs.areFunctions({ |
| 200 | a: function () {}, |
| 201 | b: 'not-a-function' |
| 202 | }, ['b', 'a'])).toBeFalsy(); |
| 203 | }); |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 204 | it('areFunctions(): extraneous stuff NOT ignored', function () { |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 205 | expect(fs.areFunctions({ |
| 206 | a: function () {}, |
| 207 | b: function () {}, |
| 208 | c: 1, |
| 209 | d: 'foo' |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 210 | }, ['a', 'b'])).toBeFalsy(); |
| 211 | }); |
| 212 | it('areFunctions(): extraneous stuff ignored (alternate fn)', function () { |
| 213 | expect(fs.areFunctionsNonStrict({ |
| 214 | a: function () {}, |
| 215 | b: function () {}, |
| 216 | c: 1, |
| 217 | d: 'foo' |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 218 | }, ['a', 'b'])).toBeTruthy(); |
| 219 | }); |
| 220 | |
Simon Hunt | 36c936b | 2015-01-30 16:07:18 -0800 | [diff] [blame] | 221 | // == use the now-tested areFunctions() on our own api: |
Matteo Scandolo | 209c6c6 | 2016-05-21 10:08:57 -0700 | [diff] [blame] | 222 | it('should define api functions', function () { |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 223 | expect(fs.areFunctions(fs, [ |
| 224 | 'isF', 'isA', 'isS', 'isO', 'contains', |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 225 | 'areFunctions', 'areFunctionsNonStrict', 'windowSize', |
Matteo Scandolo | 209c6c6 | 2016-05-21 10:08:57 -0700 | [diff] [blame] | 226 | 'isMobile', 'isChrome', 'isSafari', 'isFirefox', |
| 227 | 'debugOn', 'debug', |
| 228 | 'find', 'inArray', 'removeFromArray', 'isEmptyObject', 'sameObjProps', 'containsObj', 'cap', |
Simon Hunt | 6985517 | 2017-03-31 09:48:25 -0700 | [diff] [blame] | 229 | 'eecode', 'noPx', 'noPxStyle', 'endsWith', 'addToTrie', 'removeFromTrie', 'trieLookup', |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 230 | 'classNames', 'extend', 'sanitize' |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 231 | ])).toBeTruthy(); |
| 232 | }); |
| 233 | |
| 234 | |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 235 | // === Tests for windowSize() |
| 236 | it('windowSize(): noargs', function () { |
| 237 | var dim = fs.windowSize(); |
| 238 | expect(dim.width).toEqual(400); |
| 239 | expect(dim.height).toEqual(200); |
| 240 | }); |
| 241 | |
| 242 | it('windowSize(): adjust height', function () { |
| 243 | var dim = fs.windowSize(50); |
| 244 | expect(dim.width).toEqual(400); |
| 245 | expect(dim.height).toEqual(150); |
| 246 | }); |
| 247 | |
| 248 | it('windowSize(): adjust width', function () { |
| 249 | var dim = fs.windowSize(0, 50); |
| 250 | expect(dim.width).toEqual(350); |
| 251 | expect(dim.height).toEqual(200); |
| 252 | }); |
| 253 | |
| 254 | it('windowSize(): adjust width and height', function () { |
| 255 | var dim = fs.windowSize(101, 201); |
| 256 | expect(dim.width).toEqual(199); |
| 257 | expect(dim.height).toEqual(99); |
| 258 | }); |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 259 | |
Simon Hunt | 31bb01f | 2015-03-31 16:50:41 -0700 | [diff] [blame] | 260 | // === Tests for isMobile() |
| 261 | var uaMap = { |
| 262 | chrome: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) " + |
| 263 | "AppleWebKit/537.36 (KHTML, like Gecko) " + |
| 264 | "Chrome/41.0.2272.89 Safari/537.36", |
| 265 | |
| 266 | iPad: "Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) " + |
| 267 | "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " + |
| 268 | "Mobile/11A465 Safari/9537.53", |
| 269 | |
| 270 | iPhone: "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) " + |
| 271 | "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " + |
| 272 | "Mobile/11A465 Safari/9537.53" |
| 273 | }; |
| 274 | |
| 275 | function setUa(key) { |
| 276 | var str = uaMap[key]; |
| 277 | expect(str).toBeTruthy(); |
| 278 | mockWindow.navigator.userAgent = str; |
| 279 | } |
| 280 | |
| 281 | it('isMobile(): should be false for Chrome on Mac OS X', function () { |
| 282 | setUa('chrome'); |
| 283 | expect(fs.isMobile()).toBe(false); |
| 284 | }); |
| 285 | it('isMobile(): should be true for Safari on iPad', function () { |
| 286 | setUa('iPad'); |
| 287 | expect(fs.isMobile()).toBe(true); |
| 288 | }); |
| 289 | it('isMobile(): should be true for Safari on iPhone', function () { |
| 290 | setUa('iPhone'); |
| 291 | expect(fs.isMobile()).toBe(true); |
| 292 | }); |
Simon Hunt | 48e6167 | 2015-01-30 14:48:25 -0800 | [diff] [blame] | 293 | |
Simon Hunt | 36c936b | 2015-01-30 16:07:18 -0800 | [diff] [blame] | 294 | // === Tests for find() |
| 295 | var dataset = [ |
| 296 | { id: 'foo', name: 'Furby'}, |
| 297 | { id: 'bar', name: 'Barbi'}, |
| 298 | { id: 'baz', name: 'Basil'}, |
| 299 | { id: 'goo', name: 'Gabby'}, |
| 300 | { id: 'zoo', name: 'Zevvv'} |
| 301 | ]; |
| 302 | |
| 303 | it('should not find ooo', function () { |
| 304 | expect(fs.find('ooo', dataset)).toEqual(-1); |
| 305 | }); |
| 306 | it('should find foo', function () { |
| 307 | expect(fs.find('foo', dataset)).toEqual(0); |
| 308 | }); |
| 309 | it('should find zoo', function () { |
| 310 | expect(fs.find('zoo', dataset)).toEqual(4); |
| 311 | }); |
| 312 | |
| 313 | it('should not find Simon', function () { |
| 314 | expect(fs.find('Simon', dataset, 'name')).toEqual(-1); |
| 315 | }); |
| 316 | it('should find Furby', function () { |
| 317 | expect(fs.find('Furby', dataset, 'name')).toEqual(0); |
| 318 | }); |
| 319 | it('should find Zevvv', function () { |
| 320 | expect(fs.find('Zevvv', dataset, 'name')).toEqual(4); |
| 321 | }); |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 322 | |
| 323 | |
| 324 | // === Tests for inArray() |
| 325 | var objRef = { x:1, y:2 }, |
| 326 | array = [1, 3.14, 'hey', objRef, 'there', true], |
| 327 | array2 = ['b', 'a', 'd', 'a', 's', 's']; |
| 328 | |
| 329 | it('should return -1 on non-arrays', function () { |
| 330 | expect(fs.inArray(1, {x:1})).toEqual(-1); |
| 331 | }); |
| 332 | it('should not find HOO', function () { |
| 333 | expect(fs.inArray('HOO', array)).toEqual(-1); |
| 334 | }); |
| 335 | it('should find 1', function () { |
| 336 | expect(fs.inArray(1, array)).toEqual(0); |
| 337 | }); |
| 338 | it('should find pi', function () { |
| 339 | expect(fs.inArray(3.14, array)).toEqual(1); |
| 340 | }); |
| 341 | it('should find hey', function () { |
| 342 | expect(fs.inArray('hey', array)).toEqual(2); |
| 343 | }); |
| 344 | it('should find the object', function () { |
| 345 | expect(fs.inArray(objRef, array)).toEqual(3); |
| 346 | }); |
| 347 | it('should find there', function () { |
| 348 | expect(fs.inArray('there', array)).toEqual(4); |
| 349 | }); |
| 350 | it('should find true', function () { |
| 351 | expect(fs.inArray(true, array)).toEqual(5); |
| 352 | }); |
| 353 | |
| 354 | it('should find the first occurrence A', function () { |
| 355 | expect(fs.inArray('a', array2)).toEqual(1); |
| 356 | }); |
| 357 | it('should find the first occurrence S', function () { |
| 358 | expect(fs.inArray('s', array2)).toEqual(4); |
| 359 | }); |
| 360 | it('should not find X', function () { |
| 361 | expect(fs.inArray('x', array2)).toEqual(-1); |
| 362 | }); |
| 363 | |
| 364 | // === Tests for removeFromArray() |
| 365 | it('should ignore non-arrays', function () { |
| 366 | expect(fs.removeFromArray(1, {x:1})).toBe(false); |
| 367 | }); |
| 368 | it('should keep the array the same, for non-match', function () { |
| 369 | var array = [1, 2, 3]; |
| 370 | expect(fs.removeFromArray(4, array)).toBe(false); |
| 371 | expect(array).toEqual([1, 2, 3]); |
| 372 | }); |
| 373 | it('should remove a value', function () { |
| 374 | var array = [1, 2, 3]; |
| 375 | expect(fs.removeFromArray(2, array)).toBe(true); |
| 376 | expect(array).toEqual([1, 3]); |
| 377 | }); |
| 378 | it('should remove the first occurrence', function () { |
| 379 | var array = ['x', 'y', 'z', 'z', 'y']; |
| 380 | expect(fs.removeFromArray('y', array)).toBe(true); |
| 381 | expect(array).toEqual(['x', 'z', 'z', 'y']); |
| 382 | expect(fs.removeFromArray('x', array)).toBe(true); |
| 383 | expect(array).toEqual(['z', 'z', 'y']); |
| 384 | }); |
| 385 | |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 386 | // === Tests for isEmptyObject() |
| 387 | it('should return true if an object is empty', function () { |
| 388 | expect(fs.isEmptyObject({})).toBe(true); |
| 389 | }); |
| 390 | it('should return false if an object is not empty', function () { |
| 391 | expect(fs.isEmptyObject({foo: 'bar'})).toBe(false); |
| 392 | }); |
| 393 | |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 394 | // === Tests for cap() |
| 395 | it('should ignore non-alpha', function () { |
| 396 | expect(fs.cap('123')).toEqual('123'); |
| 397 | }); |
Matteo Scandolo | 209c6c6 | 2016-05-21 10:08:57 -0700 | [diff] [blame] | 398 | it('should capitalize first char', function () { |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 399 | expect(fs.cap('Foo')).toEqual('Foo'); |
| 400 | expect(fs.cap('foo')).toEqual('Foo'); |
| 401 | expect(fs.cap('foo bar')).toEqual('Foo bar'); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 402 | expect(fs.cap('FOO BAR')).toEqual('Foo bar'); |
| 403 | expect(fs.cap('foo Bar')).toEqual('Foo bar'); |
Simon Hunt | 27a5cc8 | 2015-02-19 14:49:55 -0800 | [diff] [blame] | 404 | }); |
| 405 | |
Bri Prebilic Cole | 08d08d4 | 2015-04-01 14:37:02 -0700 | [diff] [blame] | 406 | // === Tests for noPx() |
| 407 | it('should return the value without px suffix', function () { |
| 408 | expect(fs.noPx('10px')).toBe(10); |
| 409 | expect(fs.noPx('500px')).toBe(500); |
| 410 | expect(fs.noPx('-80px')).toBe(-80); |
| 411 | }); |
| 412 | |
| 413 | // === Tests for noPxStyle() |
| 414 | it("should give a style's property without px suffix", function () { |
| 415 | var d3Elem = d3.select('body') |
| 416 | .append('div') |
| 417 | .attr('id', 'fooElem') |
| 418 | .style({ |
| 419 | width: '500px', |
| 420 | height: '200px', |
| 421 | 'font-size': '12px' |
| 422 | }); |
| 423 | expect(fs.noPxStyle(d3Elem, 'width')).toBe(500); |
| 424 | expect(fs.noPxStyle(d3Elem, 'height')).toBe(200); |
| 425 | expect(fs.noPxStyle(d3Elem, 'font-size')).toBe(12); |
| 426 | d3.select('#fooElem').remove(); |
| 427 | }); |
| 428 | |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 429 | // === Tests for endsWith() |
| 430 | it('should return true if string ends with foo', function () { |
| 431 | expect(fs.endsWith("barfoo", "foo")).toBe(true); |
| 432 | }); |
| 433 | |
| 434 | it('should return false if string doesnt end with foo', function () { |
| 435 | expect(fs.endsWith("barfood", "foo")).toBe(false); |
| 436 | }); |
| 437 | |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 438 | // === Tests for sanitize() |
| 439 | function chkSan(u, s) { |
| 440 | expect(fs.sanitize(u)).toEqual(s); |
| 441 | } |
| 442 | function chkGood(g) { |
| 443 | chkSan(g, g) |
| 444 | } |
| 445 | it('should return foo', function () { |
| 446 | chkGood('foo'); |
| 447 | }); |
| 448 | it('should retain < b > tags', function () { |
| 449 | chkGood('foo <b>bar</b> baz'); |
| 450 | }); |
| 451 | it('should retain < i > tags', function () { |
| 452 | chkGood('foo <i>bar</i> baz'); |
| 453 | }); |
| 454 | it('should retain < p > tags', function () { |
| 455 | chkGood('foo <p>bar</p> baz'); |
| 456 | }); |
| 457 | it('should retain < em > tags', function () { |
| 458 | chkGood('foo <em>bar</em> baz'); |
| 459 | }); |
| 460 | it('should retain < strong > tags', function () { |
| 461 | chkGood('foo <strong>bar</strong> baz'); |
| 462 | }); |
| 463 | |
| 464 | it('should reject < a > tags', function () { |
| 465 | chkSan('test <a href="hah">something</a> this', 'test something this'); |
| 466 | }); |
| 467 | |
| 468 | it('should log a warning for < script > tags', function () { |
| 469 | spyOn($log, 'warn'); |
| 470 | chkSan('<script>alert("foo");</script>', ''); |
| 471 | expect($log.warn).toHaveBeenCalledWith( |
| 472 | 'Unsanitary HTML input -- <script> detected!' |
| 473 | ); |
| 474 | }); |
| 475 | it('should log a warning for < style > tags', function () { |
| 476 | spyOn($log, 'warn'); |
| 477 | chkSan('<style> h1 {color:red;} </style>', ''); |
| 478 | expect($log.warn).toHaveBeenCalledWith( |
| 479 | 'Unsanitary HTML input -- <style> detected!' |
| 480 | ); |
| 481 | }); |
| 482 | |
Simon Hunt | 4a8de82 | 2017-05-17 17:09:49 -0700 | [diff] [blame] | 483 | |
| 484 | it('should log a warning for < iframe > tags', function () { |
| 485 | spyOn($log, 'warn'); |
| 486 | chkSan('Foo<iframe><body><h1>fake</h1></body></iframe>Bar', 'FooBar'); |
| 487 | expect($log.warn).toHaveBeenCalledWith( |
| 488 | 'Unsanitary HTML input -- <iframe> detected!' |
| 489 | ); |
| 490 | }); |
| 491 | |
Simon Hunt | 0fe05d6 | 2017-05-17 16:42:32 -0700 | [diff] [blame] | 492 | it('should completely strip < script >, remove < a >, retain < i >', function () { |
| 493 | chkSan( |
| 494 | 'Hey <i>this</i> is <script>alert("foo");</script> <a href="meh">cool</a>', |
| 495 | 'Hey <i>this</i> is cool' |
| 496 | ); |
| 497 | }); |
| 498 | |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 499 | }); |
Simon Hunt | 9664137 | 2015-06-02 15:53:25 -0700 | [diff] [blame] | 500 | |