59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
|
|
import { buildConsignmentCategoryGroups } from '../src/utils/consignment-category-tree.ts'
|
|
|
|
type Item = { id: number; parentId: number | null; parentName?: string; name: string }
|
|
|
|
function build(items: Item[]) {
|
|
return buildConsignmentCategoryGroups({
|
|
items,
|
|
getCategoryId: (item) => item.id,
|
|
getParentId: (item) => item.parentId,
|
|
getParentName: (item) => item.parentName,
|
|
getFallbackName: (item) => item.name,
|
|
})
|
|
}
|
|
|
|
test('分类树:一级在前、其二级紧随其后', () => {
|
|
const groups = build([
|
|
{ id: 1, parentId: null, name: '苏尔南手枪' },
|
|
{ id: 2, parentId: null, name: '密钥' },
|
|
{ id: 11, parentId: 1, parentName: '苏尔南手枪', name: '暗潮' },
|
|
{ id: 12, parentId: 1, parentName: '苏尔南手枪', name: '怒嚎' },
|
|
])
|
|
|
|
assert.deepEqual(
|
|
groups.map((group) => group.groupName),
|
|
['苏尔南手枪', '密钥'],
|
|
)
|
|
assert.deepEqual(
|
|
groups[0]?.children.map((child) => child.name),
|
|
['暗潮', '怒嚎'],
|
|
)
|
|
assert.equal(groups[1]?.root?.name, '密钥')
|
|
})
|
|
|
|
test('分类树:只有二级配置时创建一个无 root 的分组', () => {
|
|
const groups = build([
|
|
{ id: 11, parentId: 1, parentName: '苏尔南手枪', name: '暗潮' },
|
|
{ id: 12, parentId: 1, parentName: '苏尔南手枪', name: '怒嚎' },
|
|
])
|
|
|
|
assert.equal(groups.length, 1)
|
|
assert.equal(groups[0]?.groupName, '苏尔南手枪')
|
|
assert.equal(groups[0]?.root, null)
|
|
assert.deepEqual(
|
|
groups[0]?.children.map((child) => child.name),
|
|
['暗潮', '怒嚎'],
|
|
)
|
|
})
|
|
|
|
test('分类树:没有下级的一级保持为单节点', () => {
|
|
const groups = build([{ id: 2, parentId: null, name: '密钥' }])
|
|
|
|
assert.equal(groups.length, 1)
|
|
assert.equal(groups[0]?.root?.name, '密钥')
|
|
assert.deepEqual(groups[0]?.children, [])
|
|
})
|