/** * 京东商品详情居中弹窗(JdProductDetailModal,v1.6 蓝图 Phase 4)的挂载契约。 * * 弹窗壳只管 chrome(遮罩/标题/「编辑/删除」动作分档/关闭),内容与取数在共享块 * JdProductDetailContent(与侧滑抽屉同一份实现,抽屉薄壳化行为由页面 mount 测试兜住)。 * 挂载时序与真实页面一致:先 visible=false 挂载,再置 true(Content 的 watch 靠变更触发取数)。 * * 跑法:`npx vitest run tests/mount/jd-product-detail-modal.mount.test.ts` */ import { beforeEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' import JdProductDetailModal from '@/pages/jd/JdProductDetailModal.vue' import type { JdProductItem } from '@/pages/jd/jd-product-model.ts' const jdApi = vi.hoisted(() => ({ fetchJdProductDetail: vi.fn(), })) vi.mock('@/pages/jd/jd-product-api.ts', () => jdApi) function item(): JdProductItem { return { id: 1, shopId: 3, shopName: '旗舰自营店', productCode: 'JD-10086', skuId: 'SKU-1', itemNum: 'ITEM-1', skuCount: 1, 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: 2, groupId: 7, groupName: '甲组', syncedAt: '2026-11-12 09:00:00', } as JdProductItem } /** fetchJdProductDetail 的返回(已由 parseJdProductDetail 归一的形状,与页面 mount 测试同构)。 */ function detailPayload() { return { item: item(), attrGroups: [ { groupName: '规格', attrs: [{ key: 'capacity', label: '容量', value: '500ml' }] }, ], images: [ { id: 1, imageType: 'MAIN', attrName: '', sortNo: 1, url: 'https://img.example/main.jpg', byteSize: null, width: 800, height: 800 }, { id: 2, imageType: 'DETAIL', attrName: '', sortNo: 2, url: 'https://img.example/detail.jpg', byteSize: null, width: null, height: null }, ], skus: [ { skuId: 'S1', skuName: '上衣 S', skuSeq: '1', price: '162', stock: 200, saleAttrs: [{ name: '颜色', value: '上衣' }, { name: '尺码', value: 'S' }], sortNo: 0 }, ], attrJsonRaw: '', } } beforeEach(() => { vi.clearAllMocks() }) /** 与真实页面同构的挂载:先 visible=false 挂载,再置 true 触发 Content 取数。 */ async function mountOpened(canWrite = true): Promise> { jdApi.fetchJdProductDetail.mockResolvedValue(detailPayload()) const wrapper = mount(JdProductDetailModal, { props: { item: item(), visible: false, canWrite }, }) await wrapper.setProps({ visible: true }) await flushPromises() await flushPromises() return wrapper } describe('居中弹窗 chrome(设计稿款式:居中大弹窗+顶部动作)', () => { it('打开即取数(按 item.id)并渲染共享块内容:hero 左图右信息/属性分组/SKU 矩阵/图片三区', async () => { const wrapper = await mountOpened() expect(jdApi.fetchJdProductDetail).toHaveBeenCalledWith(1) expect(wrapper.get('.jd-modal-mask').exists()).toBe(true) expect(wrapper.get('.jd-modal-head h3').text()).toBe('商品详情 · JD-10086') // 共享块(与侧滑抽屉同一份实现)——类名与页面 mount 测试钉住的抽屉断言同字。 expect(wrapper.get('.detail-hero').exists()).toBe(true) expect(wrapper.get('.hero-main').exists()).toBe(true) expect(wrapper.get('.hero-info').text()).toContain('价格区间') expect(wrapper.get('.hero-info').text()).toContain('12.9 ~ 15.9') expect(wrapper.get('.jd-modal').text()).toContain('规格') expect(wrapper.get('.jd-modal').text()).toContain('容量') expect(wrapper.get('.jd-modal').text()).toContain('销售属性 / SKU 矩阵') expect(wrapper.get('.jd-modal').text()).toContain('1 个 SKU · ¥162 · 库存 200') expect(wrapper.get('.jd-modal').text()).toContain('主图') expect(wrapper.get('.jd-modal').text()).toContain('详情图') wrapper.unmount() }) it('关闭:点遮罩自身与右上 × 都 emit close;关闭后 mask 摘掉', async () => { const wrapper = await mountOpened() await wrapper.get('.jd-modal-mask').trigger('click') expect(wrapper.emitted('close')).toHaveLength(1, 'mask @click.self(点内容区不算)') await wrapper.get('.jd-modal-close').trigger('click') expect(wrapper.emitted('close')).toHaveLength(2) await wrapper.setProps({ visible: false }) await flushPromises() expect(wrapper.find('.jd-modal-mask').exists()).toBe(false) wrapper.unmount() }) }) describe('顶部动作按写权限档分档(emit 给页面接既有链)', () => { it('可写档渲染「编辑/删除」:点击分别 emit edit/remove 且不冒泡成 close', async () => { const wrapper = await mountOpened(true) const actions = wrapper.get('.jd-modal-actions') const buttons = actions.findAll('button') expect(buttons.map((b) => b.text())).toEqual(['编辑', '删除']) await buttons[0]!.trigger('click') expect(wrapper.emitted('edit')).toHaveLength(1) expect(wrapper.emitted('close')).toBeFalsy() await buttons[1]!.trigger('click') expect(wrapper.emitted('remove')).toHaveLength(1) expect(wrapper.emitted('close')).toBeFalsy() wrapper.unmount() }) it('只读档(canWrite=false):无动作区(普通账号只读,与现表格操作列分档同口径)', async () => { const wrapper = await mountOpened(false) expect(wrapper.find('.jd-modal-actions').exists()).toBe(false) expect(wrapper.findAll('button').map((b) => b.text())).not.toContain('编辑') expect(wrapper.findAll('button').map((b) => b.text())).not.toContain('删除') wrapper.unmount() }) }) describe('加载失败与重试(取数在共享块,两条壳同一份兜底)', () => { it('失败红条可见且重试真能重跑', async () => { jdApi.fetchJdProductDetail.mockRejectedValueOnce(new Error('上游 502')) jdApi.fetchJdProductDetail.mockResolvedValue(detailPayload()) const wrapper = mount(JdProductDetailModal, { props: { item: item(), visible: false, canWrite: true } }) await wrapper.setProps({ visible: true }) await flushPromises() await flushPromises() const bar = wrapper.get('.drawer-failure') expect(bar.text()).toContain('上游 502', 'requestErrorMessage 透传后端/网络原因(与旧抽屉同一出口)') expect(bar.find('button').text()).toBe('重试') await bar.find('button').trigger('click') await flushPromises() await flushPromises() expect(wrapper.find('.drawer-failure').exists()).toBe(false, '重试成功后红条消失') expect(wrapper.get('.detail-hero').exists()).toBe(true) wrapper.unmount() }) })