import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { DOMWrapper, flushPromises, mount, type VueWrapper } from '@vue/test-utils' import ElementPlus from 'element-plus' import { createPinia, setActivePinia, type Pinia } from 'pinia' import MenusPage from '@/pages/account/MenusPage.vue' import type { MenuManageNode } from '@/pages/account/menu-manage-model' import { useAdminSessionStore } from '@/stores/admin-session' import { useCompanyTabStore } from '@/stores/company-tab' import { usePlatformTabStore } from '@/stores/platform-tab' const menuApi = vi.hoisted(() => ({ loadMenuManageTree: vi.fn(), createMenu: vi.fn(), updateMenu: vi.fn(), deleteMenu: vi.fn(), reorderMenus: vi.fn(), })) vi.mock('@/pages/account/menu-manage-api', () => menuApi) /** 同一次加载返回完整字典,平台隔离由真实页面完成。 */ function mixedMenuTree(): MenuManageNode[] { let nextId = 1 function node( columnKey: string, name: string, menuType: 'admin' | 'app', children: MenuManageNode[] = [], ): MenuManageNode { const id = nextId++ return { id, columnKey, name, menuType, parentId: null, routePath: '', sortOrder: id, children: children.map((child) => ({ ...child, parentId: id })), } } return [ node('video_image', '视频图片', 'app'), node('admin_account', '账号与权限管理', 'admin', [ node('admin_columns', '菜单权限管理', 'admin'), node('admin_users', '用户管理', 'admin'), node('admin_group_manage', '分组管理', 'admin'), node('admin_companies', '公司管理', 'admin'), node('admin_user_secrets', '密钥管理', 'admin'), node('admin_user_secret_usage', '密钥用量统计', 'admin'), ]), node('admin_data', '数据管理', 'admin', [ node('admin_shop_manage', '店铺管理', 'admin'), node('admin_appearance_patent', '外观专利管理', 'admin'), node('admin_similar_asin', '相似ASIN管理', 'admin'), node('admin_price_track', '跟价管理', 'admin'), node('admin_collect_data', '采集数据管理', 'admin'), node('admin_publish', '发布管理', 'admin'), node('admin_brand', '品牌管理', 'admin'), node('admin_records', '生成记录管理', 'admin'), node('admin_jd_database', '京东数据库', 'admin'), ]), node('jd_tools', '京东工具台', 'app', [ node('jd-collect', '京东店铺数据采集', 'app'), node('jd-publish', '京东商品上架', 'app'), ]), ] } let pinia: Pinia let wrapper: VueWrapper | undefined let host: HTMLDivElement beforeEach(() => { vi.clearAllMocks() window.localStorage.removeItem('admin-platform-tab') pinia = createPinia() setActivePinia(pinia) useAdminSessionStore().user = { id: 1, username: 'root', role: 'super_admin' } useCompanyTabStore().setActiveId(7) menuApi.loadMenuManageTree.mockResolvedValue(mixedMenuTree()) host = document.createElement('div') document.body.appendChild(host) }) afterEach(() => { wrapper?.unmount() wrapper = undefined host.remove() window.localStorage.removeItem('admin-platform-tab') }) async function mountMenus() { wrapper = mount(MenusPage, { attachTo: host, global: { plugins: [pinia, ElementPlus] } }) await flushPromises() expect(menuApi.loadMenuManageTree).toHaveBeenCalledWith(7) return wrapper } function visibleNames(page: VueWrapper) { return page.findAll('tbody .menu-name-text').map((cell) => cell.text()) } async function clickNextPage(page: VueWrapper) { const next = page.findAll('.old-pagination button').find((button) => button.text() === '下一页') expect(next).toBeTruthy() expect(next!.attributes('disabled')).toBeUndefined() await next!.trigger('click') await flushPromises() } /** 通过真实分页收齐表格行,避免只检查首屏而漏掉后页的跨平台菜单。 */ async function allVisibleNames(page: VueWrapper) { const names = visibleNames(page) while (true) { const next = page.findAll('.old-pagination button').find((button) => button.text() === '下一页') if (!next || next.attributes('disabled') !== undefined) return names await clickNextPage(page) names.push(...visibleNames(page)) } } async function pickMenuType(page: VueWrapper, label: string) { const input = page.get('[aria-label="筛选菜单类型"]') await input.trigger('click') await flushPromises() const contentId = input.attributes('aria-controls') const panel = contentId ? document.getElementById(contentId) : null expect(panel).toBeTruthy() const option = [...panel!.querySelectorAll('.el-select-dropdown__item')] .find((item) => !item.classList.contains('is-hidden') && item.textContent?.trim() === label) expect(option).toBeTruthy() await new DOMWrapper(option!).trigger('click') await flushPromises() } describe('菜单权限配置页的平台隔离', () => { it('京东表格排除视频图片和密钥,保留账号基础菜单、数据管理祖先及京东工具', async () => { usePlatformTabStore().setActive('jd') const page = await mountMenus() expect(await allVisibleNames(page)).toEqual([ '账号与权限管理', '菜单权限管理', '用户管理', '分组管理', '公司管理', '数据管理', '京东数据库', '京东工具台', '京东店铺数据采集', '京东商品上架', ]) const databaseRow = page.findAll('tbody tr') .find((row) => row.find('.menu-name-text').text() === '京东数据库') expect(databaseRow!.findAll('td')[4].text()).toBe('数据管理') expect(databaseRow!.find('.column-child-mark').exists()).toBe(true) }) it('从京东切回亚马逊,视频图片和密钥仍在,京东数据库及京东工具均消失', async () => { usePlatformTabStore().setActive('jd') const page = await mountMenus() usePlatformTabStore().setActive('amazon') await flushPromises() expect(await allVisibleNames(page)).toEqual([ '视频图片', '账号与权限管理', '菜单权限管理', '用户管理', '分组管理', '公司管理', '密钥管理', '密钥用量统计', '数据管理', '店铺管理', '外观专利管理', '相似ASIN管理', '跟价管理', '采集数据管理', '发布管理', '品牌管理', '生成记录管理', ]) expect(menuApi.loadMenuManageTree).toHaveBeenCalledTimes(1) }) it('带名称和类型筛选进入第二页后切平台,回到第一页并清空两项筛选', async () => { const page = await mountMenus() const nameInput = page.get('input[placeholder="输入菜单名称"]') await nameInput.setValue('管理') await pickMenuType(page, '后台') expect(page.findAll('.menus-filter-row .el-select__selected-item').map((item) => item.text())).toContain('后台') expect(page.findAll('tbody .menu-type-text').every((cell) => cell.text() === '后台')).toBe(true) await clickNextPage(page) expect(page.get('.page-cur').text()).toBe('第 2 / 2 页') expect(visibleNames(page).length).toBeGreaterThan(0) expect(visibleNames(page).every((name) => name.includes('管理'))).toBe(true) usePlatformTabStore().setActive('jd') await flushPromises() expect(nameInput.element.value).toBe('') expect(page.findAll('.menus-filter-row .el-select__selected-item').map((item) => item.text())).toContain('全部') expect(page.get('.page-cur').text()).toBe('第 1 / 1 页') expect(page.get('.page-total').text()).toBe('共 10 条') expect(visibleNames(page)).toEqual([ '账号与权限管理', '菜单权限管理', '用户管理', '分组管理', '公司管理', '数据管理', '京东数据库', '京东工具台', '京东店铺数据采集', '京东商品上架', ]) expect(menuApi.loadMenuManageTree).toHaveBeenCalledTimes(1) }) })