Skip to content
This repository was archived by the owner on Jun 3, 2019. It is now read-only.

Commit 3623eae

Browse files
dariobanfiSteven Truesdell
authored andcommitted
Added 100% coverage for utils folder (#484)
1 parent 405af6f commit 3623eae

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { removeNil } from '../';
2+
3+
describe('removeNil', () => {
4+
test('should do nothing if there are no null or undefined values', () => {
5+
const array = ['val1', 'val2', 'val3'];
6+
expect(removeNil(array)).toEqual(array);
7+
});
8+
9+
test('should remove undefined and null values', () => {
10+
const array = ['val1', 'val2', undefined, 'val3', null];
11+
expect(removeNil(array)).toEqual(['val1', 'val2', 'val3']);
12+
});
13+
14+
test('should do nothing on an empty array', () => {
15+
const array = [];
16+
expect(removeNil(array)).toEqual([]);
17+
});
18+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { ifElse } from '../';
2+
3+
describe('ifElse', () => {
4+
test('returns a function', () => {
5+
expect(typeof ifElse(true)).toBe('function');
6+
});
7+
8+
test('the condition can be a function', () => {
9+
const ifTrue = ifElse(() => true);
10+
const result = ifTrue('true', 'false');
11+
expect(result).toBe('true');
12+
});
13+
14+
test('when true, returns the first argument', () => {
15+
const ifTrue = ifElse(true);
16+
const result = ifTrue('true', 'false');
17+
expect(result).toBe('true');
18+
});
19+
20+
test('when false, returns the second argument', () => {
21+
const ifFalse = ifElse(false);
22+
const result = ifFalse('true', 'false');
23+
expect(result).toBe('false');
24+
});
25+
26+
test('when true, can call a function if given as parameter', () => {
27+
const ifTrue = ifElse(true);
28+
const functionMock = jest.fn();
29+
ifTrue(functionMock('true'), functionMock('false'));
30+
expect(functionMock.mock.calls[0][0]).toBe('true');
31+
});
32+
33+
test('when false, can call a function if given as parameter', () => {
34+
const ifFalse = ifElse(false);
35+
const functionMock = jest.fn();
36+
ifFalse('true', functionMock('called'));
37+
expect(functionMock.mock.calls[0][0]).toBe('called');
38+
});
39+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { filterWithRules } from '../';
2+
3+
describe('filterWithRules', () => {
4+
test('throws an exception if rules argument is not an object', () => {
5+
const rules = 'something else';
6+
const obj = { a: 1, b: 2 };
7+
expect(() => filterWithRules(rules, obj)).toThrowError(
8+
'Filter set an "allow" on path "0", however, this path was not found on the source object.',
9+
);
10+
});
11+
12+
test('throws an exception if the object to filter is not an object', () => {
13+
const filterRules = { a: true };
14+
const obj = 'something else';
15+
expect(() => filterWithRules(filterRules, obj)).toThrowError(
16+
'Filter set an "allow" on path "a", however, this path was not found on the source object.',
17+
);
18+
});
19+
20+
test("throws an exception if the rule doesn't match the object", () => {
21+
const filterRules = { a: { something: true }, b: true };
22+
const obj = { a: 1, b: 2 };
23+
expect(() => filterWithRules(filterRules, obj)).toThrowError(
24+
'Expected prop at path "a" to be an object',
25+
);
26+
});
27+
28+
test('applies filter rules on nested objects', () => {
29+
const filterRules = { a: { something: true }, b: true };
30+
const obj = { a: { something: 1 }, b: 2 };
31+
expect(filterWithRules(filterRules, obj)).toEqual({
32+
a: { something: 1 },
33+
b: 2,
34+
});
35+
});
36+
37+
test('applies filter rules to an object', () => {
38+
const filterRules = { a: true, b: false };
39+
const obj = { a: 1, b: 2 };
40+
expect(filterWithRules(filterRules, obj)).toEqual({ a: 1 });
41+
});
42+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { mergeDeep } from '../';
2+
3+
describe('mergeDeep', () => {
4+
test('merges deeply two objects together', () => {
5+
const object1 = { a: 1, b: 2, c: { a: 1, b: 2 } };
6+
const object2 = { a: 1, c: { c: 3 } };
7+
expect(mergeDeep(object1, object2)).toEqual({
8+
a: 1,
9+
b: 2,
10+
c: {
11+
a: 1,
12+
b: 2,
13+
c: 3,
14+
},
15+
});
16+
});
17+
18+
test('the object to the right takes the priority', () => {
19+
const object1 = { a: 1, b: 2 };
20+
const object2 = { a: 1, b: 3 };
21+
expect(mergeDeep(object1, object2)).toEqual({
22+
a: 1,
23+
b: 3,
24+
});
25+
});
26+
27+
test('returns an empty object if no args are given', () => {
28+
expect(mergeDeep()).toEqual({});
29+
});
30+
31+
test('returns the only object given as arg if no other args are given', () => {
32+
expect(mergeDeep({ a: 1 })).toEqual({ a: 1 });
33+
});
34+
});

0 commit comments

Comments
 (0)