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:
@@ -0,0 +1,182 @@
|
||||
/**
|
||||
* 商品目录层级纯函数契约(2026-11-26 客户口径「怎么都没有分类一级二级分类」「管理员要
|
||||
* 分类时候那么多怎么看」)——node --test 直跑(纯函数零依赖)。
|
||||
*
|
||||
* 覆盖:
|
||||
* 1. `parseJdCategoryNodeTree` 脏值归一(空名节点丢自身保子级、value 缺失空串、超深截断);
|
||||
* 2. `buildJdCatalogTree` 三个口径:计数按**去重名字集合**求和(同名跨支不重复计)、
|
||||
* 零计数分支剪枝(但保底「树外兜底」)、树空退化为末级平铺(上一窗行为逐字不变);
|
||||
* 3. `flattenJdCatalogRows` 折叠语义(折叠节点的子孙不出现);
|
||||
* 4. `findJdCatalogPath` / `jdCatalogPathIndex` / `findJdCatalogPathByKey` 的定位口径
|
||||
* (点行下发 path、挂载展开祖先链都靠它)。
|
||||
*/
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import {
|
||||
buildJdCatalogTree,
|
||||
findJdCatalogPath,
|
||||
findJdCatalogPathByKey,
|
||||
flattenJdCatalogRows,
|
||||
jdCatalogPathIndex,
|
||||
parseJdCategoryNodeTree,
|
||||
type JdCategoryNode,
|
||||
} from '../src/pages/jd/jd-category-hierarchy.ts'
|
||||
|
||||
/** 本店真机形状的缩影(生产实测:商品末级名 100% 精确命中树节点名)。 */
|
||||
const TREE: JdCategoryNode[] = [
|
||||
{
|
||||
name: '厨房卫浴',
|
||||
value: '1',
|
||||
children: [
|
||||
{
|
||||
name: '厨卫挂件',
|
||||
value: '11',
|
||||
children: [
|
||||
{ name: '其他收纳架', value: '111' },
|
||||
{ name: '牙刷架/牙具座', value: '112' },
|
||||
],
|
||||
},
|
||||
{ name: '沐浴桶', value: '12', children: [{ name: '泡脚桶/袋/盆', value: '121' }] },
|
||||
],
|
||||
},
|
||||
{
|
||||
name: '家居饰品',
|
||||
value: '2',
|
||||
children: [
|
||||
{ name: '摆件', value: '21', children: [{ name: '桌面摆件', value: '211' }] },
|
||||
],
|
||||
},
|
||||
// 零计数整支:授权树里本店可卖、但这批采集没采到的类目。
|
||||
{
|
||||
name: '服饰内衣',
|
||||
value: '3',
|
||||
children: [{ name: '女装', value: '31', children: [{ name: '连衣裙', value: '311' }] }],
|
||||
},
|
||||
]
|
||||
|
||||
test('parseJdCategoryNodeTree:空名节点丢自身保子级、value 缺失按空串、超深截断', () => {
|
||||
const parsed = parseJdCategoryNodeTree([
|
||||
{ name: ' ', value: '9', children: [{ name: '子级', value: '91' }] },
|
||||
{ value: '8', children: [{ name: '孤儿', value: '81' }] },
|
||||
{ name: '有值', value: 7 },
|
||||
'not-an-object',
|
||||
])
|
||||
assert.deepEqual(parsed.map((node) => node.name), ['子级', '孤儿', '有值'])
|
||||
assert.equal(parsed[2].value, '7', '数字 value 归一成字符串')
|
||||
assert.equal(parseJdCategoryNodeTree([{ name: 'x' }])[0].value, '', 'value 缺失按空串')
|
||||
assert.deepEqual(parseJdCategoryNodeTree('bad'), [], '非数组回空')
|
||||
|
||||
// 深度护栏:9 层嵌套只保留前 9 层(depth > 8 起截断),不抛栈。
|
||||
let deep: unknown = { name: 'L9', value: '9' }
|
||||
for (let level = 8; level >= 1; level -= 1) deep = { name: `L${level}`, value: `${level}`, children: [deep] }
|
||||
let node: JdCategoryNode | undefined = parseJdCategoryNodeTree([deep])[0]
|
||||
let depth = 0
|
||||
while (node) {
|
||||
depth += 1
|
||||
node = node.children?.[0]
|
||||
}
|
||||
assert.ok(depth >= 1 && depth <= 9, `深度应被截断在 9 层内,实际 ${depth}`)
|
||||
})
|
||||
|
||||
test('buildJdCatalogTree:父级徽标=子树命中行数,末级=自身名行数,总数=全部行求和', () => {
|
||||
const catalog = buildJdCatalogTree(TREE, [
|
||||
{ name: '其他收纳架', productCount: 17 },
|
||||
{ name: '牙刷架/牙具座', productCount: 2 },
|
||||
{ name: '泡脚桶/袋/盆', productCount: 32 },
|
||||
{ name: '桌面摆件', productCount: 9 },
|
||||
])
|
||||
const kitchen = catalog.roots.find((row) => row.name === '厨房卫浴')!
|
||||
assert.equal(kitchen.productCount, 51, '一级=51(17+2+32)')
|
||||
assert.equal(kitchen.isLeaf, false)
|
||||
assert.deepEqual(kitchen.names.slice().sort(),
|
||||
['厨房卫浴', '厨卫挂件', '其他收纳架', '牙刷架/牙具座', '沐浴桶', '泡脚桶/袋/盆'].sort(),
|
||||
'父级点选下发的名字集合=整棵子树的节点名(主体是末级,父级名带上兜底)')
|
||||
const hook = kitchen.children.find((row) => row.name === '厨卫挂件')!
|
||||
assert.equal(hook.productCount, 19, '二级=19(17+2)')
|
||||
assert.equal(hook.depth, 1)
|
||||
const leaf = hook.children.find((row) => row.name === '其他收纳架')!
|
||||
assert.equal(leaf.productCount, 17)
|
||||
assert.equal(leaf.isLeaf, true, '无子级=末级(单名过滤)')
|
||||
assert.equal(leaf.depth, 2)
|
||||
assert.deepEqual(leaf.names, ['其他收纳架'])
|
||||
assert.equal(catalog.totalCount, 60, '「全部商品」=全部计数行求和')
|
||||
assert.deepEqual(catalog.orphans, [], '全部命中树,无兜底行')
|
||||
})
|
||||
|
||||
test('buildJdCatalogTree:零计数分支剪掉(授权树≠有货树),整树都被剪时回空根', () => {
|
||||
const catalog = buildJdCatalogTree(TREE, [{ name: '桌面摆件', productCount: 9 }])
|
||||
assert.deepEqual(catalog.roots.map((row) => row.name), ['家居饰品'],
|
||||
'「厨房卫浴」「服饰内衣」两支全 0 ⇒ 不出现(否则四百多节点铺满侧栏,反而看不清)')
|
||||
assert.equal(catalog.roots[0].productCount, 9)
|
||||
assert.deepEqual(buildJdCatalogTree(TREE, []).roots, [], '一行都没有 ⇒ 空根(侧栏走空态)')
|
||||
})
|
||||
|
||||
test('buildJdCatalogTree:同名跨支按去重名字集合求和,父级徽标不重复计', () => {
|
||||
const tree: JdCategoryNode[] = [
|
||||
{ name: '内衣', value: 'a', children: [{ name: '吊带/背心', value: 'a1' }] },
|
||||
{ name: '女装', value: 'b', children: [{ name: '吊带/背心', value: 'b1' }] },
|
||||
]
|
||||
const catalog = buildJdCatalogTree(tree, [{ name: '吊带/背心', productCount: 5 }])
|
||||
assert.deepEqual(catalog.roots.map((row) => row.productCount), [5, 5],
|
||||
'两个一级各自 5(同一批行按名字命中两支),不是各 2.5 也不是 10')
|
||||
assert.equal(catalog.totalCount, 5, '「全部商品」只算一次')
|
||||
})
|
||||
|
||||
test('buildJdCatalogTree:树外的历史末级名进兜底组(不藏、按行数降序)', () => {
|
||||
const catalog = buildJdCatalogTree(TREE, [
|
||||
{ name: '其他收纳架', productCount: 17 },
|
||||
{ name: '已下架历史类目', productCount: 12 },
|
||||
{ name: '另一个历史类目', productCount: 3 },
|
||||
])
|
||||
assert.deepEqual(catalog.orphans.map((row) => row.name), ['已下架历史类目', '另一个历史类目'],
|
||||
'树里搜不到的名字单列一组,按行数降序——藏了这些商品就点不到')
|
||||
assert.equal(catalog.totalCount, 32, '兜底行的行数照样进「全部商品」')
|
||||
})
|
||||
|
||||
test('buildJdCatalogTree:树为空 ⇒ 退化为末级平铺(上一窗行为逐字不变)', () => {
|
||||
const catalog = buildJdCatalogTree([], [
|
||||
{ name: '连衣裙', productCount: 7 },
|
||||
{ name: ' ', productCount: 4 },
|
||||
{ name: '短裤', productCount: -1 },
|
||||
])
|
||||
assert.deepEqual(catalog.roots.map((row) => [row.name, row.productCount, row.depth, row.isLeaf]), [
|
||||
['连衣裙', 7, 0, true],
|
||||
], '空名/非正计数不进列表;平铺行 depth 0、按末级处理')
|
||||
assert.deepEqual(catalog.orphans, [], '没有树就没有「树外」这个概念')
|
||||
assert.equal(catalog.totalCount, 7)
|
||||
})
|
||||
|
||||
test('flattenJdCatalogRows:折叠节点的子孙不进列表;展开哪个只影响那个分支', () => {
|
||||
const catalog = buildJdCatalogTree(TREE, [
|
||||
{ name: '其他收纳架', productCount: 17 },
|
||||
{ name: '桌面摆件', productCount: 9 },
|
||||
])
|
||||
assert.deepEqual(flattenJdCatalogRows(catalog.roots, []).map((row) => row.name),
|
||||
['厨房卫浴', '家居饰品'], '默认全收起:先看清有哪些大类')
|
||||
|
||||
const kitchen = catalog.roots.find((row) => row.name === '厨房卫浴')!
|
||||
const opened = flattenJdCatalogRows(catalog.roots, [kitchen.key])
|
||||
assert.deepEqual(opened.map((row) => row.name), ['厨房卫浴', '厨卫挂件', '家居饰品'],
|
||||
'只展开一级 ⇒ 出它的二级,别的分支不动')
|
||||
|
||||
const fully = flattenJdCatalogRows(catalog.roots, [kitchen.key, '11'])
|
||||
assert.deepEqual(fully.map((row) => row.name), ['厨房卫浴', '厨卫挂件', '其他收纳架', '家居饰品'])
|
||||
})
|
||||
|
||||
test('路径定位:点行下发一级到本行的名称链;挂载按选中行展开祖先', () => {
|
||||
const catalog = buildJdCatalogTree(TREE, [
|
||||
{ name: '其他收纳架', productCount: 17 },
|
||||
{ name: '桌面摆件', productCount: 9 },
|
||||
])
|
||||
const leaf = findJdCatalogPathByKey(catalog.roots, '111')
|
||||
assert.deepEqual(leaf.map((row) => row.name), ['厨房卫浴', '厨卫挂件', '其他收纳架'])
|
||||
assert.deepEqual(findJdCatalogPath(catalog.roots, '桌面摆件').map((row) => row.name),
|
||||
['家居饰品', '摆件', '桌面摆件'])
|
||||
assert.deepEqual(findJdCatalogPath(catalog.roots, '不存在的类目'), [],
|
||||
'选中名不在树里(树外兜底行)⇒ 空链,侧栏不展开任何分支')
|
||||
assert.deepEqual(findJdCatalogPathByKey(catalog.roots, ''), [])
|
||||
|
||||
const index = jdCatalogPathIndex(catalog.roots)
|
||||
assert.deepEqual(index.get('21'), ['家居饰品', '摆件'], '索引供点行时算 path')
|
||||
})
|
||||
@@ -97,25 +97,37 @@ test('列表行价格:BigDecimal 的 JSON number 形状与字符串形状都
|
||||
assert.equal(jdPriceRange(missing), '', '两侧都空回空串(列表显示 -)')
|
||||
})
|
||||
|
||||
/** 类目筛选(2026-11-26 客户口径「京东分类是百分百有的,京东内分类就不需要」):
|
||||
* 左栏商品目录=京东平台类目(`category_path` DISTINCT 末级名)平铺,点行=单名等值;
|
||||
* 空串不下发。旧「父级点选→按授权树/内置快照展开子树集合」那一层随之退役。 */
|
||||
test('类目筛选参数序列化:空串不下发、选中末级类目=单元素集合(等值)', async () => {
|
||||
/** 类目筛选(2026-11-26 客户口径「怎么都没有分类一级二级分类」「管理员要分类时候那么多
|
||||
* 怎么看」):左栏商品目录=京麦授权类目**层级树**+`category_path` 末级名计数。
|
||||
* 末级行=单名等值;一级/二级行=整棵子树的节点名集合(后端 `category_path IN (...)` 多值)。
|
||||
* 展开集合由侧栏在选中那一刻算好(`names`),筛选层只负责序列化,不持有树。 */
|
||||
test('类目筛选参数序列化:末级单名/父级集合/空串不下发', async () => {
|
||||
const { createJdProductFilterState, toJdProductListParams, jdProductFilterActive } =
|
||||
await import('../src/pages/jd/jd-product-filter.ts')
|
||||
const empty = createJdProductFilterState()
|
||||
assert.equal('category' in toJdProductListParams(empty, 1, 20), false, '全部类目不下发 category')
|
||||
assert.equal(jdProductFilterActive(empty), false)
|
||||
|
||||
const leaf = { ...empty, category: ' 艺考形体服 ' }
|
||||
const leaf = { ...empty, category: ' 艺考形体服 ', categoryNames: ['艺考形体服'] }
|
||||
assert.deepEqual(toJdProductListParams(leaf, 1, 20).category, ['艺考形体服'],
|
||||
'末级类目名 trim 后单元素集合(后端 IN 单值等价 eq)')
|
||||
'末级类目:单元素集合(后端 IN 单值等价 eq)')
|
||||
assert.equal(jdProductFilterActive(leaf), true, '选中类目算激活筛选')
|
||||
|
||||
// 历史「父级路径展开」字段已随内置快照树退役:带上也不影响序列化结果(不会凭空多出集合)。
|
||||
const legacy = { ...empty, category: '女装', categoryPath: ['服饰内衣', '女装'] }
|
||||
assert.deepEqual(toJdProductListParams(legacy, 1, 20).category, ['女装'],
|
||||
'没有子树展开集合这一层:父级路径就算传进来也只按单名等值下发')
|
||||
const parent = {
|
||||
...empty,
|
||||
category: '服饰内衣',
|
||||
categoryPath: ['服饰内衣'],
|
||||
categoryNames: ['服饰内衣', '女装', '连衣裙', '连衣裙 '],
|
||||
}
|
||||
assert.deepEqual(toJdProductListParams(parent, 1, 20).category,
|
||||
['服饰内衣', '女装', '连衣裙'],
|
||||
'父级点选:整棵子树的节点名集合(trim+去重),后端按多值 IN 命中')
|
||||
assert.deepEqual(toJdProductListParams(parent, 1, 20).page, 1, '类目筛选不动分页')
|
||||
|
||||
// 只有节点名、没有展开集合(树未就绪 / 树外兜底行 / 老调用点):退化为单名等值,不丢筛选。
|
||||
const single = { ...empty, category: ' 女装 ' }
|
||||
assert.deepEqual(toJdProductListParams(single, 1, 20).category, ['女装'],
|
||||
'没有展开集合时按节点名单名下发')
|
||||
})
|
||||
|
||||
test('自定义分类使用 group_id,不发送分类状态参数', async () => {
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user