Skip to content

Commit 87eaff5

Browse files
committed
fix(fn.utils): cover with tests critical paths (though coverage should be increased definitely for those utils)
1 parent e6eb712 commit 87eaff5

2 files changed

Lines changed: 58 additions & 8 deletions

File tree

src/utils/fn.utils.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function isEmpty(value: any[] | string): boolean {
1111
}
1212

1313
export function trim(value: string): string {
14-
return typeof value === 'string' ? value.trim() : '';
14+
return isNil(value) ? '' : value.trim();
1515
}
1616

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

25-
export function get(value: any, path: string = '', defaultValue?: any) {
25+
export function get(value: any, path: string, defaultValue?: any) {
2626
let result = value;
2727

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

6666
export function defaultsDeep(target: any, ... sources: any[]): any {
67-
return [target].concat(sources || []).reduce((result: any, source: any) => {
67+
return [target].concat(sources).reduce((result: any, source: any) => {
6868
if (!source) {
6969
return result;
7070
}
@@ -90,11 +90,8 @@ export function includes(target: string | any[], value: any): boolean {
9090
return false;
9191
}
9292

93-
if (typeof target === 'string') {
94-
return target.indexOf(value as string) > -1;
95-
}
96-
97-
return target.indexOf(value) > -1;
93+
const index = typeof target === 'string' ? target.indexOf(value as string) : target.indexOf(value);
94+
return index > -1;
9895
}
9996

10097
export function isNil(value: any): boolean {

test/utils/fn.utils.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as fn from '../../src/utils/fn.utils';
2+
3+
describe('fn.utils - multipurpose functions', () => {
4+
describe('trim', () => {
5+
it('returns empty string when input value is null or undefined', () => {
6+
expect(fn.trim(null)).toEqual('');
7+
expect(fn.trim(undefined)).toEqual('');
8+
});
9+
10+
it('uses native trim method under the hood', () => {
11+
const stringSpy = jasmine.createSpyObj('string', ['trim']);
12+
stringSpy.trim.and.returnValue('Boo!');
13+
expect(fn.trim(stringSpy as string)).toEqual('Boo!');
14+
});
15+
});
16+
17+
describe('once', () => {
18+
it('executes function only ones', () => {
19+
const onceTargetSpy = jasmine.createSpy('once');
20+
const onceExecutableFn = fn.once(onceTargetSpy);
21+
22+
onceExecutableFn('Hello', ', ', 'World');
23+
onceExecutableFn('Hello');
24+
25+
expect(onceTargetSpy).toHaveBeenCalledTimes(1);
26+
expect(onceTargetSpy).toHaveBeenCalledWith('Hello', ', ', 'World');
27+
expect(onceTargetSpy).not.toHaveBeenCalledWith('Hello');
28+
});
29+
});
30+
31+
describe('defaultsDeep', () => {
32+
it('uses empty array if there were no sources given', () => {
33+
const options = fn.defaultsDeep({ msg: 'Boo!' });
34+
35+
expect(options).toEqual({ msg: 'Boo!' });
36+
});
37+
});
38+
39+
describe('defaultsDeep', () => {
40+
it('uses empty array if there were no sources given', () => {
41+
const options = fn.defaultsDeep({ msg: 'Boo!' });
42+
43+
expect(options).toEqual({ msg: 'Boo!' });
44+
});
45+
});
46+
47+
describe('includes', () => {
48+
it('works with strings', () => {
49+
expect(fn.includes('world', 'rl')).toEqual(true);
50+
expect(fn.includes('world', 'rrl')).toEqual(false);
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)