ant-design/components/config-provider/__tests__/form.test.js
afc163 30ac6bd4e4
test: wrap React.StrictMode for test cases (#35026)
* test: React StrictMode

* test: fix Spin test

* chore: wrapper enzyme

* test: fix setState

* test: more test cover

* test: more test cover

* test: more test cover

* test: more test cover

* test: more test cover

* test: more test case

* test: more test case

* test: more test case

* test: more test case

* test: more test case

* test: more test case

* test: more test case

* test: more test case

* test: more test case

* test: more test case

* test: more test case

* test: more test case

* test: disable part of it

* test: fix test & add placeholder

* test: Use orign enzyme mount

Co-authored-by: zombiej <smith3816@gmail.com>
2022-04-18 21:02:11 +08:00

126 lines
3.4 KiB
JavaScript

import React from 'react';
import { mount } from 'enzyme';
import { act } from 'react-dom/test-utils';
import { render } from '../../../tests/utils';
import ConfigProvider from '..';
import zhCN from '../../locale/zh_CN';
import Form from '../../form';
describe('ConfigProvider.Form', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
describe('form validateMessages', () => {
const renderComponent = ({ validateMessages }) => {
const formRef = React.createRef();
const { container } = render(
<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>,
);
return [container, formRef];
};
it('set locale zhCN', async () => {
const [container, formRef] = renderComponent({});
await act(async () => {
try {
await formRef.current.validateFields();
} catch (e) {
// Do nothing
}
});
await act(async () => {
jest.runAllTimers();
await Promise.resolve();
});
expect(container.querySelector('.ant-form-item-explain')).toHaveTextContent('请输入姓名');
});
it('set locale zhCN and set form validateMessages one item, other use default message', async () => {
const [container, formRef] = renderComponent({ validateMessages: { required: '必须' } });
await act(async () => {
try {
await formRef.current.validateFields();
} catch (e) {
// Do nothing
}
});
await act(async () => {
jest.runAllTimers();
await Promise.resolve();
});
const explains = Array.from(container.querySelectorAll('.ant-form-item-explain'));
expect(explains[0]).toHaveTextContent('必须');
expect(explains[explains.length - 1]).toHaveTextContent('年龄必须等于17');
});
});
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>,
);
expect(wrapper.render()).toMatchSnapshot();
});
});
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();
});
});
});