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

Commit 41c7df2

Browse files
committed
test(validators): unit tests
1 parent 454503e commit 41c7df2

File tree

3 files changed

+202
-3
lines changed

3 files changed

+202
-3
lines changed

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
moduleFileExtensions: ['js', 'jsx', 'ts', 'json', 'vue'],
3+
verbose: true,
34

45
transform: {
56
'^.+\\.vue$': 'vue-jest',
@@ -30,5 +31,5 @@ module.exports = {
3031
'jest-watch-typeahead/testname',
3132
],
3233
collectCoverage: true,
33-
collectCoverageFrom: ['<rootDir>/src/**/*.vue'],
34+
collectCoverageFrom: ['<rootDir>/src/**/*.(vue|ts)'],
3435
};

src/core/factories.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ export const TextField = ({
6666

6767
export const TextAreaField = ({
6868
value,
69-
cols,
70-
rows,
69+
cols = 20,
70+
rows = 3,
7171
...rest
7272
}: Partial<TextAreaInput>): TextAreaInput => ({
7373
...FieldBase(rest),

src/core/utils/validators.spec.ts

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
import {
2+
isEmptyInputValue,
3+
required,
4+
min,
5+
max,
6+
email,
7+
url,
8+
minLength,
9+
maxLength,
10+
pattern,
11+
} from './validators';
12+
13+
describe('Validators', () => {
14+
test('isEmptyInputValue should return true if value is null', () => {
15+
const value = null;
16+
const validation = isEmptyInputValue(value);
17+
expect(validation).toBeTruthy();
18+
});
19+
20+
test('isEmptyInputValue should return true if value is empty string', () => {
21+
const value = '';
22+
const validation = isEmptyInputValue(value);
23+
expect(validation).toBeTruthy();
24+
});
25+
26+
test('isEmptyInputValue should return true if value is null', () => {
27+
const value = null;
28+
const validation = isEmptyInputValue(value);
29+
expect(validation).toBeTruthy();
30+
});
31+
32+
test('required should return true if value is empty', () => {
33+
const value = null;
34+
const validation = required(value);
35+
expect(validation.required).toBeTruthy();
36+
});
37+
38+
test('required should return null if value is valid', () => {
39+
const value = 'Awiwi';
40+
const validation = required(value);
41+
expect(validation.required).toBeNull();
42+
});
43+
44+
test('min should return null if value is empty', () => {
45+
const minValue = 4;
46+
const value = null;
47+
const validation = min(minValue)(value);
48+
expect(validation.min).toBeNull();
49+
});
50+
51+
test('min should return null if value is greater than minValue', () => {
52+
const minValue = 4;
53+
const value = 8;
54+
const validation = min(minValue)(value);
55+
expect(validation.min).toBeNull();
56+
});
57+
58+
test('min should return true if value is less than minValue', () => {
59+
const minValue = 4;
60+
const value = 3;
61+
const validation = min(minValue)(value);
62+
expect(validation.min).toStrictEqual({ min: minValue, actual: value });
63+
});
64+
65+
test('max should return null if value is empty', () => {
66+
const maxValue = 4;
67+
const value = null;
68+
const validation = max(maxValue)(value);
69+
expect(validation.max).toBeNull();
70+
});
71+
72+
test('max should return null if value is greater than maxValue', () => {
73+
const maxValue = 4;
74+
const value = 3;
75+
const validation = max(maxValue)(value);
76+
expect(validation.max).toBeNull();
77+
});
78+
79+
test('max should return true if value is less than maxValue', () => {
80+
const maxValue = 4;
81+
const value = 8;
82+
const validation = max(maxValue)(value);
83+
expect(validation.max).toStrictEqual({ max: maxValue, actual: value });
84+
});
85+
86+
test('email should return true if value is empty', () => {
87+
const value = null;
88+
const validation = email(value);
89+
expect(validation.email).toBeNull();
90+
});
91+
92+
test('email should return true if value is invalid', () => {
93+
const value = 'hola.com';
94+
const validation = email(value);
95+
expect(validation.email).toBeTruthy();
96+
});
97+
98+
test('email should return null if value is valid', () => {
99+
const value = '[email protected]';
100+
const validation = email(value);
101+
expect(validation.email).toBeNull();
102+
});
103+
104+
test('url should return true if value is empty', () => {
105+
const value = null;
106+
const validation = url(value);
107+
expect(validation.url).toBeNull();
108+
});
109+
110+
test('url should return true if value is invalid', () => {
111+
const value = 'ftp://hola.com';
112+
const validation = url(value);
113+
expect(validation.url).toBeTruthy();
114+
});
115+
116+
test('url should return null if value is valid', () => {
117+
const value = 'https://alvarosaburido.dev/blog';
118+
const validation = url(value);
119+
expect(validation.url).toBeNull();
120+
});
121+
122+
test('minLength should return null if value is empty', () => {
123+
const minLengthValue = 4;
124+
const value = null;
125+
const validation = minLength(minLengthValue)(value);
126+
expect(validation.minLength).toBeNull();
127+
});
128+
129+
test('minLength should return null if value is shorter than minLengthValue', () => {
130+
const minLengthValue = 4;
131+
const value = 'arepa ipsum';
132+
const validation = minLength(minLengthValue)(value);
133+
expect(validation.minLength).toBeNull();
134+
});
135+
136+
test('minLength should return true if value is longer than minLengthValue', () => {
137+
const minLengthValue = 4;
138+
const value = 'awi';
139+
const validation = minLength(minLengthValue)(value);
140+
expect(validation.minLength).toStrictEqual({
141+
requiredLength: minLengthValue,
142+
actualLength: value.length,
143+
});
144+
});
145+
146+
test('maxLength should return null if value is empty', () => {
147+
const maxLengthValue = 4;
148+
const value = null;
149+
const validation = maxLength(maxLengthValue)(value);
150+
expect(validation.maxLength).toBeNull();
151+
});
152+
153+
test('maxLength should return null if value is longer than maxLengthValue', () => {
154+
const maxLengthValue = 4;
155+
const value = 'awi';
156+
const validation = maxLength(maxLengthValue)(value);
157+
expect(validation.maxLength).toBeNull();
158+
});
159+
160+
test('maxLength should return true if value is shorter than maxLengthValue', () => {
161+
const maxLengthValue = 4;
162+
const value = 'arepa ipsum';
163+
164+
const validation = maxLength(maxLengthValue)(value);
165+
expect(validation.maxLength).toStrictEqual({
166+
requiredLength: maxLengthValue,
167+
actualLength: value.length,
168+
});
169+
});
170+
171+
test('pattern should return true if value is empty', () => {
172+
const value = null;
173+
const validation = pattern(
174+
'^(?=.*[a-z])(?=.*[A-Z])(?=.*)(?=.*[#$^+=!*()@%&]).{8,12}$',
175+
)(value);
176+
expect(validation.pattern).toBeNull();
177+
});
178+
179+
test('pattern should return true if value is invalid', () => {
180+
const value = 'abcd1234';
181+
const validation = pattern(
182+
'^(?=.*[a-z])(?=.*[A-Z])(?=.*)(?=.*[#$^+=!*()@%&]).{8,12}$',
183+
)(value);
184+
expect(validation.pattern).toStrictEqual({
185+
actualValue: value,
186+
requiredPattern:
187+
'^(?=.*[a-z])(?=.*[A-Z])(?=.*)(?=.*[#$^+=!*()@%&]).{8,12}$',
188+
});
189+
});
190+
191+
test('pattern should return null if value is valid', () => {
192+
const value = 'Abcd@1234';
193+
const validation = pattern(
194+
'^(?=.*[a-z])(?=.*[A-Z])(?=.*)(?=.*[#$^+=!*()@%&]).{8,12}$',
195+
)(value);
196+
expect(validation.pattern).toBeNull();
197+
});
198+
});

0 commit comments

Comments
 (0)