This commit is contained in:
2026-08-17 17:49:10 +08:00
parent 911a3620e1
commit c7334b2cf8
30 changed files with 1043 additions and 249 deletions
@@ -1,13 +1,15 @@
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import ElementPlus from 'element-plus'
import ElementPlus, { ElMessage } from 'element-plus'
import { createPinia, setActivePinia } from 'pinia'
import { flushPromises, mount } from '@vue/test-utils'
import { createMemoryHistory, createRouter } from 'vue-router'
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { useAccessPreviewStore } from '@/core/access/accessPreview'
import { useAuthStore } from '@/core/auth/auth'
import { createPlatformExportTask } from '@/modules/downloads'
import { listPlatformTenants, type PlatformTenantResource } from '@/modules/iam'
import { showExportTaskCreatedDialog } from '@/shared/components/exportTaskCreatedDialog'
import { listFinanceAccountLedger, listFinanceAccounts } from '../api/finance'
import FinanceCustomerAccountsView from './FinanceCustomerAccountsView.vue'
@@ -18,6 +20,8 @@ vi.mock('../api/finance', () => ({
updateFinanceCustomerTerms: vi.fn(),
}))
vi.mock('@/modules/iam', () => ({ listPlatformTenants: vi.fn() }))
vi.mock('@/modules/downloads', () => ({ createPlatformExportTask: vi.fn() }))
vi.mock('@/shared/components/exportTaskCreatedDialog', () => ({ showExportTaskCreatedDialog: vi.fn() }))
class ResizeObserverStub implements ResizeObserver { observe() {} unobserve() {} disconnect() {} }
@@ -26,6 +30,21 @@ function memoryStorage(): Storage {
return { get length() { return values.size }, clear: () => values.clear(), getItem: (key) => values.get(key) ?? null, key: (index) => [...values.keys()][index] ?? null, removeItem: (key) => values.delete(key), setItem: (key, value) => values.set(key, value) }
}
async function mountAccountsView(permissions: string[], initialPath = '/finance/customer-accounts') {
const pinia = createPinia(); setActivePinia(pinia)
const auth = useAuthStore(); auth.token = 'token'; auth.permissions = permissions
useAccessPreviewStore().initializeAfterLogin()
const router = createRouter({ history: createMemoryHistory(), routes: [
{ path: '/finance/customer-accounts', name: 'finance-customer-accounts', component: FinanceCustomerAccountsView },
{ path: '/finance/customer-accounts/:customerId', name: 'finance-customer-account-detail', component: FinanceCustomerAccountsView },
{ path: '/downloads', name: 'download-center', component: { template: '<div>下载中心</div>' } },
] })
await router.push(initialPath); await router.isReady()
const wrapper = mount(FinanceCustomerAccountsView, { global: { plugins: [pinia, ElementPlus, router] } })
await flushPromises()
return Object.assign(wrapper, { router })
}
describe('FinanceCustomerAccountsView delivery', () => {
const page = readFileSync(resolve(process.cwd(), 'src/modules/finance/pages/FinanceCustomerAccountsView.vue'), 'utf8')
const routes = readFileSync(resolve(process.cwd(), 'src/modules/finance/routes.ts'), 'utf8')
@@ -92,6 +111,8 @@ describe('FinanceCustomerAccountsView delivery', () => {
vi.mocked(listFinanceAccounts).mockReset().mockResolvedValue({ records: [{ customerId: '10001', customerCode: 'C-001', customerName: '客户A', contactName: '张三', contactPhone: '13800000000', outstandingReceivable: 100, pendingBilling: 20, receivedInPeriod: 30, outstandingPayable: 40, status: 'ACTIVE', lastChangedAt: '2026-08-13 10:00:00' }], total: 1, pages: 1 })
vi.mocked(listFinanceAccountLedger).mockReset().mockResolvedValue({ records: [{ ledgerId: '1', occurredAt: '2026-08-13T10:00:00', businessType: 'REC', documentType: 'RECEIPT', documentId: '2', documentNo: 'REC-2', receivableChange: -10, payableChange: 0, outstandingReceivableAfter: 90, operatorName: 'operator-1000001', remark: '收款' }], total: 1, pages: 1 })
vi.mocked(listPlatformTenants).mockReset().mockResolvedValue({ records: [{ id: '10001', tenantName: '客户A', status: 'ACTIVE' } as PlatformTenantResource], page: 1, size: 200, total: 1, pages: 1 })
vi.mocked(createPlatformExportTask).mockReset().mockResolvedValue({ id: 'exp-1', taskNo: 'EXP-1', taskName: '客户账户', status: 'PENDING' } as never)
vi.mocked(showExportTaskCreatedDialog).mockReset().mockResolvedValue(false)
})
it('restores account summary and ledger after opening the detail URL directly', async () => {
@@ -133,6 +154,57 @@ describe('FinanceCustomerAccountsView delivery', () => {
expect(wrapper.text()).not.toContain('2026-08-13T10:00:00')
wrapper.unmount()
})
it('creates account and ledger export tasks from the complete active filters without page parameters', async () => {
const permissions = ['platform:finance:customer-account:list', 'platform:finance:customer-account:export', 'platform:download:list']
const listWrapper = await mountAccountsView(permissions)
const listVm = listWrapper.vm as unknown as { filters: Record<string, unknown>; exportAccounts(): Promise<void> }
Object.assign(listVm.filters, { customerId: '10001', status: 'ACTIVE' })
await listVm.exportAccounts()
expect(createPlatformExportTask).toHaveBeenLastCalledWith({
exportType: 'FINANCE_CUSTOMER_ACCOUNT_LIST', format: 'XLSX', taskName: '客户账户',
parameters: { customerId: '10001', status: 'ACTIVE' },
})
expect(createPlatformExportTask).not.toHaveBeenCalledWith(expect.objectContaining({ parameters: expect.objectContaining({ page: expect.anything() }) }))
listWrapper.unmount()
vi.mocked(createPlatformExportTask).mockResolvedValueOnce({ id: 'exp-2', taskNo: 'EXP-2', taskName: '客户账户流水', status: 'PENDING' } as never)
const detailWrapper = await mountAccountsView(permissions, '/finance/customer-accounts/10001')
const detailVm = detailWrapper.vm as unknown as { ledgerFilters: Record<string, unknown>; exportLedger(): Promise<void> }
Object.assign(detailVm.ledgerFilters, { businessType: 'RECEIPT', occurredRange: ['2026-08-01', '2026-08-31'] })
await detailVm.exportLedger()
expect(createPlatformExportTask).toHaveBeenLastCalledWith({
exportType: 'FINANCE_CUSTOMER_ACCOUNT_LEDGER', format: 'XLSX', taskName: '客户账户流水',
parameters: { customerId: '10001', businessType: 'RECEIPT', startDate: '2026-08-01', endDate: '2026-08-31' },
})
expect(showExportTaskCreatedDialog).toHaveBeenLastCalledWith({ taskName: '客户账户流水', taskNo: 'EXP-2' })
detailWrapper.unmount()
})
it('requires both the finance export permission and download-center access', async () => {
const wrapper = await mountAccountsView(['platform:finance:customer-account:list', 'platform:finance:customer-account:export'])
expect(wrapper.findAll('button').map((button) => button.text())).not.toContain('导出Excel')
expect(createPlatformExportTask).not.toHaveBeenCalled()
wrapper.unmount()
})
it('opens the download center after confirmation and reports task creation failures', async () => {
const permissions = ['platform:finance:customer-account:list', 'platform:finance:customer-account:export', 'platform:download:list']
vi.mocked(showExportTaskCreatedDialog).mockResolvedValueOnce(true)
const wrapper = await mountAccountsView(permissions)
await (wrapper.vm as unknown as { exportAccounts(): Promise<void> }).exportAccounts()
expect(wrapper.router.currentRoute.value.name).toBe('download-center')
wrapper.unmount()
const errorSpy = vi.spyOn(ElMessage, 'error').mockImplementation(() => undefined as never)
vi.mocked(createPlatformExportTask).mockRejectedValueOnce(new Error('导出服务不可用'))
const failedWrapper = await mountAccountsView(permissions)
await (failedWrapper.vm as unknown as { exportAccounts(): Promise<void> }).exportAccounts()
expect(errorSpy).toHaveBeenCalledWith('客户账户导出任务创建失败')
expect(showExportTaskCreatedDialog).toHaveBeenCalledTimes(1)
failedWrapper.unmount()
errorSpy.mockRestore()
})
})
afterAll(() => vi.unstubAllGlobals())