/** * 京东数据库侧栏(JdCategorySidebar)的挂载契约。 * * 纯展示组件:取数与失败兜底全在页面(props 流入、emit 流出),本文件零 api mock—— * 钉的是双 Tab 切换、商品目录「京东平台类目层级树」口径(2026-11-26 客户口径 * 「怎么都没有分类一级二级分类」「管理员要分类时候那么多怎么看」:一级/二级可展开、 * 默认全收起只露大类、零计数分支不出现、父级点选=整棵子树名字集合、树外历史名单列 * 兜底组)、可写档分档入口。 * * 跑法:`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' import type { JdCategoryNode } from '@/pages/jd/jd-category-hierarchy.ts' /** 类目计数夹具:树内名(服饰内衣/女装/连衣裙支+艺考形体服支)+树外历史名, * 读数=全部商品 37、服饰内衣 20(7+4+9)、运动户外 5、树外兜底 12。 */ const CATEGORY_OPTIONS: JdProductCategoryOption[] = [ { name: '连衣裙', productCount: 7 }, { name: '短袖连衣裙', productCount: 4 }, { name: '艺考形体服', productCount: 5 }, { name: '短裤', productCount: 9 }, { name: '已下架历史类目', productCount: 12 }, ] /** 授权树夹具(京麦 `{name, value, children}` 形状;生产真机最深 4 级,这里两层足够)。 */ const CATEGORY_TREE: JdCategoryNode[] = [ { name: '服饰内衣', value: '1', children: [ { name: '女装', value: '11', children: [ { name: '连衣裙', value: '111' }, { name: '短袖连衣裙', value: '112' }, { name: '短裤', value: '113' }, ], }, ], }, { name: '运动户外', value: '2', children: [ { name: '舞蹈体操鞋服/装备', value: '21', children: [{ name: '艺考形体服', value: '211' }], }, ], }, // 授权树里本店可卖、但这批采集 0 行:零计数整支必须被剪掉。 { name: '母婴', value: '3', children: [{ name: '童装', value: '31', children: [{ name: '儿童礼服', value: '311' }] }], }, ] 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[]; names?: string[] } | null selectedGroupId?: number | null selectedShopName?: string | null categories?: JdProductGroup[] categoryOptions?: JdProductCategoryOption[] categoryTree?: JdCategoryNode[] } function mountSidebar(options: MountOptions = {}): VueWrapper { return mount(JdCategorySidebar, { props: { categoryOptions: options.categoryOptions ?? CATEGORY_OPTIONS, categoryTree: options.categoryTree ?? CATEGORY_TREE, 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('出厂全收起:只露一级大类(零计数分支不出现),父级徽标=子树命中行数', () => { 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:not(.jd-side-orphan)') expect(rows.map((row) => row.findAll('.jd-side-name').at(0)!.text())).toEqual([ '服饰内衣', '运动户外', ], '默认只露一级:母婴整支 0 行被剪掉,女装/末级都要点开才出来') expect(rows.map((row) => row.find('.jd-side-count').text())).toEqual(['20', '5'], '一级徽标=整棵子树命中行数(服饰内衣=7+4+9)') expect(rows.every((row) => row.classes().includes('depth-0'))).toBe(true) expect(rows.every((row) => row.classes().includes('branch'))).toBe(true, '一级有下级=可展开行') expect(catalog.findAll('.jd-side-arrow')).toHaveLength(2, '每个有下级的行一个展开箭头') wrapper.unmount() }) it('点箭头只展开/收起(不触发筛选),展开后按层级缩进露出下一级', async () => { const wrapper = mountSidebar() const catalog = wrapper.get('.jd-side-catalog') const first = catalog.findAll('.jd-side-tree .jd-side-row:not(.jd-side-orphan)')[0] await first.get('.jd-side-arrow').trigger('click') await flushPromises() expect(wrapper.emitted('select-category')).toBeUndefined('点箭头是展开态,不是筛选') const rows = catalog.findAll('.jd-side-tree .jd-side-row:not(.jd-side-orphan)') expect(rows.map((row) => row.findAll('.jd-side-name').at(0)!.text())) .toEqual(['服饰内衣', '女装', '运动户外'], '只展开这一个分支,运动户外不动') expect(rows[1].classes()).toContain('depth-1') expect(rows[1].find('.jd-side-count').text()).toBe('20', '二级徽标=自身子树(7+4+9)') expect(first.get('.jd-side-arrow').attributes('aria-expanded')).toBe('true') // 再点一次收起:子孙行消失。 await first.get('.jd-side-arrow').trigger('click') await flushPromises() expect(catalog.findAll('.jd-side-tree .jd-side-row:not(.jd-side-orphan)')).toHaveLength(2) wrapper.unmount() }) it('点一级行 emit 整棵子树的名字集合(父级也能选来批量分类)', 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: ['服饰内衣'], names: ['服饰内衣', '女装', '连衣裙', '短袖连衣裙', '短裤'], }, ]], 'names=本节点名+全部子孙名(末级为主,父级名兜底),页面按 IN 集合下发') wrapper.unmount() }) it('点末级行 emit 单名集合(与旧平铺行同形);选中态按名称+祖先链高亮', async () => { const wrapper = mountSidebar({ categoryTree: CATEGORY_TREE, selectedCategory: { name: '短裤', path: ['服饰内衣', '女装', '短裤'] }, }) await flushPromises() const catalog = wrapper.get('.jd-side-catalog') const leaf = catalog.findAll('.jd-side-tree .jd-side-row:not(.jd-side-orphan)') .find((row) => row.text().includes('短裤'))! expect(leaf.classes()).toContain('selected', '挂载即按选中行展开祖先链') expect(leaf.classes()).toContain('depth-2') await leaf.trigger('click') await flushPromises() expect(wrapper.emitted('select-category')).toEqual([[ { name: '短裤', path: ['服饰内衣', '女装', '短裤'], names: ['短裤'] }, ]], '末级行单名过滤') expect(catalog.get('.jd-side-all').classes()).not.toContain('selected', '选了类目就不再是全选态') wrapper.unmount() }) it('树里搜不到的历史末级名:单列「平台树外」组,点了按单名过滤(不藏、不冒充树内节点)', async () => { const wrapper = mountSidebar() const catalog = wrapper.get('.jd-side-catalog') expect(catalog.get('.jd-side-group-head').text()).toContain('平台树外') await rowByText(catalog, '已下架历史类目').trigger('click') await flushPromises() expect(wrapper.emitted('select-category')).toEqual([[ { name: '已下架历史类目', path: [], names: ['已下架历史类目'] }, ]]) 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({ categoryTree: [] }) const catalog = wrapper.get('.jd-side-catalog') const rows = catalog.findAll('.jd-side-tree .jd-side-row:not(.jd-side-orphan)') expect(rows.map((row) => row.findAll('.jd-side-name').at(0)!.text())).toEqual([ '连衣裙', '短袖连衣裙', '艺考形体服', '短裤', '已下架历史类目', ], '没有树就没有层级概念:计数行按后端顺序平铺') expect(catalog.find('.jd-side-arrow').exists()).toBe(false, '平铺行没有展开箭头') expect(catalog.find('.jd-side-group-head').exists()).toBe(false, '没有树也就没有「树外」') wrapper.unmount() }) }) describe('类目计数未就绪(首帧加载态)', () => { it('未就绪 ⇒ 只留加载行,不画列表(避免空列表先闪一下再跳成有数据)', () => { const wrapper = mount(JdCategorySidebar, { props: { categoryOptions: CATEGORY_OPTIONS, categoryTree: CATEGORY_TREE, 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, categoryTree: CATEGORY_TREE, 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:not(.jd-side-orphan)')).toHaveLength(2, '一级两个(默认收起)') wrapper.unmount() }) it('计数为空(从未采集过)⇒ 空态一句话,不是空白列表', () => { const wrapper = mount(JdCategorySidebar, { props: { categoryOptions: [], categoryTree: CATEGORY_TREE, 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() }) })