Files
JayuCloud/JC-Salon-Frontend/tests/api/vnext-api-contracts.test.ts
T

124 lines
4.8 KiB
TypeScript

// @vitest-environment jsdom
import type { AxiosAdapter, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const messageError = vi.hoisted(() => vi.fn())
vi.mock('ant-design-vue', () => ({
message: {
error: messageError
}
}))
import adminApi, {
adminAfterSaleApi,
adminArtistScheduleApi,
adminAuthApi,
adminDashboardApi,
adminInventoryApi,
adminWorkbenchRegistrationApi
} from '../../src/api/admin'
import api from '../../src/api'
import { afterSaleApi } from '../../src/api/order'
type CapturedRequest = {
method?: string
url?: string
params?: any
data?: any
}
function okResponse(config: InternalAxiosRequestConfig): AxiosResponse {
return {
data: {
code: 200,
message: 'ok',
data: {}
},
status: 200,
statusText: 'OK',
headers: {},
config,
request: {}
}
}
function parseData(data: unknown) {
return typeof data === 'string' && data ? JSON.parse(data) : data
}
function captureAdapter(requests: CapturedRequest[]): AxiosAdapter {
return (config: InternalAxiosRequestConfig) => {
requests.push({
method: config.method,
url: config.url,
params: config.params,
data: parseData(config.data)
})
return Promise.resolve(okResponse(config))
}
}
describe('vNext API contracts', () => {
const originalAdminAdapter = adminApi.defaults.adapter
const originalApiAdapter = api.defaults.adapter
beforeEach(() => {
messageError.mockClear()
localStorage.clear()
sessionStorage.clear()
})
afterEach(() => {
adminApi.defaults.adapter = originalAdminAdapter
api.defaults.adapter = originalApiAdapter
localStorage.clear()
sessionStorage.clear()
})
it('uses backend admin vNext endpoints', async () => {
const requests: CapturedRequest[] = []
adminApi.defaults.adapter = captureAdapter(requests)
await adminArtistScheduleApi.listSchedules({ artistId: 9, startDate: '2026-06-01', endDate: '2026-06-07' })
await adminArtistScheduleApi.saveSchedule({ artistId: 9, workDate: '2026-06-01', startTime: '09:00', endTime: '18:00' })
await adminAfterSaleApi.listAfterSales({ status: 'APPLIED', page: 1, size: 20 })
await adminAfterSaleApi.approveAfterSale(5, { auditReason: 'approved' })
await adminInventoryApi.saveItem({ itemName: 'Base Gel', category: 'gel', unit: 'bottle', stockQuantity: 12 })
await adminInventoryApi.adjustStock(7, { changeType: 'IN', changeQuantity: 3, remark: 'restock' })
await adminDashboardApi.getOverview({ date: '2026-10-25', days: 7 })
await adminAuthApi.changePassword({ currentPassword: 'OldPass1!', newPassword: 'FreshPass2!' })
await adminWorkbenchRegistrationApi.list()
await adminWorkbenchRegistrationApi.approve(12)
await adminWorkbenchRegistrationApi.reject(12, '资料不完整')
expect(requests).toEqual([
{ method: 'get', url: '/artist-schedule/list', params: { artistId: 9, startDate: '2026-06-01', endDate: '2026-06-07' }, data: undefined },
{ method: 'post', url: '/artist-schedule/save', params: undefined, data: { artistId: 9, workDate: '2026-06-01', startTime: '09:00', endTime: '18:00' } },
{ method: 'get', url: '/after-sale/list', params: { status: 'APPLIED', page: 1, size: 20 }, data: undefined },
{ method: 'put', url: '/after-sale/5/approve', params: undefined, data: { auditReason: 'approved' } },
{ method: 'post', url: '/inventory/save', params: undefined, data: { itemName: 'Base Gel', category: 'gel', unit: 'bottle', stockQuantity: 12 } },
{ method: 'post', url: '/inventory/7/adjust', params: undefined, data: { changeType: 'IN', changeQuantity: 3, remark: 'restock' } },
{ method: 'get', url: '/dashboard/overview', params: { date: '2026-10-25', days: 7 }, data: undefined },
{ method: 'post', url: '/auth/password', params: undefined, data: { currentPassword: 'OldPass1!', newPassword: 'FreshPass2!' } },
{ method: 'get', url: '/user/workbench/registration/list', params: undefined, data: undefined },
{ method: 'post', url: '/user/workbench/registration/12/approve', params: undefined, data: undefined },
{ method: 'post', url: '/user/workbench/registration/12/reject', params: undefined, data: { reason: '资料不完整' } }
])
})
it('uses backend user vNext endpoints', async () => {
const requests: CapturedRequest[] = []
api.defaults.adapter = captureAdapter(requests)
await afterSaleApi.apply({ orderId: 42, type: 'REFUND', reason: 'schedule changed', refundAmount: 88 })
await afterSaleApi.listMy({ page: 1, size: 5 })
expect(requests).toEqual([
{ method: 'post', url: '/after-sale/apply', params: undefined, data: { orderId: 42, type: 'REFUND', reason: 'schedule changed', refundAmount: 88 } },
{ method: 'get', url: '/after-sale/my', params: { page: 1, size: 5 }, data: undefined }
])
})
})