Files
admin-web/src/modules/customers/overseasStorageFee.test.ts
T

75 lines
4.0 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { getCustomerOverseasStorageFees, updateCustomerOverseasStorageFees } from './overseasStorageFee'
import { http } from '@/shared/api/http'
const overseasListSource = readFileSync(resolve(process.cwd(), 'src/modules/customers/pages/OverseasStorageFeesView.vue'), 'utf8')
const overseasPanelSource = readFileSync(resolve(process.cwd(), 'src/modules/customers/CustomerOverseasStorageFeePanel.vue'), 'utf8')
vi.mock('@/shared/api/http', () => ({
http: { get: vi.fn(), put: vi.fn() },
unwrapApiResponse: <T>(response: { data: T }) => {
const value = response.data as T & { code?: string; message?: string; data?: unknown }
if (value && typeof value === 'object' && 'code' in value) {
if (value.code !== '200') throw new Error(value.message || 'request failed')
return value.data as T
}
return value
},
}))
const config = { id: 12, customerId: 9007199254740993n.toString(), customerName: '客户', currency: 'USD', tiers: [], version: 0, createdAt: null, updatedAt: null }
describe('customer overseas storage fee api', () => {
beforeEach(() => vi.clearAllMocks())
it('keeps overseas fee business actions as semantic buttons at narrow widths', () => {
expect(overseasListSource).toContain('density-table density-table--editable customer-fee-tier-table')
expect(overseasListSource).toContain('class="density-action" size="small" plain')
expect(overseasListSource).toContain('size="small" plain type="danger"')
expect(overseasListSource).toContain('<thead><tr><th>操作</th><th>开始天数</th><th>结束天数</th><th>价格</th></tr></thead>')
expect(overseasListSource).not.toContain('<Pencil')
expect(overseasListSource).not.toContain('<Trash2')
expect(overseasListSource).toContain('@media (max-width:720px)')
expect(overseasPanelSource).toContain('size="small" plain type="danger"')
expect(overseasPanelSource).toContain('@media (max-width:760px)')
})
it('keeps customer ids as strings and uses the customer detail path', async () => {
vi.mocked(http.get).mockResolvedValue({ data: config })
const result = await getCustomerOverseasStorageFees(config.customerId)
expect(http.get).toHaveBeenCalledWith(`/platform/v1/iam/tenants/${config.customerId}/overseas-storage-fees`)
expect(result.id).toBe('12')
expect(result.customerId).toBe(config.customerId)
})
it('submits the complete group and reloads the new version', async () => {
vi.mocked(http.put).mockResolvedValue({ data: null })
vi.mocked(http.get).mockResolvedValue({ data: { ...config, version: 1 } })
const request = { customerId: config.customerId, customerName: '客户', currency: 'CNY', version: 0, tiers: [{ feeType: 'STORAGE' as const, billingUnit: 'CBM' as const, startDay: 1, endDay: null, unitPrice: 0 }] }
const result = await updateCustomerOverseasStorageFees(config.customerId, request)
expect(http.put).toHaveBeenCalledWith(`/platform/v1/iam/tenants/${config.customerId}/overseas-storage-fees`, request)
expect(result.version).toBe(1)
})
it('rejects a business error returned by the update endpoint', async () => {
vi.mocked(http.put).mockResolvedValue({ data: { code: 'A0400', message: '璇锋眰鏃犳晥', data: null } })
await expect(updateCustomerOverseasStorageFees(config.customerId, {
customerId: config.customerId,
customerName: '瀹㈡埛',
currency: 'USD',
version: 0,
tiers: [{ feeType: 'STORAGE', billingUnit: 'CBM', startDay: 1, endDay: null, unitPrice: 0 }],
})).rejects.toThrow('璇锋眰鏃犳晥')
expect(http.get).not.toHaveBeenCalled()
})
it('normalizes an omitted end day as an open-ended tier', async () => {
vi.mocked(http.get).mockResolvedValue({ data: { ...config, tiers: [{ id: 1, startDay: 91, unitPrice: 2, sortNo: 5, feeType: 'STORAGE', billingUnit: 'CBM' }] } })
const result = await getCustomerOverseasStorageFees(config.customerId)
expect(result.tiers[0]?.endDay).toBeNull()
})
})