Files
crawler-plugin/admin-frontend-vue/tests/mount/jd-category-sidebar.mount.test.ts
T

404 lines
21 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 京东数据库侧栏(JdCategorySidebar,v1.6 蓝图 Phase 2)的挂载契约。
*
* 纯展示组件:取数与失败兜底全在页面(props 流入、emit 流出),本文件零 api mock——
* 钉的是「徽标数=点节点后列表命中的行数」(jdCategoryCountTree 同源)、双 Tab 切换、
* 点节点 emit 形状(树内带路径/树外兜底 path 空/全部商品 null)、可写档分档入口。
*
* 跑法:`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<Element> {
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<Element>, text: string) {
const row = scope.findAll('.jd-side-row').find((node) => node.text().includes(text))
expect(row, `应存在文案含「${text}」的侧栏行`).toBeTruthy()
return row!
}
beforeEach(() => {
// 纯展示组件无副作用;占位保持与页面 mount 测试同构。
})
describe('商品目录 Tab(京麦授权类目树+计数徽标)', () => {
it('出厂停在商品目录 Tab;徽标与 Phase 1 聚合同源(根=子树去重和、全部商品=计数行总和)', () => {
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(rowByText(catalog, '服饰内衣').text()).toContain('20',
'服饰内衣根徽标=子树去重和(连衣裙7+短袖4+短裤9,短裤跨支同名只记一次)')
expect(rowByText(catalog, '母婴').text()).toContain('0', '无词条一级归 0 不缺节点')
expect(rowByText(catalog, '运动户外').text()).toContain('5')
wrapper.unmount()
})
it('一级默认展开(二级首屏可见);逐级展开后末级行点行 emit {name, path}', async () => {
const wrapper = mountSidebar()
const catalog = wrapper.get('.jd-side-catalog')
const women = rowByText(catalog, '女装')
expect(women.classes()).toContain('depth-1', '一级默认展开 ⇒ 二级行(depth-1 缩进)首屏可见')
// 末级艺考形体服在 depth-2:其父(舞蹈体操鞋服/装备)未展开时不可见。
expect(catalog.findAll('.jd-side-row').some((node) => node.text().includes('艺考形体服'))).toBe(false)
await catalog.get('[aria-label="展开或收起 舞蹈体操鞋服/装备"]').trigger('click')
await flushPromises()
const yikao = rowByText(catalog, '艺考形体服')
expect(yikao.classes()).toContain('depth-2', '艺考形体服是 level 3(depth 2 缩进)')
await yikao.trigger('click')
await flushPromises()
expect(wrapper.emitted('select-category')).toEqual([[{ name: '艺考形体服', path: ['运动户外', '舞蹈体操鞋服/装备', '艺考形体服'] }]])
wrapper.unmount()
})
it('点二级节点 emit 完整路径(页面按子树展开 IN);箭头只折展不选中', async () => {
const wrapper = mountSidebar()
const catalog = wrapper.get('.jd-side-catalog')
// 女装行未展开时,其子行(连衣裙)不可见。
expect(catalog.findAll('.jd-side-row').some((node) => node.text().includes('连衣裙'))).toBe(false)
const women = rowByText(catalog, '女装')
await women.trigger('click')
await flushPromises()
expect(wrapper.emitted('select-category')).toEqual([[{ name: '女装', path: ['服饰内衣', '女装'] }]],
'点行=选中即筛,emit 完整名称路径')
// 点行顺手展开(下钻感):子行现在可见。
expect(wrapper.emitted('select-category')).toHaveLength(1)
expect(catalog.findAll('.jd-side-row').some((node) => node.text().includes('连衣裙'))).toBe(true,
'非末级行点行后展开子级')
// 箭头(role=button, aria-label)只折展:点它不得触发 select-category。
const before = wrapper.emitted('select-category')?.length ?? 0
await catalog.get('[aria-label="展开或收起 女装"]').trigger('click')
await flushPromises()
expect(wrapper.emitted('select-category')?.length).toBe(before, '箭头点击不选中(click.stop 防冒泡)')
expect(catalog.findAll('.jd-side-row').some((node) => node.text().includes('连衣裙'))).toBe(false,
'再点箭头=收起(子行隐藏)')
wrapper.unmount()
})
it('「全部商品」行 emit null(页面清类目筛选重查);挂载即带选中态时祖先自动展开并高亮', async () => {
const wrapper = mountSidebar({ selectedCategory: { name: '连衣裙', path: ['服饰内衣', '女装', '连衣裙'] } })
const catalog = wrapper.get('.jd-side-catalog')
const dress = rowByText(catalog, '连衣裙')
expect(dress.classes()).toContain('selected', '外部选中态回显高亮')
expect(dress.classes()).toContain('depth-2', '挂载即带选中 ⇒ watch immediate 展开祖先(女装已展开,行可见)')
await wrapper.setProps({ selectedCategory: null })
expect(catalog.get('.jd-side-all').classes()).toContain('selected', 'null=全部商品高亮')
await catalog.get('.jd-side-all').trigger('click')
await flushPromises()
expect(wrapper.emitted('select-category')).toEqual([[null]], '点全部商品=清选')
wrapper.unmount()
})
})
describe('树外兜底组(树上对不上的历史行,单名等值出口)', () => {
it('兜底组头渲染计数和=extraTotal;行点击 emit {name, path:[]}(页面按单名等值回落)', async () => {
const wrapper = mountSidebar()
const catalog = wrapper.get('.jd-side-catalog')
const head = catalog.get('.jd-side-extra-head')
expect(head.text()).toContain('树外兜底')
expect(head.text()).toContain('12', '兜底组头徽标=extraTotal(历史行求和)')
const extra = rowByText(catalog, '已下架历史类目')
await extra.trigger('click')
await flushPromises()
expect(wrapper.emitted('select-category')).toEqual([[{ name: '已下架历史类目', path: [] }]],
'树外名 path 空数组=无路径序列化(单名等值旧形状,不静默丢筛选)')
wrapper.unmount()
})
it('全部是树内名时兜底组整体不渲染(不占一条空组)', () => {
const wrapper = mount(JdCategorySidebar, {
props: {
categoryOptions: [{ name: '连衣裙', productCount: 7 }],
categories: CATEGORIES,
editable: true,
selectedCategory: null,
selectedGroupId: null,
},
})
expect(wrapper.find('.jd-side-extra').exists()).toBe(false)
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-empty').text()).toBe('本组暂无分类,先新建一个')
writable.unmount()
const readonly = mount(JdCategorySidebar, {
props: { categoryOptions: [], categories: [], editable: false, selectedCategory: null, selectedGroupId: null },
})
expect(readonly.get('.jd-side-empty').text()).toBe('本组暂无分类')
readonly.unmount()
})
})
/** 店铺名单(v1.7 F6 完整对齐原型《商品数据商城》):分类行展开箭头→店铺行(点选过滤/
* 行内改名移除)+「+ 添加店铺」入口+分类行 hover 改名/删除。侧栏纯展示:全部只 emit,
* API 调用与刷新在页面。 */
describe('店铺名单(v1.7:分类下挂店铺名单)', () => {
async function openCustomTab(wrapper: VueWrapper<Element>): Promise<void> {
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.get('[aria-label="展开或收起 裙装 的店铺名单"]').exists()).toBe(true, '有名单出箭头')
expect(custom.find('[aria-label="展开或收起 裤装 的店铺名单"]').exists()).toBe(false, '无名单不出箭头')
expect(custom.findAll('.jd-side-shop')).toHaveLength(0, '出厂收起(店铺行不可见)')
await custom.get('[aria-label="展开或收起 裙装 的店铺名单"]').trigger('click')
await flushPromises()
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('+ 添加店铺', '可写档名单尾出添加入口')
await custom.get('[aria-label="展开或收起 裙装 的店铺名单"]').trigger('click')
await flushPromises()
expect(wrapper.get('.jd-side-custom').findAll('.jd-side-shop')).toHaveLength(0, '再点箭头=收起')
wrapper.unmount()
})
it('点店铺行 emit {groupId, shopName} 成对下发;选中回流高亮;再点已选 emit null(只清店铺保留分类)', async () => {
const wrapper = mountSidebar({ categories: CATEGORIES_WITH_SHOPS, selectedGroupId: 7 })
await openCustomTab(wrapper)
await wrapper.get('[aria-label="展开或收起 裙装 的店铺名单"]').trigger('click')
await flushPromises()
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)
expect(wrapper.get('.jd-side-custom').findAll('.jd-side-shop')).toHaveLength(2,
'watch immediate 展开宿主分类')
const selected = wrapper.get('.jd-side-custom').findAll('.jd-side-shop').find((row) => row.text().includes('花屿巷旗舰店'))
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 操作钮在展开后可见。
await custom.get('[aria-label="展开或收起 裙装 的店铺名单"]').trigger('click')
await flushPromises()
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()
// 店铺改名:✎ → 预填 → 改名 → ✓ 提交。
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, '分类行无删除钮')
await custom.get('[aria-label="展开或收起 裙装 的店铺名单"]').trigger('click')
await flushPromises()
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)
await wrapper.get('[aria-label="展开或收起 裙装 的店铺名单"]').trigger('click')
await flushPromises()
expect(rowByText(wrapper, '裙装').classes()).toContain('selected', '分类行选中(含选店铺时)')
await rowByText(wrapper, '裙装').trigger('click')
await flushPromises()
expect(wrapper.emitted('select-group')).toEqual([[null]], '再点已选分类=清选(页面同清店铺筛选)')
wrapper.unmount()
})
})