Skip to content

Commit 27e914c

Browse files
authored
fix: Improve code and simplify logic (#2651)
* fix: Improve code and simplify logic
1 parent 394b56d commit 27e914c

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

packages/designer/src/document/node/node.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,23 +440,23 @@ export class Node<Schema extends IPublicTypeNodeSchema = IPublicTypeNodeSchema>
440440
}
441441

442442
private initialChildren(children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined): IPublicTypeNodeData[] {
443-
// FIXME! this is dirty code
443+
const { initialChildren } = this.componentMeta.advanced;
444+
444445
if (children == null) {
445-
const { initialChildren } = this.componentMeta.advanced;
446446
if (initialChildren) {
447447
if (typeof initialChildren === 'function') {
448448
return initialChildren(this.internalToShellNode()!) || [];
449449
}
450450
return initialChildren;
451451
}
452+
return [];
452453
}
454+
453455
if (Array.isArray(children)) {
454456
return children;
455-
} else if (children) {
456-
return [children];
457-
} else {
458-
return [];
459457
}
458+
459+
return [children];
460460
}
461461

462462
isContainer(): boolean {
@@ -1094,7 +1094,7 @@ export class Node<Schema extends IPublicTypeNodeSchema = IPublicTypeNodeSchema>
10941094
}
10951095

10961096
/**
1097-
* 是否可执行某action
1097+
* 是否可执行某 action
10981098
*/
10991099
canPerformAction(actionName: string): boolean {
11001100
const availableActions =

packages/designer/tests/document/node/node.test.ts

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,60 @@ describe('Node 方法测试', () => {
5454
project = null;
5555
});
5656

57-
it('condition group', () => {});
57+
// Case 1: When children is null
58+
test('initialChildren returns result of initialChildren function when children is null ', () => {
59+
const node = new Node(doc, { componentName: 'Button', props: { a: 1 } });
60+
const result = node.initialChildren(null);
61+
// 预期结果是一个空数组
62+
expect(result).toEqual([]);
63+
});
64+
65+
// Case 2: When children is undefined
66+
test('initialChildren returns result of initialChildren function when children is null ', () => {
67+
const node = new Node(doc, { componentName: 'Button', props: { a: 1 } });
68+
const result = node.initialChildren(undefined);
69+
// 预期结果是一个空数组
70+
expect(result).toEqual([]);
71+
});
72+
73+
// Case 3: When children is array
74+
test('initialChildren returns result of initialChildren function when children is null ', () => {
75+
const node = new Node(doc, { componentName: 'Button', props: { a: 1 } });
76+
const childrenArray = [{ id: 1, name: 'Child 1' }, { id: 2, name: 'Child 2' }];
77+
const result = node.initialChildren(childrenArray);
78+
// 预期结果是一个数组
79+
expect(result).toEqual(childrenArray);
80+
});
81+
82+
// Case 4: When children is not null and not an array
83+
test('initialChildren returns result of initialChildren function when children is null ', () => {
84+
const node = new Node(doc, { componentName: 'Button', props: { a: 1 } });
85+
const childObject = { id: 1, name: 'Child 1' };
86+
const result = node.initialChildren(childObject);
87+
// 预期结果是一个数组
88+
expect(result).toEqual([childObject]);
89+
});
90+
91+
// Case 5: When children 0
92+
test('initialChildren returns result of initialChildren function when children is null ', () => {
93+
const node = new Node(doc, { componentName: 'Button', props: { a: 1 } });
94+
const childObject = 0;
95+
const result = node.initialChildren(childObject);
96+
// 预期结果是一个数组
97+
expect(result).toEqual([0]);
98+
});
99+
100+
// Case 6: When children false
101+
test('initialChildren returns result of initialChildren function when children is null ', () => {
102+
const node = new Node(doc, { componentName: 'Button', props: { a: 1 } });
103+
const childObject = false;
104+
const result = node.initialChildren(childObject);
105+
// 预期结果是一个数组
106+
expect(result).toEqual([false]);
107+
});
108+
109+
110+
it('condition group', () => { });
58111

59112
it('getExtraProp / setExtraProp', () => {
60113
const firstBtn = doc.getNode('node_k1ow3cbn')!;
@@ -367,7 +420,7 @@ describe('Node 方法测试', () => {
367420
expect(mockFn).not.toHaveBeenCalled();
368421
});
369422

370-
it('addSlot / unlinkSlot / removeSlot', () => {});
423+
it('addSlot / unlinkSlot / removeSlot', () => { });
371424

372425
it('setProps', () => {
373426
const firstBtn = doc.getNode('node_k1ow3cbn')!;
@@ -407,7 +460,7 @@ describe('Node 方法测试', () => {
407460
designer.createComponentMeta(btnMetadata);
408461
const btn = doc.getNode('node_k1ow3cbn');
409462
// 从 componentMeta 中获取到 title 值
410-
expect(btn.title).toEqual({ type: 'i18n', 'zh-CN': '按钮', 'en-US': 'Button' } );
463+
expect(btn.title).toEqual({ type: 'i18n', 'zh-CN': '按钮', 'en-US': 'Button' });
411464
// 从 extraProp 中获取值
412465
btn.setExtraProp('title', 'hello button');
413466
expect(btn.title).toBe('hello button');
@@ -546,7 +599,7 @@ describe('Node 方法测试', () => {
546599
expect(comparePosition(firstBtn, firstCard)).toBe(PositionNO.BeforeOrAfter);
547600
});
548601

549-
it('getZLevelTop', () => {});
602+
it('getZLevelTop', () => { });
550603
it('propsData', () => {
551604
expect(new Node(doc, { componentName: 'Leaf' }).propsData).toBeNull();
552605
expect(new Node(doc, { componentName: 'Fragment' }).propsData).toBeNull();

0 commit comments

Comments
 (0)