88 lines
3.3 KiB
TypeScript
88 lines
3.3 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',
|
||
certificateExpiresOn: '2028-09-23',
|
||
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',
|
||
certificateExpiresOn: new Date('2028-09-23T00:00:00.000Z'),
|
||
reportUsageRules: '完整说明\n第二行',
|
||
platformName: '品牌',
|
||
}),
|
||
})
|
||
})
|
||
|
||
test('原材料没有报告名称时拒绝创建素材壳', async () => {
|
||
const response = await POST(request({ ...input, reportTitle: '' }))
|
||
expect(response.status).toBe(400)
|
||
expect(prisma.material.create).not.toHaveBeenCalled()
|
||
})
|
||
|
||
test('成品即使有 SPU,上传时也必须填写报告名称', async () => {
|
||
const response = await POST(request({ ...input, reportKind: '成品', productModel: 'SPU-001', reportTitle: ' ' }))
|
||
expect(response.status).toBe(400)
|
||
await expect(response.json()).resolves.toEqual({ error: '请填写报告名称 / 检测对象' })
|
||
expect(prisma.material.create).not.toHaveBeenCalled()
|
||
})
|
||
|
||
test('到期时间早于检测日期时不写入素材', async () => {
|
||
const response = await POST(request({ ...input, certificateExpiresOn: '2027-09-22' }))
|
||
expect(response.status).toBe(400)
|
||
expect(prisma.material.create).not.toHaveBeenCalled()
|
||
})
|
||
|
||
test.each(['', undefined, null])('到期时间留空时写入按检测日期计算的日期 (%s)', async (certificateExpiresOn) => {
|
||
const response = await POST(request({ ...input, certificateTestedOn: '2024-02-29', certificateExpiresOn }))
|
||
expect(response.status).toBe(200)
|
||
expect(prisma.material.create).toHaveBeenCalledWith({ data: expect.objectContaining({
|
||
certificateTestedOn: new Date('2024-02-29T00:00:00.000Z'),
|
||
certificateExpiresOn: new Date('2025-02-28T00:00:00.000Z'),
|
||
}) })
|
||
})
|
||
})
|