fn-spec.js 14.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
/*
 * Copyright 2014-present Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 ONOS GUI -- Util -- General Purpose Functions - Unit Tests
 */
describe('factory: fw/util/fn.js', function() {
    var $window,
        fs,
        someFunction = function () {},
        someArray = [1, 2, 3],
        someObject = { foo: 'bar'},
        someNumber = 42,
        someString = 'xyyzy',
        someDate = new Date(),
        stringArray = ['foo', 'bar'];

    beforeEach(module('onosUtil'));

    var mockWindow = {
        innerWidth: 400,
        innerHeight: 200,
        navigator: {
            userAgent: 'defaultUA'
        }
    };

    beforeEach(function () {
        module(function ($provide) {
            $provide.value('$window', mockWindow);
        });
    });

    beforeEach(inject(function (_$window_, FnService) {
        $window = _$window_;
        fs = FnService;
    }));

    // === Tests for isF()
    it('isF(): null for undefined', function () {
        expect(fs.isF(undefined)).toBeNull();
    });
    it('isF(): null for null', function () {
        expect(fs.isF(null)).toBeNull();
    });
    it('isF(): the reference for function', function () {
        expect(fs.isF(someFunction)).toBe(someFunction);
    });
    it('isF(): null for string', function () {
        expect(fs.isF(someString)).toBeNull();
    });
    it('isF(): null for number', function () {
        expect(fs.isF(someNumber)).toBeNull();
    });
    it('isF(): null for Date', function () {
        expect(fs.isF(someDate)).toBeNull();
    });
    it('isF(): null for array', function () {
        expect(fs.isF(someArray)).toBeNull();
    });
    it('isF(): null for object', function () {
        expect(fs.isF(someObject)).toBeNull();
    });


    // === Tests for isA()
    it('isA(): null for undefined', function () {
        expect(fs.isA(undefined)).toBeNull();
    });
    it('isA(): null for null', function () {
        expect(fs.isA(null)).toBeNull();
    });
    it('isA(): null for function', function () {
        expect(fs.isA(someFunction)).toBeNull();
    });
    it('isA(): null for string', function () {
        expect(fs.isA(someString)).toBeNull();
    });
    it('isA(): null for number', function () {
        expect(fs.isA(someNumber)).toBeNull();
    });
    it('isA(): null for Date', function () {
        expect(fs.isA(someDate)).toBeNull();
    });
    it('isA(): the reference for array', function () {
        expect(fs.isA(someArray)).toBe(someArray);
    });
    it('isA(): null for object', function () {
        expect(fs.isA(someObject)).toBeNull();
    });


    // === Tests for isS()
    it('isS(): null for undefined', function () {
        expect(fs.isS(undefined)).toBeNull();
    });
    it('isS(): null for null', function () {
        expect(fs.isS(null)).toBeNull();
    });
    it('isS(): null for function', function () {
        expect(fs.isS(someFunction)).toBeNull();
    });
    it('isS(): the reference for string', function () {
        expect(fs.isS(someString)).toBe(someString);
    });
    it('isS(): null for number', function () {
        expect(fs.isS(someNumber)).toBeNull();
    });
    it('isS(): null for Date', function () {
        expect(fs.isS(someDate)).toBeNull();
    });
    it('isS(): null for array', function () {
        expect(fs.isS(someArray)).toBeNull();
    });
    it('isS(): null for object', function () {
        expect(fs.isS(someObject)).toBeNull();
    });


    // === Tests for isO()
    it('isO(): null for undefined', function () {
        expect(fs.isO(undefined)).toBeNull();
    });
    it('isO(): null for null', function () {
        expect(fs.isO(null)).toBeNull();
    });
    it('isO(): null for function', function () {
        expect(fs.isO(someFunction)).toBeNull();
    });
    it('isO(): null for string', function () {
        expect(fs.isO(someString)).toBeNull();
    });
    it('isO(): null for number', function () {
        expect(fs.isO(someNumber)).toBeNull();
    });
    it('isO(): null for Date', function () {
        expect(fs.isO(someDate)).toBeNull();
    });
    it('isO(): null for array', function () {
        expect(fs.isO(someArray)).toBeNull();
    });
    it('isO(): the reference for object', function () {
        expect(fs.isO(someObject)).toBe(someObject);
    });

    // === Tests for contains()
    it('contains(): false for improper args', function () {
        expect(fs.contains()).toBeFalsy();
    });
    it('contains(): false for non-array', function () {
        expect(fs.contains(null, 1)).toBeFalsy();
    });
    it('contains(): true for contained item', function () {
        expect(fs.contains(someArray, 1)).toBeTruthy();
        expect(fs.contains(stringArray, 'bar')).toBeTruthy();
    });
    it('contains(): false for non-contained item', function () {
        expect(fs.contains(someArray, 109)).toBeFalsy();
        expect(fs.contains(stringArray, 'zonko')).toBeFalsy();
    });

    // === Tests for areFunctions()
    it('areFunctions(): false for non-array', function () {
        expect(fs.areFunctions({}, 'not-an-array')).toBeFalsy();
    });
    it('areFunctions(): true for empty-array', function () {
        expect(fs.areFunctions({}, [])).toBeTruthy();
    });
    it('areFunctions(): true for some api', function () {
        expect(fs.areFunctions({
            a: function () {},
            b: function () {}
        }, ['b', 'a'])).toBeTruthy();
    });
    it('areFunctions(): false for some other api', function () {
        expect(fs.areFunctions({
            a: function () {},
            b: 'not-a-function'
        }, ['b', 'a'])).toBeFalsy();
    });
    it('areFunctions(): extraneous stuff NOT ignored', function () {
        expect(fs.areFunctions({
            a: function () {},
            b: function () {},
            c: 1,
            d: 'foo'
        }, ['a', 'b'])).toBeFalsy();
    });
    it('areFunctions(): extraneous stuff ignored (alternate fn)', function () {
        expect(fs.areFunctionsNonStrict({
            a: function () {},
            b: function () {},
            c: 1,
            d: 'foo'
        }, ['a', 'b'])).toBeTruthy();
    });

    // == use the now-tested areFunctions() on our own api:
    it('should define api functions', function () {
        expect(fs.areFunctions(fs, [
            'isF', 'isA', 'isS', 'isO', 'contains',
            'areFunctions', 'areFunctionsNonStrict', 'windowSize', 'isMobile',
            'find', 'inArray', 'removeFromArray', 'isEmptyObject', 'cap',
            'noPx', 'noPxStyle', 'endsWith', 'parseBitRate'
        ])).toBeTruthy();
    });


    // === Tests for windowSize()
    it('windowSize(): noargs', function () {
        var dim = fs.windowSize();
        expect(dim.width).toEqual(400);
        expect(dim.height).toEqual(200);
    });

    it('windowSize(): adjust height', function () {
        var dim = fs.windowSize(50);
        expect(dim.width).toEqual(400);
        expect(dim.height).toEqual(150);
    });

    it('windowSize(): adjust width', function () {
        var dim = fs.windowSize(0, 50);
        expect(dim.width).toEqual(350);
        expect(dim.height).toEqual(200);
    });

    it('windowSize(): adjust width and height', function () {
        var dim = fs.windowSize(101, 201);
        expect(dim.width).toEqual(199);
        expect(dim.height).toEqual(99);
    });

    // === Tests for isMobile()
    var uaMap = {
        chrome: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) " +
                "AppleWebKit/537.36 (KHTML, like Gecko) " +
                "Chrome/41.0.2272.89 Safari/537.36",

        iPad: "Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) " +
                "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " +
                "Mobile/11A465 Safari/9537.53",

        iPhone: "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) " +
                "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " +
                "Mobile/11A465 Safari/9537.53"
    };

    function setUa(key) {
        var str = uaMap[key];
        expect(str).toBeTruthy();
        mockWindow.navigator.userAgent = str;
    }

    it('isMobile(): should be false for Chrome on Mac OS X', function () {
        setUa('chrome');
        expect(fs.isMobile()).toBe(false);
    });
    it('isMobile(): should be true for Safari on iPad', function () {
        setUa('iPad');
        expect(fs.isMobile()).toBe(true);
    });
    it('isMobile(): should be true for Safari on iPhone', function () {
        setUa('iPhone');
        expect(fs.isMobile()).toBe(true);
    });

    // === Tests for find()
    var dataset = [
        { id: 'foo', name: 'Furby'},
        { id: 'bar', name: 'Barbi'},
        { id: 'baz', name: 'Basil'},
        { id: 'goo', name: 'Gabby'},
        { id: 'zoo', name: 'Zevvv'}
    ];

    it('should not find ooo', function () {
        expect(fs.find('ooo', dataset)).toEqual(-1);
    });
    it('should find foo', function () {
        expect(fs.find('foo', dataset)).toEqual(0);
    });
    it('should find zoo', function () {
        expect(fs.find('zoo', dataset)).toEqual(4);
    });

    it('should not find Simon', function () {
        expect(fs.find('Simon', dataset, 'name')).toEqual(-1);
    });
    it('should find Furby', function () {
        expect(fs.find('Furby', dataset, 'name')).toEqual(0);
    });
    it('should find Zevvv', function () {
        expect(fs.find('Zevvv', dataset, 'name')).toEqual(4);
    });


    // === Tests for inArray()
    var objRef = { x:1, y:2 },
        array = [1, 3.14, 'hey', objRef, 'there', true],
        array2 = ['b', 'a', 'd', 'a', 's', 's'];

    it('should return -1 on non-arrays', function () {
        expect(fs.inArray(1, {x:1})).toEqual(-1);
    });
    it('should not find HOO', function () {
        expect(fs.inArray('HOO', array)).toEqual(-1);
    });
    it('should find 1', function () {
        expect(fs.inArray(1, array)).toEqual(0);
    });
    it('should find pi', function () {
        expect(fs.inArray(3.14, array)).toEqual(1);
    });
    it('should find hey', function () {
        expect(fs.inArray('hey', array)).toEqual(2);
    });
    it('should find the object', function () {
        expect(fs.inArray(objRef, array)).toEqual(3);
    });
    it('should find there', function () {
        expect(fs.inArray('there', array)).toEqual(4);
    });
    it('should find true', function () {
        expect(fs.inArray(true, array)).toEqual(5);
    });

    it('should find the first occurrence A', function () {
        expect(fs.inArray('a', array2)).toEqual(1);
    });
    it('should find the first occurrence S', function () {
        expect(fs.inArray('s', array2)).toEqual(4);
    });
    it('should not find X', function () {
        expect(fs.inArray('x', array2)).toEqual(-1);
    });

    // === Tests for removeFromArray()
    it('should ignore non-arrays', function () {
        expect(fs.removeFromArray(1, {x:1})).toBe(false);
    });
    it('should keep the array the same, for non-match', function () {
        var array = [1, 2, 3];
        expect(fs.removeFromArray(4, array)).toBe(false);
        expect(array).toEqual([1, 2, 3]);
    });
    it('should remove a value', function () {
        var array = [1, 2, 3];
        expect(fs.removeFromArray(2, array)).toBe(true);
        expect(array).toEqual([1, 3]);
    });
    it('should remove the first occurrence', function () {
        var array = ['x', 'y', 'z', 'z', 'y'];
        expect(fs.removeFromArray('y', array)).toBe(true);
        expect(array).toEqual(['x', 'z', 'z', 'y']);
        expect(fs.removeFromArray('x', array)).toBe(true);
        expect(array).toEqual(['z', 'z', 'y']);
    });

    // === Tests for isEmptyObject()
    it('should return true if an object is empty', function () {
        expect(fs.isEmptyObject({})).toBe(true);
    });
    it('should return false if an object is not empty', function () {
        expect(fs.isEmptyObject({foo: 'bar'})).toBe(false);
    });

    // === Tests for cap()
    it('should ignore non-alpha', function () {
        expect(fs.cap('123')).toEqual('123');
    });
    it('should capitalize first char', function () {
        expect(fs.cap('Foo')).toEqual('Foo');
        expect(fs.cap('foo')).toEqual('Foo');
        expect(fs.cap('foo bar')).toEqual('Foo bar');
        expect(fs.cap('FOO BAR')).toEqual('Foo bar');
        expect(fs.cap('foo Bar')).toEqual('Foo bar');
    });

    // === Tests for noPx()
    it('should return the value without px suffix', function () {
        expect(fs.noPx('10px')).toBe(10);
        expect(fs.noPx('500px')).toBe(500);
        expect(fs.noPx('-80px')).toBe(-80);
    });

    // === Tests for noPxStyle()
    it("should give a style's property without px suffix", function () {
        var d3Elem = d3.select('body')
            .append('div')
            .attr('id', 'fooElem')
            .style({
                width: '500px',
                height: '200px',
                'font-size': '12px'
            });
        expect(fs.noPxStyle(d3Elem, 'width')).toBe(500);
        expect(fs.noPxStyle(d3Elem, 'height')).toBe(200);
        expect(fs.noPxStyle(d3Elem, 'font-size')).toBe(12);
        d3.select('#fooElem').remove();
    });

    // === Tests for endsWith()
    it('should return true if string ends with foo', function () {
        expect(fs.endsWith("barfoo", "foo")).toBe(true);
    });

    it('should return false if string doesnt end with foo', function () {
        expect(fs.endsWith("barfood", "foo")).toBe(false);
    });

    // === Tests for parseBitRate()
    it('should return 5 - a', function () {
        expect(fs.parseBitRate('5.47 KBps')).toBe(5);
    });

    it('should return 5 - b', function () {
        expect(fs.parseBitRate('5. KBps')).toBe(5);
    });

    it('should return 5 - c', function () {
        expect(fs.parseBitRate('5 KBps')).toBe(5);
    });

    it('should return 5 - d', function () {
        expect(fs.parseBitRate('5 Kbps')).toBe(5);
    });

    it('should return 2001', function () {
        expect(fs.parseBitRate('2,001.59 Gbps')).toBe(2001);
    });
});