71 lines
3.0 KiB
TypeScript
71 lines
3.0 KiB
TypeScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readSource } from './helpers.ts'
|
|
import { compactDirectGrantIds, type MenuOptionNode } from '../src/pages/account/user-menu-auth.ts'
|
|
|
|
// 验收反馈:创建/编辑用户的权限菜单里,勾选父菜单要自动全选子菜单。
|
|
// 实现:el-tree 去掉 check-strictly 走父子联动;提交时把勾选 id 压缩为最小
|
|
// 直接授权集(父级已勾选时后代不再重复提交,后端按“父级授权展开后代”判断权限)。
|
|
|
|
const tree: MenuOptionNode[] = [
|
|
{
|
|
id: 1,
|
|
key: 'root',
|
|
name: '账号权限',
|
|
sort: 1,
|
|
parentId: null,
|
|
type: 'admin',
|
|
children: [
|
|
{ id: 11, key: 'users', name: '用户管理', sort: 1, parentId: 1, type: 'admin' },
|
|
{ id: 12, key: 'menus', name: '菜单管理', sort: 2, parentId: 1, type: 'admin' },
|
|
{
|
|
id: 13,
|
|
key: 'sub',
|
|
name: '子分组',
|
|
sort: 3,
|
|
parentId: 1,
|
|
type: 'admin',
|
|
children: [
|
|
{ id: 131, key: 'leaf-a', name: '叶子A', sort: 1, parentId: 13, type: 'admin' },
|
|
{ id: 132, key: 'leaf-b', name: '叶子B', sort: 2, parentId: 13, type: 'admin' },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{ id: 2, key: 'wb', name: '视频', sort: 1, parentId: null, type: 'app' },
|
|
]
|
|
|
|
test('compactDirectGrantIds_keep_parent_only_when_children_also_checked', () => {
|
|
// 勾选父 + 联动全选的子:压缩后只保留最上层父级。
|
|
assert.deepEqual(compactDirectGrantIds([1, 11, 12, 13, 131, 132], tree), [1])
|
|
})
|
|
|
|
test('compactDirectGrantIds_keep_partial_children_when_parent_unchecked', () => {
|
|
// 父级未勾选、只勾部分子级:原样保留(父级半选,子级为直接授权)。
|
|
assert.deepEqual(compactDirectGrantIds([11, 131], tree), [11, 131])
|
|
})
|
|
|
|
test('compactDirectGrantIds_keep_topmost_checked_in_multi_level', () => {
|
|
// 多层嵌套:爷勾选后,父/孙虽也被勾选但均被覆盖,只留爷。
|
|
assert.deepEqual(compactDirectGrantIds([1, 13, 131], tree), [1])
|
|
// 爷未勾、子分组勾选时保留子分组(覆盖其叶子)。
|
|
assert.deepEqual(compactDirectGrantIds([13, 131], tree), [13])
|
|
})
|
|
|
|
test('compactDirectGrantIds_merges_two_groups_and_dedupes', () => {
|
|
const merged = [...tree, { id: 21, key: 'dup', name: '重复', sort: 1, parentId: null, type: 'admin' }]
|
|
assert.deepEqual(compactDirectGrantIds([1, 11, 2, 21, 21], merged), [1, 2, 21])
|
|
})
|
|
|
|
test('compactDirectGrantIds_empty_inputs', () => {
|
|
assert.deepEqual(compactDirectGrantIds([], tree), [])
|
|
assert.deepEqual(compactDirectGrantIds([11], []), [11])
|
|
})
|
|
|
|
test('user_menu_auth_tree_uses_linked_check_and_compacts', () => {
|
|
const component = readSource('src/pages/account/UserMenuAuthTree.vue')
|
|
assert.ok(!/check-strictly/.test(component), '必须去掉 check-strictly,勾父联动全选子')
|
|
assert.match(component, /compactDirectGrantIds/, '@check 提交需压缩为最小直接授权集')
|
|
assert.match(component, /default-expand-all/, '默认展开全部菜单')
|
|
})
|