dify/web/app/components/base/image-uploader/__tests__/image-link-input.spec.tsx
Saumya Talwani f50e44b24a
test: improve coverage for some test files (#32916)
Signed-off-by: edvatar <88481784+toroleapinc@users.noreply.github.com>
Signed-off-by: -LAN- <laipz8200@outlook.com>
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: majiayu000 <1835304752@qq.com>
Co-authored-by: Poojan <poojan@infocusp.com>
Co-authored-by: sahil-infocusp <73810410+sahil-infocusp@users.noreply.github.com>
Co-authored-by: 非法操作 <hjlarry@163.com>
Co-authored-by: Pandaaaa906 <ye.pandaaaa906@gmail.com>
Co-authored-by: Asuka Minato <i@asukaminato.eu.org>
Co-authored-by: heyszt <270985384@qq.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Ijas <ijas.ahmd.ap@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: 木之本澪 <kinomotomiovo@gmail.com>
Co-authored-by: KinomotoMio <200703522+KinomotoMio@users.noreply.github.com>
Co-authored-by: 不做了睡大觉 <64798754+stakeswky@users.noreply.github.com>
Co-authored-by: User <user@example.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: edvatar <88481784+toroleapinc@users.noreply.github.com>
Co-authored-by: -LAN- <laipz8200@outlook.com>
Co-authored-by: Leilei <138381132+Inlei@users.noreply.github.com>
Co-authored-by: HaKu <104669497+haku-ink@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: wangxiaolei <fatelei@gmail.com>
Co-authored-by: Varun Chawla <34209028+veeceey@users.noreply.github.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
Co-authored-by: yyh <yuanyouhuilyz@gmail.com>
Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
Co-authored-by: tda <95275462+tda1017@users.noreply.github.com>
Co-authored-by: root <root@DESKTOP-KQLO90N>
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Co-authored-by: Niels Kaspers <153818647+nielskaspers@users.noreply.github.com>
Co-authored-by: hj24 <mambahj24@gmail.com>
Co-authored-by: Tyson Cung <45380903+tysoncung@users.noreply.github.com>
Co-authored-by: Stephen Zhou <hi@hyoban.cc>
Co-authored-by: FFXN <31929997+FFXN@users.noreply.github.com>
Co-authored-by: slegarraga <64795732+slegarraga@users.noreply.github.com>
Co-authored-by: 99 <wh2099@pm.me>
Co-authored-by: Br1an <932039080@qq.com>
Co-authored-by: L1nSn0w <l1nsn0w@qq.com>
Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai>
Co-authored-by: akkoaya <151345394+akkoaya@users.noreply.github.com>
Co-authored-by: 盐粒 Yanli <yanli@dify.ai>
Co-authored-by: lif <1835304752@qq.com>
Co-authored-by: weiguang li <codingpunk@gmail.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
Co-authored-by: HanWenbo <124024253+hwb96@users.noreply.github.com>
Co-authored-by: Coding On Star <447357187@qq.com>
Co-authored-by: CodingOnStar <hanxujiang@dify.com>
Co-authored-by: Stable Genius <stablegenius043@gmail.com>
Co-authored-by: Stable Genius <259448942+stablegenius49@users.noreply.github.com>
Co-authored-by: ふるい <46769295+Echo0ff@users.noreply.github.com>
Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
2026-03-06 18:59:16 +08:00

187 lines
6.1 KiB
TypeScript

import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { TransferMethod } from '@/types/app'
import ImageLinkInput from '../image-link-input'
describe('ImageLinkInput', () => {
const defaultProps = {
onUpload: vi.fn(),
}
beforeEach(() => {
vi.clearAllMocks()
})
describe('Rendering', () => {
it('should render without crashing', () => {
render(<ImageLinkInput {...defaultProps} />)
expect(screen.getByRole('textbox')).toBeInTheDocument()
})
it('should render an input with placeholder text', () => {
render(<ImageLinkInput {...defaultProps} />)
const input = screen.getByRole('textbox')
expect(input).toHaveAttribute('placeholder')
expect(input).toHaveAttribute('type', 'text')
})
it('should render a submit button', () => {
render(<ImageLinkInput {...defaultProps} />)
expect(screen.getByRole('button')).toBeInTheDocument()
})
})
describe('Props', () => {
it('should disable the button when input is empty', () => {
render(<ImageLinkInput {...defaultProps} />)
expect(screen.getByRole('button')).toBeDisabled()
})
it('should disable the button when disabled prop is true', async () => {
const user = userEvent.setup()
render(<ImageLinkInput {...defaultProps} disabled />)
const input = screen.getByRole('textbox')
await user.type(input, 'https://example.com/image.png')
expect(screen.getByRole('button')).toBeDisabled()
})
it('should enable the button when input has text and not disabled', async () => {
const user = userEvent.setup()
render(<ImageLinkInput {...defaultProps} />)
const input = screen.getByRole('textbox')
await user.type(input, 'https://example.com/image.png')
expect(screen.getByRole('button')).toBeEnabled()
})
})
describe('User Interactions', () => {
it('should update input value when typing', async () => {
const user = userEvent.setup()
render(<ImageLinkInput {...defaultProps} />)
const input = screen.getByRole('textbox')
await user.type(input, 'https://example.com/image.png')
expect(input).toHaveValue('https://example.com/image.png')
})
it('should call onUpload with progress 0 when URL matches http/https/ftp pattern', async () => {
const user = userEvent.setup()
const onUpload = vi.fn()
render(<ImageLinkInput onUpload={onUpload} />)
const input = screen.getByRole('textbox')
await user.type(input, 'https://example.com/image.png')
await user.click(screen.getByRole('button'))
expect(onUpload).toHaveBeenCalledTimes(1)
expect(onUpload).toHaveBeenCalledWith(
expect.objectContaining({
type: TransferMethod.remote_url,
url: 'https://example.com/image.png',
progress: 0,
fileId: '',
}),
)
})
it('should call onUpload with progress -1 when URL does not match pattern', async () => {
const user = userEvent.setup()
const onUpload = vi.fn()
render(<ImageLinkInput onUpload={onUpload} />)
const input = screen.getByRole('textbox')
await user.type(input, 'not-a-valid-url')
await user.click(screen.getByRole('button'))
expect(onUpload).toHaveBeenCalledTimes(1)
expect(onUpload).toHaveBeenCalledWith(
expect.objectContaining({
progress: -1,
url: 'not-a-valid-url',
}),
)
})
it('should set progress 0 for http:// URLs', async () => {
const user = userEvent.setup()
const onUpload = vi.fn()
render(<ImageLinkInput onUpload={onUpload} />)
await user.type(screen.getByRole('textbox'), 'http://example.com/img.jpg')
await user.click(screen.getByRole('button'))
expect(onUpload).toHaveBeenCalledWith(
expect.objectContaining({ progress: 0 }),
)
})
it('should set progress 0 for ftp:// URLs', async () => {
const user = userEvent.setup()
const onUpload = vi.fn()
render(<ImageLinkInput onUpload={onUpload} />)
await user.type(screen.getByRole('textbox'), 'ftp://files.example.com/img.png')
await user.click(screen.getByRole('button'))
expect(onUpload).toHaveBeenCalledWith(
expect.objectContaining({ progress: 0 }),
)
})
it('should not call onUpload when disabled and button is clicked', async () => {
const user = userEvent.setup()
const onUpload = vi.fn()
render(<ImageLinkInput onUpload={onUpload} disabled />)
const input = screen.getByRole('textbox')
await user.type(input, 'https://example.com/image.png')
const button = screen.getByRole('button')
expect(button).toBeDisabled()
await user.click(button)
expect(onUpload).not.toHaveBeenCalled()
})
it('should include _id as a timestamp string in the uploaded file', async () => {
const user = userEvent.setup()
const onUpload = vi.fn()
const dateNowSpy = vi.spyOn(Date, 'now').mockReturnValue(1234567890)
render(<ImageLinkInput onUpload={onUpload} />)
await user.type(screen.getByRole('textbox'), 'https://example.com/img.png')
await user.click(screen.getByRole('button'))
expect(onUpload).toHaveBeenCalledWith(
expect.objectContaining({ _id: '1234567890' }),
)
dateNowSpy.mockRestore()
})
})
describe('Edge Cases', () => {
it('should handle empty string input without errors', () => {
render(<ImageLinkInput {...defaultProps} />)
const input = screen.getByRole('textbox')
expect(input).toHaveValue('')
expect(screen.getByRole('button')).toBeDisabled()
})
it('should handle URL-like strings without protocol prefix', async () => {
const user = userEvent.setup()
const onUpload = vi.fn()
render(<ImageLinkInput onUpload={onUpload} />)
await user.type(screen.getByRole('textbox'), 'example.com/image.png')
await user.click(screen.getByRole('button'))
expect(onUpload).toHaveBeenCalledWith(
expect.objectContaining({ progress: -1 }),
)
})
})
})