|
| 1 | +import { IconProp } from '@fortawesome/fontawesome-svg-core' |
| 2 | +import { faCertificate } from '@fortawesome/free-solid-svg-icons' |
| 3 | +import { render, screen } from '@testing-library/react' |
| 4 | +import React from 'react' |
| 5 | + |
| 6 | +import '@testing-library/jest-dom' |
| 7 | + |
| 8 | +// Mock the Tooltip component to prevent async state update warnings |
| 9 | +jest.mock('@heroui/tooltip', () => ({ |
| 10 | + Tooltip: ({ children, content }: { children: React.ReactNode; content: string }) => ( |
| 11 | + <div data-testid="tooltip" title={content}> |
| 12 | + {children} |
| 13 | + </div> |
| 14 | + ), |
| 15 | +})) |
| 16 | + |
| 17 | +import GeneralCompliantComponent from 'components/GeneralCompliantComponent' |
| 18 | + |
| 19 | +type GeneralCompliantComponentProps = { |
| 20 | + compliant: boolean |
| 21 | + icon: IconProp |
| 22 | + title: string |
| 23 | +} |
| 24 | + |
| 25 | +describe('GeneralCompliantComponent', () => { |
| 26 | + const baseProps: GeneralCompliantComponentProps = { |
| 27 | + compliant: true, |
| 28 | + icon: faCertificate, |
| 29 | + title: 'Test Title', |
| 30 | + } |
| 31 | + |
| 32 | + it('renders successfully with minimal required props', () => { |
| 33 | + const { container } = render(<GeneralCompliantComponent {...baseProps} />) |
| 34 | + expect(container).toBeInTheDocument() |
| 35 | + }) |
| 36 | + |
| 37 | + it('applies correct color for compliant=true', () => { |
| 38 | + const { container } = render(<GeneralCompliantComponent {...baseProps} compliant={true} />) |
| 39 | + const svg = container.querySelector('svg') |
| 40 | + expect(svg).toBeInTheDocument() |
| 41 | + expect(svg).toHaveClass('text-green-400/80') |
| 42 | + }) |
| 43 | + |
| 44 | + it('applies correct color for compliant=false', () => { |
| 45 | + const { container } = render(<GeneralCompliantComponent {...baseProps} compliant={false} />) |
| 46 | + const svg = container.querySelector('svg') |
| 47 | + expect(svg).toBeInTheDocument() |
| 48 | + expect(svg).toHaveClass('text-red-400/80') |
| 49 | + }) |
| 50 | + |
| 51 | + it('renders the correct icon structure', () => { |
| 52 | + const { container } = render(<GeneralCompliantComponent {...baseProps} />) |
| 53 | + const icons = container.querySelectorAll('svg') |
| 54 | + expect(icons).toHaveLength(2) |
| 55 | + }) |
| 56 | + |
| 57 | + it('renders tooltip wrapper with title attribute', () => { |
| 58 | + render(<GeneralCompliantComponent {...baseProps} title="Tooltip Title" />) |
| 59 | + const tooltip = screen.getByTestId('tooltip') |
| 60 | + expect(tooltip).toBeInTheDocument() |
| 61 | + expect(tooltip).toHaveAttribute('title', 'Tooltip Title') |
| 62 | + }) |
| 63 | + |
| 64 | + it('handles edge case: empty title', () => { |
| 65 | + const { container } = render(<GeneralCompliantComponent {...baseProps} title="" />) |
| 66 | + expect(container).toBeInTheDocument() |
| 67 | + }) |
| 68 | + |
| 69 | + it('has accessible SVG icons', () => { |
| 70 | + const { container } = render(<GeneralCompliantComponent {...baseProps} />) |
| 71 | + const icons = container.querySelectorAll('svg[role="img"]') |
| 72 | + expect(icons).toHaveLength(2) |
| 73 | + expect(icons[0]).toHaveAttribute('aria-hidden', 'true') |
| 74 | + expect(icons[1]).toHaveAttribute('aria-hidden', 'true') |
| 75 | + }) |
| 76 | + |
| 77 | + it('renders with custom icon', () => { |
| 78 | + const customIcon = faCertificate |
| 79 | + const { container } = render(<GeneralCompliantComponent {...baseProps} icon={customIcon} />) |
| 80 | + expect(container.querySelector('svg')).toBeInTheDocument() |
| 81 | + }) |
| 82 | +}) |
0 commit comments