import React from 'react'; import { mount } from 'enzyme'; import Form from '..'; describe('Form', () => { it('should display two message', () => { const rules = [ { pattern: /^\w+$/, message: 'Error message 1', }, { pattern: /^\w+$/, message: 'Error message 2', }, ]; let myForm; const Form1 = Form.create()(({ form }) => { myForm = form; return (
{form.getFieldDecorator('account', { initialValue: '+=-/', rules })()}
); }); const wrapper = mount(); myForm.validateFields(); wrapper.update(); expect(wrapper.render()).toMatchSnapshot(); }); it('should display custom message', () => { const rules = [ { pattern: /^$/, message: ( Account does not exist,{' '} Forgot account? ), }, ]; let myForm; const Form1 = Form.create()(({ form }) => { myForm = form; return (
{form.getFieldDecorator('account', { initialValue: 'antd', rules })()}
); }); const wrapper = mount(); myForm.validateFields(); wrapper.update(); expect(wrapper.render()).toMatchSnapshot(); }); it('support error message with reactNode', () => { let myForm; const Form1 = Form.create()(({ form }) => { myForm = form; return (
{form.getFieldDecorator('account')()}
); }); const wrapper = mount(); myForm.setFields({ account: { errors: [
Error 1
,
Error 2
], }, }); expect(wrapper.render()).toMatchSnapshot(); }); it('should print warning for not generating help and validateStatus automatically', () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const Form1 = Form.create()(({ form }) => { return (
{form.getFieldDecorator('account')()} {form.getFieldDecorator('account')()}
); }); mount(); expect(errorSpy).toBeCalledWith( 'Warning: `Form.Item` cannot generate `validateStatus` and `help` automatically, while there are more than one `getFieldDecorator` in it.', ); errorSpy.mockRestore(); }); // https://github.com/ant-design/ant-design/issues/14911 it('should not print warning for not generating help and validateStatus automatically when help or validateStatus is specified', () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const Form1 = Form.create()(({ form }) => { return (
{form.getFieldDecorator('account')()} {form.getFieldDecorator('account')()}
); }); mount(); expect(errorSpy).not.toHaveBeenCalled(); errorSpy.mockRestore(); }); });