import { MemoryRouter, Route, Routes } from 'react-router-dom'; import { render, screen, fireEvent, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import '@testing-library/jest-dom'; import { EXTERNAL_DOCUMENTATION_BASE_URL, INTERNAL_DOCUMENTATION_BASE_URL, } from '~/consts/documentation'; import { ModalProvider } from '../../modal/ModalProvider'; import { HelpDropdown } from '../HelpDropdown'; const mockStartTour = jest.fn(); const mockGetToursByRoute = jest.fn().mockReturnValue([]); const mockCollectAndMerge = jest .fn() .mockReturnValue({ mergedSteps: [], sourceIds: [], hasPrompt: false }); const mockUseTour = jest .fn() .mockReturnValue({ startTour: mockStartTour, currentRoute: undefined }); jest.mock('~/shared/components/GuidedTours', () => ({ useTour: (...args: unknown[]) => mockUseTour(...args), getToursByRoute: (...args: unknown[]) => mockGetToursByRoute(...args), collectAndMerge: (...args: unknown[]) => mockCollectAndMerge(...args), })); const mockUseKonfluxPublicInfo = jest.fn(); jest.mock('~/components/CLILogin/CliLoginModal', () => ({ createCliLoginModal: jest.fn(() => () =>
), })); jest.mock('~/hooks/useKonfluxPublicInfo', () => ({ useKonfluxPublicInfo: () => mockUseKonfluxPublicInfo(), })); const renderWithModalProvider = (component: React.ReactElement, path = '/') => { return render( {component}} /> {component}} /> , ); }; describe('HelpDropdown Component', () => { beforeEach(() => { jest.clearAllMocks(); mockUseKonfluxPublicInfo.mockReturnValue([{ visibility: 'public' }]); mockUseTour.mockReturnValue({ startTour: mockStartTour, currentRoute: undefined }); mockGetToursByRoute.mockReturnValue([]); mockCollectAndMerge.mockReturnValue({ mergedSteps: [], sourceIds: [], hasPrompt: false }); }); describe('Rendering', () => { it('should render the help icon with tooltip', () => { renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); expect(helpIcon).toBeInTheDocument(); // Test tooltip trigger - PatternFly tooltips may not render consistently in tests fireEvent.mouseOver(helpIcon); expect(helpIcon).toBeInTheDocument(); }); it('should not show dropdown menu initially', () => { renderWithModalProvider(); expect(screen.queryByText('About Konflux')).not.toBeInTheDocument(); expect(screen.queryByText('Copy login command')).not.toBeInTheDocument(); expect(screen.queryByText('Documentation')).not.toBeInTheDocument(); expect(screen.queryByText('Share feedback')).not.toBeInTheDocument(); }); it('should not show AboutModal initially', () => { renderWithModalProvider(); expect(screen.queryByTestId('about-modal')).not.toBeInTheDocument(); }); it('should not show FeedbackModal initially', () => { renderWithModalProvider(); expect(screen.queryByTestId('feedback-modal')).not.toBeInTheDocument(); }); }); describe('Dropdown Functionality', () => { it('should open dropdown menu when help icon is clicked', async () => { renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); fireEvent.click(helpIcon); await waitFor(() => { expect(screen.getByText('About Konflux')).toBeInTheDocument(); expect(screen.getByText('Copy login command')).toBeInTheDocument(); expect(screen.getByText('Documentation')).toBeInTheDocument(); expect(screen.getByText('Share feedback')).toBeInTheDocument(); }); }); it('should close dropdown menu when clicked again', async () => { renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); // Open dropdown fireEvent.click(helpIcon); await waitFor(() => { expect(screen.getByText('About Konflux')).toBeInTheDocument(); }); // Close dropdown fireEvent.click(helpIcon); await waitFor(() => { expect(screen.queryByText('About Konflux')).not.toBeInTheDocument(); }); }); it('should have correct data-test attributes on dropdown items', async () => { renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); fireEvent.click(helpIcon); await waitFor(() => { const aboutItem = screen.getByTestId('help-dropdown-about'); const cliLoginItem = screen.getByTestId('help-dropdown-cli-login'); const docItem = screen.getByTestId('help-dropdown-documentation'); const feedbackItem = screen.getByTestId('help-dropdown-feedback'); expect(aboutItem).toBeInTheDocument(); expect(cliLoginItem).toBeInTheDocument(); expect(docItem).toBeInTheDocument(); expect(feedbackItem).toBeInTheDocument(); }); }); it('should call startTour with merged steps when guided tour is clicked', async () => { const fakeTour = { id: 'test-tour', steps: [] }; const mergedSteps = [{ type: 'modal', title: 'Welcome', content: 'Hello' }]; mockUseTour.mockReturnValue({ startTour: mockStartTour, currentRoute: 'ns/:workspaceName/applications', }); mockGetToursByRoute.mockReturnValue([fakeTour]); mockCollectAndMerge.mockReturnValue({ mergedSteps, sourceIds: ['test-tour'], hasPrompt: false, }); renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); fireEvent.click(helpIcon); await waitFor(() => { expect(screen.getByText('Guided tour')).toBeInTheDocument(); }); fireEvent.click(screen.getByText('Guided tour')); expect(mockCollectAndMerge).toHaveBeenCalledWith([fakeTour]); expect(mockStartTour).toHaveBeenCalledWith(mergedSteps, ['test-tour']); }); it('should display external link icon for documentation item', async () => { renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); fireEvent.click(helpIcon); await waitFor(() => { const docItem = screen.getByTestId('help-dropdown-documentation'); expect(docItem.querySelector('svg')).toBeInTheDocument(); }); }); }); describe('Copy login command', () => { it('shows Copy login command as the second menu item', async () => { renderWithModalProvider(); fireEvent.click(screen.getByLabelText('Help menu toggle')); const items = await screen.findAllByRole('menuitem'); expect(items).toHaveLength(4); expect(items[0]).toHaveTextContent('About Konflux'); expect(items[1]).toHaveTextContent('Copy login command'); expect(items[2]).toHaveTextContent('Documentation'); expect(items[3]).toHaveTextContent('Share feedback'); expect(screen.queryByRole('link', { name: /Copy login command/i })).not.toBeInTheDocument(); }); it('opens the CLI login modal when Copy login command is clicked', async () => { const user = userEvent.setup(); renderWithModalProvider(, '/ns/my-tenant/applications'); await user.click(screen.getByLabelText('Help menu toggle')); await user.click(await screen.findByRole('menuitem', { name: /Copy login command/i })); expect(await screen.findByTestId('cli-login-modal')).toBeInTheDocument(); }); it('calls onCliLoginClick once when Copy login command is clicked', async () => { const onCliLoginClick = jest.fn(); const user = userEvent.setup(); renderWithModalProvider( , '/ns/my-tenant/applications', ); await user.click(screen.getByLabelText('Help menu toggle')); await user.click(await screen.findByRole('menuitem', { name: /Copy login command/i })); expect(onCliLoginClick).toHaveBeenCalledTimes(1); }); it('still opens the CLI login modal when onCliLoginClick is provided', async () => { const onCliLoginClick = jest.fn(); const user = userEvent.setup(); renderWithModalProvider( , '/ns/my-tenant/applications', ); await user.click(screen.getByLabelText('Help menu toggle')); await user.click(await screen.findByRole('menuitem', { name: /Copy login command/i })); expect(onCliLoginClick).toHaveBeenCalledTimes(1); expect(await screen.findByTestId('cli-login-modal')).toBeInTheDocument(); }); }); describe('About Modal Functionality', () => { it('should have About Konflux menu item with correct attributes', async () => { renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); fireEvent.click(helpIcon); await waitFor(() => { const aboutButton = screen.getByRole('menuitem', { name: /About Konflux/i }); expect(aboutButton).toBeInTheDocument(); expect(aboutButton).toHaveAttribute('role', 'menuitem'); }); }); }); describe('Feedback Modal Functionality', () => { it('should have Share feedback menu item with correct attributes', async () => { renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); fireEvent.click(helpIcon); await waitFor(() => { const feedbackButton = screen.getByRole('menuitem', { name: /Share feedback/i }); expect(feedbackButton).toBeInTheDocument(); expect(feedbackButton).toHaveAttribute('role', 'menuitem'); }); }); }); describe('Documentation Link Functionality', () => { it('should use external documentation URL for public visibility', async () => { mockUseKonfluxPublicInfo.mockReturnValue([{ visibility: 'public' }]); renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); fireEvent.click(helpIcon); await waitFor(() => { const docLink = screen.getByRole('link', { name: /Documentation/i }); expect(docLink).toBeInTheDocument(); expect(docLink).toHaveAttribute('href', EXTERNAL_DOCUMENTATION_BASE_URL); expect(docLink).toHaveAttribute('target', '_blank'); expect(docLink).toHaveAttribute('rel', 'noopener noreferrer'); }); }); it('should use internal documentation URL for private visibility', async () => { mockUseKonfluxPublicInfo.mockReturnValue([{ visibility: 'private' }]); renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); fireEvent.click(helpIcon); await waitFor(() => { const docLink = screen.getByRole('link', { name: /Documentation/i }); expect(docLink).toBeInTheDocument(); expect(docLink).toHaveAttribute('href', INTERNAL_DOCUMENTATION_BASE_URL); expect(docLink).toHaveAttribute('target', '_blank'); expect(docLink).toHaveAttribute('rel', 'noopener noreferrer'); }); }); it('should close dropdown when documentation link is clicked', async () => { renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); fireEvent.click(helpIcon); await waitFor(() => { expect(screen.getByText('Documentation')).toBeInTheDocument(); }); // Click the Documentation link - should not throw const docLink = screen.getByRole('link', { name: /Documentation/i }); expect(() => fireEvent.click(docLink)).not.toThrow(); }); it('should default to external documentation when visibility is undefined', async () => { mockUseKonfluxPublicInfo.mockReturnValue([{}]); renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); fireEvent.click(helpIcon); await waitFor(() => { const docLink = screen.getByRole('link', { name: /Documentation/i }); expect(docLink).toBeInTheDocument(); expect(docLink).toHaveAttribute('href', EXTERNAL_DOCUMENTATION_BASE_URL); expect(docLink).toHaveAttribute('target', '_blank'); expect(docLink).toHaveAttribute('rel', 'noopener noreferrer'); }); }); }); describe('Accessibility', () => { it('should have proper aria labels', async () => { renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); expect(helpIcon).toHaveAttribute('aria-label', 'Help menu toggle'); // Open dropdown to access menu fireEvent.click(helpIcon); await waitFor(() => { const menu = screen.getByRole('menu'); expect(menu).toBeInTheDocument(); }); }); it('should handle keyboard navigation', () => { renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); // Test Enter key to open dropdown helpIcon.focus(); fireEvent.keyDown(helpIcon, { key: 'Enter', code: 'Enter' }); // PatternFly might not respond to keyDown in tests, so let's just test focus expect(helpIcon).toHaveFocus(); }); }); describe('Error Handling', () => { it('should handle missing Konflux public info gracefully', async () => { mockUseKonfluxPublicInfo.mockReturnValue([null]); expect(() => renderWithModalProvider()).not.toThrow(); const helpIcon = screen.getByLabelText('Help menu toggle'); fireEvent.click(helpIcon); await waitFor(() => { // Should default to external documentation const docLink = screen.getByRole('link', { name: /Documentation/i }); expect(docLink).toBeInTheDocument(); expect(docLink).toHaveAttribute('href', EXTERNAL_DOCUMENTATION_BASE_URL); expect(docLink).toHaveAttribute('target', '_blank'); expect(docLink).toHaveAttribute('rel', 'noopener noreferrer'); }); }); it('should render documentation link correctly even with ExternalLink component', async () => { renderWithModalProvider(); const helpIcon = screen.getByLabelText('Help menu toggle'); fireEvent.click(helpIcon); await waitFor(() => { const docLink = screen.getByRole('link', { name: /Documentation/i }); expect(docLink).toBeInTheDocument(); // ExternalLink should render properly with all required attributes expect(docLink).toHaveAttribute('href', EXTERNAL_DOCUMENTATION_BASE_URL); expect(docLink).toHaveAttribute('target', '_blank'); expect(docLink).toHaveAttribute('rel', 'noopener noreferrer'); // Should have external link icon expect(docLink.querySelector('svg')).toBeInTheDocument(); }); }); }); });