106 lines
5.0 KiB
TypeScript
106 lines
5.0 KiB
TypeScript
import ElementPlus from 'element-plus'
|
|
import { createPinia, setActivePinia } from 'pinia'
|
|
import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
downloadPlatformExportTask,
|
|
listPlatformExportTasks,
|
|
retryPlatformExportTask,
|
|
type PlatformExportTask,
|
|
} from '@/modules/downloads/api/platformExportTasks'
|
|
import { useAccessPreviewStore } from '@/core/access/accessPreview'
|
|
import { useAuthStore } from '@/core/auth/auth'
|
|
import DownloadCenterView from './DownloadCenterView.vue'
|
|
|
|
vi.mock('@/modules/downloads/api/platformExportTasks', () => ({
|
|
downloadPlatformExportTask: vi.fn(),
|
|
listPlatformExportTasks: vi.fn(),
|
|
retryPlatformExportTask: vi.fn(),
|
|
}))
|
|
|
|
class ResizeObserverStub implements ResizeObserver {
|
|
observe() {}
|
|
unobserve() {}
|
|
disconnect() {}
|
|
}
|
|
|
|
const task: PlatformExportTask = {
|
|
id: '9', taskNo: 'EXP9', exportType: 'CUSTOMER_LIST', format: 'XLSX', taskName: '客户列表',
|
|
status: 'SUCCEEDED', totalRows: 12, processedRows: 12, fileName: '客户列表.xlsx',
|
|
contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', sizeBytes: 2048,
|
|
failureMessage: null, requestedBy: '1', requestedUsername: 'admin', requestedAt: '2026-08-03T10:00:00',
|
|
startedAt: '2026-08-03T10:00:01', completedAt: '2026-08-03T10:00:02', expiresAt: '2026-08-10T10:00:02',
|
|
}
|
|
|
|
enableAutoUnmount(afterEach)
|
|
afterEach(() => vi.useRealTimers())
|
|
|
|
describe('DownloadCenterView', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
vi.stubGlobal('ResizeObserver', ResizeObserverStub)
|
|
Object.defineProperty(URL, 'createObjectURL', { configurable: true, value: vi.fn(() => 'blob:export') })
|
|
Object.defineProperty(URL, 'revokeObjectURL', { configurable: true, value: vi.fn() })
|
|
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
|
|
const pinia = createPinia()
|
|
setActivePinia(pinia)
|
|
const auth = useAuthStore()
|
|
auth.token = 'token'
|
|
auth.permissions = ['platform:download:list', 'platform:customer:export']
|
|
useAccessPreviewStore().initializeAfterLogin()
|
|
vi.mocked(listPlatformExportTasks).mockResolvedValue({ records: [task], page: 1, size: 20, total: 1, pages: 1 })
|
|
vi.mocked(downloadPlatformExportTask).mockResolvedValue(new Blob(['xlsx']))
|
|
vi.mocked(retryPlatformExportTask).mockResolvedValue({ ...task, status: 'PENDING' })
|
|
})
|
|
|
|
it('lists completed tasks and downloads the backend-generated artifact', async () => {
|
|
const pinia = setActivePinia(createPinia())
|
|
const auth = useAuthStore()
|
|
auth.token = 'token'
|
|
auth.permissions = ['platform:download:list', 'platform:customer:export']
|
|
useAccessPreviewStore().initializeAfterLogin()
|
|
mount(DownloadCenterView, { attachTo: document.body, global: { plugins: [pinia, ElementPlus] } })
|
|
await flushPromises()
|
|
|
|
expect(document.body.textContent).toContain('客户列表')
|
|
expect(document.querySelector('.failure-cell')?.classList.contains('failure-message')).toBe(false)
|
|
const button = [...document.querySelectorAll<HTMLButtonElement>('button')].find((item) => item.textContent?.trim() === '下载')
|
|
button?.click()
|
|
await flushPromises()
|
|
|
|
expect(downloadPlatformExportTask).toHaveBeenCalledWith('9')
|
|
expect(URL.createObjectURL).toHaveBeenCalled()
|
|
})
|
|
|
|
it('shows retry only for failed tasks with the source export permission', async () => {
|
|
vi.mocked(listPlatformExportTasks).mockResolvedValue({ records: [{ ...task, status: 'FAILED', failureMessage: 'IAM timeout' }], page: 1, size: 20, total: 1, pages: 1 })
|
|
const pinia = setActivePinia(createPinia())
|
|
const auth = useAuthStore()
|
|
auth.token = 'token'
|
|
auth.permissions = ['platform:download:list', 'platform:customer:export']
|
|
useAccessPreviewStore().initializeAfterLogin()
|
|
mount(DownloadCenterView, { attachTo: document.body, global: { plugins: [pinia, ElementPlus] } })
|
|
await flushPromises()
|
|
|
|
expect(document.querySelector('.failure-cell')?.classList.contains('failure-message')).toBe(true)
|
|
const button = [...document.querySelectorAll<HTMLButtonElement>('button')].find((item) => item.textContent?.trim() === '重试')
|
|
button?.click()
|
|
await flushPromises()
|
|
expect(retryPlatformExportTask).toHaveBeenCalledWith('9')
|
|
})
|
|
|
|
it('does not schedule another refresh after an in-flight load finishes on an unmounted page', async () => {
|
|
vi.useFakeTimers()
|
|
let resolveRequest!: (value: { records: PlatformExportTask[]; page: number; size: number; total: number; pages: number }) => void
|
|
vi.mocked(listPlatformExportTasks).mockReturnValue(new Promise((resolve) => { resolveRequest = resolve }))
|
|
const wrapper = mount(DownloadCenterView, { global: { plugins: [createPinia(), ElementPlus] } })
|
|
|
|
wrapper.unmount()
|
|
resolveRequest({ records: [{ ...task, status: 'RUNNING' }], page: 1, size: 20, total: 1, pages: 1 })
|
|
await flushPromises()
|
|
await vi.advanceTimersByTimeAsync(5000)
|
|
|
|
expect(listPlatformExportTasks).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|