59 lines
2.5 KiB
TypeScript
59 lines
2.5 KiB
TypeScript
/** @jest-environment node */
|
|
|
|
import { NextRequest } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { getPerformanceConversionWorkflows, PerformanceConversionError } from '@/lib/performance-conversion'
|
|
|
|
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
|
|
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
|
|
jest.mock('@/lib/performance-conversion', () => {
|
|
class MockPerformanceConversionError extends Error {
|
|
status: number
|
|
constructor(message: string, status = 400) { super(message); this.status = status }
|
|
}
|
|
return {
|
|
getPerformanceConversionWorkflows: jest.fn(),
|
|
PerformanceConversionError: MockPerformanceConversionError,
|
|
}
|
|
})
|
|
jest.mock('@/lib/brand-center-dashboard-access', () => ({
|
|
BrandCenterDashboardAccessError: class BrandCenterDashboardAccessError extends Error {
|
|
status: number
|
|
constructor(message: string, status = 403) { super(message); this.status = status }
|
|
},
|
|
}))
|
|
|
|
const { GET } = require('./route') as typeof import('./route')
|
|
|
|
describe('GET /api/department-dashboard/performance-conversion/workflows', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
;(getServerSession as jest.Mock).mockResolvedValue({ user: { id: 'manager-1' } })
|
|
;(getPerformanceConversionWorkflows as jest.Mock).mockResolvedValue({ items: [], total: 0 })
|
|
})
|
|
|
|
it('passes bounded list inputs and returns private data', async () => {
|
|
const response = await GET(new NextRequest('http://localhost/api/department-dashboard/performance-conversion/workflows?brand=%E7%B4%A2%E8%8F%B2%E4%BA%9A&page=2&pageSize=20&q=1077&completedFrom=2026-07-01&completedTo=2026-07-31'))
|
|
expect(response.status).toBe(200)
|
|
expect(getPerformanceConversionWorkflows).toHaveBeenCalledWith(
|
|
{ user: { id: 'manager-1' } },
|
|
{
|
|
brand: '索菲亚',
|
|
page: 2,
|
|
pageSize: 20,
|
|
query: '1077',
|
|
completedFrom: '2026-07-01',
|
|
completedTo: '2026-07-31',
|
|
},
|
|
)
|
|
expect(response.headers.get('cache-control')).toContain('no-store')
|
|
})
|
|
|
|
it('maps service errors without leaking internals', async () => {
|
|
;(getPerformanceConversionWorkflows as jest.Mock).mockRejectedValue(new PerformanceConversionError('品牌参数无效', 400))
|
|
const response = await GET(new NextRequest('http://localhost/api/department-dashboard/performance-conversion/workflows?brand=bad'))
|
|
expect(response.status).toBe(400)
|
|
await expect(response.json()).resolves.toEqual({ success: false, error: '品牌参数无效' })
|
|
})
|
|
})
|