/** * 京东分类管理页(V204,2026-11-17 用户口径「店铺分类弹窗删了,加个京东分类管理的子菜单 * 维护 CRUD」)的挂载契约:当前数据权限分组的分类建/改名/删除独立成页;分组作用域与 * 京东数据库页同一套(京东侧分组,非亚马逊——platform-scope JD_MENU_KEYS); * 商品归类不在这里(去京东数据库页「批量编辑分类」)。 * * 跑法:`npx vitest run tests/mount/jd-category-manager-page.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 JdCategoryManagerPage from '@/pages/jd/JdCategoryManagerPage.vue' const jdApi = vi.hoisted(() => ({ fetchJdCategoryScope: vi.fn(), fetchJdProductGroups: vi.fn(), createJdProductGroup: vi.fn(), updateJdProductGroup: vi.fn(), deleteJdProductGroup: vi.fn(), })) const confirmMock = vi.hoisted(() => vi.fn()) vi.mock('@/pages/jd/jd-product-api.ts', () => jdApi) vi.mock('@/components/admin-confirm', () => ({ openAdminConfirm: confirmMock })) /** 分组 id 常量:默认作用域就这一个可见且可写的组(组长档形状)。 */ const GROUP_A = 11 function singleGroupScope(editable = true) { return { platformSuperAdmin: false, defaultGroupId: GROUP_A, groups: [{ id: GROUP_A, name: '甲组', label: '甲组', editable }], } } function group(id: number, name: string, productCount = 0, sortOrder = 0) { return { id, name, ownerGroupId: GROUP_A, sortOrder, productCount } } let pinia: Pinia beforeEach(() => { vi.clearAllMocks() pinia = createPinia() setActivePinia(pinia) jdApi.fetchJdCategoryScope.mockResolvedValue(singleGroupScope()) jdApi.fetchJdProductGroups.mockResolvedValue([]) confirmMock.mockResolvedValue(true) }) async function mountPage(): Promise> { const wrapper = mount(JdCategoryManagerPage, { global: { plugins: [pinia] } }) await flushPromises() await flushPromises() return wrapper } describe('京东分类管理(V204 子菜单页)', () => { it('首屏按默认组取分类;多组可见出「分组」下拉,切组重取', async () => { jdApi.fetchJdCategoryScope.mockResolvedValue({ platformSuperAdmin: true, defaultGroupId: GROUP_A, groups: [ { id: GROUP_A, name: 'A组', label: '甲公司 / A组', editable: true }, { id: 12, name: 'B组', label: '甲公司 / B组', editable: false }, ], }) jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) const wrapper = await mountPage() expect(jdApi.fetchJdProductGroups).toHaveBeenCalledWith(GROUP_A) const picker = wrapper.get('[aria-label="当前分组"]') expect((picker.element as HTMLSelectElement).closest('.form-group')?.querySelector('label')?.textContent).toBe('分组', '分组是带标签的表单控件(与京东数据库页筛选行同口径)') expect(picker.findAll('option').map((o) => o.text())).toEqual(['甲公司 / A组', '甲公司 / B组']) await picker.setValue('12') await flushPromises() // 切组后分类按新组重取(跨组不可见,V203)。 expect(jdApi.fetchJdProductGroups).toHaveBeenLastCalledWith(12) wrapper.unmount() }) it('新建分类:落当前分组归属(ownerGroupId 必带),成功后清输入并重取', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) const wrapper = await mountPage() await wrapper.get('.group-create input').setValue('上新区') await wrapper.findAll('button').find((b) => b.text() === '新建')!.trigger('click') await flushPromises() expect(jdApi.createJdProductGroup).toHaveBeenCalledWith({ name: '上新区', sortOrder: 0, ownerGroupId: GROUP_A }) expect((wrapper.get('.group-create input').element as HTMLInputElement).value).toBe('') expect(jdApi.fetchJdProductGroups).toHaveBeenCalledTimes(2, '成功后重取分类列表') wrapper.unmount() }) it('改名:行内编辑后按 (id, {name, sortOrder}) 提交', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) const wrapper = await mountPage() await wrapper.findAll('button').find((b) => b.text() === '改名')!.trigger('click') await wrapper.get('.rename-input').setValue('长裙') await wrapper.findAll('button').find((b) => b.text() === '保存')!.trigger('click') await flushPromises() expect(jdApi.updateJdProductGroup).toHaveBeenCalledWith(7, { name: '长裙', sortOrder: 0 }) wrapper.unmount() }) it('删除:二次确认文案点名商品数(删除不删商品,只解挂归属),确认后调端点重取', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) const wrapper = await mountPage() await wrapper.findAll('button').find((b) => b.text() === '删除')!.trigger('click') await flushPromises() expect(confirmMock).toHaveBeenCalledTimes(1) const options = confirmMock.mock.calls[0][0] expect(options.title).toBe('删除分类') expect(options.message).toContain('确定删除分类「裙装」吗') expect(options.message).toContain('3 个商品回到未分类', '文案点名该分类下的商品数') expect(options.danger).toBe(true) expect(jdApi.deleteJdProductGroup).toHaveBeenCalledWith(7) wrapper.unmount() }) it('只读档(editable=false):无新建行、无改名/删除按钮,空表文案不带「先新建」', async () => { jdApi.fetchJdCategoryScope.mockResolvedValue(singleGroupScope(false)) jdApi.fetchJdProductGroups.mockResolvedValue([]) const wrapper = await mountPage() expect(wrapper.find('.group-create').exists()).toBe(false, '只读账号不渲染新建行') expect(wrapper.findAll('button').some((b) => b.text() === '改名')).toBe(false) expect(wrapper.findAll('button').some((b) => b.text() === '删除')).toBe(false) expect(wrapper.find('thead').text()).not.toContain('操作', '只读档连操作列表头都不渲染') expect(wrapper.find('.empty-tip').text()).toBe('本组暂无分类') wrapper.unmount() }) it('不属于任何数据权限分组:锁定芯片直说空态,不出下拉与新建行', async () => { jdApi.fetchJdCategoryScope.mockResolvedValue({ platformSuperAdmin: false, defaultGroupId: null, groups: [] }) const wrapper = await mountPage() expect(wrapper.find('[aria-label="当前分组"]').exists()).toBe(false) expect(wrapper.get('.group-locked').text()).toBe('你还不属于任何数据权限分组') expect(wrapper.find('.group-create').exists()).toBe(false) expect(wrapper.find('.empty-tip').text()).toBe('你还不属于任何数据权限分组') wrapper.unmount() }) it('加载失败:红条可见且重试真能重跑(取数页硬规矩),切组后重取成功即清', async () => { jdApi.fetchJdProductGroups.mockRejectedValueOnce(new Error('上游 502')) const wrapper = mount(JdCategoryManagerPage, { global: { plugins: [pinia] } }) await flushPromises() await flushPromises() const bar = wrapper.find('.load-failure-bar') expect(bar.exists()).toBe(true, '失败态必须可见(不是一闪而过的 toast)') expect(bar.text()).toContain('分类列表加载失败') expect(bar.text()).toContain('上游 502', '后端/网络原因要进文案') jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) await bar.find('button').trigger('click') await flushPromises() await flushPromises() expect(wrapper.find('.load-failure-bar').exists()).toBe(false, '重试成功后红条消失') expect(wrapper.text()).toContain('裙装') wrapper.unmount() }) it('页面职责边界(反向钉):归类不在本页——页面不出现批量归类/商品列表入口', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) const wrapper = await mountPage() expect(wrapper.findAll('button').some((b) => b.text() === '批量编辑分类')).toBe(false, '归类入口在京东数据库页') expect(wrapper.text()).toContain('京东数据库', '页内提示指路「京东数据库」页') expect(jdApi.deleteJdProductGroup).not.toHaveBeenCalled() wrapper.unmount() }) })