Skip to content

Commit fa68a8a

Browse files
committed
test(utils): Adding tests to the utils functions
1 parent e29ea0c commit fa68a8a

File tree

5 files changed

+154
-38
lines changed

5 files changed

+154
-38
lines changed

jest.config.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const { jest: jestConfig } = require('kcd-scripts/config');
22

33
module.exports = Object.assign(jestConfig, {
4+
coveragePathIgnorePatterns: ['src/index.d.ts'],
45
coverageThreshold: {
56
global: {
6-
statements: 0,
7-
branches: 0,
8-
functions: 0,
9-
lines: 0,
7+
statements: 8,
8+
branches: 8,
9+
functions: 8,
10+
lines: 8,
1011
},
1112
},
1213
testEnvironment: 'node',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"node_modules",
7575
"coverage",
7676
"dist",
77+
"storybook-static",
7778
"stories"
7879
],
7980
"repository": {

src/__tests__/utils.test.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import parse from 'date-fns/parse';
2+
import startOfDay from 'date-fns/start_of_day';
3+
import localeEn from '../locales/en-US';
4+
import {
5+
formatDate,
6+
formatSelectedDate,
7+
getToday,
8+
isSelectable,
9+
moveElementsByN,
10+
omit,
11+
pick,
12+
} from '../utils';
13+
14+
const objectTest = { a: 'a', b: 'b', c: 'c' };
15+
const dateTest = parse('2018-06-21');
16+
const june14 = parse('2018-06-14');
17+
const june20 = parse('2018-06-20');
18+
const june25 = parse('2018-06-25');
19+
const june28 = parse('2018-06-28');
20+
21+
describe('moveElementsByN', () => {
22+
it('should return the same array if `n` is zero', () => {
23+
expect(moveElementsByN(0, localeEn.weekdays)).toEqual(localeEn.weekdays);
24+
});
25+
26+
it('should return the correct array if `n` is different than zero', () => {
27+
expect(moveElementsByN(3, localeEn.weekdays)).toEqual([
28+
'Wednesday',
29+
'Thursday',
30+
'Friday',
31+
'Saturday',
32+
'Sunday',
33+
'Monday',
34+
'Tuesday',
35+
]);
36+
37+
expect(moveElementsByN(5, localeEn.weekdays)).toEqual([
38+
'Friday',
39+
'Saturday',
40+
'Sunday',
41+
'Monday',
42+
'Tuesday',
43+
'Wednesday',
44+
'Thursday',
45+
]);
46+
});
47+
});
48+
49+
describe('omit', () => {
50+
it('should return the same object if the array of keys is empty', () => {
51+
expect(omit([], objectTest)).toEqual(objectTest);
52+
});
53+
54+
it('should return a new object without the given keys', () => {
55+
expect(omit(['a', 'b'], objectTest)).toEqual({ c: 'c' });
56+
});
57+
});
58+
59+
describe('pick', () => {
60+
it('should return an empty object if the array of keys is empty', () => {
61+
expect(pick([], objectTest)).toEqual({});
62+
});
63+
64+
it('should return a new object with the given keys', () => {
65+
expect(pick(['a', 'b'], objectTest)).toEqual({ a: 'a', b: 'b' });
66+
});
67+
});
68+
69+
describe('formatDate', () => {
70+
it('should return `undefined` if an invalid date is given', () => {
71+
expect(formatDate(null, 'DD/MM/YYYY')).toBe(undefined);
72+
});
73+
74+
it('should format correctly if a valid date is given', () => {
75+
expect(formatDate(dateTest, 'DD/MM/YYYY')).toBe('21/06/2018');
76+
});
77+
});
78+
79+
describe('isSelectable', () => {
80+
it('should return true if the first date is in the informed range', () => {
81+
expect(isSelectable(dateTest, june14, june28)).toBe(true);
82+
});
83+
84+
it('should return false if the first date is not in the informed range', () => {
85+
expect(isSelectable(dateTest, june14, june20)).toBe(false);
86+
});
87+
88+
it('should return true if the first date is after the minDate', () => {
89+
expect(isSelectable(dateTest, june14, null)).toBe(true);
90+
});
91+
92+
it('should return false if the first date is after the minDate', () => {
93+
expect(isSelectable(dateTest, june25, null)).toBe(false);
94+
});
95+
96+
it('should return true if the first date is before the maxDate', () => {
97+
expect(isSelectable(dateTest, null, june25)).toBe(true);
98+
});
99+
100+
it('should return false if the first date is before the maxDate', () => {
101+
expect(isSelectable(dateTest, null, june14)).toBe(false);
102+
});
103+
104+
it('should return true if we only provide one date to the function', () => {
105+
expect(isSelectable(dateTest)).toBe(true);
106+
});
107+
});
108+
109+
describe('getToday', () => {
110+
const today = startOfDay(new Date());
111+
112+
it('should return the correct result if `today` is not selectable', () => {
113+
expect(getToday(june14, june28)).toEqual({
114+
date: today,
115+
selectable: false,
116+
selected: false,
117+
today: true,
118+
});
119+
});
120+
121+
it('should return the correct result if `today` is selectable', () => {
122+
expect(getToday(june14)).toEqual({
123+
date: today,
124+
selectable: true,
125+
selected: false,
126+
today: true,
127+
});
128+
});
129+
});
130+
131+
describe('formatSelectedDate', () => {
132+
it('should return an empty string if an invalid date is given', () => {
133+
expect(formatSelectedDate(null, 'DD/MM/YYYY')).toBe('');
134+
});
135+
136+
it('should return the correct result if a valid date is given', () => {
137+
expect(formatSelectedDate(june14, 'DD/MM/YYYY')).toBe('14/06/2018');
138+
});
139+
140+
it('should return the correct result if a valid array of dates is given', () => {
141+
expect(formatSelectedDate([june14, june20], 'DD/MM/YYYY')).toBe(
142+
'14/06/2018 - 20/06/2018'
143+
);
144+
});
145+
});

src/utils/index.js renamed to src/utils.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import format from 'date-fns/format';
22
import isBefore from 'date-fns/is_before';
33
import startOfDay from 'date-fns/start_of_day';
44

5-
function isSelectable(date, minDate, maxDate) {
5+
export const isSelectable = (date, minDate, maxDate) => {
66
if (
77
(minDate && isBefore(date, minDate)) ||
88
(maxDate && isBefore(maxDate, date))
@@ -11,7 +11,7 @@ function isSelectable(date, minDate, maxDate) {
1111
}
1212

1313
return true;
14-
}
14+
};
1515

1616
export const getToday = (minDate, maxDate) => {
1717
const today = new Date();
@@ -45,8 +45,7 @@ export const pick = (keysToPick, obj) => {
4545
return newObj;
4646
};
4747

48-
export const moveElementsByN = (n = 0, arr = []) =>
49-
arr.slice(n).concat(arr.slice(0, n));
48+
export const moveElementsByN = (n, arr) => arr.slice(n).concat(arr.slice(0, n));
5049

5150
export const formatSelectedDate = (selectedDate, dateFormat) => {
5251
if (!selectedDate) {

src/utils/__tests__/index.test.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)