Files
shanxia2.0/web-admin/src/utils/mp-crm-product-category-tree.test.ts
T
wuzilongandCursor ffd25d93e9 feat(web-admin): add product category tree expand helpers.
Centralize expand-id collection and child meta labels for the mp category list.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-18 16:24:12 +08:00

58 lines
1.5 KiB
TypeScript

import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import {
collectExpandIds,
areAllExpandableExpanded,
categoryChildCount,
categoryChildMetaLabel
} from './mp-crm-product-category-tree'
type Node = {
id: string
children?: Node[]
}
const sample: Node[] = [
{
id: '1',
children: [
{ id: '11', children: [{ id: '111' }] },
{ id: '12' }
]
},
{ id: '2' }
]
describe('collectExpandIds', () => {
it('marks every node that has children', () => {
assert.deepEqual(collectExpandIds(sample), { '1': true, '11': true })
})
})
describe('areAllExpandableExpanded', () => {
it('returns true when all expandable ids are true', () => {
assert.equal(areAllExpandableExpanded(sample, { '1': true, '11': true }), true)
})
it('returns false when any expandable id is missing or false', () => {
assert.equal(areAllExpandableExpanded(sample, { '1': true }), false)
assert.equal(areAllExpandableExpanded(sample, { '1': true, '11': false }), false)
})
it('returns true for empty tree', () => {
assert.equal(areAllExpandableExpanded([], {}), true)
})
})
describe('categoryChildCount / categoryChildMetaLabel', () => {
it('counts direct children only', () => {
assert.equal(categoryChildCount(sample[0]), 2)
assert.equal(categoryChildCount(sample[1]), 0)
})
it('labels leaf vs parent', () => {
assert.equal(categoryChildMetaLabel(sample[0]), '2 个子类')
assert.equal(categoryChildMetaLabel(sample[1]), '末级')
})
})