gongxiangp2/components/upload/StandardModelUploaders.drag-drop.test.tsx
2026-07-24 18:15:27 +08:00

113 lines
4.3 KiB
TypeScript

import { createEvent, fireEvent, render, screen } from '@testing-library/react'
import StandardModelSkuZipUpload from '@/components/upload/StandardModelSkuZipUpload'
import StandardModelWhiteBackgroundUpload from '@/components/upload/StandardModelWhiteBackgroundUpload'
describe('标准模型拖拽上传', () => {
test('模型压缩包拖入非法格式时应显示错误且不替换文件', () => {
const onSelectFile = jest.fn()
const { container } = render(
<StandardModelSkuZipUpload
selectedFile={null}
onSelectFile={onSelectFile}
onRemoveFile={jest.fn()}
/>,
)
const dropZone = container.querySelector('label[for="standard-model-sku-zip-upload"]') as HTMLLabelElement
const invalidFile = new File(['text'], 'readme.txt', { type: 'text/plain' })
fireEvent.drop(dropZone, {
dataTransfer: { files: [invalidFile], types: ['Files'], dropEffect: 'copy' },
})
expect(onSelectFile).not.toHaveBeenCalled()
expect(screen.getByText('压缩包仅支持 ZIP、RAR、7Z 格式')).toBeInTheDocument()
})
test('模型压缩包上传中不应接受新的拖入文件', () => {
const onSelectFile = jest.fn()
const { container } = render(
<StandardModelSkuZipUpload
selectedFile={null}
uploading
onSelectFile={onSelectFile}
onRemoveFile={jest.fn()}
/>,
)
const dropZone = container.querySelector('label[for="standard-model-sku-zip-upload"]') as HTMLLabelElement
const zipFile = new File(['zip'], 'model.zip', { type: 'application/zip' })
fireEvent.drop(dropZone, {
dataTransfer: { files: [zipFile], types: ['Files'], dropEffect: 'copy' },
})
expect(onSelectFile).not.toHaveBeenCalled()
expect(screen.getByText('文件正在上传,请等待完成后再更换')).toBeInTheDocument()
})
test('白底图拖拽经过槽位内部元素时应保持高亮,离开槽位后清除', () => {
const { container } = render(
<StandardModelWhiteBackgroundUpload
imageUrls={{}}
otherImages={[]}
onSelectImage={jest.fn()}
onRemoveImage={jest.fn()}
onSelectOtherImages={jest.fn()}
onRemoveOtherImage={jest.fn()}
/>,
)
const dropZone = container.querySelector('label[for="white-bg-front"]') as HTMLLabelElement
const child = dropZone.querySelector('span') as HTMLSpanElement
fireEvent.dragEnter(dropZone, {
dataTransfer: { files: [], types: ['Files'], dropEffect: 'none' },
})
expect(dropZone).toHaveClass('border-blue-500')
const internalDragLeave = createEvent.dragLeave(dropZone)
Object.defineProperties(internalDragLeave, {
relatedTarget: { value: child },
dataTransfer: { value: { files: [], types: ['Files'], dropEffect: 'copy' } },
})
fireEvent(dropZone, internalDragLeave)
expect(dropZone).toHaveClass('border-blue-500')
const outerDragLeave = createEvent.dragLeave(dropZone)
Object.defineProperties(outerDragLeave, {
relatedTarget: { value: document.body },
dataTransfer: { value: { files: [], types: ['Files'], dropEffect: 'copy' } },
})
fireEvent(dropZone, outerDragLeave)
expect(dropZone).not.toHaveClass('border-blue-500')
})
test('白底图上传中不应接受固定角度或其他图片拖入', () => {
const onSelectImage = jest.fn()
const onSelectOtherImages = jest.fn()
const { container } = render(
<StandardModelWhiteBackgroundUpload
imageUrls={{}}
otherImages={[]}
uploading
onSelectImage={onSelectImage}
onRemoveImage={jest.fn()}
onSelectOtherImages={onSelectOtherImages}
onRemoveOtherImage={jest.fn()}
/>,
)
const imageFile = new File(['image'], 'front.png', { type: 'image/png' })
const fixedDropZone = container.querySelector('label[for="white-bg-front"]') as HTMLLabelElement
const otherDropZone = container.querySelector('label[for="white-bg-other-images"]') as HTMLLabelElement
fireEvent.drop(fixedDropZone, {
dataTransfer: { files: [imageFile], types: ['Files'], dropEffect: 'none' },
})
fireEvent.drop(otherDropZone, {
dataTransfer: { files: [imageFile], types: ['Files'], dropEffect: 'none' },
})
expect(onSelectImage).not.toHaveBeenCalled()
expect(onSelectOtherImages).not.toHaveBeenCalled()
})
})