-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.test.tsx
53 lines (45 loc) · 1.45 KB
/
regex.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
import { mount } from 'enzyme';
import React from 'react';
import {
Validator,
ValidatorArea,
ValidatorAreaProps,
regex
} from '../../../src';
import tick from '../../common/tick';
describe('test regex rule', () => {
beforeEach(() => {
Validator.extend('regex', regex);
});
it('should always validate inputs and not validate non-inputs', async (): Promise<void> => {
const input = document.createElement('input');
const canvas = document.createElement('canvas');
input.value = 'foo,|bar';
const validator_input = new Validator([
input
],
['regex:(\\w)+,(\\w)+'],
'');
const validator_canvas = new Validator([
canvas
],
['regex:(\\w)+,(\\w)+'],
'');
await validator_input.validate();
expect(validator_input.getErrors().length).toBe(1);
await validator_canvas.validate();
expect(validator_canvas.getErrors().length).toBe(0);
});
it('should validate select', async () => {
const area = mount<ValidatorArea, ValidatorAreaProps>(
<ValidatorArea rules="regex:(\w)+,(\w)+">
<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);
});
});