Skip to content

Commit

Permalink
fix(fn.utils): cover with tests critical paths (though coverage shoul…
Browse files Browse the repository at this point in the history
…d be increased definitely for those utils)
  • Loading branch information
rychkog committed Jul 2, 2017
1 parent e6eb712 commit 87eaff5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/utils/fn.utils.ts
Expand Up @@ -11,7 +11,7 @@ export function isEmpty(value: any[] | string): boolean {
}

export function trim(value: string): string {
return typeof value === 'string' ? value.trim() : '';
return isNil(value) ? '' : value.trim();
}

export function has(value: any, prop: string): boolean {
Expand All @@ -22,7 +22,7 @@ export function isFunction(value: any) {
return typeof value === 'function';
}

export function get(value: any, path: string = '', defaultValue?: any) {
export function get(value: any, path: string, defaultValue?: any) {
let result = value;

for (const prop of path.split('.')) {
Expand Down Expand Up @@ -64,7 +64,7 @@ export function once(fn: Once): Once {
}

export function defaultsDeep(target: any, ... sources: any[]): any {
return [target].concat(sources || []).reduce((result: any, source: any) => {
return [target].concat(sources).reduce((result: any, source: any) => {
if (!source) {
return result;
}
Expand All @@ -90,11 +90,8 @@ export function includes(target: string | any[], value: any): boolean {
return false;
}

if (typeof target === 'string') {
return target.indexOf(value as string) > -1;
}

return target.indexOf(value) > -1;
const index = typeof target === 'string' ? target.indexOf(value as string) : target.indexOf(value);
return index > -1;
}

export function isNil(value: any): boolean {
Expand Down
53 changes: 53 additions & 0 deletions test/utils/fn.utils.spec.ts
@@ -0,0 +1,53 @@
import * as fn from '../../src/utils/fn.utils';

describe('fn.utils - multipurpose functions', () => {
describe('trim', () => {
it('returns empty string when input value is null or undefined', () => {
expect(fn.trim(null)).toEqual('');
expect(fn.trim(undefined)).toEqual('');
});

it('uses native trim method under the hood', () => {
const stringSpy = jasmine.createSpyObj('string', ['trim']);
stringSpy.trim.and.returnValue('Boo!');
expect(fn.trim(stringSpy as string)).toEqual('Boo!');
});
});

describe('once', () => {
it('executes function only ones', () => {
const onceTargetSpy = jasmine.createSpy('once');
const onceExecutableFn = fn.once(onceTargetSpy);

onceExecutableFn('Hello', ', ', 'World');
onceExecutableFn('Hello');

expect(onceTargetSpy).toHaveBeenCalledTimes(1);
expect(onceTargetSpy).toHaveBeenCalledWith('Hello', ', ', 'World');
expect(onceTargetSpy).not.toHaveBeenCalledWith('Hello');
});
});

describe('defaultsDeep', () => {
it('uses empty array if there were no sources given', () => {
const options = fn.defaultsDeep({ msg: 'Boo!' });

expect(options).toEqual({ msg: 'Boo!' });
});
});

describe('defaultsDeep', () => {
it('uses empty array if there were no sources given', () => {
const options = fn.defaultsDeep({ msg: 'Boo!' });

expect(options).toEqual({ msg: 'Boo!' });
});
});

describe('includes', () => {
it('works with strings', () => {
expect(fn.includes('world', 'rl')).toEqual(true);
expect(fn.includes('world', 'rrl')).toEqual(false);
});
});
});

0 comments on commit 87eaff5

Please sign in to comment.