-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax.test.tsx
97 lines (81 loc) · 2.56 KB
/
max.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import React from 'react';
import { mount } from 'enzyme';
import {
Validator,
ValidatorArea,
ValidatorAreaProps,
IncorrectArgumentTypeError,
max
} from '../../../src';
import tick from '../../common/tick';
describe('test max rule', () => {
beforeEach(() => {
Validator.extend('max', max);
});
it('should always validate inputs and not validate non-inputs', async () => {
const input = document.createElement('input');
const meter = document.createElement('meter');
const output = document.createElement('output');
const progress = document.createElement('progress');
const canvas = document.createElement('canvas');
input.value = '7';
output.value = '7';
meter.value = 6;
meter.max = 10;
progress.value = 6;
progress.max = 10;
const validator_input = new Validator([
input
],
['max:5'],
'');
const validator_meter = new Validator([
meter
],
['max:5'],
'');
const validator_output = new Validator([
output
],
['max:5'],
'');
const validator_progress = new Validator([
progress
],
['max:5'],
'');
const validator_canvas = new Validator([
canvas
],
['max:5'],
'');
const validator_wrong_arg = new Validator([
input
],
['max:foo'],
'');
await validator_input.validate();
expect(validator_input.getErrors().length).toBe(1);
await validator_meter.validate();
expect(validator_meter.getErrors().length).toBe(1);
await validator_output.validate();
expect(validator_output.getErrors().length).toBe(1);
await validator_progress.validate();
expect(validator_progress.getErrors().length).toBe(1);
await validator_canvas.validate();
expect(validator_canvas.getErrors().length).toBe(0);
await expect( validator_wrong_arg.validate()).rejects.toBeInstanceOf(IncorrectArgumentTypeError);
});
it('should validate select', async () => {
const area = mount<ValidatorArea, ValidatorAreaProps>(
<ValidatorArea rules="max:3">
<select name="test">
<option value="4" selected>Option</option>
</select>
</ValidatorArea>
);
area.find('select').simulate('blur');
await tick();
expect(area.state().errors.length).toBe(1);
});
});