mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-03-13 09:00:16 +00:00
Always sort workflows tree (#1456)
* Always sort workflows tree * nit * nit * Add tests * nit * nit
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { buildTree } from '@/utils/treeUtil'
|
||||
import { buildTree, sortedTree } from '@/utils/treeUtil'
|
||||
import { TreeNode } from 'primevue/treenode'
|
||||
|
||||
describe('buildTree', () => {
|
||||
it('should handle empty folder items correctly', () => {
|
||||
@@ -61,3 +62,98 @@ describe('buildTree', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('sortedTree', () => {
|
||||
const createNode = (label: string, leaf = false): TreeNode => ({
|
||||
key: label,
|
||||
label,
|
||||
leaf,
|
||||
children: []
|
||||
})
|
||||
|
||||
it('should return a new node instance', () => {
|
||||
const node = createNode('root')
|
||||
const result = sortedTree(node)
|
||||
expect(result).not.toBe(node)
|
||||
expect(result).toEqual(node)
|
||||
})
|
||||
|
||||
it('should sort children by label', () => {
|
||||
const node: TreeNode = {
|
||||
key: 'root',
|
||||
label: 'root',
|
||||
children: [createNode('c'), createNode('a'), createNode('b')]
|
||||
}
|
||||
|
||||
const result = sortedTree(node)
|
||||
expect(result.children?.map((c) => c.label)).toEqual(['a', 'b', 'c'])
|
||||
})
|
||||
|
||||
it('should handle undefined labels', () => {
|
||||
const node: TreeNode = {
|
||||
key: 'root',
|
||||
label: 'root',
|
||||
children: [
|
||||
{ key: '1', label: 'b' },
|
||||
{ key: '2', label: 'a' },
|
||||
{ key: '3', label: undefined }
|
||||
]
|
||||
}
|
||||
|
||||
const result = sortedTree(node)
|
||||
expect(result.children?.map((c) => c.label)).toEqual([undefined, 'a', 'b'])
|
||||
})
|
||||
|
||||
describe('with groupLeaf=true', () => {
|
||||
it('should group folders before files', () => {
|
||||
const node: TreeNode = {
|
||||
key: 'root',
|
||||
label: 'root',
|
||||
children: [
|
||||
createNode('file.txt', true),
|
||||
createNode('folder1'),
|
||||
createNode('another.txt', true),
|
||||
createNode('folder2')
|
||||
]
|
||||
}
|
||||
|
||||
const result = sortedTree(node, { groupLeaf: true })
|
||||
const labels = result.children?.map((c) => c.label)
|
||||
expect(labels).toEqual(['folder1', 'folder2', 'another.txt', 'file.txt'])
|
||||
})
|
||||
|
||||
it('should sort recursively', () => {
|
||||
const node: TreeNode = {
|
||||
key: 'root',
|
||||
label: 'root',
|
||||
children: [
|
||||
{
|
||||
...createNode('folder1'),
|
||||
children: [
|
||||
createNode('z.txt', true),
|
||||
createNode('subfolder2'),
|
||||
createNode('a.txt', true),
|
||||
createNode('subfolder1')
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
const result = sortedTree(node, { groupLeaf: true })
|
||||
const folder = result.children?.[0]
|
||||
const subLabels = folder?.children?.map((c) => c.label)
|
||||
expect(subLabels).toEqual(['subfolder1', 'subfolder2', 'a.txt', 'z.txt'])
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle nodes without children', () => {
|
||||
const node: TreeNode = {
|
||||
key: 'leaf',
|
||||
label: 'leaf',
|
||||
leaf: true
|
||||
}
|
||||
|
||||
const result = sortedTree(node)
|
||||
expect(result).toEqual(node)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user