Files
dify/web/app/components/explore/try-app/tab.spec.tsx
CodingOnStar 031c0134c5 test: add comprehensive tests for try-app components with 95%+ coverage
Add automated tests for all components in web/app/components/explore/try-app:
- index.tsx and tab.tsx (root components)
- app/index.tsx, app/chat.tsx, app/text-generation.tsx
- app-info/index.tsx, app-info/use-get-requirements.ts
- preview/index.tsx, preview/basic-app-preview.tsx, preview/flow-app-preview.tsx

Test coverage:
- 137 tests passing
- All files exceed 95% coverage threshold
- Tests cover loading states, user interactions, different app modes,
  mobile/PC responsive behavior, and edge cases

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 16:51:13 +08:00

59 lines
1.7 KiB
TypeScript

import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import Tab, { TypeEnum } from './tab'
vi.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key: string) => {
const translations: Record<string, string> = {
'tryApp.tabHeader.try': 'Try',
'tryApp.tabHeader.detail': 'Detail',
}
return translations[key] || key
},
}),
}))
describe('Tab', () => {
afterEach(() => {
cleanup()
})
it('renders tab with TRY value selected', () => {
const mockOnChange = vi.fn()
render(<Tab value={TypeEnum.TRY} onChange={mockOnChange} />)
expect(screen.getByText('Try')).toBeInTheDocument()
expect(screen.getByText('Detail')).toBeInTheDocument()
})
it('renders tab with DETAIL value selected', () => {
const mockOnChange = vi.fn()
render(<Tab value={TypeEnum.DETAIL} onChange={mockOnChange} />)
expect(screen.getByText('Try')).toBeInTheDocument()
expect(screen.getByText('Detail')).toBeInTheDocument()
})
it('calls onChange when clicking a tab', () => {
const mockOnChange = vi.fn()
render(<Tab value={TypeEnum.TRY} onChange={mockOnChange} />)
fireEvent.click(screen.getByText('Detail'))
expect(mockOnChange).toHaveBeenCalledWith(TypeEnum.DETAIL)
})
it('calls onChange when clicking Try tab', () => {
const mockOnChange = vi.fn()
render(<Tab value={TypeEnum.DETAIL} onChange={mockOnChange} />)
fireEvent.click(screen.getByText('Try'))
expect(mockOnChange).toHaveBeenCalledWith(TypeEnum.TRY)
})
it('exports TypeEnum correctly', () => {
expect(TypeEnum.TRY).toBe('try')
expect(TypeEnum.DETAIL).toBe('detail')
})
})