feat(后管): 京东数据库商品目录恢复一级/二级/末级层级树(2026-11-26 晚客户口径「怎么都没有分类一级二级分类」「管理员要分类时候那么多怎么看」)

- 新纯函数 jd-category-hierarchy.ts:京麦授权类目真树(GET /api/jd-shop-category-tree,
  按平台 node_id 并集)× /categories 末级名计数,名称精确等值挂接;计数按去重名字集合
  求和(同名跨支不重复计)、零计数分支剪枝、树外历史名进「平台树外」兜底组;
  树空=退化末级平铺(上一窗行为逐字相同),没有内置快照——第二份树不再存在
- 新 jd-shop-category-api.ts:只剩服务端真树一条路,失败/空静默回空数组
- 侧栏层级渲染:展开/收起、点父级=整棵子树末级名集合过滤(11-18 口径恢复)、
  挂载即展开选中行祖先链;同名跨支按路径核选中态
- filter/api:categoryPath(只喂回显展开)+categoryNames(选中那一刻展开好的过滤集合)
  复活,下发走后端既有 category IN 多值;清选时两者一起清,不留幽灵谓词
- 判据:jd-category-hierarchy.test.ts(挂接/去重/剪枝/兜底/平铺退化)+
  sidebar/database-page mount(真树层级、父级点选下发集合、树空平铺、回显展开)
This commit is contained in:
2026-11-26 17:59:43 +08:00
parent 9092849ae9
commit 2aa05398f9
10 changed files with 971 additions and 126 deletions
@@ -2,9 +2,10 @@
* 京东数据库侧栏(JdCategorySidebar)的挂载契约。
*
* 纯展示组件:取数与失败兜底全在页面(props 流入、emit 流出),本文件零 api mock——
* 钉的是双 Tab 切换、商品目录「京东平台类目平铺」口径(2026-11-26 客户口径「京东分类
* 百分百有,京东内分类不需要」:平铺一层、点行=末级类目名等值、path 恒空、无授权树/
* 无内置快照/无「树外兜底」分组)、可写档分档入口。
* 钉的是双 Tab 切换、商品目录「京东平台类目层级树」口径(2026-11-26 客户口径
* 「怎么都没有分类一级二级分类」「管理员要分类时候那么多怎么看」:一级/二级可展开、
* 默认全收起只露大类、零计数分支不出现、父级点选=整棵子树名字集合、树外历史名单列
* 兜底组)、可写档分档入口。
*
* 跑法:`npx vitest run tests/mount/jd-category-sidebar.mount.test.ts`
*/
@@ -14,9 +15,10 @@ 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'
/** 类目计数夹具:树内名(连衣裙支+同名跨支短裤)+树外历史名,读数与 Phase 1
* 纯函数测试同源——服饰内衣根徽标 20(子树去重)、全部商品 37、兜底 12。 */
/** 类目计数夹具:树内名(服饰内衣/女装/连衣裙支+艺考形体服支)+树外历史名,
* 读数=全部商品 37、服饰内衣 20(7+4+9)、运动户外 5、树外兜底 12。 */
const CATEGORY_OPTIONS: JdProductCategoryOption[] = [
{ name: '连衣裙', productCount: 7 },
{ name: '短袖连衣裙', productCount: 4 },
@@ -25,6 +27,42 @@ const CATEGORY_OPTIONS: JdProductCategoryOption[] = [
{ 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 },
@@ -38,16 +76,19 @@ const CATEGORIES_WITH_SHOPS: JdProductGroup[] = [
interface MountOptions {
editable?: boolean
selectedCategory?: { name: string; path: string[] } | null
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<Element> {
return mount(JdCategorySidebar, {
props: {
categoryOptions: CATEGORY_OPTIONS,
categoryOptions: options.categoryOptions ?? CATEGORY_OPTIONS,
categoryTree: options.categoryTree ?? CATEGORY_TREE,
categories: options.categories ?? CATEGORIES,
editable: options.editable ?? true,
selectedCategory: options.selectedCategory ?? null,
@@ -64,47 +105,96 @@ function rowByText(scope: VueWrapper<Element>, text: string) {
return row!
}
describe('商品目录 Tab(京东平台类目平铺,2026-11-26)', () => {
it('出厂停在商品目录 Tab;「全部商品」徽标=计数行求和;一类目一行平铺(无树层级/无分组头)', () => {
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)')
expect(all.text()).toContain('37', '全部商品徽标=5 条计数行求和(7+4+5+9+12,树外兜底行也算)')
const rows = catalog.findAll('.jd-side-tree .jd-side-row')
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.map((row) => row.find('.jd-side-count').text())).toEqual(['7', '4', '5', '9', '12'],
'每行徽标=该类目的商品数(后端 productCount 原样)')
// 平铺形状:没有层级缩进类、没有箭头、没有「授权树外的类目」分组头、没有展开操作行。
'服饰内衣',
'运动户外',
], '默认只露一级:母婴整支 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(catalog.find('.jd-side-arrow').exists()).toBe(false, '平铺列表没有展开箭头')
expect(catalog.find('.jd-side-tree-ops').exists()).toBe(false, '平铺列表没有展开/收起操作行')
expect(catalog.text()).not.toContain('授权树外的类目', '不再有树外兜底分组(客户口径:京东分类百分百有)')
expect(rows.every((row) => row.classes().includes('branch'))).toBe(true, '一级有下级=可展开行')
expect(catalog.findAll('.jd-side-arrow')).toHaveLength(2, '每个有下级的行一个展开箭头')
wrapper.unmount()
})
it('点类目行 emit {name, path: []}(页面按单名集合 IN 下发);选中态按类目名高亮', async () => {
it('点箭头只展开/收起(不触发筛选),展开后按层级缩进露出下一级', async () => {
const wrapper = mountSidebar()
const catalog = wrapper.get('.jd-side-catalog')
await rowByText(catalog, '连衣裙').trigger('click')
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')).toEqual([[{ name: '连衣裙', path: [] }]],
'平铺行不带路径:path 空=单名等值,与本页旧「树外名」同形')
expect(wrapper.emitted('select-category')).toBeUndefined('点箭头是展开态,不是筛选')
await wrapper.setProps({ selectedCategory: { name: '短裤', path: [] } })
expect(rowByText(catalog, '短裤').classes()).toContain('selected')
expect(rowByText(catalog, '连衣裙').classes()).not.toContain('selected')
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')
@@ -114,11 +204,19 @@ describe('商品目录 Tab(京东平台类目平铺,2026-11-26)', () => {
wrapper.unmount()
})
it('挂载即带选中态:对应行直接高亮(平铺无祖先链,无需自动展开)', () => {
const wrapper = mountSidebar({ selectedCategory: { name: '已下架历史类目', path: [] } })
it('树没上报过(空树)⇒ 退化为末级平铺,行为与上一窗逐字相同', () => {
const wrapper = mountSidebar({ categoryTree: [] })
const catalog = wrapper.get('.jd-side-catalog')
expect(rowByText(catalog, '已下架历史类目').classes()).toContain('selected')
expect(catalog.get('.jd-side-all').classes()).not.toContain('selected')
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()
})
})
@@ -128,6 +226,7 @@ describe('类目计数未就绪(首帧加载态)', () => {
const wrapper = mount(JdCategorySidebar, {
props: {
categoryOptions: CATEGORY_OPTIONS,
categoryTree: CATEGORY_TREE,
categories: CATEGORIES,
editable: true,
selectedCategory: null,
@@ -142,7 +241,7 @@ describe('类目计数未就绪(首帧加载态)', () => {
wrapper.unmount()
})
it('数据到达(就绪)后同一实例切出列表与计数,加载行消失', async () => {
it('数据到达(就绪)后同一实例切出层级与计数,加载行消失', async () => {
const wrapper = mount(JdCategorySidebar, {
props: {
categoryOptions: [],
@@ -153,18 +252,29 @@ describe('类目计数未就绪(首帧加载态)', () => {
categoriesReady: false,
},
})
await wrapper.setProps({ categoryOptions: CATEGORY_OPTIONS, categoriesReady: true })
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')).toHaveLength(5)
expect(catalog.findAll('.jd-side-tree .jd-side-row:not(.jd-side-orphan)')).toHaveLength(2, '一级两个(默认收起)')
wrapper.unmount()
})
it('计数为空(从未采集过)⇒ 空态一句话,不是空白列表', () => {
const wrapper = mount(JdCategorySidebar, {
props: { categoryOptions: [], categories: [], editable: true, selectedCategory: null, selectedGroupId: null },
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('还没有采集到类目数据')
@@ -57,7 +57,14 @@ const groupApi = vi.hoisted(() => ({
createShopGroup: vi.fn(),
}))
/** 授权类目树 API(2026-11-26 恢复层级:页面取 `GET /api/jd-shop-category-tree`)。
* 必须 mock——不 mock 就真发请求(jsdom 里静默失败 ⇒ 树恒空 ⇒ 层级用例假绿)。 */
const treeApi = vi.hoisted(() => ({
fetchJdShopCategoryTree: vi.fn(),
}))
vi.mock('@/pages/jd/jd-product-api.ts', () => jdApi)
vi.mock('@/pages/jd/jd-shop-category-api.ts', () => treeApi)
vi.mock('@/pages/account/shop-group-api.ts', () => groupApi)
vi.mock('@/components/admin-confirm', () => ({ openAdminConfirm: confirmMock }))
@@ -129,6 +136,9 @@ beforeEach(() => {
jdApi.fetchJdCategoryScope.mockResolvedValue(singleGroupScope())
jdApi.fetchJdProductGroups.mockResolvedValue([])
jdApi.fetchJdProductCategories.mockResolvedValue([])
// 授权类目树:默认「还没上报过」(树空 ⇒ 侧栏退化为末级平铺,保持既有用例的行集不变);
// 层级用例显式给树。
treeApi.fetchJdShopCategoryTree.mockResolvedValue({ nodes: [], shops: [] })
// 店铺名单(v1.7)默认空回执:只有名单用例显式变形。
jdApi.removeJdGroupShop.mockResolvedValue(undefined)
jdApi.renameJdGroupShop.mockResolvedValue(undefined)
@@ -319,55 +329,77 @@ describe('布局:筛选行+工具栏上移顶部通栏(2026-11-21 用户
})
})
describe('侧栏商品目录(京东平台类目平铺,2026-11-26)', () => {
it('类目行点选=末级类目名等值下发(category+path 空),「全部商品」清选重查', async () => {
/** 授权树夹具(2026-11-26 恢复层级):与 CATEGORY_OPTIONS 同源,父级徽标=子树命中行数。 */
const CATEGORY_TREE = [
{
name: '服饰内衣',
value: '1',
children: [
{
name: '女装',
value: '11',
children: [
{ name: '连衣裙', value: '111' },
{ name: '艺考形体服', value: '112' },
],
},
],
},
]
describe('侧栏商品目录(京东平台类目层级树,2026-11-26)', () => {
it('页面取授权树画一级/二级;点一级=整棵子树名字集合下发(父级也能选来批量分类)', async () => {
jdApi.fetchJdProductCategories.mockResolvedValue(CATEGORY_OPTIONS)
treeApi.fetchJdShopCategoryTree.mockResolvedValue({ nodes: CATEGORY_TREE, shops: [] })
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 50 })
expect(jdApi.fetchJdProductCategories).toHaveBeenCalledWith()
expect(treeApi.fetchJdShopCategoryTree).toHaveBeenCalled()
const catalog = wrapper.get('.jd-side-catalog')
// 一类目一行平铺:末级类目名直接点,不再有「一级/二级下钻」与展开箭头。
expect(catalog.find('.jd-side-arrow').exists()).toBe(false, '平铺列表没有展开箭头')
expect(catalog.findAll('.jd-side-tree .jd-side-row').length).toBeGreaterThan(0)
// 默认收起:只露一级(「服饰内衣」),但有展开箭头;末级要展开才出来。
expect(catalog.find('.jd-side-arrow').exists()).toBe(true, '有下级的一级行带展开箭头')
expect(sideRowBy(catalog, '服饰内衣').find('.jd-side-count').text()).toBe('12',
'一级徽标=子树命中行数(连衣裙 7+艺考形体服 5)')
expect(catalog.text()).toContain('平台树外', '「已下架历史类目」不在树里 ⇒ 进兜底组')
await sideRowBy(catalog, '艺考形体服').trigger('click')
await sideRowBy(catalog, '服饰内衣').trigger('click')
await flushPromises()
const leaf = jdApi.fetchJdProductList.mock.calls.at(-1)![0]
expect(leaf).toMatchObject({ category: '艺考形体服', page: 1 },
'末级类目名等值过滤(后端 category_path IN 单元素集合)')
expect('categoryPath' in leaf, '父级路径这一层已退役:api 入参不再有 categoryPath').toBe(false)
expect('categoryNames' in leaf, '子树展开集合同样退役:入参不再有 categoryNames').toBe(false)
const parent = jdApi.fetchJdProductList.mock.calls.at(-1)![0]
expect(parent).toMatchObject({ category: '服饰内衣', page: 1 },
'父级点选:category 存回显名')
expect(parent.categoryNames).toEqual(['服饰内衣', '女装', '连衣裙', '艺考形体服'],
'整棵子树的节点名集合(后端 category_path IN 多值)')
expect(parent.categoryPath).toEqual(['服饰内衣'], '路径只供侧栏展开回显,不参与过滤')
// 点「全部商品」行=清选重查(category 回空、query 层不下发,回第一页)。
// 点「全部商品」行=清选重查(名字与集合一起清干净,不留幽灵谓词)。
const callsBefore = jdApi.fetchJdProductList.mock.calls.length
await catalog.get('.jd-side-all').trigger('click')
await flushPromises()
expect(jdApi.fetchJdProductList.mock.calls.length).toBe(callsBefore + 1, '清选也要重查')
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0].category).toBe('')
expect('categoryPath' in jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toBe(false)
const cleared = jdApi.fetchJdProductList.mock.calls.at(-1)![0]
expect(cleared.category).toBe('')
expect(cleared.categoryNames).toEqual([])
expect(cleared.categoryPath).toEqual([])
wrapper.unmount()
})
it('2026-11-26:商品目录只读京东平台类目,页面不再取授权类目树(/jd-shop-category-tree)', async () => {
it('点末级行=单名集合;树未上报(空树)时退化为末级平铺(上一窗行为不变)', async () => {
jdApi.fetchJdProductCategories.mockResolvedValue([
{ name: '收纳盒', productCount: 23 },
{ name: '收纳篮', productCount: 14 },
])
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 50 })
// 客户口径「京东分类是百分百有的,京东内分类就不需要」:左栏直接是 category_path
// 平铺,不再有按店铺上报的授权树、也不再有内置花屿巷快照/树外兜底分组。
// 默认 beforeEach 给的是空树:没有层级,一行一个末级名,点行=单名额。
const catalog = wrapper.get('.jd-side-catalog')
expect(catalog.find('.jd-side-arrow').exists()).toBe(false, '没有树就没有展开箭头')
expect(sideRowBy(catalog, '收纳盒').text()).toContain('23')
expect(catalog.text()).not.toContain('授权树外的类目')
expect(catalog.text()).not.toContain('本店无匹配')
await sideRowBy(catalog, '收纳盒').trigger('click')
await flushPromises()
const last = jdApi.fetchJdProductList.mock.calls.at(-1)![0]
expect(last).toMatchObject({ category: '收纳盒', page: 1 })
expect('categoryNames' in last, '不再有「页面按树展开的集合」这层:平铺行单名等值').toBe(false)
expect('categoryPath' in last, '也不再传类目路径').toBe(false)
expect(last.categoryNames).toEqual(['收纳盒'], '平铺行按单名集合下发(与父级同一参数面)')
wrapper.unmount()
})
})