import type { SiteInfo } from '@/models/share' import { cleanup, fireEvent, render, screen } from '@testing-library/react' import { afterEach, describe, expect, it, vi } from 'vitest' import InfoModal from './info-modal' // Only mock react-i18next for translations vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key, }), })) afterEach(() => { cleanup() }) describe('InfoModal', () => { const mockOnClose = vi.fn() const baseSiteInfo: SiteInfo = { title: 'Test App', icon: '🚀', icon_type: 'emoji', icon_background: '#ffffff', } beforeEach(() => { vi.clearAllMocks() }) describe('rendering', () => { it('should not render when isShow is false', () => { render( , ) expect(screen.queryByText('Test App')).not.toBeInTheDocument() }) it('should render when isShow is true', () => { render( , ) expect(screen.getByText('Test App')).toBeInTheDocument() }) it('should render app title', () => { render( , ) expect(screen.getByText('Test App')).toBeInTheDocument() }) it('should render copyright when provided', () => { const siteInfoWithCopyright: SiteInfo = { ...baseSiteInfo, copyright: 'Dify Inc.', } render( , ) expect(screen.getByText(/Dify Inc./)).toBeInTheDocument() }) it('should render current year in copyright', () => { const siteInfoWithCopyright: SiteInfo = { ...baseSiteInfo, copyright: 'Test Company', } render( , ) const currentYear = new Date().getFullYear().toString() expect(screen.getByText(new RegExp(currentYear))).toBeInTheDocument() }) it('should render custom disclaimer when provided', () => { const siteInfoWithDisclaimer: SiteInfo = { ...baseSiteInfo, custom_disclaimer: 'This is a custom disclaimer', } render( , ) expect(screen.getByText('This is a custom disclaimer')).toBeInTheDocument() }) it('should not render copyright section when not provided', () => { render( , ) const year = new Date().getFullYear().toString() expect(screen.queryByText(new RegExp(`©.*${year}`))).not.toBeInTheDocument() }) it('should render with undefined data', () => { render( , ) // Modal should still render but without content expect(screen.queryByText('Test App')).not.toBeInTheDocument() }) it('should render with image icon type', () => { const siteInfoWithImage: SiteInfo = { ...baseSiteInfo, icon_type: 'image', icon_url: 'https://example.com/icon.png', } render( , ) expect(screen.getByText(siteInfoWithImage.title!)).toBeInTheDocument() }) }) describe('close functionality', () => { it('should call onClose when close button is clicked', () => { render( , ) // Find the close icon (RiCloseLine) which has text-text-tertiary class const closeIcon = document.querySelector('[class*="text-text-tertiary"]') expect(closeIcon).toBeInTheDocument() if (closeIcon) { fireEvent.click(closeIcon) expect(mockOnClose).toHaveBeenCalled() } }) }) describe('both copyright and disclaimer', () => { it('should render both when both are provided', () => { const siteInfoWithBoth: SiteInfo = { ...baseSiteInfo, copyright: 'My Company', custom_disclaimer: 'Disclaimer text here', } render( , ) expect(screen.getByText(/My Company/)).toBeInTheDocument() expect(screen.getByText('Disclaimer text here')).toBeInTheDocument() }) }) })