dify/web/app/components/base/copy-feedback/__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

99 lines
3.2 KiB
TypeScript

import { fireEvent, render, screen } from '@testing-library/react'
import CopyFeedback, { CopyFeedbackNew } from '..'
const mockCopy = vi.fn()
const mockReset = vi.fn()
let mockCopied = false
vi.mock('@/hooks/use-clipboard', () => ({
useClipboard: () => ({
copy: mockCopy,
reset: mockReset,
copied: mockCopied,
}),
}))
describe('CopyFeedback', () => {
beforeEach(() => {
mockCopied = false
vi.clearAllMocks()
})
describe('Rendering', () => {
it('renders the action button with copy icon', () => {
render(<CopyFeedback content="test content" />)
expect(screen.getByRole('button')).toBeInTheDocument()
})
it('renders the copied icon when copied is true', () => {
mockCopied = true
render(<CopyFeedback content="test content" />)
expect(screen.getByRole('button')).toBeInTheDocument()
})
})
describe('User Interactions', () => {
it('calls copy with content when clicked', () => {
render(<CopyFeedback content="test content" />)
const button = screen.getByRole('button')
fireEvent.click(button.firstChild as Element)
expect(mockCopy).toHaveBeenCalledWith('test content')
})
it('calls reset on mouse leave', () => {
render(<CopyFeedback content="test content" />)
const button = screen.getByRole('button')
fireEvent.mouseLeave(button.firstChild as Element)
expect(mockReset).toHaveBeenCalledTimes(1)
})
})
})
describe('CopyFeedbackNew', () => {
beforeEach(() => {
mockCopied = false
vi.clearAllMocks()
})
describe('Rendering', () => {
it('renders the component', () => {
const { container } = render(<CopyFeedbackNew content="test content" />)
expect(container.querySelector('.cursor-pointer')).toBeInTheDocument()
})
it('renders with custom className', () => {
const { container } = render(<CopyFeedbackNew content="test content" className="test-class" />)
expect(container.querySelector('.test-class')).toBeInTheDocument()
})
it('applies copied CSS class when copied is true', () => {
mockCopied = true
const { container } = render(<CopyFeedbackNew content="test content" />)
const feedbackIcon = container.firstChild?.firstChild as Element
expect(feedbackIcon).toHaveClass(/_copied_.*/)
})
it('does not apply copied CSS class when not copied', () => {
const { container } = render(<CopyFeedbackNew content="test content" />)
const feedbackIcon = container.firstChild?.firstChild as Element
expect(feedbackIcon).not.toHaveClass(/_copied_.*/)
})
})
describe('User Interactions', () => {
it('calls copy with content when clicked', () => {
const { container } = render(<CopyFeedbackNew content="test content" />)
const clickableArea = container.querySelector('.cursor-pointer')!.firstChild as HTMLElement
fireEvent.click(clickableArea)
expect(mockCopy).toHaveBeenCalledWith('test content')
})
it('calls reset on mouse leave', () => {
const { container } = render(<CopyFeedbackNew content="test content" />)
const clickableArea = container.querySelector('.cursor-pointer')!.firstChild as HTMLElement
fireEvent.mouseLeave(clickableArea)
expect(mockReset).toHaveBeenCalledTimes(1)
})
})
})