/** * 京东数据库侧栏(JdCategorySidebar)的挂载契约。 * * 纯展示组件:取数与失败兜底全在页面(props 流入、emit 流出),本文件零 api mock—— * 钉的是双 Tab 切换、商品目录「京东平台类目平铺」口径(2026-11-26 客户口径「京东分类 * 百分百有,京东内分类不需要」:平铺一层、点行=末级类目名等值、path 恒空、无授权树/ * 无内置快照/无「树外兜底」分组)、可写档分档入口。 * * 跑法:`npx vitest run tests/mount/jd-category-sidebar.mount.test.ts` */ import { beforeEach, describe, expect, it } from 'vitest' import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' import JdCategorySidebar from '@/pages/jd/JdCategorySidebar.vue' import type { JdProductCategoryOption, JdProductGroup } from '@/pages/jd/jd-product-api.ts' /** 类目计数夹具:树内名(连衣裙支+同名跨支短裤)+树外历史名,读数与 Phase 1 * 纯函数测试同源——服饰内衣根徽标 20(子树去重)、全部商品 37、兜底 12。 */ const CATEGORY_OPTIONS: JdProductCategoryOption[] = [ { name: '连衣裙', productCount: 7 }, { name: '短袖连衣裙', productCount: 4 }, { name: '艺考形体服', productCount: 5 }, { name: '短裤', productCount: 9 }, { name: '已下架历史类目', productCount: 12 }, ] const CATEGORIES: JdProductGroup[] = [ { id: 7, name: '裙装', ownerGroupId: 11, sortOrder: 0, productCount: 3 }, { id: 8, name: '裤装', ownerGroupId: 11, sortOrder: 1, productCount: 1 }, ] /** 店铺名单夹具(v1.7 F6):裙装带两名单店,裤装无名单(无箭头)。 */ const CATEGORIES_WITH_SHOPS: JdProductGroup[] = [ { id: 7, name: '裙装', ownerGroupId: 11, sortOrder: 0, productCount: 3, shops: ['绿屿街道店', '花屿巷旗舰店'] }, { id: 8, name: '裤装', ownerGroupId: 11, sortOrder: 1, productCount: 1 }, ] interface MountOptions { editable?: boolean selectedCategory?: { name: string; path: string[] } | null selectedGroupId?: number | null selectedShopName?: string | null categories?: JdProductGroup[] } function mountSidebar(options: MountOptions = {}): VueWrapper { return mount(JdCategorySidebar, { props: { categoryOptions: CATEGORY_OPTIONS, categories: options.categories ?? CATEGORIES, editable: options.editable ?? true, selectedCategory: options.selectedCategory ?? null, selectedGroupId: options.selectedGroupId ?? null, selectedShopName: options.selectedShopName ?? null, }, }) } /** 在指定 Tab 体内按文案找行(两个 Tab 体 v-show 共存于 DOM,必须先圈定范围)。 */ function rowByText(scope: VueWrapper, text: string) { const row = scope.findAll('.jd-side-row').find((node) => node.text().includes(text)) expect(row, `应存在文案含「${text}」的侧栏行`).toBeTruthy() return row! } describe('商品目录 Tab(京东平台类目平铺,2026-11-26)', () => { it('出厂停在商品目录 Tab;「全部商品」徽标=计数行求和;一类目一行平铺(无树层级/无分组头)', () => { const wrapper = mountSidebar() const catalog = wrapper.get('.jd-side-catalog') const all = catalog.get('.jd-side-all') expect(all.text()).toContain('全部商品') expect(all.text()).toContain('37', '全部商品徽标=5 条计数行求和(7+4+5+9+12)') const rows = catalog.findAll('.jd-side-tree .jd-side-row') expect(rows.map((row) => row.findAll('.jd-side-name').at(0)!.text())).toEqual([ '连衣裙', '短袖连衣裙', '艺考形体服', '短裤', '已下架历史类目', ], '按后端给的类目名顺序平铺,一行一个末级类目名') expect(rows.map((row) => row.find('.jd-side-count').text())).toEqual(['7', '4', '5', '9', '12'], '每行徽标=该类目的商品数(后端 productCount 原样)') // 平铺形状:没有层级缩进类、没有箭头、没有「授权树外的类目」分组头、没有展开操作行。 expect(rows.every((row) => row.classes().includes('depth-0'))).toBe(true) expect(catalog.find('.jd-side-arrow').exists()).toBe(false, '平铺列表没有展开箭头') expect(catalog.find('.jd-side-tree-ops').exists()).toBe(false, '平铺列表没有展开/收起操作行') expect(catalog.text()).not.toContain('授权树外的类目', '不再有树外兜底分组(客户口径:京东分类百分百有)') wrapper.unmount() }) it('点类目行 emit {name, path: []}(页面按单名集合 IN 下发);选中态按类目名高亮', async () => { const wrapper = mountSidebar() const catalog = wrapper.get('.jd-side-catalog') await rowByText(catalog, '连衣裙').trigger('click') await flushPromises() expect(wrapper.emitted('select-category')).toEqual([[{ name: '连衣裙', path: [] }]], '平铺行不带路径:path 空=单名等值,与本页旧「树外名」同形') await wrapper.setProps({ selectedCategory: { name: '短裤', path: [] } }) expect(rowByText(catalog, '短裤').classes()).toContain('selected') expect(rowByText(catalog, '连衣裙').classes()).not.toContain('selected') expect(catalog.get('.jd-side-all').classes()).not.toContain('selected', '选了类目就不再是全选态') wrapper.unmount() }) it('「全部商品」行 emit null(页面清类目筛选重查)', async () => { const wrapper = mountSidebar() const catalog = wrapper.get('.jd-side-catalog') await catalog.get('.jd-side-all').trigger('click') await flushPromises() expect(wrapper.emitted('select-category')).toEqual([[null]]) wrapper.unmount() }) it('挂载即带选中态:对应行直接高亮(平铺无祖先链,无需自动展开)', () => { const wrapper = mountSidebar({ selectedCategory: { name: '已下架历史类目', path: [] } }) const catalog = wrapper.get('.jd-side-catalog') expect(rowByText(catalog, '已下架历史类目').classes()).toContain('selected') expect(catalog.get('.jd-side-all').classes()).not.toContain('selected') wrapper.unmount() }) }) describe('类目计数未就绪(首帧加载态)', () => { it('未就绪 ⇒ 只留加载行,不画列表(避免空列表先闪一下再跳成有数据)', () => { const wrapper = mount(JdCategorySidebar, { props: { categoryOptions: CATEGORY_OPTIONS, categories: CATEGORIES, editable: true, selectedCategory: null, selectedGroupId: null, categoriesReady: false, }, }) const catalog = wrapper.get('.jd-side-catalog') expect(catalog.text()).toContain('类目加载中…') expect(catalog.find('.jd-side-tree').exists()).toBe(false, '未就绪不画列表') expect(catalog.get('.jd-side-all').text()).toContain('—', '总数未就绪显示占位') wrapper.unmount() }) it('数据到达(就绪)后同一实例切出列表与计数,加载行消失', async () => { const wrapper = mount(JdCategorySidebar, { props: { categoryOptions: [], categories: CATEGORIES, editable: true, selectedCategory: null, selectedGroupId: null, categoriesReady: false, }, }) await wrapper.setProps({ categoryOptions: CATEGORY_OPTIONS, categoriesReady: true }) await flushPromises() const catalog = wrapper.get('.jd-side-catalog') expect(catalog.find('.jd-side-loading').exists()).toBe(false) expect(catalog.get('.jd-side-all').text()).toContain('37') expect(catalog.findAll('.jd-side-tree .jd-side-row')).toHaveLength(5) wrapper.unmount() }) it('计数为空(从未采集过)⇒ 空态一句话,不是空白列表', () => { const wrapper = mount(JdCategorySidebar, { props: { categoryOptions: [], categories: [], editable: true, selectedCategory: null, selectedGroupId: null }, }) const catalog = wrapper.get('.jd-side-catalog') expect(catalog.get('.jd-side-empty').text()).toContain('还没有采集到类目数据') wrapper.unmount() }) }) describe('店铺分类 Tab(当前分组分类平铺+入口分档)', () => { it('点店铺分类切 Tab;分类行带计数;点行 emit id、再点已选中的 emit null(toggle 清选)', async () => { const wrapper = mountSidebar() const tabs = wrapper.findAll('.jd-sidebar-tab') expect(tabs.map((tab) => tab.text())).toEqual(['商品目录', '店铺分类']) await tabs[1]!.trigger('click') await flushPromises() expect(tabs[1]!.classes()).toContain('active') const custom = wrapper.get('.jd-side-custom') const skirt = rowByText(custom, '裙装') expect(skirt.text()).toContain('3', '分类行计数徽标') await skirt.trigger('click') await flushPromises() expect(wrapper.emitted('select-group')).toEqual([[7]]) await wrapper.setProps({ selectedGroupId: 7 }) expect(rowByText(custom, '裙装').classes()).toContain('selected', '选中态回显') await rowByText(custom, '裙装').trigger('click') await flushPromises() expect(wrapper.emitted('select-group')?.at(-1)).toEqual([null], '再点已选中=清选回全部分类') wrapper.unmount() }) it('可写档出「新建分类」行:emit 分类名并清输入,空名按钮置灰;只读档不渲染该行', async () => { const editable = mountSidebar({ editable: true }) const custom = editable.get('.jd-side-custom') const input = custom.get('[aria-label="新分类名称"]') const createBtn = custom.findAll('button').find((b) => b.text() === '新建分类')! expect(createBtn.attributes('disabled')).toBeDefined() await input.setValue('上新区') await createBtn.trigger('click') await flushPromises() expect(editable.emitted('create-category')).toEqual([['上新区']], '侧栏只 emit,建分类的 API 调用与刷新在页面(Phase 5 接线)') expect((input.element as HTMLInputElement).value).toBe('', '提交即清输入') await input.setValue(' ') expect(createBtn.attributes('disabled')).toBeDefined(), '纯空白名按钮置灰' editable.unmount() const readonly = mountSidebar({ editable: false }) expect(readonly.get('.jd-side-custom').find('[aria-label="新分类名称"]').exists()).toBe(false, '只读档不渲染新建行') readonly.unmount() }) it('「分类管理」入口对所有人渲染(同组普通号只读可看 V203),点击 emit manage-categories', async () => { for (const editable of [true, false]) { const wrapper = mountSidebar({ editable }) const manage = wrapper.get('.jd-side-custom').get('.jd-side-manage') expect(manage.text()).toBe('分类管理') await manage.trigger('click') await flushPromises() expect(wrapper.emitted('manage-categories')).toHaveLength(1) wrapper.unmount() } }) it('空分类空态文案分档:可写档引导新建,只读档直说', () => { const writable = mount(JdCategorySidebar, { props: { categoryOptions: [], categories: [], editable: true, selectedCategory: null, selectedGroupId: null }, }) expect(writable.get('.jd-side-custom').get('.jd-side-empty').text()).toBe('本组暂无分类,先新建一个') writable.unmount() const readonly = mount(JdCategorySidebar, { props: { categoryOptions: [], categories: [], editable: false, selectedCategory: null, selectedGroupId: null }, }) expect(readonly.get('.jd-side-custom').get('.jd-side-empty').text()).toBe('本组暂无分类') readonly.unmount() }) }) /** 店铺名单(v1.7 F6,2026-11-22 原型对齐铺开形态):分类行下直接铺店铺行(点选过滤/ * 行内改名移除)+「+ 添加店铺」入口常驻(空分类也能加第一个店——旧折叠版箭头只在 * 已有名单时才出 ⇒ 空分类永远加不进第一个店铺的死锁,判据就在下面的空分类用例)。 * 侧栏纯展示:全部只 emit,API 调用与刷新在页面。 */ describe('店铺名单(v1.7 铺开形态:分类下挂店铺名单)', () => { async function openCustomTab(wrapper: VueWrapper): Promise { const tabs = wrapper.findAll('.jd-sidebar-tab') await tabs[1]!.trigger('click') await flushPromises() } it('店铺名单铺开:店铺行直接可见(无折叠箭头),可写档名单尾出添加店铺入口', async () => { const wrapper = mountSidebar({ categories: CATEGORIES_WITH_SHOPS }) await openCustomTab(wrapper) const custom = wrapper.get('.jd-side-custom') expect(custom.find('[aria-label="展开或收起 裙装 的店铺名单"]').exists()).toBe(false, '铺开形态无店铺名单折叠箭头(类目树的箭头不受影响)') const shops = custom.findAll('.jd-side-shop') expect(shops).toHaveLength(2, '店铺行首屏直接可见(服务端升序原样渲染)') expect(shops[0]!.text()).toContain('绿屿街道店') expect(shops[1]!.text()).toContain('花屿巷旗舰店') expect(custom.get('.jd-side-shop-add').text()).toContain('+ 添加店铺', '可写档名单尾出添加入口') wrapper.unmount() }) it('空分类也常驻「+ 添加店铺」入口(2026-11-22 死锁修复:空分类能加进第一个店铺)', async () => { const wrapper = mountSidebar({ categories: CATEGORIES }) await openCustomTab(wrapper) const custom = wrapper.get('.jd-side-custom') expect(custom.findAll('.jd-side-shop')).toHaveLength(0, '两个分类都无名单') const addRows = custom.findAll('.jd-side-shop-add') expect(addRows).toHaveLength(2, '空分类名单尾照样出添加入口(旧折叠版此处为 0=加不进第一个店)') await addRows[1]!.trigger('click') await flushPromises() expect(wrapper.emitted('add-shops')).toEqual([[CATEGORIES[1]]], '点裤装(空分类)的添加入口=emit 该分类') wrapper.unmount() }) it('点店铺行 emit {groupId, shopName} 成对下发;选中回流高亮;再点已选 emit null(只清店铺保留分类)', async () => { const wrapper = mountSidebar({ categories: CATEGORIES_WITH_SHOPS, selectedGroupId: 7 }) await openCustomTab(wrapper) const shopRow = wrapper.get('.jd-side-custom').findAll('.jd-side-shop')[0]! await shopRow.trigger('click') await flushPromises() expect(wrapper.emitted('select-shop')).toEqual([[{ groupId: 7, shopName: '绿屿街道店' }]], '点店铺=分类+店铺成对(页面发 group_id+shop_name 精确过滤)') await wrapper.setProps({ selectedShopName: '绿屿街道店' }) expect(wrapper.get('.jd-side-custom').findAll('.jd-side-shop')[0]!.classes()).toContain('selected', '选中回流高亮') await wrapper.get('.jd-side-custom').findAll('.jd-side-shop')[0]!.trigger('click') await flushPromises() expect(wrapper.emitted('select-shop')?.at(-1)).toEqual([null], '再点已选=清店铺筛选(分类保留)') wrapper.unmount() }) it('页面带着店铺筛选挂载:店铺行直接可见并高亮(铺开态无需展开)', async () => { const wrapper = mountSidebar({ categories: CATEGORIES_WITH_SHOPS, selectedGroupId: 7, selectedShopName: '花屿巷旗舰店' }) await openCustomTab(wrapper) const selected = wrapper.get('.jd-side-custom').findAll('.jd-side-shop').find((row) => row.text().includes('花屿巷旗舰店')) expect(selected, '铺开态店铺行首屏可见').toBeTruthy() expect(selected!.classes()).toContain('selected') wrapper.unmount() }) it('可写档:分类行与店铺行 hover 出改名/删除操作钮;分类行内改名 Enter 提交 emit (cat, 新名)', async () => { const wrapper = mountSidebar({ categories: CATEGORIES_WITH_SHOPS }) await openCustomTab(wrapper) const custom = wrapper.get('.jd-side-custom') expect(custom.get('[aria-label="改名分类 裙装"]').exists()).toBe(true) expect(custom.get('[aria-label="删除分类 裙装"]').exists()).toBe(true) // 分类行内改名:点 ✎ → 行替换成编辑输入(预填原名)→ Enter 提交。 await custom.get('[aria-label="改名分类 裙装"]').trigger('click') await flushPromises() const editInput = custom.get('[aria-label="修改分类名称(原:裙装)"]') expect((editInput.element as HTMLInputElement).value).toBe('裙装', '编辑形态预填原名') await editInput.setValue('长裙') await editInput.trigger('keyup.enter') await flushPromises() expect(wrapper.emitted('rename-category')).toEqual([[CATEGORIES_WITH_SHOPS[0], '长裙']], '侧栏只 emit(API 调用与刷新在页面)') // 空名/未改动不 emit(静默收编辑态)。 await custom.get('[aria-label="改名分类 裙装"]').trigger('click') await flushPromises() const unchanged = custom.get('[aria-label="修改分类名称(原:裙装)"]') await unchanged.setValue(' 裙装 ') await unchanged.trigger('keyup.enter') await flushPromises() expect(wrapper.emitted('rename-category')).toHaveLength(1, '未改动不白发请求') // 店铺行 hover 操作钮随铺开直接可见。 expect(custom.get('[aria-label="改名店铺 绿屿街道店"]').exists()).toBe(true) expect(custom.get('[aria-label="移除店铺 绿屿街道店"]').exists()).toBe(true) wrapper.unmount() }) it('店铺行内改名 emit (cat, oldName, newName);移除 emit (cat, shopName);添加店铺入口 emit (cat)', async () => { const wrapper = mountSidebar({ categories: CATEGORIES_WITH_SHOPS }) await openCustomTab(wrapper) const custom = wrapper.get('.jd-side-custom') // 店铺改名:✎ → 预填 → 改名 → ✓ 提交。 await custom.get('[aria-label="改名店铺 绿屿街道店"]').trigger('click') await flushPromises() const shopEdit = custom.get('[aria-label="修改店铺名称(原:绿屿街道店)"]') expect((shopEdit.element as HTMLInputElement).value).toBe('绿屿街道店') await shopEdit.setValue('绿屿新址店') await custom.get('[aria-label="确认改名店铺 绿屿街道店"]').trigger('click') await flushPromises() expect(wrapper.emitted('rename-shop')).toEqual([[CATEGORIES_WITH_SHOPS[0], '绿屿街道店', '绿屿新址店']]) // 移除:hover ✕ 直接 emit(可重做,不设二次确认)。 await custom.get('[aria-label="移除店铺 花屿巷旗舰店"]').trigger('click') await flushPromises() expect(wrapper.emitted('remove-shop')).toEqual([[CATEGORIES_WITH_SHOPS[0], '花屿巷旗舰店']]) // 添加店铺入口。 await custom.get('.jd-side-shop-add').trigger('click') await flushPromises() expect(wrapper.emitted('add-shops')).toEqual([[CATEGORIES_WITH_SHOPS[0]]]) wrapper.unmount() }) it('只读档:无 hover 操作钮、无添加店铺入口;店铺行仍可见可点(读面 V203)', async () => { const wrapper = mountSidebar({ categories: CATEGORIES_WITH_SHOPS, editable: false }) await openCustomTab(wrapper) const custom = wrapper.get('.jd-side-custom') expect(custom.find('[aria-label="改名分类 裙装"]').exists()).toBe(false, '分类行无改名钮') expect(custom.find('[aria-label="删除分类 裙装"]').exists()).toBe(false, '分类行无删除钮') expect(custom.findAll('.jd-side-shop')).toHaveLength(2, '店铺行照常可见(读面)') expect(custom.find('.jd-side-shop-add').exists()).toBe(false, '只读档无添加店铺入口') expect(custom.find('[aria-label="改名店铺 绿屿街道店"]').exists()).toBe(false, '店铺行无改名钮') expect(custom.find('[aria-label="移除店铺 绿屿街道店"]').exists()).toBe(false, '店铺行无移除钮') await custom.findAll('.jd-side-shop')[0]!.trigger('click') await flushPromises() expect(wrapper.emitted('select-shop')).toEqual([[{ groupId: 7, shopName: '绿屿街道店' }]], '只读档点店铺照常过滤') wrapper.unmount() }) it('分类行仍是 toggle 选中(点行 emit id/再点 null),店铺行高亮不改变分类行选中判定', async () => { const wrapper = mountSidebar({ categories: CATEGORIES_WITH_SHOPS, selectedGroupId: 7 }) await openCustomTab(wrapper) expect(rowByText(wrapper, '裙装').classes()).toContain('selected', '分类行选中(含选店铺时)') await rowByText(wrapper, '裙装').trigger('click') await flushPromises() expect(wrapper.emitted('select-group')).toEqual([[null]], '再点已选分类=清选(页面同清店铺筛选)') wrapper.unmount() }) })