You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.5 KiB
128 lines
3.5 KiB
2 years ago
|
import React from 'react';
|
||
4 years ago
|
import { act } from 'react-dom/test-utils';
|
||
|
import ConfigProvider from '..';
|
||
2 years ago
|
import { render } from '../../../tests/utils';
|
||
4 years ago
|
import Form from '../../form';
|
||
2 years ago
|
import zhCN from '../../locale/zh_CN';
|
||
4 years ago
|
|
||
4 years ago
|
describe('ConfigProvider.Form', () => {
|
||
4 years ago
|
beforeAll(() => {
|
||
|
jest.useFakeTimers();
|
||
|
});
|
||
|
|
||
|
afterAll(() => {
|
||
|
jest.useRealTimers();
|
||
|
});
|
||
|
|
||
4 years ago
|
describe('form validateMessages', () => {
|
||
2 years ago
|
const renderComponent = ({ validateMessages }: { validateMessages?: any }) => {
|
||
|
const formRef = React.createRef<any>();
|
||
3 years ago
|
const { container } = render(
|
||
4 years ago
|
<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>,
|
||
|
);
|
||
2 years ago
|
return [container, formRef] as const;
|
||
4 years ago
|
};
|
||
|
|
||
|
it('set locale zhCN', async () => {
|
||
3 years ago
|
const [container, formRef] = renderComponent({});
|
||
4 years ago
|
|
||
|
await act(async () => {
|
||
|
try {
|
||
2 years ago
|
await formRef.current?.validateFields();
|
||
4 years ago
|
} catch (e) {
|
||
|
// Do nothing
|
||
|
}
|
||
|
});
|
||
|
|
||
|
await act(async () => {
|
||
|
jest.runAllTimers();
|
||
|
await Promise.resolve();
|
||
|
});
|
||
|
|
||
2 years ago
|
act(() => {
|
||
|
jest.runAllTimers();
|
||
|
});
|
||
|
|
||
3 years ago
|
expect(container.querySelector('.ant-form-item-explain')).toHaveTextContent('请输入姓名');
|
||
4 years ago
|
});
|
||
|
|
||
|
it('set locale zhCN and set form validateMessages one item, other use default message', async () => {
|
||
3 years ago
|
const [container, formRef] = renderComponent({ validateMessages: { required: '必须' } });
|
||
4 years ago
|
|
||
|
await act(async () => {
|
||
|
try {
|
||
2 years ago
|
await formRef.current?.validateFields();
|
||
4 years ago
|
} catch (e) {
|
||
|
// Do nothing
|
||
|
}
|
||
|
});
|
||
|
|
||
|
await act(async () => {
|
||
|
jest.runAllTimers();
|
||
|
await Promise.resolve();
|
||
|
});
|
||
|
|
||
2 years ago
|
act(() => {
|
||
|
jest.runAllTimers();
|
||
|
});
|
||
|
|
||
3 years ago
|
const explains = Array.from(container.querySelectorAll('.ant-form-item-explain'));
|
||
|
|
||
|
expect(explains[0]).toHaveTextContent('必须');
|
||
|
expect(explains[explains.length - 1]).toHaveTextContent('年龄必须等于17');
|
||
4 years ago
|
});
|
||
|
});
|
||
4 years ago
|
|
||
|
describe('form requiredMark', () => {
|
||
2 years ago
|
it('set requiredMark optional', () => {
|
||
|
const { container } = render(
|
||
4 years ago
|
<ConfigProvider form={{ requiredMark: 'optional' }}>
|
||
|
<Form initialValues={{ age: 18 }}>
|
||
|
<Form.Item name="age" label="年龄" rules={[{ type: 'number', len: 17 }]}>
|
||
|
<input />
|
||
|
</Form.Item>
|
||
|
</Form>
|
||
|
</ConfigProvider>,
|
||
|
);
|
||
2 years ago
|
expect(container.firstChild).toMatchSnapshot();
|
||
4 years ago
|
});
|
||
|
});
|
||
3 years ago
|
|
||
|
describe('form colon', () => {
|
||
2 years ago
|
it('set colon false', () => {
|
||
|
const { container } = render(
|
||
3 years ago
|
<ConfigProvider form={{ colon: false }}>
|
||
|
<Form>
|
||
|
<Form.Item label="没有冒号">
|
||
|
<input />
|
||
|
</Form.Item>
|
||
|
</Form>
|
||
|
</ConfigProvider>,
|
||
|
);
|
||
2 years ago
|
expect(container.querySelector('.ant-form-item-no-colon')).toBeTruthy();
|
||
3 years ago
|
});
|
||
|
|
||