|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { BrowserRouter } from 'react-router-dom'; |
| 4 | +import Header from '../Header'; |
| 5 | + |
| 6 | +// Mock FontAwesomeIcon for BahmniIcon |
| 7 | +jest.mock('@fortawesome/react-fontawesome', () => ({ |
| 8 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 9 | + FontAwesomeIcon: ({ icon, size, color, ...props }: any) => ( |
| 10 | + <svg |
| 11 | + data-testid={props['data-testid']} |
| 12 | + data-icon={icon[1]} |
| 13 | + data-prefix={icon[0]} |
| 14 | + data-size={size} |
| 15 | + data-color={color} |
| 16 | + {...props} |
| 17 | + /> |
| 18 | + ), |
| 19 | +})); |
| 20 | + |
| 21 | +describe('Header Component', () => { |
| 22 | + // Helper function to render the component with router |
| 23 | + const renderWithRouter = () => |
| 24 | + render( |
| 25 | + <BrowserRouter> |
| 26 | + <Header /> |
| 27 | + </BrowserRouter>, |
| 28 | + ); |
| 29 | + |
| 30 | + // Happy Path Tests |
| 31 | + describe('Happy Path', () => { |
| 32 | + test('renders the header with correct aria-label', () => { |
| 33 | + renderWithRouter(); |
| 34 | + const header = screen.getByRole('banner'); |
| 35 | + expect(header).toHaveAttribute('aria-label', 'Bahmni Clinical'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('renders the Bahmni Clinical header text', () => { |
| 39 | + renderWithRouter(); |
| 40 | + const headerText = screen.getByText('Bahmni Clinical'); |
| 41 | + expect(headerText).toBeInTheDocument(); |
| 42 | + expect(headerText.tagName).toBe('A'); // Should be a link |
| 43 | + expect(headerText).toHaveAttribute('href', '/'); |
| 44 | + }); |
| 45 | + |
| 46 | + test('renders the Home menu item with icon', () => { |
| 47 | + renderWithRouter(); |
| 48 | + const homeMenuItem = screen.getByText('Home'); |
| 49 | + expect(homeMenuItem).toBeInTheDocument(); |
| 50 | + |
| 51 | + // Check if it's a link to the home page |
| 52 | + const homeLink = homeMenuItem.closest('a'); |
| 53 | + expect(homeLink).toHaveAttribute('href', '/'); |
| 54 | + |
| 55 | + // Check if the icon is rendered |
| 56 | + const homeIcon = screen.getByTestId('homeIcon'); |
| 57 | + expect(homeIcon).toBeInTheDocument(); |
| 58 | + }); |
| 59 | + |
| 60 | + test('renders the HeaderNavigation with correct aria-label', () => { |
| 61 | + renderWithRouter(); |
| 62 | + const navigation = screen.getByRole('navigation'); |
| 63 | + expect(navigation).toHaveAttribute('aria-label', 'Main Navigation'); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + // Edge Cases |
| 68 | + describe('Edge Cases', () => { |
| 69 | + test('supports keyboard navigation', () => { |
| 70 | + renderWithRouter(); |
| 71 | + |
| 72 | + // Get all interactive elements |
| 73 | + const interactiveElements = screen.getAllByRole('link'); |
| 74 | + |
| 75 | + // Ensure they have proper tab index |
| 76 | + interactiveElements.forEach((element) => { |
| 77 | + expect(element.tabIndex).not.toBe(-1); |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + // Accessibility Tests |
| 83 | + describe('Accessibility', () => { |
| 84 | + test('all interactive elements have accessible names', () => { |
| 85 | + renderWithRouter(); |
| 86 | + |
| 87 | + // Check all buttons and links have accessible names |
| 88 | + const links = screen.getAllByRole('link', { hidden: true }); |
| 89 | + |
| 90 | + links.forEach((element) => { |
| 91 | + expect(element).toHaveAccessibleName(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments