|
1 |
| -import React from 'react'; |
| 1 | +import React, { useState } from 'react'; |
2 | 2 | import { mount } from 'enzyme';
|
3 | 3 | import type { FormInstance } from '../src';
|
4 | 4 | import { List } from '../src';
|
5 | 5 | import Form, { Field } from '../src';
|
6 | 6 | import timeout from './common/timeout';
|
7 | 7 | import { act } from 'react-dom/test-utils';
|
8 | 8 | import { Input } from './common/InfoField';
|
| 9 | +import { stringify } from '../src/useWatch'; |
9 | 10 |
|
10 | 11 | describe('useWatch', () => {
|
11 | 12 | let staticForm: FormInstance<any>;
|
@@ -350,4 +351,45 @@ describe('useWatch', () => {
|
350 | 351 |
|
351 | 352 | expect(updateA > updateB).toBeTruthy();
|
352 | 353 | });
|
| 354 | + |
| 355 | + it('mount while unmount', () => { |
| 356 | + const Demo = () => { |
| 357 | + const [form] = Form.useForm(); |
| 358 | + const [type, setType] = useState(true); |
| 359 | + const name = Form.useWatch<string>('name', form); |
| 360 | + |
| 361 | + return ( |
| 362 | + <Form form={form}> |
| 363 | + <button type="button" onClick={() => setType(c => !c)}> |
| 364 | + type |
| 365 | + </button> |
| 366 | + {type && ( |
| 367 | + <Field name="name" key="a"> |
| 368 | + <Input /> |
| 369 | + </Field> |
| 370 | + )} |
| 371 | + {!type && ( |
| 372 | + <Field name="name" key="b"> |
| 373 | + <Input /> |
| 374 | + </Field> |
| 375 | + )} |
| 376 | + <div className="value">{name}</div> |
| 377 | + </Form> |
| 378 | + ); |
| 379 | + }; |
| 380 | + |
| 381 | + const wrapper = mount(<Demo />); |
| 382 | + wrapper |
| 383 | + .find('input') |
| 384 | + .first() |
| 385 | + .simulate('change', { target: { value: 'bamboo' } }); |
| 386 | + wrapper.find('button').at(0).simulate('click'); |
| 387 | + expect(wrapper.find('.value').text()).toEqual('bamboo'); |
| 388 | + }); |
| 389 | + it('stringify error', () => { |
| 390 | + const obj: any = {}; |
| 391 | + obj.name = obj; |
| 392 | + const str = stringify(obj); |
| 393 | + expect(typeof str === 'number').toBeTruthy(); |
| 394 | + }); |
353 | 395 | });
|
0 commit comments