Initial commit
This commit is contained in:
137
api/utils/__tests__/response.test.ts
Normal file
137
api/utils/__tests__/response.test.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { successResponse, errorResponse } from '../response'
|
||||
import type { Response } from 'express'
|
||||
|
||||
vi.mock('express', () => ({
|
||||
default: {},
|
||||
}))
|
||||
|
||||
describe('successResponse', () => {
|
||||
it('应返回正确格式的成功响应(默认状态码)', () => {
|
||||
const mockRes = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn().mockReturnThis(),
|
||||
} as unknown as Response
|
||||
|
||||
const data = { message: '操作成功' }
|
||||
successResponse(mockRes, data)
|
||||
|
||||
expect(mockRes.status).toHaveBeenCalledWith(200)
|
||||
expect(mockRes.json).toHaveBeenCalledWith({
|
||||
success: true,
|
||||
data: { message: '操作成功' },
|
||||
})
|
||||
})
|
||||
|
||||
it('应使用自定义状态码', () => {
|
||||
const mockRes = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn().mockReturnThis(),
|
||||
} as unknown as Response
|
||||
|
||||
const data = { id: 123 }
|
||||
successResponse(mockRes, data, 201)
|
||||
|
||||
expect(mockRes.status).toHaveBeenCalledWith(201)
|
||||
expect(mockRes.json).toHaveBeenCalledWith({
|
||||
success: true,
|
||||
data: { id: 123 },
|
||||
})
|
||||
})
|
||||
|
||||
it('应正确处理数组数据', () => {
|
||||
const mockRes = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn().mockReturnThis(),
|
||||
} as unknown as Response
|
||||
|
||||
const data = [1, 2, 3]
|
||||
successResponse(mockRes, data)
|
||||
|
||||
expect(mockRes.json).toHaveBeenCalledWith({
|
||||
success: true,
|
||||
data: [1, 2, 3],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('errorResponse', () => {
|
||||
it('应返回正确格式的错误响应', () => {
|
||||
const mockRes = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn().mockReturnThis(),
|
||||
} as unknown as Response
|
||||
|
||||
errorResponse(mockRes, 400, 'BAD_REQUEST', '请求参数错误')
|
||||
|
||||
expect(mockRes.status).toHaveBeenCalledWith(400)
|
||||
expect(mockRes.json).toHaveBeenCalledWith({
|
||||
success: false,
|
||||
error: {
|
||||
code: 'BAD_REQUEST',
|
||||
message: '请求参数错误',
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it('应在非生产环境包含 details', () => {
|
||||
const mockRes = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn().mockReturnThis(),
|
||||
} as unknown as Response
|
||||
|
||||
const details = { field: 'username', reason: 'required' }
|
||||
errorResponse(mockRes, 422, 'VALIDATION_ERROR', '验证失败', details)
|
||||
|
||||
expect(mockRes.json).toHaveBeenCalledWith({
|
||||
success: false,
|
||||
error: {
|
||||
code: 'VALIDATION_ERROR',
|
||||
message: '验证失败',
|
||||
details: { field: 'username', reason: 'required' },
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it('应在生产环境不包含 details', () => {
|
||||
const originalEnv = process.env.NODE_ENV
|
||||
process.env.NODE_ENV = 'production'
|
||||
|
||||
const mockRes = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn().mockReturnThis(),
|
||||
} as unknown as Response
|
||||
|
||||
const details = { field: 'username', reason: 'required' }
|
||||
errorResponse(mockRes, 422, 'VALIDATION_ERROR', '验证失败', details)
|
||||
|
||||
expect(mockRes.json).toHaveBeenCalledWith({
|
||||
success: false,
|
||||
error: {
|
||||
code: 'VALIDATION_ERROR',
|
||||
message: '验证失败',
|
||||
details: undefined,
|
||||
},
|
||||
})
|
||||
|
||||
process.env.NODE_ENV = originalEnv
|
||||
})
|
||||
|
||||
it('应正确处理不带 details 的错误响应', () => {
|
||||
const mockRes = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
json: vi.fn().mockReturnThis(),
|
||||
} as unknown as Response
|
||||
|
||||
errorResponse(mockRes, 404, 'NOT_FOUND', '资源不存在')
|
||||
|
||||
expect(mockRes.status).toHaveBeenCalledWith(404)
|
||||
expect(mockRes.json).toHaveBeenCalledWith({
|
||||
success: false,
|
||||
error: {
|
||||
code: 'NOT_FOUND',
|
||||
message: '资源不存在',
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user