|
1 |
| -import React from 'react'; |
| 1 | +import React, { useState } from 'react'; |
2 | 2 | import { fireEvent, render, act } from '@testing-library/react';
|
3 | 3 | import { resetWarned } from '@rc-component/util/lib/warning';
|
4 | 4 | import Form, { Field, List } from '../src';
|
@@ -937,4 +937,67 @@ describe('Form.List', () => {
|
937 | 937 |
|
938 | 938 | expect(formRef.current!.getFieldValue('list')).toEqual([{ user: '1' }, { user: '3' }]);
|
939 | 939 | });
|
| 940 | + |
| 941 | + it('list unmount', async () => { |
| 942 | + const valueRef = React.createRef(); |
| 943 | + |
| 944 | + const Demo = () => { |
| 945 | + const [isShow, setIsShow] = useState(true); |
| 946 | + return ( |
| 947 | + <Form |
| 948 | + initialValues={{ |
| 949 | + users: [ |
| 950 | + { name: 'a', age: '1' }, |
| 951 | + { name: 'b', age: '2' }, |
| 952 | + ], |
| 953 | + }} |
| 954 | + onFinish={values => { |
| 955 | + valueRef.current = values; |
| 956 | + }} |
| 957 | + > |
| 958 | + <Form.List name="users"> |
| 959 | + {fields => { |
| 960 | + return fields.map(field => ( |
| 961 | + <div key={field.key} style={{ display: 'flex', gap: 10 }}> |
| 962 | + <InfoField name={[field.name, 'name']}> |
| 963 | + <Input /> |
| 964 | + </InfoField> |
| 965 | + {isShow && ( |
| 966 | + <InfoField name={[field.name, 'age']}> |
| 967 | + <Input /> |
| 968 | + </InfoField> |
| 969 | + )} |
| 970 | + </div> |
| 971 | + )); |
| 972 | + }} |
| 973 | + </Form.List> |
| 974 | + <button data-testid="hide" type="button" onClick={() => setIsShow(c => !c)}> |
| 975 | + 隐藏 |
| 976 | + </button> |
| 977 | + <button type="submit" data-testid="submit"> |
| 978 | + Submit |
| 979 | + </button> |
| 980 | + </Form> |
| 981 | + ); |
| 982 | + }; |
| 983 | + |
| 984 | + const { queryByTestId } = render(<Demo />); |
| 985 | + fireEvent.click(queryByTestId('submit')); |
| 986 | + await act(async () => { |
| 987 | + await timeout(); |
| 988 | + }); |
| 989 | + expect(valueRef.current).toEqual({ |
| 990 | + users: [ |
| 991 | + { name: 'a', age: '1' }, |
| 992 | + { name: 'b', age: '2' }, |
| 993 | + ], |
| 994 | + }); |
| 995 | + |
| 996 | + fireEvent.click(queryByTestId('hide')); |
| 997 | + fireEvent.click(queryByTestId('submit')); |
| 998 | + await act(async () => { |
| 999 | + await timeout(); |
| 1000 | + }); |
| 1001 | + expect(valueRef.current).toEqual({ users: [{ name: 'a' }, { name: 'b' }] }); |
| 1002 | + }); |
940 | 1003 | });
|
0 commit comments