import React from 'react'; import ProForm, { ProFormText, ProFormList, ProFormDependency, ProFormGroup, } from '@ant-design/pro-form'; import { act } from 'react-dom/test-utils'; import { mount } from 'enzyme'; import { waitForComponentToPaint } from '../util'; import { SnippetsOutlined, CloseOutlined } from '@ant-design/icons'; import { Form } from 'antd'; import _ from 'lodash'; import type { NamePath } from 'antd/es/form/interface'; describe('ProForm List', () => { it('♨️ ProForm.List', async () => { const fn = jest.fn(); const html = mount( { fn(Object.keys(values['users'][0])); }} > , ); act(() => { html.find('.ant-btn.ant-btn-primary').simulate('click'); }); await waitForComponentToPaint(html); expect(fn).toBeCalledWith(['name', 'nickName']); }); it('♨️ ProForm.List add button', async () => { const fn = jest.fn(); const html = mount( { fn(Object.keys(values['users'][1])); }} > , ); await waitForComponentToPaint(html); expect(html.find('.ant-btn.ant-pro-form-list-creator-button-bottom').exists()).toBeTruthy(); expect(html.find('.ant-btn.ant-pro-form-list-creator-button-top').exists()).toBeFalsy(); act(() => { html.find('.ant-btn.ant-pro-form-list-creator-button-bottom').simulate('click'); }); act(() => { html.find('.ant-btn.ant-btn-primary').simulate('click'); }); await waitForComponentToPaint(html); expect(fn).toBeCalledWith([]); }); it('♨️ ProForm.List render children', async () => { const fn = jest.fn(); const html = mount( { fn(values['users'][0]); }} > {(fields) => { return fields.map((filed) => ( <> )); }} , ); await waitForComponentToPaint(html); act(() => { html.find('.ant-btn.ant-btn-primary').simulate('click'); }); await waitForComponentToPaint(html); expect(fn).toBeCalledWith({ name: '1111', nickName: '1111', }); }); it('♨️ ProForm.List close button', async () => { const html = mount( , ); await waitForComponentToPaint(html); expect(html.find('.ant-btn.ant-pro-form-list-creator-button-bottom').exists()).toBeFalsy(); }); it('♨️ ProForm.List add button when creatorRecord', async () => { const fn = jest.fn(); const html = mount( { fn(values['users'][1]); }} > , ); await waitForComponentToPaint(html); act(() => { html.find('.ant-btn.ant-pro-form-list-creator-button-bottom').simulate('click'); }); act(() => { html.find('.ant-btn.ant-btn-primary').simulate('click'); }); await waitForComponentToPaint(html); expect(fn).toBeCalledWith({ name: '2222', nickName: '2222', }); }); it('♨️ ProForm.List add button on top', async () => { const fn = jest.fn(); const html = mount( { fn(Object.keys(values['users'][0] || {})); }} > , ); await waitForComponentToPaint(html); expect(html.find('.ant-btn.ant-pro-form-list-creator-button-top').exists()).toBeTruthy(); expect(html.find('.ant-btn.ant-pro-form-list-creator-button-bottom').exists()).toBeFalsy(); act(() => { html.find('.ant-btn.ant-pro-form-list-creator-button-top').simulate('click'); }); act(() => { html.find('.ant-btn.ant-btn-primary').simulate('click'); }); await waitForComponentToPaint(html); expect(fn).toBeCalledWith([]); }); it('♨️ ProForm.List copy to newline', async () => { const fn = jest.fn(); const html = mount( { fn(values['users'][1]); }} > , ); act(() => { html.find('.ant-pro-form-list-action .ant-pro-form-list-action-icon').at(0).simulate('click'); }); act(() => { html.find('.ant-btn.ant-btn-primary').simulate('click'); }); await waitForComponentToPaint(html); expect(fn).toBeCalledWith({ name: '1111', nickName: '1111', }); }); it('♨️ ProForm.List delete icon', async () => { const fn = jest.fn(); const html = mount( { fn(values['users'][0]); }} > , ); act(() => { html .find('.ant-pro-form-list-action') .at(0) .find('span.ant-pro-form-list-action-icon') .at(1) .simulate('click'); }); act(() => { html.find('.ant-btn.ant-btn-primary').simulate('click'); }); await waitForComponentToPaint(html); expect(fn).toBeCalledWith({ name: '2222', nickName: '2222', }); }); it('♨️ ProForm.List itemRender', async () => { const fn = jest.fn(); const html = mount( { fn(values['users'][0]); }} > { return (
{listDom} {action}
); }} name="users" label="用户信息" initialValue={[ { name: '1111', nickName: '1111', }, { name: '2222', nickName: '2222', }, ]} >
, ); await waitForComponentToPaint(html); expect(html.find('#test').exists()).toBe(true); }); it('♨️ ProForm.List in ProForm.List', async () => { const fn = jest.fn(); const html = mount( { fn(values['users'][0]['tag']); }} > { return (
{listDom} {action}
); }} name="users" label="用户信息" initialValue={[ { name: '1111', nickName: '1111', tag: [ { name: '1212', }, ], }, { name: '2222', nickName: '2222', tag: [ { name: '1212', }, ], }, ]} >
, ); await waitForComponentToPaint(html); act(() => { html.find('.ant-pro-form-list .ant-pro-form-list .ant-btn-dashed').at(0).simulate('click'); }); await waitForComponentToPaint(html); act(() => { html.find('.ant-btn.ant-btn-primary').simulate('click'); }); await waitForComponentToPaint(html); expect(fn).toBeCalledWith([ { name: '1212', }, { name: 'test', }, ]); }); it('♨️ ProForm.List support ProFormDependency', async () => { const fn = jest.fn(); const html = mount( {({ nickName }) => { console.log(nickName); if (!nickName) { return null; } fn(nickName); return ; }} , ); expect(html.find('input.ant-input').length).toBe(3); act(() => { html .find('input.ant-input') .at(2) .simulate('change', { target: { value: '222', }, }); }); await waitForComponentToPaint(html); expect(html.find('input.ant-input').length).toBe(4); expect(fn).toBeCalledWith('222'); }); it('♨️ ProForm.List support ProFormDependency2', async () => { const initialValues = { a: 1, b: 2, c: { a: 3, b: 4, c: { a: 5, }, d: [{ a: 6, b: 7 }], e: [{ a: 8, b: 9 }], }, }; const depName1: NamePath[] = [ 'a', 'b', ['c', 'a'], ['c', 'b'], ['c', 'c', 'a'], ['c', 'd'], ['c', 'e'], ]; const depName2: NamePath[] = ['a', 'b', ['c', 'a']]; const depName3: NamePath[] = ['a', 'b', ['c', 'a']]; const html = mount( {(depValues) => ( `} extra="a, b, c.a取自局部" >
                        {JSON.stringify(depValues, null, 2)}
                      
)}
{(depValues) => ( `} extra="a, b, c.a取自全局" >
                        {JSON.stringify(depValues, null, 2)}
                      
)}
`} > {(depValues) => (
                {JSON.stringify(depValues, null, 2)}
              
)}
, ); await waitForComponentToPaint(html); const namePaths2PropertyPaths = (name: NamePath[]) => { return name.map((item) => (Array.isArray(item) ? item.join('.') : item)); }; expect(html.find('code.case1').text()).toBe( JSON.stringify(_.pick(initialValues, namePaths2PropertyPaths(depName1)), null, 2), ); expect(html.find('code.case2').text()).toBe( JSON.stringify(_.pick(initialValues, namePaths2PropertyPaths(depName2)), null, 2), ); expect(html.find('code.case3').text()).toBe( JSON.stringify({ a: initialValues.c.d[0].a, b: initialValues.c.d[0].b, c: {} }, null, 2), ); }); it('♨️ ProForm.List support copyIconProps and deleteIconProps', async () => { const html = mount( , ); await waitForComponentToPaint(html); expect(html.find('.ant-pro-form-list-action').exists()).toBeFalsy(); }); it('♨️ ProForm.List support copyIconProps.icon and deleteIconProps.icon', async () => { const html = mount( , ); await waitForComponentToPaint(html); expect(html.find('.anticon-snippets').exists()).toBeTruthy(); expect(html.find('.anticon-close').exists()).toBeTruthy(); }); });