-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(common): Improve coverage for utility functions
- Loading branch information
Showing
16 changed files
with
89 additions
and
51 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
/** | ||
* Is nil | ||
* @param {*} value | ||
* @returns {boolean} | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
/** | ||
* To number | ||
* @param {*} value | ||
* @returns {number} | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
/** | ||
* To string | ||
* @param {*} value | ||
* @returns {string} | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | ||
}); | ||
}); |