|
1 |
| -import React, { useState } from 'react'; |
| 1 | +import React, { useRef, useState } from 'react'; |
2 | 2 | import { mount } from 'enzyme';
|
3 | 3 | import type { FormInstance } from '../src';
|
4 | 4 | import { List } from '../src';
|
@@ -219,6 +219,7 @@ describe('useWatch', () => {
|
219 | 219 | expect(errorSpy).toHaveBeenCalledWith(
|
220 | 220 | 'Warning: useWatch requires a form instance since it can not auto detect from context.',
|
221 | 221 | );
|
| 222 | + errorSpy.mockRestore(); |
222 | 223 | });
|
223 | 224 |
|
224 | 225 | it('no more render time', () => {
|
@@ -392,4 +393,38 @@ describe('useWatch', () => {
|
392 | 393 | const str = stringify(obj);
|
393 | 394 | expect(typeof str === 'number').toBeTruthy();
|
394 | 395 | });
|
| 396 | + it('first undefined', () => { |
| 397 | + const errorSpy = jest.spyOn(console, 'error'); |
| 398 | + const Demo = () => { |
| 399 | + const formRef = useRef(); |
| 400 | + const name = Form.useWatch('name', formRef.current); |
| 401 | + const [, setUpdate] = useState({}); |
| 402 | + |
| 403 | + return ( |
| 404 | + <> |
| 405 | + <div className="setUpdate" onClick={() => setUpdate({})} /> |
| 406 | + <div className="value">{name}</div> |
| 407 | + <Form ref={formRef} initialValues={{ name: 'default' }}> |
| 408 | + <Field name="name" key="a"> |
| 409 | + <Input /> |
| 410 | + </Field> |
| 411 | + </Form> |
| 412 | + </> |
| 413 | + ); |
| 414 | + }; |
| 415 | + |
| 416 | + const wrapper = mount(<Demo />); |
| 417 | + expect(wrapper.find('.value').text()).toEqual(''); |
| 418 | + wrapper.find('.setUpdate').at(0).simulate('click'); |
| 419 | + expect(wrapper.find('.value').text()).toEqual('default'); |
| 420 | + wrapper |
| 421 | + .find('input') |
| 422 | + .at(0) |
| 423 | + .simulate('change', { target: { value: 'bamboo' } }); |
| 424 | + expect(wrapper.find('.value').text()).toEqual('bamboo'); |
| 425 | + expect(errorSpy).not.toHaveBeenCalledWith( |
| 426 | + 'Warning: useWatch requires a form instance since it can not auto detect from context.', |
| 427 | + ); |
| 428 | + errorSpy.mockRestore(); |
| 429 | + }); |
395 | 430 | });
|
0 commit comments