/** * 京东工作台卡片按 app 菜单权限(V209)显隐的挂载用例。 * * 四态判据,与 AmazonConsolePage 同形: * ① 组键 jd_tools 命中 ⇒ 两张卡都在(V209 预授权授的就是组行,线上默认形态); * ② 只授 jd-collect ⇒ 只剩采集卡(上架占位同样受控,转正后行为不变); * ③ 权限集与京东无关 ⇒ 两张卡都隐藏; * ④ 权限接口异常 ⇒ 展示全部(现用人不因接口抖动丢功能)。 * * 跑法:cd frontend-vue && npx vitest run tests/mount/jd-console-permission.mount.test.ts */ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { mount, type VueWrapper } from '@vue/test-utils' vi.mock('@/shared/api/permission', () => ({ getCurrentUserAppColumnKeys: vi.fn(), })) import { getCurrentUserAppColumnKeys } from '@/shared/api/permission' import JdConsolePage from '@/pages/jd/JdConsolePage.vue' let wrapper: VueWrapper | null = null const mockKeys = (keys: string[]) => vi.mocked(getCurrentUserAppColumnKeys).mockResolvedValue(keys) async function mountConsole() { wrapper = mount(JdConsolePage, { global: { stubs: { NotificationBell: true, RouterLink: { template: '' }, 'router-link': { template: '' }, }, }, }) // onMounted 里权限是异步 await,等过滤落地后再断言 await vi.waitFor(() => { expect(vi.mocked(getCurrentUserAppColumnKeys).mock.calls.length).toBeGreaterThan(0) }) return wrapper } describe('京东工作台按菜单权限显隐(V209)', () => { beforeEach(() => { window.localStorage.setItem('username', 'admin') vi.mocked(getCurrentUserAppColumnKeys).mockReset() }) afterEach(() => { wrapper?.unmount() wrapper = null window.localStorage.clear() }) it('组键命中 ⇒ 两张卡都在', async () => { mockKeys(['jd_tools', 'jd-tools']) const w = await mountConsole() await vi.waitFor(() => expect(w.findAll('.tool-card').length).toBe(2)) }) it('只授 jd-collect ⇒ 只剩获取店铺数据卡', async () => { mockKeys(['jd-collect']) const w = await mountConsole() await vi.waitFor(() => expect(w.findAll('.tool-card').length).toBe(1)) // 断言圈在卡片节点上:hero 文案("店铺采集 · 商品上架")与计数是页面静态区, // 不参与权限过滤,拿整页 text() 判"商品上架消失"会误红。 const cards = w.findAll('.tool-card') expect(cards[0].text()).toContain('获取店铺数据') expect(cards.every((card) => !card.classes().includes('tool-card--pending'))).toBe(true) expect(w.find('.group-count').text()).toContain('1 个工具') }) it('权限集与京东无关 ⇒ 两张卡都隐藏', async () => { mockKeys(['wb', 'collect-data']) const w = await mountConsole() await vi.waitFor(() => expect(w.findAll('.tool-card').length).toBe(0)) expect(w.findAll('.tool-card').length).toBe(0) }) it('权限接口异常 ⇒ 展示全部(兜底不让现用人丢入口)', async () => { vi.mocked(getCurrentUserAppColumnKeys).mockRejectedValue(new Error('获取菜单权限失败')) const w = await mountConsole() await vi.waitFor(() => expect(w.findAll('.tool-card').length).toBe(2)) }) })