dify/web/app/components/base/copy-icon/__tests__/index.spec.tsx
BrianWang1990 9308287fea
fix: copy button not working on API Server and API Key pages (#34515)
Co-authored-by: Brian Wang <BrianWang1990@users.noreply.github.com>
Co-authored-by: test <test@testdeMac-mini.local>
Co-authored-by: BrianWang1990 <512dabing99@163.com>
Co-authored-by: Stephen Zhou <hi@hyoban.cc>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
2026-04-09 02:49:40 +00:00

50 lines
1.4 KiB
TypeScript

import { fireEvent, render, screen } from '@testing-library/react'
import CopyIcon from '..'
const copy = vi.fn()
const reset = vi.fn()
let copied = false
vi.mock('@/hooks/use-clipboard', () => ({
useClipboard: () => ({
copy,
reset,
copied,
}),
}))
describe('copy icon component', () => {
beforeEach(() => {
vi.resetAllMocks()
copied = false
})
it('renders normally', () => {
render(<CopyIcon content="this is some test content for the copy icon component" />)
const icon = screen.getByTestId('copy-icon')
expect(icon).toBeInTheDocument()
})
it('shows copy check icon when copied', () => {
copied = true
render(<CopyIcon content="this is some test content for the copy icon component" />)
const icon = screen.getByTestId('copied-icon')
expect(icon).toBeInTheDocument()
})
it('handles copy when clicked', () => {
render(<CopyIcon content="this is some test content for the copy icon component" />)
const icon = screen.getByTestId('copy-icon')
fireEvent.click(icon as Element)
expect(copy).toBeCalledTimes(1)
})
it('resets on mouse leave', () => {
render(<CopyIcon content="this is some test content for the copy icon component" />)
const icon = screen.getByTestId('copy-icon')
const div = icon?.parentElement as HTMLElement
fireEvent.mouseLeave(div)
expect(reset).toBeCalledTimes(1)
})
})