51 lines
2.3 KiB
TypeScript
51 lines
2.3 KiB
TypeScript
/** @jest-environment node */
|
|
|
|
import { NextRequest } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { getPerformanceConversionTimeseries, 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 {
|
|
getPerformanceConversionTimeseries: 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/timeseries', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
;(getServerSession as jest.Mock).mockResolvedValue({ user: { id: 'manager-1' } })
|
|
;(getPerformanceConversionTimeseries as jest.Mock).mockResolvedValue({ daily: [] })
|
|
})
|
|
|
|
it('passes the workflow and product id pair to the service', async () => {
|
|
const response = await GET(new NextRequest('http://localhost/api/department-dashboard/performance-conversion/timeseries?workflowId=flow-1&productId=1077434823611'))
|
|
expect(response.status).toBe(200)
|
|
expect(getPerformanceConversionTimeseries).toHaveBeenCalledWith(
|
|
{ user: { id: 'manager-1' } },
|
|
{ workflowId: 'flow-1', productId: '1077434823611' },
|
|
)
|
|
})
|
|
|
|
it('preserves authorization-style service status codes', async () => {
|
|
;(getPerformanceConversionTimeseries as jest.Mock).mockRejectedValue(new PerformanceConversionError('商品 ID 不属于该流程', 403))
|
|
const response = await GET(new NextRequest('http://localhost/api/department-dashboard/performance-conversion/timeseries?workflowId=flow-1&productId=999999'))
|
|
expect(response.status).toBe(403)
|
|
await expect(response.json()).resolves.toEqual({ success: false, error: '商品 ID 不属于该流程' })
|
|
})
|
|
})
|