刘光辉
9 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { describe, expect, it } from 'vitest';
 
import { setTreePathLabels, TREE_PATH_LABEL_FIELD } from './utils';
 
describe('setTreePathLabels', () => {
  it('joins each node label with its ancestor labels', () => {
    const treeData: any[] = [
      {
        id: 'warehouse-1',
        fullName: '仓库1',
        children: [
          {
            id: 'area-101',
            fullName: '区域101',
            children: [
              {
                id: 'floor-101',
                fullName: '楼层 101',
                children: [
                  {
                    id: 'shelf-101',
                    fullName: '货架101',
                    children: [{ id: 'layer-101', fullName: '层数 101' }],
                  },
                ],
              },
            ],
          },
          { id: 'area-102', fullName: '区域102' },
        ],
      },
    ];
 
    setTreePathLabels(treeData, { children: 'children', key: 'id', label: 'fullName', value: 'id' });
 
    expect(treeData[0]![TREE_PATH_LABEL_FIELD]).toBe('仓库1');
    expect(treeData[0]!.children[0]![TREE_PATH_LABEL_FIELD]).toBe('仓库1/区域101');
    expect(treeData[0]!.children[0]!.children[0]!.children[0]!.children[0]![TREE_PATH_LABEL_FIELD]).toBe('仓库1/区域101/楼层 101/货架101/层数 101');
    expect(treeData[0]!.children[1]![TREE_PATH_LABEL_FIELD]).toBe('仓库1/区域102');
  });
 
  it('supports custom field names and ignores empty labels', () => {
    const treeData: any[] = [{ name: '仓库1', nodes: [{ name: '', nodes: [{ name: '层数 101', code: 101 }] }] }];
 
    setTreePathLabels(treeData, { children: 'nodes', key: 'code', label: 'name', value: 'code' });
 
    expect(treeData[0]!.nodes[0]!.nodes[0]![TREE_PATH_LABEL_FIELD]).toBe('仓库1/层数 101');
  });
});