/** * 京东数据库页(v1.4 Phase 5)的挂载契约。 * * 形态:panel-box + 筛选行(店铺/商品编码/标题关键字/商品状态/分组/同步起止 date 对)+ * 查询/导出/重置/管理分组 + 自绘明细表(点行开详情抽屉)+ OldPagination; * 公司 tab 切换=清筛选重取(company-tab-wiring 同口径)。 * 不用 readFileSync(source-pin-ratchet 对台账外新文件带读点即红),全部走真实挂载断言。 * * 跑法:`npx vitest run tests/mount/jd-database-page.mount.test.ts` */ import { beforeEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' import { createPinia, setActivePinia, type Pinia } from 'pinia' import JdDatabasePage from '@/pages/jd/JdDatabasePage.vue' import JdProductGroupDialog from '@/pages/jd/JdProductGroupDialog.vue' import { useAdminSessionStore } from '@/stores/admin-session' import { useCompanyTabStore } from '@/stores/company-tab' const jdApi = vi.hoisted(() => ({ JD_PRODUCT_ENDPOINT: '/api/admin/jd-products', JD_PRODUCT_GROUP_ENDPOINT: '/api/admin/jd-product-groups', fetchJdProductList: vi.fn(), fetchJdProductDetail: vi.fn(), exportJdProductList: vi.fn(), fetchJdProductGroups: vi.fn(), createJdProductGroup: vi.fn(), updateJdProductGroup: vi.fn(), deleteJdProductGroup: vi.fn(), })) vi.mock('@/pages/jd/jd-product-api.ts', () => jdApi) function item(id: number, over: Record = {}) { return { id, shopId: 3, shopName: '旗舰自营店', productCode: `JD-100${id}`, skuId: `SKU-${id}`, title: '304不锈钢保温杯大容量便携', brand: '锅宝', categoryPath: '家居 > 厨具 > 保温杯', priceMin: '12.90', priceMax: '15.90', stockTotal: 500, saleStatus: '上架中', listTab: 'search', jdUpdatedAt: '2026-11-10 08:30:00', mainImageUrl: `https://img.example/main-${id}.jpg`, attrCount: 6, imageCount: 9, groupId: 7, groupName: '甲组', syncedAt: '2026-11-12 09:00:00', ...over, } } let pinia: Pinia beforeEach(() => { vi.clearAllMocks() pinia = createPinia() setActivePinia(pinia) useAdminSessionStore().user = { id: 1, username: 'root', role: 'super_admin' } useCompanyTabStore().setActiveId(1) jdApi.fetchJdProductGroups.mockResolvedValue([]) }) async function mountWith(payload: unknown): Promise> { jdApi.fetchJdProductList.mockImplementation(() => Promise.resolve(payload)) const wrapper = mount(JdDatabasePage, { global: { plugins: [pinia] } }) await flushPromises() await flushPromises() return wrapper } describe('京东数据库页(v1.4 Phase 5)', () => { it('挂载后按首屏分页参数取列表(pageSize 默认 20、带公司 tab),行数=本页条数', async () => { const wrapper = await mountWith({ items: [item(1), item(2), item(3)], total: 42, page: 1, pageSize: 20 }) expect(jdApi.fetchJdProductList).toHaveBeenCalledTimes(1) const firstCall = jdApi.fetchJdProductList.mock.calls[0][0] expect(firstCall.page).toBe(1) expect(firstCall.pageSize).toBe(20) expect(firstCall.companyId).toBe(1) expect(wrapper.findAll('tbody tr')).toHaveLength(3) expect(wrapper.find('.jd-summary').text()).toContain('共 42 条京东商品') }) it('行内容=主图/店铺/商品编码/标题/品牌/价格区间/库存/状态/分组/京东更新时间/同步时间(11 列)', async () => { const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 }) const cells = wrapper.findAll('tbody tr')[0].findAll('td') expect(cells).toHaveLength(11) expect(cells[1].text()).toBe('旗舰自营店') expect(cells[2].text()).toBe('JD-1001') expect(cells[3].text()).toContain('304不锈钢保温杯') expect(cells[4].text()).toBe('锅宝') expect(cells[5].text()).toBe('12.90 ~ 15.90', '价格区间=min ~ max') expect(cells[6].text()).toBe('500') expect(cells[7].text()).toBe('上架中') expect(cells[8].text()).toBe('甲组') expect(cells[9].text()).not.toBe('-', '京东更新时间格式化展示') expect(cells[10].text()).not.toBe('-', '同步时间格式化展示') expect(wrapper.find('.jd-thumb').exists()).toBe(true, '主图列渲染缩略图') }) it('分组读不到的那行展示「未分组」(不猜名字)', async () => { const wrapper = await mountWith({ items: [item(1, { groupId: null, groupName: '' })], total: 1, page: 1, pageSize: 20 }) expect(wrapper.findAll('tbody tr')[0].findAll('td')[8].text()).toBe('未分组') }) it('筛选生效:店铺/编码/关键字/状态/分组/起止日期,查询回第一页重发', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([{ id: 7, name: '甲组', sortOrder: 0, productCount: 3 }]) const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 20 }) const textInputs = wrapper.findAll('.form-group input[type="text"]') expect(textInputs).toHaveLength(4, '店铺+商品编码+标题关键字+商品状态四个文本筛选框') await textInputs[0].setValue('旗舰自营店') await textInputs[1].setValue('JD-1001') await textInputs[2].setValue('保温杯') await textInputs[3].setValue('上架中') await wrapper.find('select').setValue('7') const dateInputs = wrapper.findAll('input[type="date"]') await dateInputs[0].setValue('2026-11-01') await dateInputs[1].setValue('2026-11-09') await wrapper.findAll('button').find((b) => b.text() === '查询')!.trigger('click') await flushPromises() const last = jdApi.fetchJdProductList.mock.calls.at(-1)![0] expect(last).toMatchObject({ shopName: '旗舰自营店', productCode: 'JD-1001', titleKeyword: '保温杯', saleStatus: '上架中', groupId: '7', syncedFrom: '2026-11-01', syncedTo: '2026-11-09', page: 1, companyId: 1, }) }) it('分组下拉选中即查', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([ { id: 7, name: '甲组', sortOrder: 0, productCount: 3 }, { id: 8, name: '乙组', sortOrder: 1, productCount: 1 }, ]) const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 20 }) expect(jdApi.fetchJdProductGroups).toHaveBeenCalledWith(1) const groupSelect = wrapper.find('select') const optionTexts = groupSelect.findAll('option').map((o) => o.text()) expect(optionTexts).toContain('全部分组') expect(optionTexts).toContain('甲组') const callsBeforeSelect = jdApi.fetchJdProductList.mock.calls.length await groupSelect.setValue('7') await flushPromises() expect(jdApi.fetchJdProductList.mock.calls.length).toBe(callsBeforeSelect + 1, '选中分组必须立刻重查') expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ groupId: '7', page: 1 }) }) it('重置把筛选面回空并回第一页重取', async () => { const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 20 }) const textInputs = wrapper.findAll('.form-group input[type="text"]') await textInputs[0].setValue('旗舰自营店') await textInputs[2].setValue('保温杯') const callsBefore = jdApi.fetchJdProductList.mock.calls.length await wrapper.findAll('button').find((b) => b.text() === '重置')!.trigger('click') await flushPromises() expect(jdApi.fetchJdProductList.mock.calls.length).toBeGreaterThan(callsBefore) const last = jdApi.fetchJdProductList.mock.calls.at(-1)![0] expect(last).toMatchObject({ shopName: '', titleKeyword: '', page: 1 }, '重置后筛选全空、回第一页') }) it('翻页与改每页条数都重发请求,改条数回第一页', async () => { const wrapper = await mountWith({ items: [item(1)], total: 42, page: 2, pageSize: 20 }) await wrapper.findAll('button').find((b) => b.text() === '下一页')!.trigger('click') await flushPromises() expect(jdApi.fetchJdProductList).toHaveBeenCalledTimes(2) expect(jdApi.fetchJdProductList.mock.calls[1][0].page).toBe(3) const sizeSelect = wrapper.find('.page-size-select') await sizeSelect.setValue('50') await flushPromises() const last = jdApi.fetchJdProductList.mock.calls.at(-1)![0] expect(last.pageSize).toBe(50) expect(last.page).toBe(1, '改每页条数后回第一页') }) it('导出=当前过滤面全量(含公司 tab 与分组),成功反馈点名落盘文件', async () => { const createObjectURL = vi.fn(() => 'blob:mock-url') const revokeObjectURL = vi.fn() const urlRecord = URL as unknown as Record Object.defineProperty(URL, 'createObjectURL', { value: createObjectURL, configurable: true, writable: true }) Object.defineProperty(URL, 'revokeObjectURL', { value: revokeObjectURL, configurable: true, writable: true }) const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {}) jdApi.exportJdProductList.mockImplementation(() => Promise.resolve({ blob: new Blob(['xlsx']), filename: '京东数据库-20261114090000.xlsx' }), ) try { const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 }) const btn = wrapper.findAll('button').find((b) => b.text() === '导出') expect(btn, '筛选行必须有「导出」按钮').toBeTruthy() await btn!.trigger('click') await flushPromises() await flushPromises() expect(jdApi.exportJdProductList).toHaveBeenCalledTimes(1) const call = jdApi.exportJdProductList.mock.calls[0][0] expect(call).toMatchObject({ shopName: '', groupId: '', companyId: 1 }) expect(createObjectURL).toHaveBeenCalledTimes(1) expect(clickSpy).toHaveBeenCalled() expect(document.body.textContent).toContain('已导出', '成功反馈点名落盘文件') } finally { Object.defineProperty(URL, 'createObjectURL', { value: undefined, configurable: true, writable: true }) Object.defineProperty(URL, 'revokeObjectURL', { value: undefined, configurable: true, writable: true }) clickSpy.mockRestore() } }) it('导出失败把服务端业务文案弹出来,不许静默;在途标记复位', async () => { jdApi.exportJdProductList.mockImplementation(() => Promise.reject(new Error('京东商品共 80001 行,超过导出上限 50000 行,请缩小筛选范围后重试'))) const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 }) await wrapper.findAll('button').find((b) => b.text() === '导出')!.trigger('click') await flushPromises() await flushPromises() expect(document.body.textContent).toContain('超过导出上限') const btn = wrapper.findAll('button').find((b) => b.text() === '导出') expect(btn!.text()).toBe('导出', '失败后在途标记必须复位') expect((btn!.element as HTMLButtonElement).disabled).toBe(false) }) it('点行打开详情抽屉:按 (id, companyId) 取详情,attrGroups 分块渲染', async () => { jdApi.fetchJdProductDetail.mockImplementation(() => Promise.resolve({ item: item(1), attrGroups: [{ groupName: '规格', attrs: [{ key: 'capacity', label: '容量', value: '500ml' }] }], images: [{ imageType: 'MAIN', attrName: '', sortNo: 1, url: 'https://img.example/main-1.jpg', byteSize: null, width: 800, height: 800 }], attrJsonRaw: '', }), ) const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 }) expect(wrapper.find('.drawer-mask').exists()).toBe(false, '初始不显示抽屉') await wrapper.findAll('tbody tr')[0].trigger('click') await flushPromises() await flushPromises() expect(wrapper.find('.drawer-mask').exists()).toBe(true) expect(jdApi.fetchJdProductDetail).toHaveBeenCalledTimes(1) expect(jdApi.fetchJdProductDetail).toHaveBeenCalledWith(1, 1) expect(wrapper.find('.drawer-mask').text()).toContain('基本信息') expect(wrapper.find('.drawer-mask').text()).toContain('规格') expect(wrapper.find('.drawer-mask').text()).toContain('容量') expect(wrapper.find('.drawer-mask').text()).toContain('500ml') expect(wrapper.find('.drawer-mask').text()).toContain('主图') }) it('管理分组弹窗:打开即拉分组列表,变更后刷新分组下拉与列表', async () => { const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 }) const callsBefore = jdApi.fetchJdProductList.mock.calls.length await wrapper.findAll('button').find((b) => b.text() === '管理分组')!.trigger('click') await flushPromises() await flushPromises() expect(wrapper.find('.group-modal').exists()).toBe(true) // 打开弹窗 = 拉一次分组列表(第 2 次:首屏已拉过一次) expect(jdApi.fetchJdProductGroups).toHaveBeenCalledTimes(2) expect(jdApi.fetchJdProductGroups).toHaveBeenLastCalledWith(1) // 模拟分组管理里删了一个分组:emit changed → 页面刷新分组下拉与列表(回第一页) wrapper.findComponent(JdProductGroupDialog).vm.$emit('changed') await flushPromises() await flushPromises() expect(jdApi.fetchJdProductList.mock.calls.length).toBeGreaterThan(callsBefore) expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ page: 1, companyId: 1 }) }) it('切公司 Tab 重取并清掉上一家的筛选(company-tab-wiring 同口径)', async () => { jdApi.fetchJdProductGroups.mockResolvedValue([{ id: 7, name: '甲组', sortOrder: 0, productCount: 3 }]) const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 }) const textInputs = wrapper.findAll('.form-group input[type="text"]') await textInputs[0].setValue('旗舰自营店') await textInputs[2].setValue('保温杯') await wrapper.find('select').setValue('7') const callsBefore = jdApi.fetchJdProductList.mock.calls.length useCompanyTabStore().setActiveId(2) await flushPromises() await flushPromises() expect(jdApi.fetchJdProductList.mock.calls.length).toBeGreaterThan(callsBefore) const last = jdApi.fetchJdProductList.mock.calls.at(-1)![0] expect(last.companyId).toBe(2) expect(last.shopName).toBe('', '切公司必须清掉上一家的店铺名') expect(last.titleKeyword).toBe('', '切公司必须清掉上一家的标题关键字') expect(last.groupId).toBe('', '切公司必须清掉上一家选的分组') expect(last.page).toBe(1) expect(jdApi.fetchJdProductGroups).toHaveBeenLastCalledWith(2) }) it('空态与加载失败条(失败给重试,不把整页打死)', async () => { const empty = await mountWith({ items: [], total: 0, page: 1, pageSize: 20 }) const tip = empty.find('.empty-tip') expect(tip.exists()).toBe(true) expect(tip.text()).toContain('暂无京东商品数据') empty.unmount() jdApi.fetchJdProductList.mockRejectedValue(new Error('上游 502')) const failed = mount(JdDatabasePage, { global: { plugins: [pinia] } }) await flushPromises() await flushPromises() expect(failed.find('.load-failure-bar').exists()).toBe(true) expect(failed.find('.load-failure-bar').text()).toContain('京东商品列表加载失败') }) })