|
1 | 1 | import React from 'react';
|
2 | 2 | import { act } from 'react-dom/test-utils';
|
3 |
| -import { mount } from 'enzyme'; |
| 3 | +import { mount, ReactWrapper } from 'enzyme'; |
4 | 4 | import { resetWarned } from 'rc-util/lib/warning';
|
5 |
| -import Form, { Field, List } from '../src'; |
| 5 | +import Form, { Field, List, FormProps } from '../src'; |
| 6 | +import { ListField, ListOperations, ListProps } from '../src/List'; |
| 7 | +import { Meta } from '../src/interface'; |
6 | 8 | import { Input } from './common/InfoField';
|
7 | 9 | import { changeValue, getField } from './common';
|
8 | 10 | import timeout from './common/timeout';
|
9 | 11 |
|
10 | 12 | describe('Form.List', () => {
|
11 | 13 | let form;
|
12 | 14 |
|
13 |
| - function generateForm(renderList, formProps, listProps) { |
| 15 | + function generateForm( |
| 16 | + renderList?: ( |
| 17 | + fields: ListField[], |
| 18 | + operations: ListOperations, |
| 19 | + meta: Meta, |
| 20 | + ) => JSX.Element | React.ReactNode, |
| 21 | + formProps?: FormProps, |
| 22 | + listProps?: Partial<ListProps>, |
| 23 | + ): [ReactWrapper, () => ReactWrapper] { |
14 | 24 | const wrapper = mount(
|
15 | 25 | <div>
|
16 | 26 | <Form
|
@@ -523,7 +533,7 @@ describe('Form.List', () => {
|
523 | 533 | it('warning if children is not function', () => {
|
524 | 534 | const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
525 | 535 |
|
526 |
| - generateForm(<div />); |
| 536 | + generateForm(<div /> as any); |
527 | 537 |
|
528 | 538 | expect(errorSpy).toHaveBeenCalledWith('Warning: Form.List only accepts function as children.');
|
529 | 539 |
|
@@ -605,4 +615,35 @@ describe('Form.List', () => {
|
605 | 615 | expect(currentValue).toEqual([undefined]);
|
606 | 616 | expect(currentMeta.errors).toEqual(['Bamboo Light']);
|
607 | 617 | });
|
| 618 | + |
| 619 | + it('Nest list remove should trigger correct onValuesChange', () => { |
| 620 | + const onValuesChange = jest.fn(); |
| 621 | + |
| 622 | + const [wrapper] = generateForm( |
| 623 | + (fields, operation) => ( |
| 624 | + <div> |
| 625 | + {fields.map(field => ( |
| 626 | + <Field {...field} name={[field.name, 'first']}> |
| 627 | + <Input /> |
| 628 | + </Field> |
| 629 | + ))} |
| 630 | + <button |
| 631 | + type="button" |
| 632 | + onClick={() => { |
| 633 | + operation.remove(1); |
| 634 | + }} |
| 635 | + /> |
| 636 | + </div> |
| 637 | + ), |
| 638 | + { |
| 639 | + onValuesChange, |
| 640 | + initialValues: { |
| 641 | + list: [{ first: 'light' }, { first: 'bamboo' }], |
| 642 | + }, |
| 643 | + }, |
| 644 | + ); |
| 645 | + |
| 646 | + wrapper.find('button').simulate('click'); |
| 647 | + expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { list: [{ first: 'light' }] }); |
| 648 | + }); |
608 | 649 | });
|
0 commit comments