From 50137b9ef7aeec46c1ae722161b8f0db0b3f25e1 Mon Sep 17 00:00:00 2001 From: andylili21 <925820633@qq.com> Date: Wed, 15 Nov 2023 01:37:39 +0800 Subject: [PATCH 1/7] fix: Improve code and simplify logic --- packages/designer/src/document/node/node.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/designer/src/document/node/node.ts b/packages/designer/src/document/node/node.ts index 9effc341d..145a5a85e 100644 --- a/packages/designer/src/document/node/node.ts +++ b/packages/designer/src/document/node/node.ts @@ -440,23 +440,23 @@ export class Node } private initialChildren(children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined): IPublicTypeNodeData[] { - // FIXME! this is dirty code + const { initialChildren } = this.componentMeta.advanced; + if (children == null) { - const { initialChildren } = this.componentMeta.advanced; if (initialChildren) { if (typeof initialChildren === 'function') { return initialChildren(this.internalToShellNode()!) || []; } return initialChildren; } + return []; } + if (Array.isArray(children)) { return children; - } else if (children) { - return [children]; - } else { - return []; } + + return [children]; } isContainer(): boolean { @@ -1094,7 +1094,7 @@ export class Node } /** - * 是否可执行某action + * 是否可执行某 action */ canPerformAction(actionName: string): boolean { const availableActions = From 299af371e1324746be907f2ed60751dfa7dc208e Mon Sep 17 00:00:00 2001 From: andylili21 <925820633@qq.com> Date: Mon, 20 Nov 2023 14:19:03 +0800 Subject: [PATCH 2/7] =?UTF-8?q?#=E8=A1=A5=E5=85=85=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../designer/tests/document/node/node.test.ts | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/packages/designer/tests/document/node/node.test.ts b/packages/designer/tests/document/node/node.test.ts index c4717b3a0..553c8c923 100644 --- a/packages/designer/tests/document/node/node.test.ts +++ b/packages/designer/tests/document/node/node.test.ts @@ -29,6 +29,55 @@ import rootFooterMetadata from '../../fixtures/component-metadata/root-footer'; import { shellModelFactory } from '../../../../engine/src/modules/shell-model-factory'; import { isNode } from '@alilc/lowcode-utils'; import { Setters } from '@alilc/lowcode-shell'; +import { IPublicTypeNodeData } from '@alilc/lowcode-types'; + +//重构前的 Node 的 initialChildren 方法 +function initialChildren(children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined): IPublicTypeNodeData[] { + // FIXME! this is dirty code + if (children == null) { + const { initialChildren } = { + callbacks: ()=>{}, + getResizingHandlers: () => {}, + } + if (initialChildren) { + if (typeof initialChildren === 'function') { + return []; + } + return initialChildren; + } + } + if (Array.isArray(children)) { + return children; + } else if (children) { + return [children]; + } else { + return []; + } + } + + //重构后的 Node 的 initialChildren 方法 + function initialChildren2(children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined): IPublicTypeNodeData[] { + const { initialChildren } = { + callbacks: ()=>{}, + getResizingHandlers: () => {}, + } + + if (children == null) { + if (initialChildren) { + if (typeof initialChildren === 'function') { + return []; + } + return initialChildren; + } + return []; + } + + if (Array.isArray(children)) { + return children; + } + + return [children]; + } describe('Node 方法测试', () => { let editor: Editor; @@ -53,6 +102,14 @@ describe('Node 方法测试', () => { designer = null; project = null; }); + //测试 children 为 undefined 时重构前后输出结果 + it('initialChildren and initialChildren2 should return the same result when children is undefined', () => { + const children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined = undefined; + const result1 = initialChildren(children); + const result2 = initialChildren2(children); + + expect(result1).toEqual(result2); + }); it('condition group', () => {}); From 34059c696410d0c81ac387c8a682ede7db508a9a Mon Sep 17 00:00:00 2001 From: andylili21 <925820633@qq.com> Date: Mon, 20 Nov 2023 14:43:08 +0800 Subject: [PATCH 3/7] =?UTF-8?q?#=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../designer/tests/document/node/node.test.ts | 105 +++++++++--------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/packages/designer/tests/document/node/node.test.ts b/packages/designer/tests/document/node/node.test.ts index 553c8c923..c6dd8abd8 100644 --- a/packages/designer/tests/document/node/node.test.ts +++ b/packages/designer/tests/document/node/node.test.ts @@ -31,54 +31,6 @@ import { isNode } from '@alilc/lowcode-utils'; import { Setters } from '@alilc/lowcode-shell'; import { IPublicTypeNodeData } from '@alilc/lowcode-types'; -//重构前的 Node 的 initialChildren 方法 -function initialChildren(children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined): IPublicTypeNodeData[] { - // FIXME! this is dirty code - if (children == null) { - const { initialChildren } = { - callbacks: ()=>{}, - getResizingHandlers: () => {}, - } - if (initialChildren) { - if (typeof initialChildren === 'function') { - return []; - } - return initialChildren; - } - } - if (Array.isArray(children)) { - return children; - } else if (children) { - return [children]; - } else { - return []; - } - } - - //重构后的 Node 的 initialChildren 方法 - function initialChildren2(children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined): IPublicTypeNodeData[] { - const { initialChildren } = { - callbacks: ()=>{}, - getResizingHandlers: () => {}, - } - - if (children == null) { - if (initialChildren) { - if (typeof initialChildren === 'function') { - return []; - } - return initialChildren; - } - return []; - } - - if (Array.isArray(children)) { - return children; - } - - return [children]; - } - describe('Node 方法测试', () => { let editor: Editor; let designer: Designer; @@ -104,14 +56,61 @@ describe('Node 方法测试', () => { }); //测试 children 为 undefined 时重构前后输出结果 it('initialChildren and initialChildren2 should return the same result when children is undefined', () => { + //重构前的 Node 的 initialChildren 方法 + function initialChildren(children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined): IPublicTypeNodeData[] { + // FIXME! this is dirty code + if (children == null) { + const { initialChildren } = { + callbacks: () => { }, + getResizingHandlers: () => { }, + } + if (initialChildren) { + if (typeof initialChildren === 'function') { + return []; + } + return initialChildren; + } + } + if (Array.isArray(children)) { + return children; + } else if (children) { + return [children]; + } else { + return []; + } + } + + //重构后的 Node 的 initialChildren 方法 + function initialChildren2(children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined): IPublicTypeNodeData[] { + const { initialChildren } = { + callbacks: () => { }, + getResizingHandlers: () => { }, + } + + if (children == null) { + if (initialChildren) { + if (typeof initialChildren === 'function') { + return []; + } + return initialChildren; + } + return []; + } + + if (Array.isArray(children)) { + return children; + } + + return [children]; + } const children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined = undefined; const result1 = initialChildren(children); const result2 = initialChildren2(children); - + expect(result1).toEqual(result2); }); - it('condition group', () => {}); + it('condition group', () => { }); it('getExtraProp / setExtraProp', () => { const firstBtn = doc.getNode('node_k1ow3cbn')!; @@ -424,7 +423,7 @@ describe('Node 方法测试', () => { expect(mockFn).not.toHaveBeenCalled(); }); - it('addSlot / unlinkSlot / removeSlot', () => {}); + it('addSlot / unlinkSlot / removeSlot', () => { }); it('setProps', () => { const firstBtn = doc.getNode('node_k1ow3cbn')!; @@ -464,7 +463,7 @@ describe('Node 方法测试', () => { designer.createComponentMeta(btnMetadata); const btn = doc.getNode('node_k1ow3cbn'); // 从 componentMeta 中获取到 title 值 - expect(btn.title).toEqual({ type: 'i18n', 'zh-CN': '按钮', 'en-US': 'Button' } ); + expect(btn.title).toEqual({ type: 'i18n', 'zh-CN': '按钮', 'en-US': 'Button' }); // 从 extraProp 中获取值 btn.setExtraProp('title', 'hello button'); expect(btn.title).toBe('hello button'); @@ -603,7 +602,7 @@ describe('Node 方法测试', () => { expect(comparePosition(firstBtn, firstCard)).toBe(PositionNO.BeforeOrAfter); }); - it('getZLevelTop', () => {}); + it('getZLevelTop', () => { }); it('propsData', () => { expect(new Node(doc, { componentName: 'Leaf' }).propsData).toBeNull(); expect(new Node(doc, { componentName: 'Fragment' }).propsData).toBeNull(); From a5acc3b4e2ee7f1daad4c49d218d9f13db203c9b Mon Sep 17 00:00:00 2001 From: andylili21 <925820633@qq.com> Date: Mon, 20 Nov 2023 18:39:29 +0800 Subject: [PATCH 4/7] #Unit Testing --- .../designer/tests/document/node/node.test.ts | 83 +++++++------------ 1 file changed, 31 insertions(+), 52 deletions(-) diff --git a/packages/designer/tests/document/node/node.test.ts b/packages/designer/tests/document/node/node.test.ts index c6dd8abd8..421b418b0 100644 --- a/packages/designer/tests/document/node/node.test.ts +++ b/packages/designer/tests/document/node/node.test.ts @@ -29,7 +29,6 @@ import rootFooterMetadata from '../../fixtures/component-metadata/root-footer'; import { shellModelFactory } from '../../../../engine/src/modules/shell-model-factory'; import { isNode } from '@alilc/lowcode-utils'; import { Setters } from '@alilc/lowcode-shell'; -import { IPublicTypeNodeData } from '@alilc/lowcode-types'; describe('Node 方法测试', () => { let editor: Editor; @@ -54,62 +53,42 @@ describe('Node 方法测试', () => { designer = null; project = null; }); - //测试 children 为 undefined 时重构前后输出结果 - it('initialChildren and initialChildren2 should return the same result when children is undefined', () => { - //重构前的 Node 的 initialChildren 方法 - function initialChildren(children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined): IPublicTypeNodeData[] { - // FIXME! this is dirty code - if (children == null) { - const { initialChildren } = { - callbacks: () => { }, - getResizingHandlers: () => { }, - } - if (initialChildren) { - if (typeof initialChildren === 'function') { - return []; - } - return initialChildren; - } - } - if (Array.isArray(children)) { - return children; - } else if (children) { - return [children]; - } else { - return []; - } - } - - //重构后的 Node 的 initialChildren 方法 - function initialChildren2(children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined): IPublicTypeNodeData[] { - const { initialChildren } = { - callbacks: () => { }, - getResizingHandlers: () => { }, - } - - if (children == null) { - if (initialChildren) { - if (typeof initialChildren === 'function') { - return []; - } - return initialChildren; - } - return []; - } - if (Array.isArray(children)) { - return children; - } + // Case 1: When children is null + test('initialChildren returns result of initialChildren function when children is null ', () => { + const node = new Node(doc, { componentName: 'Button', props: { a: 1 } }); + const result = node.initialChildren(null); + // 预期结果是一个空数组 + expect(result).toEqual([]); + }); - return [children]; - } - const children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined = undefined; - const result1 = initialChildren(children); - const result2 = initialChildren2(children); + // Case 2: When children is undefined + test('initialChildren returns result of initialChildren function when children is null ', () => { + const node = new Node(doc, { componentName: 'Button', props: { a: 1 } }); + const result = node.initialChildren(undefined); + // 预期结果是一个空数组 + expect(result).toEqual([]); + }); + + // Case 3: When children is array + test('initialChildren returns result of initialChildren function when children is null ', () => { + const node = new Node(doc, { componentName: 'Button', props: { a: 1 } }); + const childrenArray = [{ id: 1, name: 'Child 1' }, { id: 2, name: 'Child 2' }]; + const result = node.initialChildren(childrenArray); + // 预期结果是一个数组 + expect(result).toEqual(childrenArray); + }); - expect(result1).toEqual(result2); + // Case 4: When children is not null and not an array + test('initialChildren returns result of initialChildren function when children is null ', () => { + const node = new Node(doc, { componentName: 'Button', props: { a: 1 } }); + const childObject = { id: 1, name: 'Child 1' }; + const result = node.initialChildren(childObject); + // 预期结果是一个数组 + expect(result).toEqual([childObject]); }); + it('condition group', () => { }); it('getExtraProp / setExtraProp', () => { From 3bfedfb971b192becc521514ff9e2efdb9f7bf27 Mon Sep 17 00:00:00 2001 From: andylili21 <925820633@qq.com> Date: Tue, 21 Nov 2023 23:17:39 +0800 Subject: [PATCH 5/7] #Adding UTs --- .../designer/tests/document/node/node.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/designer/tests/document/node/node.test.ts b/packages/designer/tests/document/node/node.test.ts index 421b418b0..119d758e2 100644 --- a/packages/designer/tests/document/node/node.test.ts +++ b/packages/designer/tests/document/node/node.test.ts @@ -88,6 +88,24 @@ describe('Node 方法测试', () => { expect(result).toEqual([childObject]); }); + // Case 5: When children is 0 + test('initialChildren returns result of initialChildren function when children is null ', () => { + const node = new Node(doc, { componentName: 'Button', props: { a: 1 } }); + const childObject = { id: 1, name: 'Child 1' }; + const result = node.initialChildren(childObject); + // 预期结果是一个数组 + expect(result).toEqual([childObject]); + }); + + // Case 6: When children is false + test('initialChildren returns result of initialChildren function when children is null ', () => { + const node = new Node(doc, { componentName: 'Button', props: { a: 1 } }); + const childObject = { id: 1, name: 'Child 1' }; + const result = node.initialChildren(childObject); + // 预期结果是一个数组 + expect(result).toEqual([childObject]); + }); + it('condition group', () => { }); From c727b41e22dd9fcfe7492587f1e8f445b9de2133 Mon Sep 17 00:00:00 2001 From: andylili21 <925820633@qq.com> Date: Wed, 22 Nov 2023 10:03:20 +0800 Subject: [PATCH 6/7] #changeing explain --- packages/designer/tests/document/node/node.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/designer/tests/document/node/node.test.ts b/packages/designer/tests/document/node/node.test.ts index 119d758e2..734ffbdd1 100644 --- a/packages/designer/tests/document/node/node.test.ts +++ b/packages/designer/tests/document/node/node.test.ts @@ -88,7 +88,7 @@ describe('Node 方法测试', () => { expect(result).toEqual([childObject]); }); - // Case 5: When children is 0 + // Case 5: When children is not null and not an array test('initialChildren returns result of initialChildren function when children is null ', () => { const node = new Node(doc, { componentName: 'Button', props: { a: 1 } }); const childObject = { id: 1, name: 'Child 1' }; @@ -97,7 +97,7 @@ describe('Node 方法测试', () => { expect(result).toEqual([childObject]); }); - // Case 6: When children is false + // Case 6: When children is not null and not an array test('initialChildren returns result of initialChildren function when children is null ', () => { const node = new Node(doc, { componentName: 'Button', props: { a: 1 } }); const childObject = { id: 1, name: 'Child 1' }; From 2b94f214c784c3d158e2a417077e7c1936e79b8a Mon Sep 17 00:00:00 2001 From: andylili21 <925820633@qq.com> Date: Thu, 23 Nov 2023 11:24:22 +0800 Subject: [PATCH 7/7] #change case --- packages/designer/tests/document/node/node.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/designer/tests/document/node/node.test.ts b/packages/designer/tests/document/node/node.test.ts index 734ffbdd1..2695d6c83 100644 --- a/packages/designer/tests/document/node/node.test.ts +++ b/packages/designer/tests/document/node/node.test.ts @@ -88,22 +88,22 @@ describe('Node 方法测试', () => { expect(result).toEqual([childObject]); }); - // Case 5: When children is not null and not an array + // Case 5: When children 0 test('initialChildren returns result of initialChildren function when children is null ', () => { const node = new Node(doc, { componentName: 'Button', props: { a: 1 } }); - const childObject = { id: 1, name: 'Child 1' }; + const childObject = 0; const result = node.initialChildren(childObject); // 预期结果是一个数组 - expect(result).toEqual([childObject]); + expect(result).toEqual([0]); }); - // Case 6: When children is not null and not an array + // Case 6: When children false test('initialChildren returns result of initialChildren function when children is null ', () => { const node = new Node(doc, { componentName: 'Button', props: { a: 1 } }); - const childObject = { id: 1, name: 'Child 1' }; + const childObject = false; const result = node.initialChildren(childObject); // 预期结果是一个数组 - expect(result).toEqual([childObject]); + expect(result).toEqual([false]); });