92 lines
4.0 KiB
TypeScript
92 lines
4.0 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { http } from '@/shared/api/http'
|
|
import {
|
|
changePlatformPartnerStatus,
|
|
createPlatformPartner,
|
|
downloadPlatformPartnerImportTemplate,
|
|
getPlatformPartner,
|
|
importPlatformPartners,
|
|
listPlatformPartners,
|
|
updatePlatformPartner,
|
|
type SavePlatformPartnerRequest,
|
|
} from './platformPartners'
|
|
|
|
vi.mock('@/shared/api/http', () => ({
|
|
http: { get: vi.fn(), post: vi.fn(), put: vi.fn(), patch: vi.fn() },
|
|
unwrapApiResponse: <T>(response: { data: T }) => response.data,
|
|
}))
|
|
|
|
const payload: SavePlatformPartnerRequest = {
|
|
partnerCode: 'CUS-001',
|
|
partnerName: '测试往来单位',
|
|
partnerType: 'SHIPPER',
|
|
types: ['SHIPPER'],
|
|
contactName: '测试联系人',
|
|
contactPhone: '13800000000',
|
|
contactEmail: null,
|
|
countryCode: 'CN',
|
|
province: '广东省',
|
|
city: '深圳市',
|
|
district: null,
|
|
addressLine: '测试地址 1 号',
|
|
postalCode: null,
|
|
status: 'ACTIVE',
|
|
remark: '测试档案',
|
|
}
|
|
|
|
describe('platform partner API', () => {
|
|
beforeEach(() => vi.clearAllMocks())
|
|
|
|
it('queries the platform partner page with filters only', async () => {
|
|
vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: { records: [], page: '1', size: '20', total: '1', pages: '1' } })
|
|
|
|
const result = await listPlatformPartners({ page: 1, size: 20, keyword: '测试', type: 'SHIPPER', status: 'ACTIVE' })
|
|
|
|
expect(http.get).toHaveBeenCalledWith('/platform/v1/partners', {
|
|
params: { page: 1, size: 20, keyword: '测试', type: 'SHIPPER', status: 'ACTIVE' },
|
|
})
|
|
expect(result).toMatchObject({ page: 1, size: 20, total: 1, pages: 1 })
|
|
})
|
|
|
|
it('uses the backend create, update and status contracts', async () => {
|
|
vi.mocked(http.post).mockResolvedValue({ code: '200', message: 'success', data: '19001' })
|
|
vi.mocked(http.put).mockResolvedValue({ code: '200', message: 'success', data: null })
|
|
vi.mocked(http.patch).mockResolvedValue({ code: '200', message: 'success', data: null })
|
|
|
|
await createPlatformPartner(payload)
|
|
await updatePlatformPartner('19001', payload)
|
|
await changePlatformPartnerStatus('19001', 'INACTIVE')
|
|
|
|
expect(http.post).toHaveBeenCalledWith('/platform/v1/partners', payload, { params: { tenantId: 0 } })
|
|
expect(http.put).toHaveBeenCalledWith('/platform/v1/partners/19001', payload, { params: { tenantId: 0 } })
|
|
expect(http.patch).toHaveBeenCalledWith('/platform/v1/partners/19001/status', { status: 'INACTIVE' }, { params: { tenantId: 0 } })
|
|
})
|
|
|
|
it('normalizes legacy detail types and uses import contracts', async () => {
|
|
vi.mocked(http.get)
|
|
.mockResolvedValueOnce({ code: '200', message: 'success', data: {
|
|
id: '19001', tenantId: 0, dataScopeId: 0, partnerCode: 'CUS-001', partnerName: '测试单位',
|
|
partnerType: 'CONSIGNEE', contactName: '张三', contactPhone: '13800000000', contactEmail: null,
|
|
countryCode: 'CN', province: null, city: '深圳市', district: null, addressLine: '测试地址',
|
|
postalCode: null, status: 'ACTIVE',
|
|
} })
|
|
.mockResolvedValueOnce(new Blob(['template']))
|
|
vi.mocked(http.post).mockResolvedValue({ code: '200', message: 'success', data: {
|
|
fileName: 'partners.xlsx', totalRows: 1, importedRows: 1, success: true, errors: [],
|
|
} })
|
|
|
|
const detail = await getPlatformPartner('19001')
|
|
const template = await downloadPlatformPartnerImportTemplate()
|
|
const file = new File(['xlsx'], 'partners.xlsx')
|
|
const result = await importPlatformPartners(file)
|
|
|
|
expect(detail.types).toEqual(['CONSIGNEE'])
|
|
expect(detail.dataScopeId).toBe(0)
|
|
expect(template).toBeInstanceOf(Blob)
|
|
expect(result.success).toBe(true)
|
|
expect(http.get).toHaveBeenNthCalledWith(1, '/platform/v1/partners/19001', { params: { tenantId: 0 } })
|
|
expect(http.get).toHaveBeenNthCalledWith(2, '/platform/v1/partners/import-template', { responseType: 'blob' })
|
|
expect(http.post).toHaveBeenCalledWith('/platform/v1/partners/import', expect.any(FormData), { params: { tenantId: 0 } })
|
|
})
|
|
})
|