|
@@ -0,0 +1,57 @@
|
|
|
|
|
+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]), '末级')
|
|
|
|
|
+ })
|
|
|
|
|
+})
|