Skip to content

Commit 25c017e

Browse files
authored
Feature/check rule applied (#20)
* WIP * Fixed required check * Fixes * Fixes
1 parent bd116b9 commit 25c017e

14 files changed

+399
-161
lines changed

__tests__/Area.test.tsx

+17-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Validator } from '@/Validator';
44
import { ValidatorArea, ValidatorAreaProps } from '@/components/ValidatorArea';
55
import ValidatorProvider, { ValidatorProviderProps } from '@/components/ValidatorProvider';
66
import { ProviderScope } from '@/ProviderScope';
7+
import required from '@/rules/required';
78

89
const tick = () => {
910
return new Promise(resolve => {
@@ -21,6 +22,7 @@ describe('test ValidatorProvider', () => {
2122
return 'not passed';
2223
}
2324
});
25+
Validator.extend('required', required)
2426
});
2527

2628
it('should render input', () => {
@@ -83,7 +85,7 @@ describe('test ValidatorProvider', () => {
8385
it('should apply rules on blur', () => {
8486
const area = mount<ValidatorArea, ValidatorAreaProps>(
8587
<ValidatorArea rules="passes_not">
86-
<input name="test" />
88+
<input name="test" value="test" />
8789
</ValidatorArea>
8890
);
8991

@@ -107,7 +109,7 @@ describe('test ValidatorProvider', () => {
107109
<ValidatorArea rules="passes_not">
108110
{({ errors }) => (
109111
<>
110-
<input name="test" />
112+
<input name="test" value="test" />
111113
{errors.length && <div>{errors[0]}</div>}
112114
</>
113115
)}
@@ -123,7 +125,7 @@ describe('test ValidatorProvider', () => {
123125

124126
const area = mount<ValidatorArea, ValidatorAreaProps>(
125127
<ValidatorArea rules="passes_not">
126-
<input name="test" onBlur={mockFn} />
128+
<input name="test" onBlur={mockFn} value="test" />
127129
</ValidatorArea>
128130
);
129131

@@ -137,7 +139,7 @@ describe('test ValidatorProvider', () => {
137139
return validator.refs().length === 2;
138140
},
139141
message(): string {
140-
return 'test';
142+
return '';
141143
}
142144
}))
143145
const mockFn = jest.fn();
@@ -147,10 +149,10 @@ describe('test ValidatorProvider', () => {
147149
{({ validate }: ProviderScope) => (
148150
<>
149151
<ValidatorArea name="test1">
150-
<input value="" />
152+
<input value="test" />
151153
</ValidatorArea>
152154
<ValidatorArea>
153-
<input value="" name="test2" />
155+
<input value="test" name="test2" />
154156
</ValidatorArea>
155157
<button onClick={() => validate(mockFn)} />
156158
</>
@@ -180,11 +182,11 @@ describe('test ValidatorProvider', () => {
180182
{({ validate }: ProviderScope) => (
181183
<>
182184
<ValidatorArea name="test1">
183-
<input value="" />
184-
<input value="" />
185+
<input value="test" />
186+
<input value="test" />
185187
</ValidatorArea>
186188
<ValidatorArea>
187-
<input value="" name="test2" />
189+
<input value="test" name="test2" />
188190
</ValidatorArea>
189191
<button onClick={() => validate(mockFn)} />
190192
</>
@@ -213,10 +215,10 @@ describe('test ValidatorProvider', () => {
213215
{({ validate }: ProviderScope) => (
214216
<>
215217
<ValidatorArea name="test1">
216-
<input value="" />
218+
<input value="test" />
217219
</ValidatorArea>
218220
<ValidatorArea>
219-
<input value="" name="test2" />
221+
<input value="test" name="test2" />
220222
</ValidatorArea>
221223
<button onClick={() => validate(mockFn)} />
222224
</>
@@ -243,7 +245,7 @@ describe('test ValidatorProvider', () => {
243245

244246
const area = mount<ValidatorArea, ValidatorAreaProps>(
245247
<ValidatorArea rules="no_other_areas">
246-
<input name="test" onBlur={mockFn} />
248+
<input name="test" value="test" onBlur={mockFn} />
247249
</ValidatorArea>
248250
);
249251

@@ -270,10 +272,10 @@ describe('test ValidatorProvider', () => {
270272
{({ validate }: ProviderScope) => (
271273
<>
272274
<ValidatorArea name="test1">
273-
<textarea value="" />
275+
<textarea value="test" />
274276
</ValidatorArea>
275277
<ValidatorArea>
276-
<input value="" name="test2" />
278+
<input value="test" name="test2" />
277279
</ValidatorArea>
278280
<button onClick={() => validate(mockFn)} />
279281
</>
@@ -298,7 +300,7 @@ describe('test ValidatorProvider', () => {
298300

299301
const area = mount<ValidatorArea, ValidatorAreaProps>(
300302
<ValidatorArea validationName="Foo" rules="passes_not">
301-
<input name="test" />
303+
<input name="test" value="test" />
302304
</ValidatorArea>
303305
);
304306

__tests__/Provider.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ describe('test ValidatorProvider', () => {
9090
{({ validate }) => (
9191
<>
9292
<ValidatorArea rules="passes_not" name="test1">
93-
<input value="" />
93+
<input value="test" />
9494
</ValidatorArea>
9595
<ValidatorArea rules="passes_not" name="test2">
96-
<input value="" />
96+
<input value="test" />
9797
</ValidatorArea>
9898
<button onClick={() => validate(mockFn)} />
9999
</>

__tests__/Validator.test.tsx

+44-5
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@ describe('test validator', () => {
4343

4444
Validator.extend('testRule', rule);
4545

46-
expect(Validator.hasRule('testRule')).toBeTruthy();
46+
expect(Validator.ruleExists('testRule')).toBeTruthy();
4747
});
4848

4949
it('should validate rules as string', () => {
50+
const input = document.createElement<'input'>('input');
51+
input.value = 'test';
52+
5053
const validator = new Validator(
5154
[
52-
document.createElement<'input'>('input')
55+
input
5356
],
5457
'rule_one|rule_two',
5558
'test'
@@ -60,9 +63,11 @@ describe('test validator', () => {
6063
});
6164

6265
it('should validate rules as array', () => {
66+
const input = document.createElement<'input'>('input');
67+
input.value = 'test';
6368
const validator = new Validator(
6469
[
65-
document.createElement<'input'>('input')
70+
input
6671
],
6772
['rule_one', 'rule_two'],
6873
'test'
@@ -73,9 +78,12 @@ describe('test validator', () => {
7378
});
7479

7580
it('should validate with parameters', () => {
81+
const input = document.createElement<'input'>('input');
82+
input.value = 'test';
83+
7684
const validator = new Validator(
7785
[
78-
document.createElement<'input'>('input')
86+
input
7987
],
8088
['rule_with_params:1,2,3,4'],
8189
'test'
@@ -86,10 +94,12 @@ describe('test validator', () => {
8694
});
8795

8896
it('throws an exception when rule is not found', () => {
97+
const input = document.createElement<'input'>('input');
98+
input.value = 'test';
8999
const throws = () => {
90100
const validator = new Validator(
91101
[
92-
document.createElement<'input'>('input')
102+
input
93103
],
94104
['not_existing_rule'],
95105
'test'
@@ -118,5 +128,34 @@ describe('test validator', () => {
118128
}
119129

120130
expect(() => throws()).toThrowError('Areas are only available when validating React components.')
131+
});
132+
133+
it('should be able to check if the value is required', () => {
134+
const input = document.createElement<'input'>('input');
135+
input.value = 'test';
136+
Validator.extend('check_if_required', (validator: Validator) => ({
137+
passed(elements: HTMLElement[]): boolean {
138+
return !elements.every((element: HTMLElement) => validator.shouldValidate(element));
139+
},
140+
message(): string {
141+
return 'Value is false negative required'
142+
}
143+
}));
144+
145+
const validator = new Validator(
146+
[
147+
input
148+
],
149+
['required', 'check_if_required'],
150+
'test'
151+
);
152+
153+
validator.validate();
154+
155+
expect(validator.getErrors()[0]).toBe('Value is false negative required');
156+
})
157+
158+
it('should return empty array when no refs provided', () => {
159+
121160
})
122161
});

__tests__/rules/max.test.tsx

+52-18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { mount } from 'enzyme';
33
import max from '@/rules/max';
44
import { Validator } from '@/Validator';
55
import { ValidatorArea, ValidatorAreaProps } from '@/components/ValidatorArea';
6+
import { IncorrectArgumentTypeError } from '@/rules';
67

78
describe('test max rule', () => {
89
beforeEach(() => {
@@ -11,39 +12,72 @@ describe('test max rule', () => {
1112

1213
it('should always validate inputs and not validate non-inputs', () => {
1314
const input = document.createElement('input');
14-
input.value = '5';
15-
16-
const throwsArgumentError = () => {
17-
const validator = new Validator([
18-
input
19-
], ['max:foo'],
20-
'validate_throws'
21-
);
22-
validator.validate();
23-
}
15+
const meter = document.createElement('meter');
16+
const output = document.createElement('output');
17+
const progress = document.createElement('progress');
18+
const canvas = document.createElement('canvas');
19+
input.value = '7';
20+
output.value = '7';
21+
meter.value = 6;
22+
meter.max = 10;
23+
progress.value = 6;
2424

2525
const validator_input = new Validator([
2626
input
2727
],
28-
['max:4'],
29-
'validator_input');
28+
['max:5'],
29+
'');
3030

31-
const progress = document.createElement('progress');
32-
progress.value = 5;
31+
const validator_meter = new Validator([
32+
meter
33+
],
34+
['max:5'],
35+
'');
36+
37+
const validator_output = new Validator([
38+
output
39+
],
40+
['max:5'],
41+
'');
3342

3443
const validator_progress = new Validator([
3544
progress
3645
],
37-
['max:4'],
38-
'validate_progress');
46+
['max:5'],
47+
'');
48+
49+
const validator_canvas = new Validator([
50+
canvas
51+
],
52+
['max:5'],
53+
'');
3954

40-
expect(() => throwsArgumentError()).toThrowError('max rule has incorrect argument foo. Expected a number.')
55+
const validator_wrong_arg = new Validator([
56+
input
57+
],
58+
['max:foo'],
59+
'');
4160

4261
validator_input.validate();
4362
expect(validator_input.getErrors().length).toBe(1);
4463

64+
validator_meter.validate();
65+
expect(validator_meter.getErrors().length).toBe(1);
66+
67+
validator_output.validate();
68+
expect(validator_output.getErrors().length).toBe(1);
69+
4570
validator_progress.validate();
46-
expect(validator_progress.getErrors().length).toBe(0);
71+
expect(validator_progress.getErrors().length).toBe(1);
72+
73+
validator_canvas.validate();
74+
expect(validator_canvas.getErrors().length).toBe(0);
75+
76+
const throwsInvalidArgument = () => {
77+
validator_wrong_arg.validate();
78+
}
79+
80+
expect(() => throwsInvalidArgument()).toThrowError(IncorrectArgumentTypeError);
4781
});
4882

4983
it('should validate select', () => {

0 commit comments

Comments
 (0)