2023-06-07 11:54:50 +08:00
|
|
|
import type { ValidateMessages } from 'rc-field-form/es/interface';
|
2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2020-08-31 17:41:56 +08:00
|
|
|
import { act } from 'react-dom/test-utils';
|
2023-01-03 09:56:47 +08:00
|
|
|
import scrollIntoView from 'scroll-into-view-if-needed';
|
2020-08-31 17:41:56 +08:00
|
|
|
import ConfigProvider from '..';
|
2023-01-03 09:56:47 +08:00
|
|
|
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
|
2023-06-07 11:54:50 +08:00
|
|
|
import Button from '../../button';
|
2022-11-11 18:15:02 +08:00
|
|
|
import type { FormInstance } from '../../form';
|
2020-08-31 17:41:56 +08:00
|
|
|
import Form from '../../form';
|
2022-11-23 11:04:23 +08:00
|
|
|
import Input from '../../input';
|
2023-02-24 18:22:48 +08:00
|
|
|
import InputNumber from '../../input-number';
|
2023-06-07 11:54:50 +08:00
|
|
|
import zhCN from '../../locale/zh_CN';
|
2020-08-31 17:41:56 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.mock('scroll-into-view-if-needed');
|
2023-01-03 09:56:47 +08:00
|
|
|
|
2020-10-24 14:27:49 +08:00
|
|
|
describe('ConfigProvider.Form', () => {
|
2023-01-03 09:56:47 +08:00
|
|
|
(scrollIntoView as any).mockImplementation(() => {});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
(scrollIntoView as any).mockReset();
|
|
|
|
});
|
|
|
|
|
2021-06-01 10:11:24 +08:00
|
|
|
beforeAll(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2021-06-01 10:11:24 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2023-01-03 09:56:47 +08:00
|
|
|
(scrollIntoView as any).mockRestore();
|
2021-06-01 10:11:24 +08:00
|
|
|
});
|
|
|
|
|
2020-08-31 17:41:56 +08:00
|
|
|
describe('form validateMessages', () => {
|
2022-08-22 22:54:38 +08:00
|
|
|
const renderComponent = ({ validateMessages }: { validateMessages?: any }) => {
|
2022-11-11 18:15:02 +08:00
|
|
|
const formRef = React.createRef<FormInstance>();
|
2022-04-06 11:07:15 +08:00
|
|
|
const { container } = render(
|
2020-08-31 17:41:56 +08:00
|
|
|
<ConfigProvider locale={zhCN} form={{ validateMessages }}>
|
|
|
|
<Form ref={formRef} initialValues={{ age: 18 }}>
|
|
|
|
<Form.Item name="test" label="姓名" rules={[{ required: true }]}>
|
|
|
|
<input />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item name="age" label="年龄" rules={[{ type: 'number', len: 17 }]}>
|
|
|
|
<input />
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
2022-08-22 22:54:38 +08:00
|
|
|
return [container, formRef] as const;
|
2020-08-31 17:41:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
it('set locale zhCN', async () => {
|
2022-04-06 11:07:15 +08:00
|
|
|
const [container, formRef] = renderComponent({});
|
2020-08-31 17:41:56 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
try {
|
2022-08-22 22:54:38 +08:00
|
|
|
await formRef.current?.validateFields();
|
2020-08-31 17:41:56 +08:00
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2020-08-31 17:41:56 +08:00
|
|
|
await Promise.resolve();
|
|
|
|
});
|
|
|
|
|
2022-07-04 13:32:48 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-07-04 13:32:48 +08:00
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
expect(container.querySelector('.ant-form-item-explain')).toHaveTextContent('请输入姓名');
|
2020-08-31 17:41:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('set locale zhCN and set form validateMessages one item, other use default message', async () => {
|
2022-04-06 11:07:15 +08:00
|
|
|
const [container, formRef] = renderComponent({ validateMessages: { required: '必须' } });
|
2020-08-31 17:41:56 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
try {
|
2022-08-22 22:54:38 +08:00
|
|
|
await formRef.current?.validateFields();
|
2020-08-31 17:41:56 +08:00
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2020-08-31 17:41:56 +08:00
|
|
|
await Promise.resolve();
|
|
|
|
});
|
|
|
|
|
2022-07-04 13:32:48 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2022-07-04 13:32:48 +08:00
|
|
|
});
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
const explains = Array.from(container.querySelectorAll('.ant-form-item-explain'));
|
|
|
|
|
|
|
|
expect(explains[0]).toHaveTextContent('必须');
|
|
|
|
expect(explains[explains.length - 1]).toHaveTextContent('年龄必须等于17');
|
2020-08-31 17:41:56 +08:00
|
|
|
});
|
2023-02-24 18:22:48 +08:00
|
|
|
|
|
|
|
it('nested description should use the default value of this warehouse first', async () => {
|
|
|
|
const validateMessages: ValidateMessages = {
|
|
|
|
number: {
|
|
|
|
// eslint-disable-next-line no-template-curly-in-string
|
|
|
|
max: '${label} 最大值为 ${max}',
|
|
|
|
/**
|
|
|
|
* Intentionally not filling `range` to test default message
|
|
|
|
* default: https://github.com/ant-design/ant-design/blob/12596a06f2ff88d8a27e72f6f9bac7c63a0b2ece/components/locale/en_US.ts#L123
|
|
|
|
*/
|
|
|
|
// range:
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const formRef = React.createRef<FormInstance>();
|
|
|
|
const { container } = render(
|
|
|
|
<ConfigProvider form={{ validateMessages }}>
|
|
|
|
<Form ref={formRef} initialValues={{ age: 1, rate: 6 }}>
|
|
|
|
<Form.Item name="rate" rules={[{ type: 'number', max: 5 }]}>
|
|
|
|
<InputNumber />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item name="age" rules={[{ type: 'number', max: 99, min: 18 }]}>
|
|
|
|
<InputNumber />
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
try {
|
|
|
|
await formRef.current?.validateFields();
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2023-02-24 18:22:48 +08:00
|
|
|
await Promise.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.runAllTimers();
|
2023-02-24 18:22:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(container.querySelectorAll('.ant-form-item-explain')).toHaveLength(2);
|
|
|
|
expect(container.querySelectorAll('.ant-form-item-explain')[0]).toHaveTextContent(
|
|
|
|
'rate 最大值为 5',
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.ant-form-item-explain')[1]).toHaveTextContent(
|
|
|
|
'age must be between 18-99',
|
|
|
|
);
|
|
|
|
});
|
2020-08-31 17:41:56 +08:00
|
|
|
});
|
2020-10-24 14:27:49 +08:00
|
|
|
|
|
|
|
describe('form requiredMark', () => {
|
2022-08-22 22:54:38 +08:00
|
|
|
it('set requiredMark optional', () => {
|
|
|
|
const { container } = render(
|
2020-10-24 14:27:49 +08:00
|
|
|
<ConfigProvider form={{ requiredMark: 'optional' }}>
|
|
|
|
<Form initialValues={{ age: 18 }}>
|
|
|
|
<Form.Item name="age" label="年龄" rules={[{ type: 'number', len: 17 }]}>
|
|
|
|
<input />
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
2022-08-22 22:54:38 +08:00
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2020-10-24 14:27:49 +08:00
|
|
|
});
|
|
|
|
});
|
2021-11-11 17:51:33 +08:00
|
|
|
|
|
|
|
describe('form colon', () => {
|
2022-08-22 22:54:38 +08:00
|
|
|
it('set colon false', () => {
|
|
|
|
const { container } = render(
|
2021-11-11 17:51:33 +08:00
|
|
|
<ConfigProvider form={{ colon: false }}>
|
|
|
|
<Form>
|
|
|
|
<Form.Item label="没有冒号">
|
|
|
|
<input />
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
2022-08-22 22:54:38 +08:00
|
|
|
expect(container.querySelector('.ant-form-item-no-colon')).toBeTruthy();
|
2021-11-11 17:51:33 +08:00
|
|
|
});
|
|
|
|
|
2022-08-22 22:54:38 +08:00
|
|
|
it('set colon default', () => {
|
|
|
|
const { container } = render(
|
2021-11-11 17:51:33 +08:00
|
|
|
<ConfigProvider>
|
|
|
|
<Form>
|
|
|
|
<Form.Item label="姓名">
|
|
|
|
<input />
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
2022-08-22 22:54:38 +08:00
|
|
|
expect(container.querySelector('.ant-form-item-no-colon')).toBeFalsy();
|
2021-11-11 17:51:33 +08:00
|
|
|
});
|
|
|
|
});
|
2022-11-23 11:04:23 +08:00
|
|
|
|
|
|
|
describe('form disabled', () => {
|
|
|
|
it('set Input enabled', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Form disabled>
|
|
|
|
<ConfigProvider componentDisabled={false}>
|
|
|
|
<Form.Item name="input1" label="启用">
|
|
|
|
<Input />
|
|
|
|
</Form.Item>
|
|
|
|
</ConfigProvider>
|
|
|
|
<Form.Item name="input" label="禁用">
|
|
|
|
<Input />
|
|
|
|
</Form.Item>
|
|
|
|
</Form>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(container.querySelector('#input1[disabled]')).toBeFalsy();
|
|
|
|
expect(container.querySelector('#input[disabled]')).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
2023-01-03 09:56:47 +08:00
|
|
|
|
|
|
|
describe('form scrollToFirstError', () => {
|
|
|
|
it('set object, form not set', async () => {
|
|
|
|
(scrollIntoView as any).mockImplementation(() => {});
|
2023-06-07 21:59:21 +08:00
|
|
|
const onFinishFailed = jest.fn();
|
2023-01-03 09:56:47 +08:00
|
|
|
|
|
|
|
const { container } = render(
|
|
|
|
<ConfigProvider form={{ scrollToFirstError: { block: 'center' } }}>
|
|
|
|
<Form onFinishFailed={onFinishFailed}>
|
|
|
|
<Form.Item name="test" rules={[{ required: true }]}>
|
|
|
|
<input />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item>
|
|
|
|
<Button htmlType="submit">Submit</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(scrollIntoView).not.toHaveBeenCalled();
|
|
|
|
fireEvent.submit(container.querySelector('form')!);
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
const inputNode = document.getElementById('test');
|
|
|
|
expect(scrollIntoView).toHaveBeenCalledWith(inputNode, {
|
|
|
|
block: 'center',
|
|
|
|
scrollMode: 'if-needed',
|
|
|
|
});
|
|
|
|
expect(onFinishFailed).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('not set, form set object', async () => {
|
|
|
|
(scrollIntoView as any).mockImplementation(() => {});
|
2023-06-07 21:59:21 +08:00
|
|
|
const onFinishFailed = jest.fn();
|
2023-01-03 09:56:47 +08:00
|
|
|
|
|
|
|
const { container } = render(
|
|
|
|
<ConfigProvider>
|
|
|
|
<Form scrollToFirstError={{ block: 'center' }} onFinishFailed={onFinishFailed}>
|
|
|
|
<Form.Item name="test" rules={[{ required: true }]}>
|
|
|
|
<input />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item>
|
|
|
|
<Button htmlType="submit">Submit</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(scrollIntoView).not.toHaveBeenCalled();
|
|
|
|
fireEvent.submit(container.querySelector('form')!);
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
const inputNode = document.getElementById('test');
|
|
|
|
expect(scrollIntoView).toHaveBeenCalledWith(inputNode, {
|
|
|
|
block: 'center',
|
|
|
|
scrollMode: 'if-needed',
|
|
|
|
});
|
|
|
|
expect(onFinishFailed).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('set object, form set false', async () => {
|
|
|
|
(scrollIntoView as any).mockImplementation(() => {});
|
2023-06-07 21:59:21 +08:00
|
|
|
const onFinishFailed = jest.fn();
|
2023-01-03 09:56:47 +08:00
|
|
|
|
|
|
|
const { container } = render(
|
|
|
|
<ConfigProvider form={{ scrollToFirstError: { block: 'center' } }}>
|
|
|
|
<Form scrollToFirstError={false} onFinishFailed={onFinishFailed}>
|
|
|
|
<Form.Item name="test" rules={[{ required: true }]}>
|
|
|
|
<input />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item>
|
|
|
|
<Button htmlType="submit">Submit</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(scrollIntoView).not.toHaveBeenCalled();
|
|
|
|
fireEvent.submit(container.querySelector('form')!);
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
expect(scrollIntoView).not.toHaveBeenCalled();
|
|
|
|
expect(onFinishFailed).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2020-08-31 17:41:56 +08:00
|
|
|
});
|