|
| 1 | +/** |
| 2 | + * Copyright 2019, Optimizely |
| 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 | +var chai = require('chai'); |
| 18 | +var assert = chai.assert; |
| 19 | +var fns = require('./'); |
| 20 | + |
| 21 | +describe('lib/utils/fns', function() { |
| 22 | + describe('APIs', function() { |
| 23 | + describe('isFinite', function() { |
| 24 | + it('should return false for invalid numbers', function() { |
| 25 | + assert.isFalse(fns.isSafeInteger(Infinity)); |
| 26 | + assert.isFalse(fns.isSafeInteger(-Infinity)); |
| 27 | + assert.isFalse(fns.isSafeInteger(NaN)); |
| 28 | + assert.isFalse(fns.isSafeInteger(undefined)); |
| 29 | + assert.isFalse(fns.isSafeInteger('3')); |
| 30 | + assert.isFalse(fns.isSafeInteger(Math.pow(2, 53) + 2)); |
| 31 | + assert.isFalse(fns.isSafeInteger(-Math.pow(2, 53) - 2)); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should return true for valid numbers', function() { |
| 35 | + assert.isTrue(fns.isSafeInteger(0)); |
| 36 | + assert.isTrue(fns.isSafeInteger(10)); |
| 37 | + assert.isTrue(fns.isSafeInteger(10.5)); |
| 38 | + assert.isTrue(fns.isSafeInteger(Math.pow(2, 53))); |
| 39 | + assert.isTrue(fns.isSafeInteger(-Math.pow(2, 53))); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('keyBy', function() { |
| 44 | + it('should return correct object when a key is provided', function() { |
| 45 | + var arr = [ |
| 46 | + { key1: 'row1', key2: 'key2row1' }, |
| 47 | + { key1: 'row2', key2: 'key2row2' }, |
| 48 | + { key1: 'row3', key2: 'key2row3' }, |
| 49 | + { key1: 'row4', key2: 'key2row4' }, |
| 50 | + ]; |
| 51 | + |
| 52 | + var obj = fns.keyBy(arr, 'key1'); |
| 53 | + |
| 54 | + assert.deepEqual(obj, { |
| 55 | + row1: { key1: 'row1', key2: 'key2row1' }, |
| 56 | + row2: { key1: 'row2', key2: 'key2row2' }, |
| 57 | + row3: { key1: 'row3', key2: 'key2row3' }, |
| 58 | + row4: { key1: 'row4', key2: 'key2row4' }, |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should return empty object when first argument is null or undefined', function() { |
| 63 | + var obj = fns.keyBy(null, 'key1'); |
| 64 | + assert.isEmpty(obj); |
| 65 | + |
| 66 | + obj = fns.keyBy(undefined, 'key1'); |
| 67 | + assert.isEmpty(obj); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('isNumber', function() { |
| 72 | + it('should return true in case of number', function() { |
| 73 | + assert.isTrue(fns.isNumber(3)); |
| 74 | + }); |
| 75 | + it('should return true in case of value from Number object ', function() { |
| 76 | + assert.isTrue(fns.isNumber(Number.MIN_VALUE)); |
| 77 | + }); |
| 78 | + it('should return true in case of Infinity ', function() { |
| 79 | + assert.isTrue(fns.isNumber(Infinity)); |
| 80 | + }); |
| 81 | + it('should return false in case of string', function() { |
| 82 | + assert.isFalse(fns.isNumber('3')); |
| 83 | + }); |
| 84 | + it('should return false in case of null', function() { |
| 85 | + assert.isFalse(fns.isNumber(null)); |
| 86 | + }); |
| 87 | + }); |
| 88 | + }); |
| 89 | +}); |
0 commit comments