dify/web/app/components/base/image-uploader/__tests__/utils.spec.ts
FFXN 0e320290e1
feat: evaluation (#35353)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: jyong <718720800@qq.com>
Co-authored-by: Yansong Zhang <916125788@qq.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: hj24 <mambahj24@gmail.com>
Co-authored-by: hj24 <huangjian@dify.ai>
Co-authored-by: Joel <iamjoel007@gmail.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
Co-authored-by: CodingOnStar <hanxujiang@dify.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: 非法操作 <hjlarry@163.com>
Co-authored-by: Ayush Baluni <73417844+aayushbaluni@users.noreply.github.com>
Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
Co-authored-by: jimcody1995 <jjimcody@gmail.com>
Co-authored-by: James <63717587+jamesrayammons@users.noreply.github.com>
Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai>
Co-authored-by: Stephen Zhou <hi@hyoban.cc>
Co-authored-by: Coding On Star <447357187@qq.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: jerryzai <jerryzh8710@protonmail.com>
Co-authored-by: NVIDIAN <speedy.hpc@hotmail.com>
Co-authored-by: ai-hpc <ai-hpc@users.noreply.github.com>
Co-authored-by: Asuka Minato <i@asukaminato.eu.org>
Co-authored-by: Junghwan <70629228+shaun0927@users.noreply.github.com>
Co-authored-by: HeYinKazune <70251095+HeYin-OS@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: yyh <yuanyouhuilyz@gmail.com>
Co-authored-by: Jingyi <jingyi.qi@dify.ai>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: sxxtony <166789813+sxxtony@users.noreply.github.com>
2026-04-17 16:37:21 +08:00

135 lines
4.7 KiB
TypeScript

import type { TFunction } from 'i18next'
import { waitFor } from '@testing-library/react'
import { upload } from '@/service/base'
import { getImageUploadErrorMessage, imageUpload } from '../utils'
vi.mock('@/service/base', () => ({
upload: vi.fn(),
}))
describe('image-uploader utils', () => {
beforeEach(() => {
vi.clearAllMocks()
})
describe('getImageUploadErrorMessage', () => {
it('should return backend message when error code is forbidden', () => {
const t = vi.fn() as unknown as TFunction
const result = getImageUploadErrorMessage(
{ response: { code: 'forbidden', message: 'Forbidden by policy' } },
'Default error',
t,
)
expect(result).toBe('Forbidden by policy')
expect(t).not.toHaveBeenCalled()
})
it('should return translated message when error code is file_extension_blocked', () => {
const t = vi.fn(() => 'common.fileUploader.fileExtensionBlocked') as unknown as TFunction
const result = getImageUploadErrorMessage(
{ response: { code: 'file_extension_blocked' } },
'Default error',
t,
)
expect(result).toBe('common.fileUploader.fileExtensionBlocked')
expect(t).toHaveBeenCalledWith('fileUploader.fileExtensionBlocked', { ns: 'common' })
})
it('should return default message when error code is unknown', () => {
const t = vi.fn() as unknown as TFunction
const result = getImageUploadErrorMessage(
{ response: { code: 'unexpected_error' } },
'Default error',
t,
)
expect(result).toBe('Default error')
expect(t).not.toHaveBeenCalled()
})
it('should return default message when error is missing response code', () => {
const t = vi.fn() as unknown as TFunction
const result = getImageUploadErrorMessage(undefined, 'Default error', t)
expect(result).toBe('Default error')
expect(t).not.toHaveBeenCalled()
})
})
describe('imageUpload', () => {
const createCallbacks = () => ({
onProgressCallback: vi.fn<(progress: number) => void>(),
onSuccessCallback: vi.fn<(res: { id: string }) => void>(),
onErrorCallback: vi.fn<(error?: unknown) => void>(),
})
it('should upload file and call success callback', async () => {
const file = new File(['hello'], 'image.png', { type: 'image/png' })
const callbacks = createCallbacks()
vi.mocked(upload).mockResolvedValue({ id: 'uploaded-id' })
imageUpload({ file, ...callbacks }, true, '/files/upload')
expect(upload).toHaveBeenCalledTimes(1)
const [options, isPublic, url] = (vi.mocked(upload).mock.calls[0] ?? []) as [any, any, any]
expect(isPublic).toBe(true)
expect(url).toBe('/files/upload')
expect(options.xhr).toBeInstanceOf(XMLHttpRequest)
expect(options.data).toBeInstanceOf(FormData)
expect((options.data as FormData).get('file')).toBe(file)
await waitFor(() => {
expect(callbacks.onSuccessCallback).toHaveBeenCalledWith({ id: 'uploaded-id' })
})
expect(callbacks.onErrorCallback).not.toHaveBeenCalled()
})
it('should call error callback when upload fails', async () => {
const file = new File(['hello'], 'image.png', { type: 'image/png' })
const callbacks = createCallbacks()
const error = new Error('Upload failed')
vi.mocked(upload).mockRejectedValue(error)
imageUpload({ file, ...callbacks })
await waitFor(() => {
expect(callbacks.onErrorCallback).toHaveBeenCalledWith(error)
})
expect(callbacks.onSuccessCallback).not.toHaveBeenCalled()
})
it('should report progress percentage when progress is computable', () => {
const file = new File(['hello'], 'image.png', { type: 'image/png' })
const callbacks = createCallbacks()
vi.mocked(upload).mockImplementation((options: { onprogress?: (e: ProgressEvent) => void }) => {
options.onprogress?.({ lengthComputable: true, loaded: 5, total: 8 } as ProgressEvent)
return Promise.resolve({ id: 'uploaded-id' })
})
imageUpload({ file, ...callbacks })
expect(callbacks.onProgressCallback).toHaveBeenCalledWith(62)
})
it('should not report progress when length is not computable', () => {
const file = new File(['hello'], 'image.png', { type: 'image/png' })
const callbacks = createCallbacks()
vi.mocked(upload).mockImplementation((options: { onprogress?: (e: ProgressEvent) => void }) => {
options.onprogress?.({ lengthComputable: false, loaded: 5, total: 8 } as ProgressEvent)
return Promise.resolve({ id: 'uploaded-id' })
})
imageUpload({ file, ...callbacks })
expect(callbacks.onProgressCallback).not.toHaveBeenCalled()
})
})
})