|
1 | 1 | import React, { useEffect } from 'react';
|
| 2 | +import { render } from '@testing-library/react'; |
2 | 3 | import { mount } from 'enzyme';
|
3 | 4 | import { act } from 'react-dom/test-utils';
|
4 | 5 | import Form, { Field, useForm } from '../src';
|
5 | 6 | import InfoField, { Input } from './common/InfoField';
|
6 | 7 | import { changeValue, matchError, getField } from './common';
|
7 | 8 | import timeout from './common/timeout';
|
8 |
| -import type { ValidateMessages } from '@/interface'; |
| 9 | +import type { FormInstance, ValidateMessages } from '../src/interface'; |
9 | 10 |
|
10 | 11 | describe('Form.Validate', () => {
|
11 | 12 | it('required', async () => {
|
@@ -867,4 +868,26 @@ describe('Form.Validate', () => {
|
867 | 868 | expect(onMetaChange).toHaveBeenNthCalledWith(3, true);
|
868 | 869 | expect(onMetaChange).toHaveBeenNthCalledWith(4, false);
|
869 | 870 | });
|
| 871 | + |
| 872 | + it('validateOnly', async () => { |
| 873 | + const formRef = React.createRef<FormInstance>(); |
| 874 | + const { container } = render( |
| 875 | + <Form ref={formRef}> |
| 876 | + <InfoField name="test" rules={[{ required: true }]}> |
| 877 | + <Input /> |
| 878 | + </InfoField> |
| 879 | + </Form>, |
| 880 | + ); |
| 881 | + |
| 882 | + // Validate only |
| 883 | + const result = await formRef.current.validateFields({ validateOnly: true }).catch(e => e); |
| 884 | + await timeout(); |
| 885 | + expect(result.errorFields).toHaveLength(1); |
| 886 | + expect(container.querySelector('.errors').textContent).toBeFalsy(); |
| 887 | + |
| 888 | + // Normal validate |
| 889 | + await formRef.current.validateFields().catch(e => e); |
| 890 | + await timeout(); |
| 891 | + expect(container.querySelector('.errors').textContent).toEqual(`'test' is required`); |
| 892 | + }); |
870 | 893 | });
|
0 commit comments