File size: 10,501 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
import React from 'react';
import type { ValidateMessages } from 'rc-field-form/es/interface';
import scrollIntoView from 'scroll-into-view-if-needed';
import ConfigProvider from '..';
import { act, fireEvent, render, waitFakeTimer } from '../../../tests/utils';
import Button from '../../button';
import type { FormInstance } from '../../form';
import Form from '../../form';
import Input from '../../input';
import InputNumber from '../../input-number';
import zhCN from '../../locale/zh_CN';
jest.mock('scroll-into-view-if-needed');
describe('ConfigProvider.Form', () => {
(scrollIntoView as any).mockImplementation(() => {});
beforeEach(() => {
(scrollIntoView as any).mockReset();
});
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
(scrollIntoView as any).mockRestore();
});
describe('form validateMessages', () => {
const renderComponent = ({ validateMessages }: { validateMessages?: any }) => {
const formRef = React.createRef<FormInstance>();
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] as const;
};
it('set locale zhCN', async () => {
const [container, formRef] = renderComponent({});
await act(async () => {
try {
await formRef.current?.validateFields();
} catch {
// Do nothing
}
});
await act(async () => {
jest.runAllTimers();
await Promise.resolve();
});
act(() => {
jest.runAllTimers();
});
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 {
// Do nothing
}
});
await act(async () => {
jest.runAllTimers();
await Promise.resolve();
});
act(() => {
jest.runAllTimers();
});
const explains = Array.from(container.querySelectorAll('.ant-form-item-explain'));
expect(explains[0]).toHaveTextContent('必须');
expect(explains[explains.length - 1]).toHaveTextContent('年龄必须等于17');
});
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 {
// Do nothing
}
});
await act(async () => {
jest.runAllTimers();
await Promise.resolve();
});
act(() => {
jest.runAllTimers();
});
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',
);
});
// https://github.com/ant-design/ant-design/issues/43210
it('should merge parent ConfigProvider validateMessages', async () => {
const MyForm = () => (
<Form>
<Form.Item name="name" label="Name" rules={[{ required: true }]}>
<Input />
</Form.Item>
<Button type="primary" htmlType="submit">
Submit
</Button>
</Form>
);
const { container, getAllByRole, getAllByText } = render(
<ConfigProvider>
<MyForm />
<ConfigProvider form={{ validateMessages: { required: 'Required' } }}>
<MyForm />
<ConfigProvider>
<MyForm />
</ConfigProvider>
</ConfigProvider>
</ConfigProvider>,
);
const submitButtons = getAllByRole('button');
expect(submitButtons).toHaveLength(3);
submitButtons.forEach(fireEvent.click);
await waitFakeTimer();
expect(container.querySelectorAll('.ant-form-item-explain-error')).toHaveLength(3);
expect(getAllByText('Please enter Name')).toHaveLength(1);
expect(getAllByText('Required')).toHaveLength(2);
});
});
describe('form requiredMark', () => {
it('set requiredMark optional', () => {
const { container } = render(
<ConfigProvider form={{ requiredMark: 'optional' }}>
<Form initialValues={{ age: 18 }}>
<Form.Item name="age" label="年龄" rules={[{ type: 'number', len: 17 }]}>
<input />
</Form.Item>
</Form>
</ConfigProvider>,
);
expect(container.firstChild).toMatchSnapshot();
});
});
describe('form colon', () => {
it('set colon false', () => {
const { container } = render(
<ConfigProvider form={{ colon: false }}>
<Form>
<Form.Item label="没有冒号">
<input />
</Form.Item>
</Form>
</ConfigProvider>,
);
expect(container.querySelector('.ant-form-item-no-colon')).toBeTruthy();
});
it('set colon default', () => {
const { container } = render(
<ConfigProvider>
<Form>
<Form.Item label="姓名">
<input />
</Form.Item>
</Form>
</ConfigProvider>,
);
expect(container.querySelector('.ant-form-item-no-colon')).toBeFalsy();
});
});
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();
});
});
describe('form scrollToFirstError', () => {
it('set object, form not set', async () => {
(scrollIntoView as any).mockImplementation(() => {});
const onFinishFailed = jest.fn();
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(() => {});
const onFinishFailed = jest.fn();
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(() => {});
const onFinishFailed = jest.fn();
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();
});
});
});
|