import { act, render, screen } from '@testing-library/react' import userEvent from '@testing-library/user-event' import SecretKeyButton from './secret-key-button' // Mock the SecretKeyModal since it has complex dependencies vi.mock('@/app/components/develop/secret-key/secret-key-modal', () => ({ default: ({ isShow, onClose, appId }: { isShow: boolean, onClose: () => void, appId?: string }) => ( isShow ? (
{`Modal for ${appId || 'no-app'}`}
) : null ), })) describe('SecretKeyButton', () => { describe('rendering', () => { it('should render the button', () => { render() expect(screen.getByRole('button')).toBeInTheDocument() }) it('should render the API key text', () => { render() expect(screen.getByText('appApi.apiKey')).toBeInTheDocument() }) it('should render the key icon', () => { const { container } = render() // RiKey2Line icon should be rendered as an svg const svg = container.querySelector('svg') expect(svg).toBeInTheDocument() }) it('should not show modal initially', () => { render() expect(screen.queryByTestId('secret-key-modal')).not.toBeInTheDocument() }) }) describe('button interaction', () => { it('should open modal when button is clicked', async () => { const user = userEvent.setup() render() const button = screen.getByRole('button') await act(async () => { await user.click(button) }) expect(screen.getByTestId('secret-key-modal')).toBeInTheDocument() }) it('should close modal when onClose is called', async () => { const user = userEvent.setup() render() // Open modal const button = screen.getByRole('button') await act(async () => { await user.click(button) }) expect(screen.getByTestId('secret-key-modal')).toBeInTheDocument() // Close modal const closeButton = screen.getByTestId('close-modal') await act(async () => { await user.click(closeButton) }) expect(screen.queryByTestId('secret-key-modal')).not.toBeInTheDocument() }) it('should toggle modal visibility', async () => { const user = userEvent.setup() render() const button = screen.getByRole('button') // Open await act(async () => { await user.click(button) }) expect(screen.getByTestId('secret-key-modal')).toBeInTheDocument() // Close const closeButton = screen.getByTestId('close-modal') await act(async () => { await user.click(closeButton) }) expect(screen.queryByTestId('secret-key-modal')).not.toBeInTheDocument() // Open again await act(async () => { await user.click(button) }) expect(screen.getByTestId('secret-key-modal')).toBeInTheDocument() }) }) describe('props', () => { it('should apply custom className', () => { render() const button = screen.getByRole('button') expect(button.className).toContain('custom-class') }) it('should pass appId to modal', async () => { const user = userEvent.setup() render() const button = screen.getByRole('button') await act(async () => { await user.click(button) }) expect(screen.getByText('Modal for app-123')).toBeInTheDocument() }) it('should handle undefined appId', async () => { const user = userEvent.setup() render() const button = screen.getByRole('button') await act(async () => { await user.click(button) }) expect(screen.getByText('Modal for no-app')).toBeInTheDocument() }) it('should apply custom textCls', () => { render() const text = screen.getByText('appApi.apiKey') expect(text.className).toContain('custom-text-class') }) }) describe('button styling', () => { it('should have px-3 padding', () => { render() const button = screen.getByRole('button') expect(button.className).toContain('px-3') }) it('should have small size', () => { render() const button = screen.getByRole('button') expect(button.className).toContain('btn-small') }) it('should have ghost variant', () => { render() const button = screen.getByRole('button') expect(button.className).toContain('btn-ghost') }) }) describe('icon styling', () => { it('should have icon container with flex layout', () => { const { container } = render() const iconContainer = container.querySelector('.flex.items-center.justify-center') expect(iconContainer).toBeInTheDocument() }) it('should have correct icon dimensions', () => { const { container } = render() const iconContainer = container.querySelector('.h-3\\.5.w-3\\.5') expect(iconContainer).toBeInTheDocument() }) it('should have tertiary text color on icon', () => { const { container } = render() const icon = container.querySelector('.text-text-tertiary') expect(icon).toBeInTheDocument() }) }) describe('text styling', () => { it('should have system-xs-medium class', () => { render() const text = screen.getByText('appApi.apiKey') expect(text.className).toContain('system-xs-medium') }) it('should have horizontal padding', () => { render() const text = screen.getByText('appApi.apiKey') expect(text.className).toContain('px-[3px]') }) it('should have tertiary text color', () => { render() const text = screen.getByText('appApi.apiKey') expect(text.className).toContain('text-text-tertiary') }) }) describe('modal props', () => { it('should pass isShow prop to modal', async () => { const user = userEvent.setup() render() // Initially modal should not be visible expect(screen.queryByTestId('secret-key-modal')).not.toBeInTheDocument() const button = screen.getByRole('button') await act(async () => { await user.click(button) }) // Now modal should be visible expect(screen.getByTestId('secret-key-modal')).toBeInTheDocument() }) it('should pass onClose callback to modal', async () => { const user = userEvent.setup() render() const button = screen.getByRole('button') await act(async () => { await user.click(button) }) const closeButton = screen.getByTestId('close-modal') await act(async () => { await user.click(closeButton) }) // Modal should be closed after clicking close expect(screen.queryByTestId('secret-key-modal')).not.toBeInTheDocument() }) }) describe('accessibility', () => { it('should have accessible button', () => { render() const button = screen.getByRole('button') expect(button).toBeInTheDocument() }) it('should be keyboard accessible', async () => { const user = userEvent.setup() render() const button = screen.getByRole('button') button.focus() expect(document.activeElement).toBe(button) // Press Enter to activate await act(async () => { await user.keyboard('{Enter}') }) expect(screen.getByTestId('secret-key-modal')).toBeInTheDocument() }) }) describe('multiple instances', () => { it('should work independently when multiple instances exist', async () => { const user = userEvent.setup() render( <> , ) const buttons = screen.getAllByRole('button') expect(buttons).toHaveLength(2) // Click first button await act(async () => { await user.click(buttons[0]) }) expect(screen.getByText('Modal for app-1')).toBeInTheDocument() // Close first modal const closeButton = screen.getByTestId('close-modal') await act(async () => { await user.click(closeButton) }) // Click second button await act(async () => { await user.click(buttons[1]) }) expect(screen.getByText('Modal for app-2')).toBeInTheDocument() }) }) })