66 lines
2.2 KiB
TypeScript

/** @jest-environment node */
import { NextRequest } from 'next/server'
import { getServerSession } from 'next-auth'
import { prisma } from '@/lib/prisma'
import { POST } from './route'
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
jest.mock('@/lib/prisma', () => ({
prisma: {
user: { findUnique: jest.fn() },
material: { create: jest.fn() },
},
}))
const input = {
productModel: '',
reportTitle: '板材检测报告',
reportKind: '原材料',
productCategoryName: '板材',
platformName: '品牌',
certificateTestedOn: '2027-09-23',
certificateNumber: 'CMA-2026-001',
certificateUsageInstructions: '使用前核对送检主体',
reportUsageRules: '完整说明\n第二行',
}
function request(body: unknown) {
return new NextRequest('http://localhost/api/materials/upload/certificate-report/init', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(body),
})
}
describe('检测报告初始化', () => {
beforeEach(() => {
jest.clearAllMocks()
;(getServerSession as jest.Mock).mockResolvedValue({ user: { id: 'user-1' } })
;(prisma.user.findUnique as jest.Mock).mockResolvedValue({ username: '上传人' })
;(prisma.material.create as jest.Mock).mockResolvedValue({ id: 'report-1' })
})
test('原材料的类别、到期日、编号和完整说明保存到同一份报告', async () => {
const response = await POST(request(input))
expect(response.status).toBe(200)
expect(prisma.material.create).toHaveBeenCalledWith({
data: expect.objectContaining({
reportKind: '原材料',
certificateTestedOn: new Date('2027-09-23T00:00:00.000Z'),
certificateNumber: 'CMA-2026-001',
certificateUsageInstructions: '使用前核对送检主体',
reportUsageRules: '完整说明\n第二行',
platformName: '品牌',
}),
})
})
test('原材料没有报告名称时拒绝创建素材壳', async () => {
const response = await POST(request({ ...input, reportTitle: '' }))
expect(response.status).toBe(400)
expect(prisma.material.create).not.toHaveBeenCalled()
})
})