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

115 lines
5.5 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')
expect(wrapper.get('.jd-card-shop').text()).toBe('旗舰自营店')
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('无图占位:缺 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()
})
})