gongxiangp2/components/upload/ProductPackageUploader.test.tsx
2026-05-07 16:37:22 +08:00

93 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import ProductPackageUploader from '@/components/upload/ProductPackageUploader'
jest.mock('@/components/upload/DetailImageSorter', () => ({
__esModule: true,
default: () => <div data-testid="detail-image-sorter" />,
}))
jest.mock('@/components/upload/UploadForm', () => ({
__esModule: true,
default: () => <div data-testid="upload-form" />,
}))
jest.mock('@/components/upload/category-handlers', () => ({
addCategory: jest.fn(),
addScene: jest.fn(),
}))
const defaultProps = {
onSuccess: jest.fn(),
onCancel: jest.fn(),
categories: [],
scenes: [],
platforms: [],
productCategories: [],
onRefreshCategories: jest.fn().mockResolvedValue(undefined),
onRefreshScenes: jest.fn().mockResolvedValue(undefined),
}
describe('ProductPackageUploader', () => {
beforeEach(() => {
jest.clearAllMocks()
Object.defineProperty(URL, 'createObjectURL', {
configurable: true,
writable: true,
value: jest.fn(() => 'blob:mock'),
})
Object.defineProperty(URL, 'revokeObjectURL', {
configurable: true,
writable: true,
value: jest.fn(),
})
})
test('任务-详情源文件选择框支持 psb/tif/tiff', async () => {
const user = userEvent.setup()
render(<ProductPackageUploader {...defaultProps} />)
const input = screen.getByLabelText(/PSD源文件/) as HTMLInputElement
expect(input.accept).toBe('.psd,.psb,.tif,.tiff,.zip,.rar,.7z')
const files = [
new File(['a'], 'sample.psb', { type: 'application/octet-stream' }),
new File(['b'], 'sample.tif', { type: 'image/tiff' }),
new File(['c'], 'sample.tiff', { type: 'image/tiff' }),
]
await user.upload(input, files)
expect(screen.getByText('sample.psb')).toBeInTheDocument()
expect(screen.getByText('sample.tif')).toBeInTheDocument()
expect(screen.getByText('sample.tiff')).toBeInTheDocument()
})
test('空的任务-详情源文件会被直接拦截并提示', async () => {
const user = userEvent.setup()
render(<ProductPackageUploader {...defaultProps} />)
const input = screen.getByLabelText(/PSD源文件/) as HTMLInputElement
const emptyFile = new File([], 'empty.psd', { type: 'application/octet-stream' })
await user.upload(input, emptyFile)
expect(screen.getByText('以下文件为空,无法上传:empty.psd')).toBeInTheDocument()
expect(screen.queryByText('empty.psd')).not.toBeInTheDocument()
})
test('应提供任务详情出图方式选择项,且默认未设置', () => {
render(<ProductPackageUploader {...defaultProps} />)
const select = screen.getByLabelText(/出图方式/) as HTMLSelectElement
expect(select.value).toBe('')
expect(screen.getByRole('option', { name: '未设置' })).toBeInTheDocument()
expect(screen.getByRole('option', { name: '实拍' })).toBeInTheDocument()
expect(screen.getByRole('option', { name: '渲染' })).toBeInTheDocument()
expect(screen.getByRole('option', { name: 'AI' })).toBeInTheDocument()
})
})