Files
crawler-plugin/admin-frontend-vue/tests/mount/jd-product-card.mount.test.ts
T

164 lines
8.2 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.
/**
* 京东商品卡片(JdProductCard,v1.6 蓝图 Phase 3)的挂载契约。
*
* 纯展示组件(零 api 依赖):字段渲染与现表格「商品信息」列同源(价格走 jdPriceRange、
* 日期走 formatDateTime);卡片点击=开详情,勾选/复制 @click.stop 防冒泡——这两处
* 防冒泡是行为红线(点了复制却弹详情=误开弹窗),逐一钉死。
*
* 跑法:`npx vitest run tests/mount/jd-product-card.mount.test.ts`
*/
import { describe, expect, it } from 'vitest'
import { mount } from '@vue/test-utils'
import JdProductCard from '@/pages/jd/JdProductCard.vue'
import type { JdProductItem } from '@/pages/jd/jd-product-model.ts'
/** 行夹具:价格用 number 形状(2026-11-16 实录:BigDecimal 序列化成 JSON number,
* 字符串夹具会藏住「解析层丢数字形状」的真红);字段与页面 mount 测试同源。 */
function item(over: Partial<JdProductItem> = {}): JdProductItem {
return {
id: 1,
shopId: 3,
shopName: '旗舰自营店',
productCode: 'JD-10086',
skuId: 'SKU-1',
itemNum: 'ITEM-1',
skuCount: 12,
healthScore: 81,
title: '304不锈钢保温杯大容量便携',
brand: '锅宝',
categoryPath: '家居 > 厨具 > 保温杯',
priceMin: '12.90',
priceMax: '15.90',
stockTotal: 500,
saleStatus: '上架中',
listTab: 'search',
jdCreatedAt: '2026-11-07 18:49:47',
jdOnlineAt: '2026-11-12 15:03:37',
jdUpdatedAt: '2026-11-13 14:06:07',
mainImageUrl: 'https://img.example/main-1.jpg',
attrCount: 6,
imageCount: 9,
groupId: 7,
groupName: '甲组',
syncedAt: '2026-11-12 09:00:00',
...over,
} as JdProductItem
}
describe('卡片字段渲染(与现表格「商品信息」列同源)', () => {
it('主图/分类条/标题/价格区间/商品编码/采集日期逐字段上卡', () => {
const wrapper = mount(JdProductCard, { props: { item: item(), selected: false, selectable: true } })
const img = wrapper.get('img.jd-card-thumb')
expect(img.attributes('src')).toBe('https://img.example/main-1.jpg')
// 2026-11-21 用户口径「这边改成分类名称就可以」:图底条显归属分类名(groupName),
// 店铺名不再上卡(详情弹窗仍可看)。
expect(wrapper.get('.jd-card-category').text()).toBe('甲组')
expect(wrapper.find('.jd-card-shop').exists()).toBe(false, '店铺名条退役')
expect(wrapper.get('.jd-card-title').text()).toBe('304不锈钢保温杯大容量便携')
expect(wrapper.get('.jd-card-price').text()).toBe('12.9 ~ 15.9', '价格走 jdPriceRange(number/字符串都收、去尾零)')
expect(wrapper.get('.jd-card-code').text()).toContain('JD-10086')
expect(wrapper.get('.jd-card-date').text()).toContain('2026-11-12 09:00:00', '采集日期走 formatDateTime')
expect(wrapper.get('.jd-card-health').text()).toBe('信息分 81', 'healthScore 徽标随图渲染')
wrapper.unmount()
})
it('未归类商品分类条常驻出「未分类」( groupName 空不过滤条,与归属三档口径同词)', () => {
const wrapper = mount(JdProductCard, { props: { item: item({ groupName: '', groupId: null }) } })
expect(wrapper.get('.jd-card-category').text()).toBe('未分类')
wrapper.unmount()
})
it('无图占位:缺 mainImageUrl 出「无图」占位不出 img 元素;缺价与缺码按占位符兜底', () => {
const wrapper = mount(JdProductCard, {
props: { item: item({ mainImageUrl: '', priceMin: '', priceMax: '', productCode: '', syncedAt: '', healthScore: null }) },
})
expect(wrapper.find('img.jd-card-thumb').exists()).toBe(false)
expect(wrapper.get('.jd-card-thumb-empty').text()).toBe('无图')
expect(wrapper.get('.jd-card-price').text()).toBe('-')
expect(wrapper.get('.jd-card-code').text()).toContain('-')
expect(wrapper.get('.jd-card-date').text()).toContain('—', 'formatDateTime 空值回 em dash 兜底')
expect(wrapper.find('.jd-card-copy').exists()).toBe(false, '缺编码不渲染复制按钮(现表格同形)')
expect(wrapper.find('.jd-card-health').exists()).toBe(false, 'healthScore null 不渲染徽标')
wrapper.unmount()
})
})
describe('交互事件(防冒泡是行为红线)', () => {
it('点卡片整体 emit open(详情入口);点复制按钮只 emit copy 不冒泡开详情', async () => {
const wrapper = mount(JdProductCard, { props: { item: item() } })
await wrapper.get('.jd-card').trigger('click')
expect(wrapper.emitted('open')).toHaveLength(1)
await wrapper.get('.jd-card-copy').trigger('click')
expect(wrapper.emitted('copy')).toHaveLength(1, '复制按钮 emit copy(页面走剪贴板与回执)')
expect(wrapper.emitted('open')?.length).toBe(1, '@click.stop 防冒泡:复制不得误开详情')
wrapper.unmount()
})
it('可勾选档渲染勾选框:change emit toggle-select;点击勾选区不冒泡开详情', async () => {
const wrapper = mount(JdProductCard, { props: { item: item(), selected: false, selectable: true } })
const box = wrapper.get('input[type="checkbox"]')
await box.setValue(true)
expect(wrapper.emitted('toggle-select')).toEqual([[true]])
await box.setValue(false)
expect(wrapper.emitted('toggle-select')?.at(-1)).toEqual([false])
// 勾选区(label @click.stop)点击不触发卡片 open。
await wrapper.get('.jd-card-check').trigger('click')
expect(wrapper.emitted('open')).toBeFalsy()
wrapper.unmount()
})
it('不可勾选档不渲染勾选框(普通账号/纯只读行集);selected 高亮类由外部选中态控制', () => {
const plain = mount(JdProductCard, { props: { item: item() } })
expect(plain.find('input[type="checkbox"]').exists()).toBe(false)
expect(plain.get('.jd-card').classes()).not.toContain('selected')
plain.unmount()
const picked = mount(JdProductCard, { props: { item: item(), selected: true, selectable: true } })
expect(picked.get('.jd-card').classes()).toContain('selected')
expect(picked.get('input[type="checkbox"]').element.checked).toBe(true)
picked.unmount()
})
})
describe('v1.7 交互(点图切换勾选+hover 提示)', () => {
it('可勾选档点图片区=切换勾选且不冒泡开详情;未选发 true、已选发 false', async () => {
const wrapper = mount(JdProductCard, { props: { item: item(), selected: false, selectable: true } })
await wrapper.get('.jd-card-figure').trigger('click')
expect(wrapper.emitted('toggle-select')).toEqual([[true]], '未选点图=选中')
expect(wrapper.emitted('open'), '点图防冒泡:不得误开详情').toBeFalsy()
const picked = mount(JdProductCard, { props: { item: item(), selected: true, selectable: true } })
await picked.get('.jd-card-figure').trigger('click')
expect(picked.emitted('toggle-select')).toEqual([[false]], '已选点图=取消')
picked.unmount()
wrapper.unmount()
})
it('不可勾选档点图片区=冒泡到卡片整体开详情(点图不得吞掉详情入口)', async () => {
const wrapper = mount(JdProductCard, { props: { item: item() } })
await wrapper.get('.jd-card-figure').trigger('click')
expect(wrapper.emitted('open')).toHaveLength(1, '普通档点图照常开详情')
expect(wrapper.emitted('toggle-select')).toBeFalsy()
expect(wrapper.find('.jd-card-hover-tip').exists()).toBe(false, '不可勾选档不出提示胶囊')
wrapper.unmount()
})
it('可勾选档出 hover 提示胶囊,文案随选中态换(未选「点击选择」/已选「点击取消」)', () => {
const fresh = mount(JdProductCard, { props: { item: item(), selected: false, selectable: true } })
expect(fresh.get('.jd-card-hover-tip').text()).toBe('点击选择')
fresh.unmount()
const picked = mount(JdProductCard, { props: { item: item(), selected: true, selectable: true } })
expect(picked.get('.jd-card-hover-tip').text()).toBe('点击取消')
picked.unmount()
})
it('「已下载」角标随 CSV 导出功能整体下线(2026-11-21 用户拍板):downloaded prop 不存在,任何档都不出角标', () => {
const wrapper = mount(JdProductCard, { props: { item: item() } })
expect(wrapper.find('.jd-card-downloaded').exists()).toBe(false)
wrapper.unmount()
})
})