/** * 京东分类管理对话框(JdCategoryManageDialog)的挂载契约。 * * 2026-11-17 用户口径「京东分类管理并到京东数据库分类编辑里,做一个搜索和添加 删除 * 编辑 分类名称即可」:V204 独立子菜单页(JdCategoryManagerPage,已删)整体并入本对话框, * 从京东数据库页工具栏「分类管理」打开。原页面契约逐条承接:当前数据权限分组的 * 分类建/改名/删除;新增名称搜索(本地过滤)。商品归类不在这里(去「批量编辑分类」)。 * 权限口径(V203)不变:editable 才渲染写控件;同组普通号只读可看。 * * 跑法:`npx vitest run tests/mount/jd-category-manage-dialog.mount.test.ts` */ import { beforeEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' import { createPinia, setActivePinia, type Pinia } from 'pinia' import JdCategoryManageDialog from '@/pages/jd/JdCategoryManageDialog.vue' const jdApi = vi.hoisted(() => ({ 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 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.fetchJdProductGroups.mockResolvedValue([]) confirmMock.mockResolvedValue(true) }) async function mountDialog(editable = true): Promise> { const wrapper = mount(JdCategoryManageDialog, { global: { plugins: [pinia] }, props: { visible: true, ownerGroupId: GROUP_A, ownerGroupName: '甲组', editable, }, }) await flushPromises() await flushPromises() return wrapper } describe('京东分类管理对话框(V204 独立页 2026-11-17 并入京东数据库页)', () => { it('打开即按当前分组取分类(ownerGroupId 必带);标题带当前分组名', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) const wrapper = await mountDialog() expect(jdApi.fetchJdProductGroups).toHaveBeenCalledWith(GROUP_A) expect(wrapper.get('.manage-modal').text()).toContain('分类管理') expect(wrapper.get('.manage-modal').text()).toContain('甲组', '标题/上下文带当前分组名') expect(wrapper.findAll('.option-row')).toHaveLength(1) wrapper.unmount() }) it('新建分类:落当前分组归属(ownerGroupId 必带),成功后清输入重取并通知父页刷新', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) const wrapper = await mountDialog() 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, '成功后重取分类列表') expect(wrapper.emitted('changed'), '写成功必须 emit changed 让父页刷筛选下拉').toBeTruthy() wrapper.unmount() }) it('搜索:按名称包含本地过滤(不发额外请求),命中后新建会清关键字', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3), group(8, '裤装', 1), group(9, '配饰', 0)]) const wrapper = await mountDialog() const callsBefore = jdApi.fetchJdProductGroups.mock.calls.length await wrapper.get('.search-input').setValue('裙') await flushPromises() expect(wrapper.findAll('.option-row')).toHaveLength(1, '「裙」只命中裙装') expect(wrapper.findAll('.option-row')[0].text()).toContain('裙装') expect(jdApi.fetchJdProductGroups.mock.calls.length).toBe(callsBefore, '搜索是本地过滤,不另发请求') expect(wrapper.text()).not.toContain('裤装', '不匹配项不出现') await wrapper.get('.group-create input').setValue('上新区') await wrapper.findAll('button').find((b) => b.text() === '新建')!.trigger('click') await flushPromises() expect((wrapper.get('.search-input').element as HTMLInputElement).value).toBe('', '新建成功清关键字(新分类不被旧搜索藏住)') wrapper.unmount() }) it('搜索无命中给显式空态文案(不是空列表静默)', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) const wrapper = await mountDialog() await wrapper.get('.search-input').setValue('不存在的分类') expect(wrapper.get('.manage-empty').text()).toBe('没有匹配的分类') wrapper.unmount() }) it('分页:每页 6 条、翻页只换当前页、改搜索回第一页(2026-11-17 用户口径「分类这也要加分页」)', async () => { jdApi.fetchJdProductGroups.mockResolvedValue( Array.from({ length: 11 }, (_, index) => group(100 + index, `分类${index + 1}`)), ) const wrapper = await mountDialog() expect(wrapper.findAll('.option-row')).toHaveLength(6, '第一页只渲染 6 行') expect(wrapper.text()).not.toContain('分类7', '第 7 条以后不在第一页') const pagerText = wrapper.get('.manage-pager').text() expect(pagerText).toContain('第 1 / 2 页') expect(pagerText).toContain('共 11 个分类', '总数取搜索命中集,不是当前页行数') await wrapper.findAll('.manage-pager button').find((b) => b.text() === '下一页')!.trigger('click') await flushPromises() expect(wrapper.findAll('.option-row')).toHaveLength(5, '第二页剩 5 行') expect(wrapper.text()).toContain('分类11') expect(wrapper.get('.manage-pager').text()).toContain('第 2 / 2 页') await wrapper.get('.search-input').setValue('分类1') await flushPromises() expect(wrapper.find('.manage-pager').exists()).toBe(false, '命中 3 条不足一页 ⇒ 不渲染页码条') expect(wrapper.findAll('.option-row')).toHaveLength(3, '搜索回第一页(不是停在旧页码的空白处)') wrapper.unmount() }) it('分页:一屏装得下就不出页码条(不白占一行)', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3), group(8, '裤装', 1)]) const wrapper = await mountDialog() expect(wrapper.findAll('.option-row')).toHaveLength(2) expect(wrapper.find('.manage-pager').exists()).toBe(false) wrapper.unmount() }) it('搜索与新建行同属 manage-bar 的两行(搜索在上、新建在下,2026-11-17 改垂直排列)', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) const wrapper = await mountDialog() const bar = wrapper.get('.manage-bar') const children = Array.from(bar.element.children) expect(children).toHaveLength(2, 'bar 只有两件:搜索框、新建行') expect(children[0].classList.contains('search-input')).toBe(true, '垂直排列时搜索在上') expect(children[1].classList.contains('group-create')).toBe(true, '新建行在下') wrapper.unmount() }) it('改名:行内编辑后按 (id, {name, sortOrder}) 提交并 emit changed', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) const wrapper = await mountDialog() 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 }) expect(wrapper.emitted('changed')).toBeTruthy() wrapper.unmount() }) it('删除:二次确认文案点名商品数(删除不删商品,只解挂归属),确认后调端点重取', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) const wrapper = await mountDialog() 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) expect(jdApi.fetchJdProductGroups).toHaveBeenCalledTimes(2, '删除成功后重取列表') expect(wrapper.emitted('changed')).toBeTruthy() wrapper.unmount() }) it('删除确认点取消就不发请求(防误删第一道闸)', async () => { confirmMock.mockResolvedValue(false) jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)]) const wrapper = await mountDialog() await wrapper.findAll('button').find((b) => b.text() === '删除')!.trigger('click') await flushPromises() expect(confirmMock).toHaveBeenCalledTimes(1) expect(jdApi.deleteJdProductGroup).not.toHaveBeenCalled() wrapper.unmount() }) it('只读档(editable=false):无新建行、无搜索仍可用、无改名/删除按钮', async () => { const wrapper = await mountDialog(false) 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.get('.search-input').exists()).toBe(true, '搜索是读面,只读档保留') wrapper.unmount() }) it('不属于任何数据权限分组(ownerGroupId<=0):不发取数请求,空态直说', async () => { const wrapper = mount(JdCategoryManageDialog, { global: { plugins: [pinia] }, props: { visible: true, ownerGroupId: 0, ownerGroupName: '', editable: false }, }) await flushPromises() await flushPromises() expect(jdApi.fetchJdProductGroups).not.toHaveBeenCalled() expect(wrapper.get('.manage-empty').text()).toBe('你还不属于任何数据权限分组') expect(wrapper.find('.group-create').exists()).toBe(false) wrapper.unmount() }) it('本组暂无分类:可写档文案引导新建,只读档直说', async () => { const writable = await mountDialog(true) expect(writable.get('.manage-empty').text()).toBe('本组暂无分类,先新建一个') writable.unmount() const readonly = await mountDialog(false) expect(readonly.get('.manage-empty').text()).toBe('本组暂无分类') readonly.unmount() }) it('加载失败:红条可见且重试真能重跑(取数页硬规矩)', async () => { jdApi.fetchJdProductGroups.mockRejectedValueOnce(new Error('上游 502')) const wrapper = mount(JdCategoryManageDialog, { global: { plugins: [pinia] }, props: { visible: true, ownerGroupId: GROUP_A, ownerGroupName: '甲组', editable: true }, }) 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 mountDialog() expect(wrapper.findAll('button').some((b) => b.text() === '批量编辑分类')).toBe(false, '归类入口在京东数据库页') expect(wrapper.text()).toContain('批量编辑分类', '但提示要指路(关掉本框去勾选商品归类)') expect(jdApi.deleteJdProductGroup).not.toHaveBeenCalled() wrapper.unmount() }) })