Skip to content

Commit

Permalink
test(common): Improve coverage for utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchin committed Apr 21, 2017
1 parent daf4ff3 commit de0f0d9
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 51 deletions.
12 changes: 0 additions & 12 deletions src/common/utils/capitalize.js

This file was deleted.

1 change: 0 additions & 1 deletion src/common/utils/includes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
* Includes item
* @param {array|string} items
* @param {array|string} item
* @returns {boolean}
Expand Down
29 changes: 8 additions & 21 deletions src/common/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
import capitalize from './capitalize';
import includes from './includes';
import isNil from './is-nil';
import isObject from './is-object';
import omitNil from './omit-nil';
import omitProperty from './omit-property';
import toNumber from './to-number';
import toSnakeCase from './to-snake-case';
import toString from './to-string';

export {
capitalize,
includes,
isNil,
isObject,
omitNil,
omitProperty,
toNumber,
toSnakeCase,
toString,
};
export { default as includes } from './includes';
export { default as isNil } from './is-nil';
export { default as isObject } from './is-object';
export { default as omitNil } from './omit-nil';
export { default as omitProperty } from './omit-property';
export { default as toNumber } from './to-number';
export { default as toSnakeCase } from './to-snake-case';
export { default as toString } from './to-string';
1 change: 0 additions & 1 deletion src/common/utils/is-nil.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
* Is nil
* @param {*} value
* @returns {boolean}
*/
Expand Down
5 changes: 1 addition & 4 deletions src/common/utils/is-object.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/**
* Is object
* @param {*} value
* @returns {boolean}
*/
export default function isObject(value) {
const type = typeof value;

return value !== null && (type === 'object' || type === 'function');
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
1 change: 0 additions & 1 deletion src/common/utils/omit-nil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import isNil from './is-nil';
import omitProperty from './omit-property';

/**
* Omit nil
* @param {Object} object
* @returns {Object}
*/
Expand Down
1 change: 0 additions & 1 deletion src/common/utils/omit-property.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import isObject from './is-object';

/**
* Omit property
* @param {Object} object
* @param {Function} predicateFn
* @returns {Object}
Expand Down
1 change: 0 additions & 1 deletion src/common/utils/to-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
* To number
* @param {*} value
* @returns {number}
*/
Expand Down
3 changes: 1 addition & 2 deletions src/common/utils/to-snake-case.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* To snake case
* @param {string} string
* @returns {string}
*/
export default function toSnakeCase(string) {
if (!string) {
if (typeof string !== 'string') {
return string;
}

Expand Down
1 change: 0 additions & 1 deletion src/common/utils/to-string.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
* To string
* @param {*} value
* @returns {string}
*/
Expand Down
23 changes: 23 additions & 0 deletions test/common/utils/includes.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import includes from '../../../src/common/utils/includes';

describe('includes', () => {
it('returns true if the array contains the item', () => {
expect(includes([1, 2, 3], 2)).toEqual(true);
});

it('returns false if the array does not contain the item', () => {
expect(includes([1, 2, 3], 4)).toEqual(false);
});

it('returns true if the string contains the substring', () => {
expect(includes('hello world', 'lo')).toEqual(true);
});

it('returns false if the string does not contain the substring', () => {
expect(includes('hello world', 'la')).toEqual(false);
});

it('returns false if the parameter is not a string or array', () => {
expect(includes({ la: 'la' }, 'la')).toEqual(false);
});
});
8 changes: 4 additions & 4 deletions test/common/utils/is-nil.spec.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import isNil from '../../../src/common/utils/is-nil';

describe('isNil', () => {
it('should return true if value is null', () => {
it('returns true if the input value is null', () => {
expect(isNil(null)).toBeTruthy();
});

it('should return true if value is undefined', () => {
it('returns true if the input value is undefined', () => {
expect(isNil(undefined)).toBeTruthy();
});

it('should return false if value is empty', () => {
it('returns false if the input value is empty', () => {
expect(isNil('')).toBeFalsy();
});

it('should return false if value is not empty', () => {
it('returns false if the input value is not empty', () => {
expect(isNil('foobar')).toBeFalsy();
});
});
11 changes: 9 additions & 2 deletions test/common/utils/omit-property.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import omitProperty from '../../../src/common/utils/omit-property';

describe('omitProperty', () => {
it('should omit property if matching predicate', () => {
it('omits the property if its key matches the predicate', () => {
const output = omitProperty({ a: 'a', b: 'b' }, value => value === 'b');

expect(output).toEqual({ a: 'a' });
});

it('should not omit property if matching predicate', () => {
it('does not omit the property if its key does not match the predicate', () => {
const output = omitProperty({ a: 'a', b: 'b' }, value => value === 'c');

expect(output).toEqual({ a: 'a', b: 'b' });
});

it('returns the parameter if it is not an object', () => {
const input = ['a', 'b'];
const output = omitProperty(input, value => value === 'b');

expect(output).toEqual(input);
});
});
11 changes: 11 additions & 0 deletions test/common/utils/to-number.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import toNumber from '../../../src/common/utils/to-number';

describe('toNumber', () => {
it('converts the number-like string into a number', () => {
expect(toNumber('1.5')).toEqual(1.5);
});

it('returns 0 if the parameter is not a number-like string', () => {
expect(toNumber('foobar')).toEqual(0);
});
});
17 changes: 17 additions & 0 deletions test/common/utils/to-snake-case.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import toSnakeCase from '../../../src/common/utils/to-snake-case';

describe('toSnakeCase', () => {
it('converts a string written in camel case into snake case', () => {
expect(toSnakeCase('toSnakeCase')).toEqual('to_snake_case');
});

it('converts a regular string into snake case', () => {
expect(toSnakeCase('To snake case')).toEqual('to_snake_case');
});

it('returns the parameter if it is not a string', () => {
const input = { message: 'To snake case' };

expect(toSnakeCase(input)).toEqual(input);
});
});
15 changes: 15 additions & 0 deletions test/common/utils/to-string.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import toString from '../../../src/common/utils/to-string';

describe('toString', () => {
it('converts the number into a string', () => {
expect(toString(1)).toEqual('1');
});

it('returns the parameter if it is already a string', () => {
expect(toString('foobar')).toEqual('foobar');
});

it('returns a blank string if the parameter is an object', () => {
expect(toString({ foobar: 'foobar' })).toEqual('');
});
});

0 comments on commit de0f0d9

Please sign in to comment.