118 lines
4.3 KiB
TypeScript
118 lines
4.3 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const apiClient = vi.hoisted(() => ({
|
|
get: vi.fn(),
|
|
post: vi.fn(),
|
|
put: vi.fn(),
|
|
delete: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/shared/api/client', () => ({ apiClient }))
|
|
|
|
import { fetchWalletBalance, fetchWalletLedger } from '@/features/wallet/api/wallet'
|
|
import {
|
|
cancelWithdrawal,
|
|
createPaymentAccount,
|
|
createWithdrawal,
|
|
deletePaymentAccount,
|
|
fetchPaymentAccount,
|
|
fetchPaymentAccounts,
|
|
fetchWithdrawal,
|
|
fetchWithdrawals,
|
|
setDefaultPaymentAccount,
|
|
updatePaymentAccount,
|
|
} from '@/features/wallet/api/withdrawal'
|
|
import { fetchAdminPayments } from '@/features/admin/api/adminPayments'
|
|
|
|
describe('financial API clients', () => {
|
|
afterEach(() => {
|
|
sessionStorage.clear()
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('fetches wallet balance and paginated ledger with server data', async () => {
|
|
apiClient.get
|
|
.mockResolvedValueOnce({ data: { data: { available_balance_cent: 100 } } })
|
|
.mockResolvedValueOnce({ data: { data: { items: [], total: 0, page: 2, page_size: 10 } } })
|
|
|
|
await expect(fetchWalletBalance()).resolves.toEqual({ available_balance_cent: 100 })
|
|
await expect(fetchWalletLedger(2, 10)).resolves.toEqual({
|
|
items: [],
|
|
total: 0,
|
|
page: 2,
|
|
page_size: 10,
|
|
})
|
|
expect(apiClient.get).toHaveBeenNthCalledWith(2, '/wallet/ledger', {
|
|
params: { page: 2, page_size: 10 },
|
|
})
|
|
})
|
|
|
|
it('supports payment-account CRUD and default selection', async () => {
|
|
const account = { id: 7, account_type: 'alipay' }
|
|
apiClient.get
|
|
.mockResolvedValueOnce({ data: { data: { items: [account], total: 1 } } })
|
|
.mockResolvedValueOnce({ data: { data: account } })
|
|
apiClient.post.mockResolvedValueOnce({ data: { data: account } })
|
|
apiClient.put.mockResolvedValueOnce({ data: { data: account } })
|
|
apiClient.delete.mockResolvedValueOnce({ data: { data: { deleted: true } } })
|
|
apiClient.post.mockResolvedValueOnce({ data: { data: { updated: true } } })
|
|
|
|
await expect(fetchPaymentAccounts()).resolves.toMatchObject({ items: [account], total: 1 })
|
|
await expect(fetchPaymentAccount(7)).resolves.toEqual(account)
|
|
await expect(
|
|
createPaymentAccount({ account_type: 'alipay', account_name: 'A', account_no: 'x' })
|
|
).resolves.toEqual(account)
|
|
await expect(updatePaymentAccount(7, { is_default: true })).resolves.toEqual(account)
|
|
await expect(deletePaymentAccount(7)).resolves.toEqual({ deleted: true })
|
|
await expect(setDefaultPaymentAccount(7)).resolves.toEqual({ updated: true })
|
|
})
|
|
|
|
it('supports withdrawal lifecycle API calls', async () => {
|
|
const withdrawal = { id: 9, status: 'pending' }
|
|
apiClient.post
|
|
.mockResolvedValueOnce({ data: { data: withdrawal } })
|
|
.mockResolvedValueOnce({ data: { data: { cancelled: true } } })
|
|
apiClient.get
|
|
.mockResolvedValueOnce({ data: { data: { items: [withdrawal], total: 1 } } })
|
|
.mockResolvedValueOnce({ data: { data: withdrawal } })
|
|
|
|
await expect(createWithdrawal({ payment_account_id: 7, amount_cent: 1000 })).resolves.toEqual(
|
|
withdrawal
|
|
)
|
|
await expect(fetchWithdrawals(1, 20)).resolves.toMatchObject({ items: [withdrawal] })
|
|
await expect(fetchWithdrawal(9)).resolves.toEqual(withdrawal)
|
|
await expect(cancelWithdrawal(9)).resolves.toEqual({ cancelled: true })
|
|
})
|
|
|
|
it('drops explicit legacy site codes from tenant withdrawal payloads', async () => {
|
|
sessionStorage.setItem('hfb_tenant_code', 'jieyou')
|
|
apiClient.post.mockResolvedValueOnce({ data: { data: { id: 10 } } })
|
|
|
|
await createWithdrawal(
|
|
{
|
|
payment_account_id: 7,
|
|
amount_cent: 1000,
|
|
distribution_site_code: 'legacy_jieyou',
|
|
},
|
|
'legacy_override'
|
|
)
|
|
|
|
expect(apiClient.post).toHaveBeenLastCalledWith('/withdrawals', {
|
|
payment_account_id: 7,
|
|
amount_cent: 1000,
|
|
})
|
|
})
|
|
|
|
it('filters empty admin-payment query values and normalizes pagination', async () => {
|
|
apiClient.get.mockResolvedValueOnce({ data: { data: undefined } })
|
|
|
|
await expect(
|
|
fetchAdminPayments({ status: '', provider: 'mock', page: 3, silent: true })
|
|
).resolves.toEqual({ items: [], total: 0, page: 3, page_size: 20 })
|
|
expect(apiClient.get).toHaveBeenLastCalledWith('/admin/payments', {
|
|
params: { provider: 'mock', page: 3 },
|
|
silent: true,
|
|
})
|
|
})
|
|
})
|