/** * 品牌Gating库页面挂载测试:渲染(统计卡 + 行)与两次操作载荷断言(新增、恢复生效)。 * 跑法:`npm run test:mount`(vitest + jsdom,配置见 vitest.config.ts)。 */ import { beforeEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' import GatingPage from '@/pages/gating/GatingPage.vue' const gatingApi = vi.hoisted(() => ({ fetchGatingPage: vi.fn(), fetchGatingStats: vi.fn(), createGating: vi.fn(), deleteGating: vi.fn(), updateGatingStatus: vi.fn(), importGatingText: vi.fn(), })) vi.mock('@/pages/gating/gating-api', () => gatingApi) const listPayload = { total: 2, rows: [ { id: 1, brand: 'jellycat', asin: 'B0ABC123', country: '英国', gatingType: '品牌gating', status: 1, source: 'AUTO', shopName: '', remark: '', createdAt: '2026-01-02 03:04:05', updatedAt: '2026-01-03 03:04:05', }, { id: 2, brand: 'crocs', asin: '', country: '全部', gatingType: '类目gating', status: 0, source: 'MANUAL', shopName: '店A', remark: '人为录入', createdAt: '2026-02-01 00:00:00', updatedAt: '2026-02-02 00:00:00', disabledAt: '2026-02-03 00:00:00', }, ], } const statsPayload = { total: 2, active: 1, disabled: 1, brands: 2, byType: [{ name: '品牌gating', count: 1 }], byCountry: [{ name: '英国', count: 1 }], bySource: [{ name: 'MANUAL', count: 1 }], } const findButton = (wrapper: VueWrapper, label: string) => wrapper.findAll('button').find((button) => button.text().trim() === label) describe('GatingPage', () => { beforeEach(() => { vi.clearAllMocks() gatingApi.fetchGatingPage.mockResolvedValue(listPayload) gatingApi.fetchGatingStats.mockResolvedValue(statsPayload) }) it('渲染统计卡、筛选区与表格行', async () => { const wrapper = mount(GatingPage) await flushPromises() const text = wrapper.text() expect(text).toContain('Gating总记录') expect(text).toContain('去重品牌数') expect(text).toContain('jellycat') expect(text).toContain('crocs') expect(text).toContain('生效') expect(text).toContain('停用') expect(text).toContain('按Gating类型') expect(wrapper.findAll('tbody tr').length).toBeGreaterThanOrEqual(2) expect(gatingApi.fetchGatingPage).toHaveBeenCalledTimes(1) expect(gatingApi.fetchGatingStats).toHaveBeenCalledTimes(1) }) it('新增弹窗提交后按驼峰字段发载荷', async () => { const wrapper = mount(GatingPage) await flushPromises() gatingApi.createGating.mockResolvedValue(1) await findButton(wrapper, '新增')!.trigger('click') const modal = wrapper.get('.modal') const inputs = modal.findAll('input[type="text"]') await inputs[0]!.setValue('jellycat') // 品牌 await inputs[1]!.setValue('B0Z9X') // ASIN await inputs[2]!.setValue('店A') // 店铺 await inputs[3]!.setValue('测试备注') // 备注 await findButton(modal, '保存')!.trigger('click') await flushPromises() expect(gatingApi.createGating).toHaveBeenCalledTimes(1) expect(gatingApi.createGating).toHaveBeenCalledWith({ brand: 'jellycat', asin: 'B0Z9X', country: '全部', gatingType: '品牌gating', source: 'MANUAL', shopName: '店A', remark: '测试备注', }) }) it('停用行点恢复 → POST status 载荷 status=1;品牌为空时新增被拦截', async () => { const wrapper = mount(GatingPage) await flushPromises() const crocsRow = wrapper .findAll('tbody tr') .find((row) => row.text().includes('crocs'))! await findButton(crocsRow, '恢复')!.trigger('click') await flushPromises() expect(gatingApi.updateGatingStatus).toHaveBeenCalledTimes(1) expect(gatingApi.updateGatingStatus).toHaveBeenCalledWith(2, 1) // 品牌必填:空品牌提交不发请求 await findButton(wrapper, '新增')!.trigger('click') await findButton(wrapper.get('.modal'), '保存')!.trigger('click') await flushPromises() expect(gatingApi.createGating).not.toHaveBeenCalled() }) })