From 3b0391e655ab8e44239ad45e91e4d71573be1843 Mon Sep 17 00:00:00 2001 From: ranqirong <274544338@qq.com> Date: Thu, 9 Jan 2025 10:28:59 +0800 Subject: [PATCH 1/5] =?UTF-8?q?fix(amis):=20table=E5=B5=8C=E5=A5=97?= =?UTF-8?q?=E5=9C=A8crud=E4=B8=8B=E9=9D=A2=EF=BC=8CsetValue=E6=97=B6?= =?UTF-8?q?=E9=80=9A=E8=BF=87crud=E8=BF=9B=E8=A1=8C=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=EF=BC=8C=E8=A7=A3=E5=86=B3crud=E7=9A=84setValue=E5=8A=A8?= =?UTF-8?q?=E4=BD=9C=E5=A4=B1=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes:11453 --- .../amis/__tests__/renderers/CRUD.test.tsx | 43 +++++++++++++++++++ packages/amis/src/renderers/Table/index.tsx | 20 ++++++--- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/packages/amis/__tests__/renderers/CRUD.test.tsx b/packages/amis/__tests__/renderers/CRUD.test.tsx index cedf6f1855a..39e931be8f3 100644 --- a/packages/amis/__tests__/renderers/CRUD.test.tsx +++ b/packages/amis/__tests__/renderers/CRUD.test.tsx @@ -1896,3 +1896,46 @@ test('25. CRUD Table Cell sync data to store', async () => { const listDoms2 = container.querySelectorAll('.cxd-ListItem-title'); expect(listDoms2.length).toEqual(0); }); + +describe.only('26. setValue 动作', () => { + it('table模式下,设置值后,页面能够自动更新', async () => { + render( + amisRender({ + type: 'crud', + id: 'crud', + data: { + label: '点击前' + }, + headerToolbar: [ + { + type: 'button', + label: '${label}', + onEvent: { + click: { + actions: [ + { + actionType: 'setValue', + componentId: 'crud', + args: { + value: {label: '点击后'} + } + } + ] + } + } + } + ] + }) + ); + + await waitFor(() => { + expect(screen.getByText('点击前')).toBeInTheDocument(); + }); + + fireEvent.click(screen.getByText('点击前')); + + await waitFor(() => { + expect(screen.getByText('点击后')).toBeInTheDocument(); + }); + }); +}); diff --git a/packages/amis/src/renderers/Table/index.tsx b/packages/amis/src/renderers/Table/index.tsx index 31493a310f8..873588ff1f0 100644 --- a/packages/amis/src/renderers/Table/index.tsx +++ b/packages/amis/src/renderers/Table/index.tsx @@ -3049,7 +3049,7 @@ export class TableRendererBase< index?: number | string, condition?: any ) { - const {store} = this.props; + const {store, host} = this.props; if (index !== undefined || condition !== undefined) { const targets = await this.getEventTargets( @@ -3060,13 +3060,19 @@ export class TableRendererBase< targets.forEach(target => { target.updateData(values); }); - } else { - const data = { - ...values, - rows: values.rows ?? values.items // 做个兼容 - }; - return store.updateData(data, undefined, replace); + return; } + + if (host) { + host.setData?.(values, replace); + } + + const data = { + ...values, + rows: values.rows ?? values.items // 做个兼容 + }; + + return store.updateData(data, undefined, replace); } getData() { From d5b7330975a81db644332c6ba6e498b66d0c2364 Mon Sep 17 00:00:00 2001 From: ranqirong <274544338@qq.com> Date: Thu, 9 Jan 2025 10:35:26 +0800 Subject: [PATCH 2/5] =?UTF-8?q?fix(amis):=20cards=E5=B5=8C=E5=A5=97?= =?UTF-8?q?=E5=9C=A8crud=E4=B8=8B=E9=9D=A2=EF=BC=8CsetValue=E6=97=B6?= =?UTF-8?q?=E9=80=9A=E8=BF=87crud=E8=BF=9B=E8=A1=8C=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=EF=BC=8C=E8=A7=A3=E5=86=B3crud=E7=9A=84setValue=E5=8A=A8?= =?UTF-8?q?=E4=BD=9C=E5=A4=B1=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/amis/__tests__/renderers/CRUD.test.tsx | 16 +++++++++++++++- packages/amis/src/renderers/Cards.tsx | 11 ++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/amis/__tests__/renderers/CRUD.test.tsx b/packages/amis/__tests__/renderers/CRUD.test.tsx index 39e931be8f3..51e3184a10d 100644 --- a/packages/amis/__tests__/renderers/CRUD.test.tsx +++ b/packages/amis/__tests__/renderers/CRUD.test.tsx @@ -1898,11 +1898,12 @@ test('25. CRUD Table Cell sync data to store', async () => { }); describe.only('26. setValue 动作', () => { - it('table模式下,设置值后,页面能够自动更新', async () => { + const renderCrud = (mode: string) => render( amisRender({ type: 'crud', id: 'crud', + mode, data: { label: '点击前' }, @@ -1928,6 +1929,9 @@ describe.only('26. setValue 动作', () => { }) ); + it('table模式下,设置值后,页面能够自动更新', async () => { + renderCrud('table'); + await waitFor(() => { expect(screen.getByText('点击前')).toBeInTheDocument(); }); @@ -1938,4 +1942,14 @@ describe.only('26. setValue 动作', () => { expect(screen.getByText('点击后')).toBeInTheDocument(); }); }); + + it('cards模式下,设置值后,页面能够自动更新', () => { + renderCrud('cards'); + + expect(screen.getByText('点击前')).toBeInTheDocument(); + + fireEvent.click(screen.getByText('点击前')); + + expect(screen.getByText('点击后')).toBeInTheDocument(); + }); }); diff --git a/packages/amis/src/renderers/Cards.tsx b/packages/amis/src/renderers/Cards.tsx index 0f23617ae59..360157ea6f0 100644 --- a/packages/amis/src/renderers/Cards.tsx +++ b/packages/amis/src/renderers/Cards.tsx @@ -1220,7 +1220,7 @@ export class CardsRenderer extends Cards { index?: number | string, condition?: any ) { - const {store} = this.props; + const {store, host} = this.props; if (index !== undefined || condition !== undefined) { const targets = await getMatchedEventTargets( @@ -1232,9 +1232,14 @@ export class CardsRenderer extends Cards { targets.forEach(target => { target.updateData(values); }); - } else { - return store.updateData(values, undefined, replace); + return; } + + if (host) { + host.setData?.(values, replace); + } + + return store.updateData(values, undefined, replace); } getData() { From a899dc49e306a4d9658d1c2cad59e6ba3129017b Mon Sep 17 00:00:00 2001 From: ranqirong <274544338@qq.com> Date: Thu, 9 Jan 2025 10:37:48 +0800 Subject: [PATCH 3/5] =?UTF-8?q?fix(amis):=20list=E5=B5=8C=E5=A5=97?= =?UTF-8?q?=E5=9C=A8crud=E4=B8=8B=E9=9D=A2=EF=BC=8CsetValue=E6=97=B6?= =?UTF-8?q?=E9=80=9A=E8=BF=87crud=E8=BF=9B=E8=A1=8C=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=EF=BC=8C=E8=A7=A3=E5=86=B3crud=E7=9A=84setValue=E5=8A=A8?= =?UTF-8?q?=E4=BD=9C=E5=A4=B1=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/amis/__tests__/renderers/CRUD.test.tsx | 12 +++++++++++- packages/amis/src/renderers/List.tsx | 11 ++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/amis/__tests__/renderers/CRUD.test.tsx b/packages/amis/__tests__/renderers/CRUD.test.tsx index 51e3184a10d..c6a7046869a 100644 --- a/packages/amis/__tests__/renderers/CRUD.test.tsx +++ b/packages/amis/__tests__/renderers/CRUD.test.tsx @@ -1897,7 +1897,7 @@ test('25. CRUD Table Cell sync data to store', async () => { expect(listDoms2.length).toEqual(0); }); -describe.only('26. setValue 动作', () => { +describe('26. setValue 动作', () => { const renderCrud = (mode: string) => render( amisRender({ @@ -1952,4 +1952,14 @@ describe.only('26. setValue 动作', () => { expect(screen.getByText('点击后')).toBeInTheDocument(); }); + + it('list模式下,设置值后,页面能够自动更新', () => { + renderCrud('list'); + + expect(screen.getByText('点击前')).toBeInTheDocument(); + + fireEvent.click(screen.getByText('点击前')); + + expect(screen.getByText('点击后')).toBeInTheDocument(); + }); }); diff --git a/packages/amis/src/renderers/List.tsx b/packages/amis/src/renderers/List.tsx index 02d02c952f7..a7a8beaa4c9 100644 --- a/packages/amis/src/renderers/List.tsx +++ b/packages/amis/src/renderers/List.tsx @@ -1173,7 +1173,7 @@ export class ListRenderer extends List { index?: number | string, condition?: any ) { - const {store} = this.props; + const {store, host} = this.props; if (index !== undefined || condition !== undefined) { const targets = await getMatchedEventTargets( @@ -1185,9 +1185,14 @@ export class ListRenderer extends List { targets.forEach(target => { target.updateData(values); }); - } else { - return store.updateData(values, undefined, replace); + return; } + + if (host) { + host.setData?.(values, replace); + } + + return store.updateData(values, undefined, replace); } getData() { From 92f19cc1303a966ba26b5c29394f0048e0905ec6 Mon Sep 17 00:00:00 2001 From: ranqirong <274544338@qq.com> Date: Sun, 12 Jan 2025 10:28:42 +0800 Subject: [PATCH 4/5] =?UTF-8?q?test(amis):=20=E4=BD=BF=E7=94=A8findByTestI?= =?UTF-8?q?d=E6=9B=BF=E6=8D=A2waitFor=EF=BC=8C=E9=81=BF=E5=85=8D=E5=9C=A8c?= =?UTF-8?q?i=E7=8E=AF=E5=A2=83=E4=B8=8B=E4=B8=8D=E7=A8=B3=E5=AE=9A?= =?UTF-8?q?=E5=BC=95=E8=B5=B7=E6=B5=81=E6=B0=B4=E7=BA=BF=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../amis/__tests__/renderers/BarCode.test.tsx | 8 ++--- .../__snapshots__/BarCode.test.tsx.snap | 31 ------------------- 2 files changed, 3 insertions(+), 36 deletions(-) delete mode 100644 packages/amis/__tests__/renderers/__snapshots__/BarCode.test.tsx.snap diff --git a/packages/amis/__tests__/renderers/BarCode.test.tsx b/packages/amis/__tests__/renderers/BarCode.test.tsx index 898dc218f32..6cf71f0cb9d 100644 --- a/packages/amis/__tests__/renderers/BarCode.test.tsx +++ b/packages/amis/__tests__/renderers/BarCode.test.tsx @@ -6,7 +6,7 @@ import {makeEnv, wait} from '../helper'; import 'jest-canvas-mock'; test('Renderer:bar-code', async () => { - const {container} = render( + const {findByTestId} = render( amisRender( { type: 'page', @@ -19,9 +19,7 @@ test('Renderer:bar-code', async () => { makeEnv({}) ) ); - await waitFor(() => - expect(container.querySelector('.cxd-BarCode')).toBeInTheDocument() - ); - expect(container).toMatchSnapshot(); + const barcodeElement = await findByTestId('barcode'); + expect(barcodeElement).toBeInTheDocument(); }); diff --git a/packages/amis/__tests__/renderers/__snapshots__/BarCode.test.tsx.snap b/packages/amis/__tests__/renderers/__snapshots__/BarCode.test.tsx.snap deleted file mode 100644 index 921d6dd07b9..00000000000 --- a/packages/amis/__tests__/renderers/__snapshots__/BarCode.test.tsx.snap +++ /dev/null @@ -1,31 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Renderer:bar-code 1`] = ` -
-
-
-
-
-
- -
-
-
-
-
-
-`; From 1011a554b7c2bdb037e338029b8ed2f8c355d50d Mon Sep 17 00:00:00 2001 From: ranqirong <274544338@qq.com> Date: Sun, 12 Jan 2025 10:48:56 +0800 Subject: [PATCH 5/5] =?UTF-8?q?test(amis):=20=E4=BD=BF=E7=94=A8findByText?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2waitFor=EF=BC=8C=E9=81=BF=E5=85=8D=E5=9C=A8ci?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E4=B8=8B=E4=B8=8D=E7=A8=B3=E5=AE=9A=E5=BC=95?= =?UTF-8?q?=E8=B5=B7=E6=B5=81=E6=B0=B4=E7=BA=BF=E5=A4=B1=E8=B4=A5=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../amis/__tests__/renderers/JSON.test.tsx | 14 +- .../__snapshots__/JSON.test.tsx.snap | 294 ------------------ 2 files changed, 5 insertions(+), 303 deletions(-) delete mode 100644 packages/amis/__tests__/renderers/__snapshots__/JSON.test.tsx.snap diff --git a/packages/amis/__tests__/renderers/JSON.test.tsx b/packages/amis/__tests__/renderers/JSON.test.tsx index 7b8b3b69f9e..28eeaa89eb5 100644 --- a/packages/amis/__tests__/renderers/JSON.test.tsx +++ b/packages/amis/__tests__/renderers/JSON.test.tsx @@ -1,22 +1,18 @@ import React = require('react'); -import {render, waitFor} from '@testing-library/react'; +import {findByText, render, waitFor, screen} from '@testing-library/react'; import '../../src'; import {render as amisRender} from '../../src'; import {makeEnv, wait} from '../helper'; test('Renderer:json', async () => { - const {container} = render( + const {findByText} = render( amisRender( { type: 'page', body: { type: 'json', value: { - a: 'a', - b: 'b', - c: { - d: 'd' - } + aKey: 'aValue' } } }, @@ -25,6 +21,6 @@ test('Renderer:json', async () => { ) ); - await wait(1000); - expect(container).toMatchSnapshot(); + const jsonElement = await findByText('aKey'); + expect(jsonElement).toBeInTheDocument(); }); diff --git a/packages/amis/__tests__/renderers/__snapshots__/JSON.test.tsx.snap b/packages/amis/__tests__/renderers/__snapshots__/JSON.test.tsx.snap deleted file mode 100644 index 7cd7e80e93c..00000000000 --- a/packages/amis/__tests__/renderers/__snapshots__/JSON.test.tsx.snap +++ /dev/null @@ -1,294 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Renderer:json 1`] = ` -
-
-
-
-
-
-
-
-
-
- - -
- - - - - -
- - - { - - - -
-
-
-
- - - - " - - - a - - - " - - - - : - - -
-
- - " - a - " - -
-
-
-
- - - - " - - - b - - - " - - - - : - - -
-
- - " - b - " - -
-
-
-
- - -
- - - - - -
- - - - " - - - c - - - " - - - - : - - - - { - -
-
-
- ... -
- - - } - - - -
-
-
- - - } - - -
-
-
-
-
-
-
-
-
-
-`;