316 lines
15 KiB
TypeScript
316 lines
15 KiB
TypeScript
/**
|
||
* 外观专利无侵权数据页(v1.3 AC-5)的挂载契约。
|
||
*
|
||
* 形态(2026-11-11 客户三连拍板:「要按主管分组显示」→「应该是只显示一个卡片的才对」→
|
||
* 「获取店铺那边是按店铺做成卡片 我们这边就是按主管划分」):<b>一主管一卡</b>,对齐
|
||
* 「店铺数据记录」页的一店一卡——卡头=主管名,卡内 条数 / 任务(几个任务·最近任务号)/
|
||
* 国家 / 最新同步时间 + [下载](导出该主管名下 xlsx)。明细分页卡片(每明细一卡)已下架:
|
||
* 3488 条明细铺成几千张卡是客户明确不要的形态;明细仍走 /export 表格看。
|
||
* 数据来自 GET /supervisors(后端按归属表达式聚合,total 是主管数)。筛选面不变
|
||
* (分组=主管/国家/同步起止,分组选中即查),取数/导出/公司 tab 契约照旧。
|
||
*
|
||
* 跑法:`npx vitest run tests/mount/appearance-patent-clean-page.test.ts`
|
||
*/
|
||
import { readFileSync } from 'node:fs'
|
||
import { resolve } from 'node:path'
|
||
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 AppearancePatentCleanItemsPage from '@/pages/patent/AppearancePatentCleanItemsPage.vue'
|
||
import { adminPages } from '@/router/routes'
|
||
import { COMPANY_SCOPED_PAGES } from '@/config/company-scope'
|
||
import { useAdminSessionStore } from '@/stores/admin-session'
|
||
import { useCompanyTabStore } from '@/stores/company-tab'
|
||
|
||
const PAGE_PATH = 'src/pages/patent/AppearancePatentCleanItemsPage.vue'
|
||
const MENU_KEY = 'admin_appearance_patent_clean_items'
|
||
|
||
const cleanApi = vi.hoisted(() => ({
|
||
fetchAppearancePatentCleanSupervisors: vi.fn(),
|
||
exportAppearancePatentCleanList: vi.fn(),
|
||
fetchDedupeTotalGroups: vi.fn(),
|
||
}))
|
||
|
||
vi.mock('@/pages/patent/appearance-patent-clean-api.ts', () => cleanApi)
|
||
|
||
function card(supervisorId: number | null, over: Record<string, unknown> = {}) {
|
||
return {
|
||
supervisorId,
|
||
supervisorName: supervisorId == null ? '' : `主管${supervisorId}`,
|
||
itemCount: 120,
|
||
taskCount: 3,
|
||
lastTaskId: 33874,
|
||
lastSyncedAt: '2026-11-11T11:34:29',
|
||
countryCodes: ['DE', 'UK'],
|
||
...over,
|
||
}
|
||
}
|
||
|
||
let pinia: Pinia
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
pinia = createPinia()
|
||
setActivePinia(pinia)
|
||
useAdminSessionStore().user = { id: 1, username: 'root', role: 'super_admin' }
|
||
useCompanyTabStore().setActiveId(1)
|
||
cleanApi.fetchDedupeTotalGroups.mockResolvedValue([])
|
||
})
|
||
|
||
async function mountWith(payload: unknown): Promise<VueWrapper<Element>> {
|
||
cleanApi.fetchAppearancePatentCleanSupervisors.mockImplementation(() => Promise.resolve(payload))
|
||
const wrapper = mount(AppearancePatentCleanItemsPage, { global: { plugins: [pinia] } })
|
||
await flushPromises()
|
||
await flushPromises()
|
||
return wrapper
|
||
}
|
||
|
||
describe('外观专利数据库页(一主管一卡,2026-11-11 客户「我们这边就是按主管划分」)', () => {
|
||
it('页面组件落位:卡片宫格 + 主管聚合端点,不再按明细铺卡', () => {
|
||
const src = readFileSync(resolve(process.cwd(), PAGE_PATH), 'utf-8')
|
||
expect(src).toContain('OldPagination')
|
||
expect(src).toContain('clean-card-grid', '卡片宫格容器必须就位')
|
||
expect(src).toContain('clean-card-head', '卡片本体结构必须就位')
|
||
expect(src).toContain('fetchAppearancePatentCleanSupervisors', '必须走主管聚合端点(一主管一卡)')
|
||
expect(src).toContain('supervisorUserId', '单卡下载按主管收窄导出')
|
||
expect(src).not.toContain('<table', '本页不是自绘行列表')
|
||
expect(src).not.toContain('el-card')
|
||
})
|
||
|
||
it('路由与公司 Tab 注册齐备(companyScoped: true)', () => {
|
||
const page = adminPages.find((p) => p.path === 'patent-center/clean-items')
|
||
expect(page).toBeTruthy()
|
||
expect(page?.menuKey).toBe(MENU_KEY)
|
||
expect(page?.title).toBe('外观专利数据库')
|
||
expect(page?.companyScoped).toBe(true)
|
||
const scoped = COMPANY_SCOPED_PAGES.find((p) => p.menuKey === MENU_KEY)
|
||
expect(scoped).toBeTruthy()
|
||
expect(scoped?.pageFiles).toContain(PAGE_PATH)
|
||
})
|
||
|
||
it('挂载后按首屏分页参数取主管聚合,卡片数=主管数而不是明细条数', async () => {
|
||
const wrapper = await mountWith({
|
||
items: [card(9), card(44), card(null, { supervisorName: '' })],
|
||
total: 3,
|
||
page: 1,
|
||
pageSize: 10,
|
||
})
|
||
|
||
expect(cleanApi.fetchAppearancePatentCleanSupervisors).toHaveBeenCalledTimes(1)
|
||
const firstCall = cleanApi.fetchAppearancePatentCleanSupervisors.mock.calls[0][0]
|
||
expect(firstCall.page).toBe(1)
|
||
expect(firstCall.pageSize).toBe(10)
|
||
expect(firstCall.companyId).toBe(1)
|
||
expect(wrapper.findAll('.clean-card')).toHaveLength(3, '一主管一卡')
|
||
expect(wrapper.text()).toContain('共 3 位主管', '汇总按主管计数(不是明细条数)')
|
||
})
|
||
|
||
it('卡片内容=主管名/条数/任务/国家/最新(对齐店铺数据记录的一卡一主体信息面)', async () => {
|
||
const wrapper = await mountWith({ items: [card(9)], total: 1, page: 1, pageSize: 10 })
|
||
const one = wrapper.find('.clean-card')
|
||
expect(one.find('.clean-card-supervisor').text()).toBe('主管9', '卡头=主管名')
|
||
const rowValue = (label: string): string => {
|
||
const row = one.findAll('.clean-card-row').find((r) => r.find('.clean-card-label').text() === label)
|
||
expect(row, `卡片必须有「${label}」行`).toBeTruthy()
|
||
return row!.find('.clean-card-value').text()
|
||
}
|
||
expect(rowValue('条数')).toContain('120 条', '明细条数是汇总不是列表')
|
||
expect(rowValue('任务')).toContain('3 个任务')
|
||
expect(rowValue('任务')).toContain('33874', '带最近任务号')
|
||
expect(rowValue('国家')).toContain('德国')
|
||
expect(rowValue('国家')).toContain('英国')
|
||
expect(rowValue('最新')).not.toBe('')
|
||
})
|
||
|
||
it('读不到主管的那一卡展示 -,且不给下载按钮(不猜是哪位主管)', async () => {
|
||
const wrapper = await mountWith({ items: [card(null)], total: 1, page: 1, pageSize: 10 })
|
||
const one = wrapper.find('.clean-card')
|
||
expect(one.find('.clean-card-supervisor').text()).toBe('-', '读不到主管不猜名字')
|
||
const downloadBtn = one.findAll('button').find((b) => b.text() === '下载')
|
||
expect(downloadBtn).toBeTruthy()
|
||
expect((downloadBtn!.element as HTMLButtonElement).disabled).toBe(true, '无主管 id 不可按主管导出')
|
||
})
|
||
|
||
it('单卡下载=按该主管导出(带 supervisorUserId 与当前过滤面)', async () => {
|
||
const createObjectURL = vi.fn(() => 'blob:mock-url')
|
||
const revokeObjectURL = vi.fn()
|
||
const urlRecord = URL as unknown as Record<string, unknown>
|
||
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(() => {})
|
||
cleanApi.exportAppearancePatentCleanList.mockImplementation(() =>
|
||
Promise.resolve({ blob: new Blob(['xlsx']), filename: '外观专利数据库-20261111120000.xlsx' }),
|
||
)
|
||
|
||
try {
|
||
const wrapper = await mountWith({ items: [card(9)], total: 1, page: 1, pageSize: 10 })
|
||
const btn = wrapper.find('.clean-card').findAll('button').find((b) => b.text() === '下载')
|
||
await btn!.trigger('click')
|
||
await flushPromises()
|
||
await flushPromises()
|
||
|
||
expect(cleanApi.exportAppearancePatentCleanList).toHaveBeenCalledTimes(1)
|
||
const call = cleanApi.exportAppearancePatentCleanList.mock.calls[0][0]
|
||
expect(call).toMatchObject({ supervisorUserId: 9, companyId: 1, country: '', groupId: '' })
|
||
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('顶部导出带当前过滤面与公司 tab,不带 supervisorUserId(全量)', async () => {
|
||
const createObjectURL = vi.fn(() => 'blob:mock-url')
|
||
const revokeObjectURL = vi.fn()
|
||
const urlRecord = URL as unknown as Record<string, unknown>
|
||
const origCreate = urlRecord.createObjectURL
|
||
const origRevoke = urlRecord.revokeObjectURL
|
||
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(() => {})
|
||
cleanApi.exportAppearancePatentCleanList.mockImplementation(() =>
|
||
Promise.resolve({ blob: new Blob(['xlsx']), filename: '外观专利数据库-20261111120000.xlsx' }),
|
||
)
|
||
|
||
try {
|
||
const wrapper = await mountWith({ items: [card(9)], total: 1, page: 1, pageSize: 10 })
|
||
await wrapper.findAll('button').find((b) => b.text() === '导出')!.trigger('click')
|
||
await flushPromises()
|
||
await flushPromises()
|
||
|
||
const call = cleanApi.exportAppearancePatentCleanList.mock.calls[0][0]
|
||
expect(call).toMatchObject({
|
||
taskId: '',
|
||
asin: '',
|
||
country: '',
|
||
groupId: '',
|
||
startDate: '',
|
||
endDate: '',
|
||
companyId: 1,
|
||
})
|
||
expect(call.supervisorUserId).toBeUndefined()
|
||
expect(createObjectURL).toHaveBeenCalledTimes(1)
|
||
expect(revokeObjectURL).toHaveBeenCalledWith('blob:mock-url')
|
||
} finally {
|
||
Object.defineProperty(URL, 'createObjectURL', { value: origCreate, configurable: true })
|
||
Object.defineProperty(URL, 'revokeObjectURL', { value: origRevoke, configurable: true })
|
||
clickSpy.mockRestore()
|
||
}
|
||
})
|
||
|
||
it('导出失败把服务端业务文案弹出来,不许静默', async () => {
|
||
cleanApi.exportAppearancePatentCleanList.mockImplementation(() =>
|
||
Promise.reject(new Error('该主管的无侵权数据共 90001 行,超过导出上限 50000 行,请缩小筛选范围后重试')))
|
||
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 10 })
|
||
await wrapper.findAll('button').find((b) => b.text() === '导出')!.trigger('click')
|
||
await flushPromises()
|
||
await flushPromises()
|
||
expect(document.body.textContent).toContain('超过导出上限')
|
||
expect(wrapper.findAll('button').find((b) => b.text() === '导出')!.text()).toBe('导出')
|
||
})
|
||
|
||
it('翻页与改每页条数都重发请求,改条数回第一页', async () => {
|
||
const wrapper = await mountWith({ items: [card(9)], total: 42, page: 2, pageSize: 10 })
|
||
|
||
await wrapper.findAll('button').find((b) => b.text() === '下一页')!.trigger('click')
|
||
await flushPromises()
|
||
expect(cleanApi.fetchAppearancePatentCleanSupervisors).toHaveBeenCalledTimes(2)
|
||
expect(cleanApi.fetchAppearancePatentCleanSupervisors.mock.calls[1][0].page).toBe(3)
|
||
|
||
const sizeSelect = wrapper.find('.page-size-select')
|
||
await sizeSelect.setValue('50')
|
||
await flushPromises()
|
||
const last = cleanApi.fetchAppearancePatentCleanSupervisors.mock.calls.at(-1)![0]
|
||
expect(last.pageSize).toBe(50)
|
||
expect(last.page).toBe(1, '改每页条数后回第一页')
|
||
})
|
||
|
||
it('分组/国家/起止时间筛选生效(任务号/ASIN 输入框已下架)', async () => {
|
||
cleanApi.fetchDedupeTotalGroups.mockResolvedValue([{ id: 7, name: '甲组', leaderUserId: 9 }])
|
||
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 10 })
|
||
|
||
const filterTextInputs = wrapper.findAll('.form-row input[type="text"]')
|
||
expect(filterTextInputs).toHaveLength(0)
|
||
|
||
const selects = wrapper.findAll('select')
|
||
await selects[0].setValue('7') // 分组(主管=组长)排在国家前
|
||
await selects[1].setValue('DE')
|
||
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 = cleanApi.fetchAppearancePatentCleanSupervisors.mock.calls.at(-1)![0]
|
||
expect(last).toMatchObject({
|
||
country: 'DE',
|
||
groupId: '7',
|
||
startDate: '2026-11-01',
|
||
endDate: '2026-11-09',
|
||
page: 1,
|
||
})
|
||
})
|
||
|
||
it('分组下拉(主管=组长)选中即查', async () => {
|
||
cleanApi.fetchDedupeTotalGroups.mockResolvedValue([
|
||
{ id: 7, name: '甲组', leaderUserId: 9 },
|
||
{ id: 8, name: '乙组', leaderUserId: 10 },
|
||
])
|
||
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 10 })
|
||
expect(cleanApi.fetchDedupeTotalGroups).toHaveBeenCalledWith(1)
|
||
|
||
const groupSelect = wrapper.findAll('select')[0]
|
||
const optionTexts = groupSelect.findAll('option').map((o) => o.text())
|
||
expect(optionTexts).toContain('全部分组')
|
||
expect(optionTexts).toContain('甲组')
|
||
|
||
const callsBeforeSelect = cleanApi.fetchAppearancePatentCleanSupervisors.mock.calls.length
|
||
await groupSelect.setValue('7')
|
||
await flushPromises()
|
||
expect(cleanApi.fetchAppearancePatentCleanSupervisors.mock.calls.length).toBe(callsBeforeSelect + 1,
|
||
'选中分组必须立刻重查')
|
||
expect(cleanApi.fetchAppearancePatentCleanSupervisors.mock.calls.at(-1)![0]).toMatchObject({
|
||
groupId: '7',
|
||
page: 1,
|
||
})
|
||
})
|
||
|
||
it('空态与加载失败条(失败给重试,不把整页打死)', async () => {
|
||
const empty = await mountWith({ items: [], total: 0, page: 1, pageSize: 10 })
|
||
const tip = empty.find('.empty-tip')
|
||
expect(tip.exists()).toBe(true)
|
||
expect(tip.text()).toContain('暂无无侵权数据')
|
||
empty.unmount()
|
||
|
||
cleanApi.fetchAppearancePatentCleanSupervisors.mockRejectedValue(new Error('上游 502'))
|
||
const failed = mount(AppearancePatentCleanItemsPage, { global: { plugins: [pinia] } })
|
||
await flushPromises()
|
||
await flushPromises()
|
||
expect(failed.find('.load-failure-bar').exists()).toBe(true)
|
||
expect(failed.find('.load-failure-bar').text()).toContain('无侵权数据加载失败')
|
||
})
|
||
|
||
it('切公司 Tab 重取并清掉上一家的筛选(company-tab-wiring 同口径)', async () => {
|
||
cleanApi.fetchDedupeTotalGroups.mockResolvedValue([{ id: 7, name: '甲组', leaderUserId: 9 }])
|
||
const wrapper = await mountWith({ items: [card(9)], total: 1, page: 1, pageSize: 10 })
|
||
const groupSelect = wrapper.findAll('select')[0]
|
||
await groupSelect.setValue('7')
|
||
const callsBefore = cleanApi.fetchAppearancePatentCleanSupervisors.mock.calls.length
|
||
|
||
useCompanyTabStore().setActiveId(2)
|
||
await flushPromises()
|
||
await flushPromises()
|
||
|
||
expect(cleanApi.fetchAppearancePatentCleanSupervisors.mock.calls.length).toBeGreaterThan(callsBefore)
|
||
const last = cleanApi.fetchAppearancePatentCleanSupervisors.mock.calls.at(-1)![0]
|
||
expect(last.companyId).toBe(2)
|
||
expect(last.groupId).toBe('', '切公司必须清掉上一家选的分组')
|
||
expect(last.page).toBe(1)
|
||
expect(cleanApi.fetchDedupeTotalGroups).toHaveBeenLastCalledWith(2)
|
||
})
|
||
})
|