Merge branch 'main' into feat/summary-index

This commit is contained in:
zxhlyh
2026-01-27 16:47:19 +08:00
124 changed files with 28915 additions and 778 deletions

View File

@ -1,10 +1,20 @@
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { fireEvent, render, screen } from '@testing-library/react'
import * as React from 'react'
import { createReactI18nextMock } from '@/test/i18n-mock'
import InputWithCopy from './index'
// Mock navigator.clipboard for foxact/use-clipboard
const mockWriteText = vi.fn(() => Promise.resolve())
// Create a controllable mock for useClipboard
const mockCopy = vi.fn()
let mockCopied = false
const mockReset = vi.fn()
vi.mock('foxact/use-clipboard', () => ({
useClipboard: () => ({
copy: mockCopy,
copied: mockCopied,
reset: mockReset,
}),
}))
// Mock the i18n hook with custom translations for test assertions
vi.mock('react-i18next', () => createReactI18nextMock({
@ -17,13 +27,9 @@ vi.mock('react-i18next', () => createReactI18nextMock({
describe('InputWithCopy component', () => {
beforeEach(() => {
vi.clearAllMocks()
mockWriteText.mockClear()
// Setup navigator.clipboard mock
Object.assign(navigator, {
clipboard: {
writeText: mockWriteText,
},
})
mockCopy.mockClear()
mockReset.mockClear()
mockCopied = false
})
it('renders correctly with default props', () => {
@ -44,31 +50,27 @@ describe('InputWithCopy component', () => {
expect(copyButton).not.toBeInTheDocument()
})
it('copies input value when copy button is clicked', async () => {
it('calls copy function with input value when copy button is clicked', () => {
const mockOnChange = vi.fn()
render(<InputWithCopy value="test value" onChange={mockOnChange} />)
const copyButton = screen.getByRole('button')
fireEvent.click(copyButton)
await waitFor(() => {
expect(mockWriteText).toHaveBeenCalledWith('test value')
})
expect(mockCopy).toHaveBeenCalledWith('test value')
})
it('copies custom value when copyValue prop is provided', async () => {
it('calls copy function with custom value when copyValue prop is provided', () => {
const mockOnChange = vi.fn()
render(<InputWithCopy value="display value" onChange={mockOnChange} copyValue="custom copy value" />)
const copyButton = screen.getByRole('button')
fireEvent.click(copyButton)
await waitFor(() => {
expect(mockWriteText).toHaveBeenCalledWith('custom copy value')
})
expect(mockCopy).toHaveBeenCalledWith('custom copy value')
})
it('calls onCopy callback when copy button is clicked', async () => {
it('calls onCopy callback when copy button is clicked', () => {
const onCopyMock = vi.fn()
const mockOnChange = vi.fn()
render(<InputWithCopy value="test value" onChange={mockOnChange} onCopy={onCopyMock} />)
@ -76,25 +78,21 @@ describe('InputWithCopy component', () => {
const copyButton = screen.getByRole('button')
fireEvent.click(copyButton)
await waitFor(() => {
expect(onCopyMock).toHaveBeenCalledWith('test value')
})
expect(onCopyMock).toHaveBeenCalledWith('test value')
})
it('shows copied state after successful copy', async () => {
it('shows copied state when copied is true', () => {
mockCopied = true
const mockOnChange = vi.fn()
render(<InputWithCopy value="test value" onChange={mockOnChange} />)
const copyButton = screen.getByRole('button')
fireEvent.click(copyButton)
// Hover over the button to trigger tooltip
fireEvent.mouseEnter(copyButton)
// Check if the tooltip shows "Copied" state
await waitFor(() => {
expect(screen.getByText('Copied')).toBeInTheDocument()
}, { timeout: 2000 })
// The icon should change to filled version when copied
// We verify the component renders without error in copied state
expect(copyButton).toBeInTheDocument()
})
it('passes through all input props correctly', () => {
@ -117,22 +115,22 @@ describe('InputWithCopy component', () => {
expect(input).toHaveClass('custom-class')
})
it('handles empty value correctly', async () => {
it('handles empty value correctly', () => {
const mockOnChange = vi.fn()
render(<InputWithCopy value="" onChange={mockOnChange} />)
const input = screen.getByDisplayValue('')
const input = screen.getByRole('textbox')
const copyButton = screen.getByRole('button')
expect(input).toBeInTheDocument()
expect(input).toHaveValue('')
expect(copyButton).toBeInTheDocument()
// Clicking copy button with empty value should call copy with empty string
fireEvent.click(copyButton)
await waitFor(() => {
expect(mockWriteText).toHaveBeenCalledWith('')
})
expect(mockCopy).toHaveBeenCalledWith('')
})
it('maintains focus on input after copy', async () => {
it('maintains focus on input after copy', () => {
const mockOnChange = vi.fn()
render(<InputWithCopy value="test value" onChange={mockOnChange} />)