2020-08-31 17:41:56 +08:00
|
|
|
import { mount } from 'enzyme';
|
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';
|
|
|
|
import ConfigProvider from '..';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { render } from '../../../tests/utils';
|
2020-08-31 17:41:56 +08:00
|
|
|
import Form from '../../form';
|
2022-06-22 14:57:09 +08:00
|
|
|
import zhCN from '../../locale/zh_CN';
|
2020-08-31 17:41:56 +08:00
|
|
|
|
2020-10-24 14:27:49 +08:00
|
|
|
describe('ConfigProvider.Form', () => {
|
2021-06-01 10:11:24 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2020-08-31 17:41:56 +08:00
|
|
|
describe('form validateMessages', () => {
|
2022-04-06 11:07:15 +08:00
|
|
|
const renderComponent = ({ validateMessages }) => {
|
2020-08-31 17:41:56 +08:00
|
|
|
const formRef = React.createRef();
|
|
|
|
|
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-04-06 11:07:15 +08:00
|
|
|
return [container, formRef];
|
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 {
|
|
|
|
await formRef.current.validateFields();
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await Promise.resolve();
|
|
|
|
});
|
|
|
|
|
2022-07-04 13:32:48 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
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 {
|
|
|
|
await formRef.current.validateFields();
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
await Promise.resolve();
|
|
|
|
});
|
|
|
|
|
2022-07-04 13:32:48 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
2020-10-24 14:27:49 +08:00
|
|
|
|
|
|
|
describe('form requiredMark', () => {
|
|
|
|
it('set requiredMark optional', async () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<ConfigProvider form={{ requiredMark: 'optional' }}>
|
|
|
|
<Form initialValues={{ age: 18 }}>
|
|
|
|
<Form.Item name="age" label="年龄" rules={[{ type: 'number', len: 17 }]}>
|
|
|
|
<input />
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
2021-12-04 17:06:36 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2020-10-24 14:27:49 +08:00
|
|
|
});
|
|
|
|
});
|
2021-11-11 17:51:33 +08:00
|
|
|
|
|
|
|
describe('form colon', () => {
|
|
|
|
it('set colon false', async () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<ConfigProvider form={{ colon: false }}>
|
|
|
|
<Form>
|
|
|
|
<Form.Item label="没有冒号">
|
|
|
|
<input />
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(wrapper.exists('.ant-form-item-no-colon')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('set colon default', async () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<ConfigProvider>
|
|
|
|
<Form>
|
|
|
|
<Form.Item label="姓名">
|
|
|
|
<input />
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(wrapper.exists('.ant-form-item-no-colon')).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
2020-08-31 17:41:56 +08:00
|
|
|
});
|