-
-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathvalidate-warning.test.tsx
More file actions
59 lines (56 loc) · 1.89 KB
/
validate-warning.test.tsx
File metadata and controls
59 lines (56 loc) · 1.89 KB
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
import React from 'react';
import Form from '../src';
import InfoField, { Input } from './common/InfoField';
import { changeValue, getInput, matchError } from './common';
import type { FormInstance, Rule } from '../src/interface';
import { render } from '@testing-library/react';
jest.mock('../src/utils/delayUtil');
describe('Form.WarningValidate', () => {
it('required', async () => {
const form = React.createRef<FormInstance>();
const { container } = render(
<Form ref={form}>
<InfoField name="name" rules={[{ required: true, warningOnly: true }]}>
<Input />
</InfoField>
</Form>,
);
await changeValue(getInput(container), ['bamboo', '']);
matchError(container, false, "'name' is required");
expect(form.current?.getFieldWarning('name')).toEqual(["'name' is required"]);
});
describe('validateFirst should not block error', () => {
const testValidateFirst = (
name: string,
validateFirst: boolean | 'parallel',
additionalRule?: Rule,
errorMessage?: string,
) => {
it(name, async () => {
const rules: Rule[] = [
additionalRule,
{ type: 'string', len: 10, warningOnly: true },
{ type: 'url' },
{ type: 'string', len: 20, warningOnly: true },
];
const { container } = render(
<Form>
<InfoField name="name" validateFirst={validateFirst} rules={rules.filter(Boolean)}>
<Input />
</InfoField>
</Form>,
);
await changeValue(getInput(container), 'bamboo');
matchError(container, errorMessage || "'name' is not a valid url", false);
});
};
testValidateFirst('default', true);
testValidateFirst(
'default',
true,
{ type: 'string', len: 3 },
"'name' must be exactly 3 characters",
);
testValidateFirst('parallel', 'parallel');
});
});