Files
ComfyUI_frontend/src/utils/treeUtil.test.ts
Alexander Brown 10feb1fd5b chore: migrate tests from tests-ui/ to colocate with source files (#7811)
## Summary

Migrates all unit tests from `tests-ui/` to colocate with their source
files in `src/`, improving discoverability and maintainability.

## Changes

- **What**: Relocated all unit tests to be adjacent to the code they
test, following the `<source>.test.ts` naming convention
- **Config**: Updated `vitest.config.ts` to remove `tests-ui` include
pattern and `@tests-ui` alias
- **Docs**: Moved testing documentation to `docs/testing/` with updated
paths and patterns

## Review Focus

- Migration patterns documented in
`temp/plans/migrate-tests-ui-to-src.md`
- Tests use `@/` path aliases instead of relative imports
- Shared fixtures placed in `__fixtures__/` directories

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-7811-chore-migrate-tests-from-tests-ui-to-colocate-with-source-files-2da6d73d36508147a4cce85365dee614)
by [Unito](https://www.unito.io)

---------

Co-authored-by: Amp <amp@ampcode.com>
Co-authored-by: GitHub Action <action@github.com>
2026-01-05 16:32:24 -08:00

148 lines
3.6 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import type { TreeNode } from '@/types/treeExplorerTypes'
import { buildTree, sortedTree } from '@/utils/treeUtil'
describe('buildTree', () => {
it('should handle empty folder items correctly', () => {
const items = [
{ path: 'a/b/c/' },
{ path: 'a/b/d.txt' },
{ path: 'a/e/' },
{ path: 'f.txt' }
]
const tree = buildTree(items, (item) => item.path.split('/'))
expect(tree).toEqual({
key: 'root',
label: 'root',
children: [
{
key: 'root/a',
label: 'a',
leaf: false,
children: [
{
key: 'root/a/b',
label: 'b',
leaf: false,
children: [
{
key: 'root/a/b/c',
label: 'c',
leaf: false,
children: [],
data: { path: 'a/b/c/' }
},
{
key: 'root/a/b/d.txt',
label: 'd.txt',
leaf: true,
children: [],
data: { path: 'a/b/d.txt' }
}
]
},
{
key: 'root/a/e',
label: 'e',
leaf: false,
children: [],
data: { path: 'a/e/' }
}
]
},
{
key: 'root/f.txt',
label: 'f.txt',
leaf: true,
children: [],
data: { path: 'f.txt' }
}
]
})
})
})
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',
leaf: false,
children: [createNode('c'), createNode('a'), createNode('b')]
}
const result = sortedTree(node)
expect(result.children?.map((c) => c.label)).toEqual(['a', 'b', 'c'])
})
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)
})
})