From 6cfad22de3512f3f2f27af9761d9bfc6f35fae32 Mon Sep 17 00:00:00 2001 From: ranqirong <274544338@qq.com> Date: Thu, 10 Oct 2024 13:46:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20FormItem=E6=94=AF=E6=8C=81=E4=BB=8E?= =?UTF-8?q?=E7=B1=BB=E7=9A=84=E9=BB=98=E8=AE=A4=E5=B1=9E=E6=80=A7=E4=B8=AD?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=A1=A8=E5=8D=95=E9=A1=B9=E7=9A=84name?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=EF=BC=8C=E4=BB=A5=E4=BE=BF=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E5=85=A8=E5=B1=80=E7=8A=B6=E6=80=81formItem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/renderers/wrapControl.test.tsx | 58 +++++++++++++++++++ .../amis-core/src/renderers/wrapControl.tsx | 4 +- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 packages/amis-core/__tests__/renderers/wrapControl.test.tsx diff --git a/packages/amis-core/__tests__/renderers/wrapControl.test.tsx b/packages/amis-core/__tests__/renderers/wrapControl.test.tsx new file mode 100644 index 000000000..2fbfeebbf --- /dev/null +++ b/packages/amis-core/__tests__/renderers/wrapControl.test.tsx @@ -0,0 +1,58 @@ +/* + * @author: ranqirong + * @date: 2024-10-10 + * @description: + */ +import React from 'react'; +import {wrapControl} from '../../src/renderers/wrapControl'; +import {render, screen} from '@testing-library/react'; +import {RootStoreContext} from '../../src/WithRootStore'; +import {RendererStore} from '../../src/store/index'; +import {FormItemProps} from '../../src/renderers/Item'; + +const renderComponent = (WrappedComponent: any, props: any) => { + const store = RendererStore.create({}); + return render( + + + + ); +}; + +describe('constructor', () => { + it('如果传入有defaultProps包含name属性,则创建FormItemStore', () => { + const OriginComponent = (props: FormItemProps) => { + // 用于断言formItem已被注册 + return
{props.formItem?.storeType}
; + }; + OriginComponent.defaultProps = { + name: 'test' + }; + const WrappedComponent = wrapControl(OriginComponent as any); + + renderComponent(WrappedComponent, {$schema: {}}); + expect(screen.getByText('FormItemStore')).toBeInTheDocument(); + }); + + it('如果$schema中包含name属性,则创建FormItemStore', () => { + const OriginComponent = (props: FormItemProps) => { + // 用于断言formItem已被注册 + return
{props.formItem?.storeType}
; + }; + const WrappedComponent = wrapControl(OriginComponent as any); + + renderComponent(WrappedComponent, {$schema: {name: 'test'}}); + expect(screen.getByText('FormItemStore')).toBeInTheDocument(); + }); + + it('如果$schema和defaultProps中都不包含name属性,则不创建FormItemStore', () => { + const OriginComponent = (props: FormItemProps) => { + // 用于断言formItem已被注册 + return
{props.formItem?.storeType}
; + }; + const WrappedComponent = wrapControl(OriginComponent as any); + + renderComponent(WrappedComponent, {$schema: {}}); + expect(screen.queryByText('FormItemStore')).not.toBeInTheDocument(); + }); +}); diff --git a/packages/amis-core/src/renderers/wrapControl.tsx b/packages/amis-core/src/renderers/wrapControl.tsx index ef5ccdd1f..7dd4ef93d 100644 --- a/packages/amis-core/src/renderers/wrapControl.tsx +++ b/packages/amis-core/src/renderers/wrapControl.tsx @@ -176,7 +176,9 @@ export function wrapControl< this.validate = this.validate.bind(this); this.flushChange = this.flushChange.bind(this); this.renderChild = this.renderChild.bind(this); - let name = this.props.$schema.name; + let name = + this.props.$schema.name || + (ComposedComponent.defaultProps as Record)?.name; // 如果 name 是表达式 // 扩充 each 用法