mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2025-12-13 04:03:07 +00:00
fix: Improve code and simplify logic (#2651)
* fix: Improve code and simplify logic
This commit is contained in:
parent
394b56d0ce
commit
27e914cece
@ -440,23 +440,23 @@ export class Node<Schema extends IPublicTypeNodeSchema = IPublicTypeNodeSchema>
|
||||
}
|
||||
|
||||
private initialChildren(children: IPublicTypeNodeData | IPublicTypeNodeData[] | undefined): IPublicTypeNodeData[] {
|
||||
// FIXME! this is dirty code
|
||||
if (children == null) {
|
||||
const { initialChildren } = this.componentMeta.advanced;
|
||||
|
||||
if (children == null) {
|
||||
if (initialChildren) {
|
||||
if (typeof initialChildren === 'function') {
|
||||
return initialChildren(this.internalToShellNode()!) || [];
|
||||
}
|
||||
return initialChildren;
|
||||
}
|
||||
}
|
||||
if (Array.isArray(children)) {
|
||||
return children;
|
||||
} else if (children) {
|
||||
return [children];
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (Array.isArray(children)) {
|
||||
return children;
|
||||
}
|
||||
|
||||
return [children];
|
||||
}
|
||||
|
||||
isContainer(): boolean {
|
||||
|
||||
@ -54,6 +54,59 @@ describe('Node 方法测试', () => {
|
||||
project = null;
|
||||
});
|
||||
|
||||
// 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([]);
|
||||
});
|
||||
|
||||
// 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);
|
||||
});
|
||||
|
||||
// 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]);
|
||||
});
|
||||
|
||||
// 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 = 0;
|
||||
const result = node.initialChildren(childObject);
|
||||
// 预期结果是一个数组
|
||||
expect(result).toEqual([0]);
|
||||
});
|
||||
|
||||
// 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 = false;
|
||||
const result = node.initialChildren(childObject);
|
||||
// 预期结果是一个数组
|
||||
expect(result).toEqual([false]);
|
||||
});
|
||||
|
||||
|
||||
it('condition group', () => { });
|
||||
|
||||
it('getExtraProp / setExtraProp', () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user