/** * 用户管理页 × 平台 tab(V206)的挂载契约: * 账号域三页两 tab 共用组件,列表请求必须带 platform=当前平台;切 tab 重取数; * 「上架上限」列仅京东侧渲染(用户口径「不需要在亚马逊那边」),文案 0=不限/null=-。 * 不用 readFileSync(source-pin-ratchet 对台账外新文件带读点即红),全部走真实挂载断言。 * * 跑法:`npx vitest run tests/mount/users-page-platform.mount.test.ts` */ import { beforeEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount } from '@vue/test-utils' import { createPinia, setActivePinia, type Pinia } from 'pinia' import UsersPage from '@/pages/account/UsersPage.vue' import { usePlatformTabStore } from '@/stores/platform-tab' import { useAdminSessionStore } from '@/stores/admin-session' const usersApi = vi.hoisted(() => ({ fetchUserList: vi.fn(), deleteUser: vi.fn(), setUserDisabled: vi.fn(), })) vi.mock('@/api/users', () => usersApi) const confirmBatch = vi.hoisted(() => vi.fn()) vi.mock('@/components/admin-confirm', () => ({ openAdminConfirm: confirmBatch })) vi.mock('@/api/companies', () => ({ fetchCompanies: vi.fn().mockResolvedValue([]) })) vi.mock('@/pages/account/user-menu-auth-api', () => ({ loadUserMenuAuth: vi.fn() })) function pageOf(total = 0) { return { items: [ { id: 7, username: '京东号', role: 'normal', platform: 'jd' }, { id: 8, username: '存量号', role: 'admin', jdPublishQuota: 0 }, { id: 9, username: '老号', role: 'normal', jdPublishQuota: 800 }, { id: 10, username: '没配号', role: 'normal' }, ], total, page: 1, pageSize: 10, } } let pinia: Pinia beforeEach(() => { vi.clearAllMocks() window.localStorage.removeItem('admin-platform-tab') pinia = createPinia() setActivePinia(pinia) const session = useAdminSessionStore() session.user = { id: 1, username: 'root', role: 'super_admin' } usersApi.fetchUserList.mockResolvedValue(pageOf()) usersApi.deleteUser.mockReset().mockResolvedValue(undefined) confirmBatch.mockReset().mockResolvedValue(false) }) it('批量删除取消不发请求;部分失败只保留失败用户', async () => { usersApi.deleteUser.mockRejectedValueOnce(new Error('无权删除此用户')) const wrapper = mount(UsersPage) await flushPromises() const button = wrapper.get('.batch-delete-actions .btn-danger') expect(button.attributes('disabled')).toBeDefined() await wrapper.get('input[aria-label="选择 京东号"]').setValue(true) await wrapper.get('input[aria-label="选择 存量号"]').setValue(true) await button.trigger('click') await flushPromises() expect(confirmBatch).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining('京东号、存量号') })) expect(usersApi.deleteUser).not.toHaveBeenCalled() confirmBatch.mockResolvedValue(true) await button.trigger('click') await flushPromises() expect(usersApi.deleteUser.mock.calls).toEqual([[7], [8]]) expect(button.text()).toBe('批量删除(1)') expect(wrapper.get('details').text()).toContain('京东号:无权删除此用户') expect(usersApi.fetchUserList).toHaveBeenCalledTimes(2) wrapper.unmount() }) it('确认弹窗期间切平台,旧勾选不发删除请求', async () => { let resolveConfirm!: (ok: boolean) => void confirmBatch.mockReturnValue(new Promise((resolve) => { resolveConfirm = resolve })) const wrapper = mount(UsersPage) await flushPromises() await wrapper.get('input[aria-label="选择 京东号"]').setValue(true) await wrapper.get('.batch-delete-actions .btn-danger').trigger('click') usePlatformTabStore().setActive('jd') resolveConfirm(true) await flushPromises() expect(usersApi.deleteUser).not.toHaveBeenCalled() expect(wrapper.get('.batch-delete-actions .btn-danger').text()).toBe('批量删除(0)') wrapper.unmount() }) describe('UsersPage × 平台 tab(V206)', () => { it('亚马逊 tab(默认):列表请求带 platform=amazon,无「上架上限」列', async () => { const wrapper = mount(UsersPage) await flushPromises() expect(usersApi.fetchUserList).toHaveBeenCalledTimes(1) const params = usersApi.fetchUserList.mock.calls[0][0] as Record expect(params.platform).toBe('amazon') expect(params).toHaveProperty('companyId') const headers = wrapper.findAll('thead th').map((th) => th.text()) expect(headers).not.toContain('上架上限') // 亚马逊侧保留原有 9 列,另加 1 列批量选择。 expect(headers.length).toBe(10) wrapper.unmount() }) it('切到京东 tab:重取数并带 platform=jd,「上架上限」列出现,文案 0=不限/null=-', async () => { const wrapper = mount(UsersPage) await flushPromises() const platformTab = usePlatformTabStore() platformTab.setActive('jd') await flushPromises() expect(usersApi.fetchUserList).toHaveBeenCalledTimes(2) const params = usersApi.fetchUserList.mock.calls[1][0] as Record expect(params.platform).toBe('jd') const headers = wrapper.findAll('thead th').map((th) => th.text()) expect(headers).toContain('上架上限') expect(headers.length).toBe(11) // quota 单元格:0=不限(存量号 8),null/缺省=-(京东号 7、没配号 10),800 原样 const quotaCells = wrapper.findAll('tbody tr').map((tr) => { const tds = tr.findAll('td') return tds[6]?.text() }) expect(quotaCells).toEqual(['-', '不限', '800', '-']) wrapper.unmount() }) it('切回亚马逊 tab:再次重取数且列消失(两 tab 各看各的数据面)', async () => { const wrapper = mount(UsersPage) await flushPromises() const platformTab = usePlatformTabStore() platformTab.setActive('jd') await flushPromises() platformTab.setActive('amazon') await flushPromises() expect(usersApi.fetchUserList).toHaveBeenCalledTimes(3) const params = usersApi.fetchUserList.mock.calls[2][0] as Record expect(params.platform).toBe('amazon') expect(wrapper.findAll('thead th').map((th) => th.text())).not.toContain('上架上限') wrapper.unmount() }) })