-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: Improve code and simplify logic #2651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
50137b9
299af37
34059c6
a5acc3b
3bfedfb
c727b41
2b94f21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ 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; | ||
|
|
@@ -53,8 +54,63 @@ 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; | ||
| } | ||
|
|
||
| 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')!; | ||
|
|
@@ -367,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')!; | ||
|
|
@@ -407,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'); | ||
|
|
@@ -546,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(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
children 为 undefined 的情况和原来输出是否一样。重构建议先补充相关的单元测试用例。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
想问一下补充相关的单元测试用例,可以在node.test.ts文件中添加测试用例,来测试函数在重构前后输出结果是否一样吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以的。