Skip to content

Commit 62c6a82

Browse files
🧪 test: add unit tests for hasValidation logic (#12934)
Co-authored-by: Bill <bluebill1049@hotmail.com>
1 parent e47b39e commit 62c6a82

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import hasValidation from '../../logic/hasValidation';
2+
import type { Field } from '../../types';
3+
4+
describe('hasValidation', () => {
5+
it('should return false if mount is not defined', () => {
6+
const noMount = { required: true } as Field['_f'];
7+
expect(hasValidation(noMount)).toBeFalsy();
8+
});
9+
const base = { mount: true } as Field['_f'];
10+
11+
it('should return false when field is not mounted', () => {
12+
expect(hasValidation({ ...base, mount: false })).toBeFalsy();
13+
});
14+
15+
it('should return false when there is no validation rule', () => {
16+
expect(hasValidation(base)).toBeFalsy();
17+
});
18+
19+
it('should return true when required is a string message', () => {
20+
expect(hasValidation({ ...base, required: 'This field is required' })).toBe(
21+
'This field is required',
22+
);
23+
});
24+
25+
it('should return false when validation rules are explicity falsy', () => {
26+
expect(hasValidation({ ...base, required: false })).toBeFalsy();
27+
expect(hasValidation({ ...base, validate: undefined })).toBeFalsy();
28+
});
29+
30+
it('should return false for falsy but valid numeric values like min: 0, max: 0, minLength: 0 or maxLength: 0', () => {
31+
expect(hasValidation({ ...base, min: 0 })).toBeFalsy();
32+
expect(hasValidation({ ...base, minLength: 0 })).toBeFalsy();
33+
expect(hasValidation({ ...base, max: 0 })).toBeFalsy();
34+
expect(hasValidation({ ...base, maxLength: 0 })).toBeFalsy();
35+
});
36+
37+
it('should return true when any validation rule is provided', () => {
38+
expect(hasValidation({ ...base, required: true })).toBe(true);
39+
expect(hasValidation({ ...base, min: 1 })).toBe(1);
40+
expect(hasValidation({ ...base, max: 10 })).toBe(10);
41+
expect(hasValidation({ ...base, minLength: 2 })).toBe(2);
42+
expect(hasValidation({ ...base, maxLength: 20 })).toBe(20);
43+
expect(hasValidation({ ...base, validate: () => true })).toBeTruthy();
44+
});
45+
46+
it('should return true when pattern is provided', () => {
47+
expect(
48+
hasValidation({
49+
...base,
50+
pattern: /test/,
51+
valueAsDate: false,
52+
valueAsNumber: false,
53+
}),
54+
).toStrictEqual(/test/);
55+
expect(
56+
hasValidation({
57+
...base,
58+
pattern: { value: /test/, message: 'invalid' },
59+
valueAsDate: false,
60+
valueAsNumber: false,
61+
}),
62+
).toStrictEqual({ value: /test/, message: 'invalid' });
63+
});
64+
65+
it('should return false when pattern is undefined or null', () => {
66+
type TestFieldForPatternOnly = Field['_f'] & {
67+
pattern: null;
68+
};
69+
expect(
70+
hasValidation({
71+
...base,
72+
pattern: undefined,
73+
valueAsDate: false,
74+
valueAsNumber: false,
75+
}),
76+
).toBeFalsy();
77+
78+
expect(
79+
hasValidation({ ...base, pattern: null } as TestFieldForPatternOnly),
80+
).toBeFalsy();
81+
});
82+
83+
it('should return pattern even when valueAsDate and valueAsNumber are not provided', () => {
84+
type TestFieldForPatternOnly = Field['_f'] & {
85+
pattern: RegExp;
86+
};
87+
expect(
88+
hasValidation({
89+
...base,
90+
pattern: /test/,
91+
} as TestFieldForPatternOnly),
92+
).toStrictEqual(/test/);
93+
});
94+
95+
it('should return true when validate is an object', () => {
96+
const validateObj = {
97+
isValid: () => true,
98+
isEmail: () => false,
99+
};
100+
expect(hasValidation({ ...base, validate: validateObj })).toBe(validateObj);
101+
});
102+
});

0 commit comments

Comments
 (0)