Files
workspace/code/fms/fms-vue/tests/unit/module-picker-modal.spec.js
T
2026-09-15 17:00:29 +08:00

85 lines
2.3 KiB
JavaScript

/**
* 模块绑定弹层(ModulePickerModal):模块树默认全部收起。
*
* 回归点:弹层曾用 default-expanded-keys 展开第一层根节点,一打开就铺开整棵树;
* 现在不设该 prop,初始只渲染根节点,子节点由用户按需逐层点开。
* 折叠态下子树不渲染(tree-node.vue 用 v-if 而非 v-show),因此可直接数 treeitem。
*/
import { beforeEach, describe, expect, it } from 'vitest'
import { flushPromises, mount } from '@vue/test-utils'
import { nextTick } from 'vue'
import ModulePickerModal from '@/views/module/menu-management/ModulePickerModal.vue'
const TREE = [
{
b_id: 'base',
b_module_type: 'module',
b_name: '基础资料',
children: [
{
b_id: 'base_otherdata',
b_module_type: 'module',
b_name: '其他数据',
children: [{ b_id: 'b_port', b_module_type: 'data', b_name: '港口' }],
},
],
},
{
b_id: 'system',
b_module_type: 'module',
b_name: '系统',
children: [{ b_id: 'b_user', b_module_type: 'data', b_name: '用户' }],
},
]
const MODULE_TYPES = {
base: 'module',
base_otherdata: 'module',
b_port: 'data',
system: 'module',
b_user: 'data',
}
async function mountModal(props = {}) {
const wrapper = mount(ModulePickerModal, {
props: {
open: true,
treeData: TREE,
checkedKeys: [],
moduleTypes: MODULE_TYPES,
...props,
},
// Modal 用 Teleport 挂到 body;打桩后内容留在组件树内,便于断言
global: { stubs: { teleport: true } },
})
await flushPromises()
await nextTick()
return wrapper
}
beforeEach(() => {
localStorage.clear()
sessionStorage.clear()
})
describe('模块绑定弹层:树默认收起', () => {
it('初始只渲染根节点,没有任何节点处于展开态', async () => {
const wrapper = await mountModal()
const items = wrapper.findAll('[role="treeitem"]')
expect(items).toHaveLength(TREE.length)
expect(items.map((item) => item.attributes('aria-expanded'))).toEqual([
'false',
'false',
])
})
it('折叠态下子节点不渲染', async () => {
const wrapper = await mountModal()
expect(wrapper.text()).toContain('基础资料')
expect(wrapper.text()).not.toContain('其他数据')
expect(wrapper.text()).not.toContain('港口')
})
})