112 lines
3.7 KiB
TypeScript
112 lines
3.7 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|||
|
|
import {
|
||
|
|
adminNavigation,
|
||
|
|
filterAdminNavigation,
|
||
|
|
getFirstNavigationLeaf,
|
||
|
|
getNavigationLeaves,
|
||
|
|
type AdminNavigationNode,
|
||
|
|
} from './adminNavigation'
|
||
|
|
|
||
|
|
describe('adminNavigation', () => {
|
||
|
|
it('keeps phase-one pages grouped without exposing the deferred dashboard', () => {
|
||
|
|
expect(adminNavigation.map((item) => item.title)).toEqual([
|
||
|
|
'业务管理',
|
||
|
|
'基础资料',
|
||
|
|
'系统管理',
|
||
|
|
])
|
||
|
|
expect(adminNavigation[0].children?.map((item) => item.title)).toEqual([
|
||
|
|
'客户管理',
|
||
|
|
'企业审核',
|
||
|
|
'仓库管理',
|
||
|
|
'头程订单',
|
||
|
|
])
|
||
|
|
expect(adminNavigation[1].children?.map((item) => item.title)).toEqual(['快递公司', '往来单位'])
|
||
|
|
expect(adminNavigation[2].children?.map((item) => item.title)).toEqual([
|
||
|
|
'内部用户',
|
||
|
|
'角色管理',
|
||
|
|
'菜单权限',
|
||
|
|
])
|
||
|
|
})
|
||
|
|
|
||
|
|
it('assigns every page a unique path and permission code', () => {
|
||
|
|
const leaves = getNavigationLeaves(adminNavigation)
|
||
|
|
const paths = leaves.map((item) => item.path)
|
||
|
|
const permissions = leaves.map((item) => item.permission)
|
||
|
|
|
||
|
|
expect(paths).toEqual([
|
||
|
|
'/customers',
|
||
|
|
'/customers/reviews',
|
||
|
|
'/warehouses',
|
||
|
|
'/orders/first-leg',
|
||
|
|
'/master-data/express-companies',
|
||
|
|
'/master-data/counterparties',
|
||
|
|
'/system/users',
|
||
|
|
'/system/roles',
|
||
|
|
'/system/permissions',
|
||
|
|
])
|
||
|
|
expect(new Set(paths).size).toBe(paths.length)
|
||
|
|
expect(new Set(permissions).size).toBe(permissions.length)
|
||
|
|
expect(leaves.every((item) => Boolean(item.icon))).toBe(true)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('recursively filters leaves without mutating the source tree', () => {
|
||
|
|
const sourceSnapshot = JSON.stringify(adminNavigation, (key, value) => key === 'icon' ? '[icon]' : value)
|
||
|
|
const filtered = filterAdminNavigation(adminNavigation, new Set(['platform:permission:list']))
|
||
|
|
|
||
|
|
expect(getNavigationLeaves(filtered).map((item) => item.path)).toEqual(['/system/permissions'])
|
||
|
|
expect(filtered).not.toBe(adminNavigation)
|
||
|
|
expect(filtered[0]).not.toBe(adminNavigation[2])
|
||
|
|
expect(JSON.stringify(adminNavigation, (key, value) => key === 'icon' ? '[icon]' : value)).toBe(sourceSnapshot)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('keeps directories based on accessible descendants instead of a directory permission', () => {
|
||
|
|
const nodes: AdminNavigationNode[] = [{
|
||
|
|
key: 'operations',
|
||
|
|
title: '运营管理',
|
||
|
|
icon: adminNavigation[0].icon,
|
||
|
|
permission: 'directory:denied',
|
||
|
|
children: [{
|
||
|
|
key: 'tracking',
|
||
|
|
title: '轨迹管理',
|
||
|
|
icon: adminNavigation[0].icon,
|
||
|
|
path: '/tracking',
|
||
|
|
permission: 'tracking:list',
|
||
|
|
}],
|
||
|
|
}]
|
||
|
|
|
||
|
|
const filtered = filterAdminNavigation(nodes, new Set(['tracking:list']))
|
||
|
|
|
||
|
|
expect(filtered).toHaveLength(1)
|
||
|
|
expect(filtered[0].children?.[0].path).toBe('/tracking')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('selects the first fallback leaf in strict depth-first configuration order', () => {
|
||
|
|
const nodes: AdminNavigationNode[] = [{
|
||
|
|
key: 'level-one',
|
||
|
|
title: '一级目录',
|
||
|
|
icon: adminNavigation[0].icon,
|
||
|
|
children: [{
|
||
|
|
key: 'level-two',
|
||
|
|
title: '二级目录',
|
||
|
|
icon: adminNavigation[0].icon,
|
||
|
|
children: [{
|
||
|
|
key: 'tracking',
|
||
|
|
title: '轨迹管理',
|
||
|
|
icon: adminNavigation[0].icon,
|
||
|
|
path: '/tracking',
|
||
|
|
permission: 'tracking:list',
|
||
|
|
}],
|
||
|
|
}, {
|
||
|
|
key: 'reports',
|
||
|
|
title: '报表管理',
|
||
|
|
icon: adminNavigation[0].icon,
|
||
|
|
path: '/reports',
|
||
|
|
permission: 'reports:list',
|
||
|
|
}],
|
||
|
|
}]
|
||
|
|
|
||
|
|
expect(getNavigationLeaves(nodes).map((item) => item.path)).toEqual(['/tracking', '/reports'])
|
||
|
|
expect(getFirstNavigationLeaf(nodes)?.path).toBe('/tracking')
|
||
|
|
})
|
||
|
|
})
|