办学质量监测教学评价系统
shenrongliang
2025-06-13 11d86cc6c26bb4f709e407acadf4805c2024e79f
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { describe, expect, it } from 'vitest';
 
import { findMenuByPath, findRootMenuByPath } from '../find-menu-by-path';
 
// 示例菜单数据
const menus: any[] = [
  { path: '/', children: [] },
  { path: '/about', children: [] },
  {
    path: '/contact',
    children: [
      { path: '/contact/email', children: [] },
      { path: '/contact/phone', children: [] },
    ],
  },
  {
    path: '/services',
    children: [
      { path: '/services/design', children: [] },
      {
        path: '/services/development',
        children: [{ path: '/services/development/web', children: [] }],
      },
    ],
  },
];
 
describe('menu Finder Tests', () => {
  it('finds a top-level menu', () => {
    const menu = findMenuByPath(menus, '/about');
    expect(menu).toBeDefined();
    expect(menu?.path).toBe('/about');
  });
 
  it('finds a nested menu', () => {
    const menu = findMenuByPath(menus, '/services/development/web');
    expect(menu).toBeDefined();
    expect(menu?.path).toBe('/services/development/web');
  });
 
  it('returns null for a non-existent path', () => {
    const menu = findMenuByPath(menus, '/non-existent');
    expect(menu).toBeNull();
  });
 
  it('handles empty menus list', () => {
    const menu = findMenuByPath([], '/about');
    expect(menu).toBeNull();
  });
 
  it('handles menu items without children', () => {
    const menu = findMenuByPath(
      [{ path: '/only', children: undefined }] as any[],
      '/only',
    );
    expect(menu).toBeDefined();
    expect(menu?.path).toBe('/only');
  });
 
  it('finds root menu by path', () => {
    const { findMenu, rootMenu, rootMenuPath } = findRootMenuByPath(
      menus,
      '/services/development/web',
    );
 
    expect(findMenu).toBeDefined();
    expect(rootMenu).toBeUndefined();
    expect(rootMenuPath).toBeUndefined();
    expect(findMenu?.path).toBe('/services/development/web');
  });
 
  it('returns null for undefined or empty path', () => {
    const menuUndefinedPath = findMenuByPath(menus);
    const menuEmptyPath = findMenuByPath(menus, '');
    expect(menuUndefinedPath).toBeNull();
    expect(menuEmptyPath).toBeNull();
  });
 
  it('checks for root menu when path does not exist', () => {
    const { findMenu, rootMenu, rootMenuPath } = findRootMenuByPath(
      menus,
      '/non-existent',
    );
    expect(findMenu).toBeNull();
    expect(rootMenu).toBeUndefined();
    expect(rootMenuPath).toBeUndefined();
  });
});