|
1 | | -import { render, screen } from '@testing-library/react'; |
| 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' |
2 | 5 |
|
3 | | -import '@testing-library/jest-dom'; |
4 | | -import { faCertificate } from '@fortawesome/free-solid-svg-icons'; |
| 6 | +import '@testing-library/jest-dom' |
5 | 7 |
|
6 | | -import GeneralCompliantComponent from '../../../src/components/GeneralCompliantComponent'; |
| 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' |
7 | 18 |
|
8 | 19 | type GeneralCompliantComponentProps = { |
9 | | - compliant: boolean; |
10 | | - icon: any; |
11 | | - title: string; |
12 | | -}; |
| 20 | + compliant: boolean |
| 21 | + icon: IconProp |
| 22 | + title: string |
| 23 | +} |
13 | 24 |
|
14 | 25 | describe('GeneralCompliantComponent', () => { |
15 | 26 | const baseProps: GeneralCompliantComponentProps = { |
16 | 27 | compliant: true, |
17 | 28 | icon: faCertificate, |
18 | 29 | title: 'Test Title', |
19 | | - }; |
| 30 | + } |
20 | 31 |
|
21 | 32 | it('renders successfully with minimal required props', () => { |
22 | | - render(<GeneralCompliantComponent {...baseProps} />); |
23 | | - expect(screen.getByText('Test Title')).toBeInTheDocument(); |
24 | | - }); |
| 33 | + const { container } = render(<GeneralCompliantComponent {...baseProps} />) |
| 34 | + expect(container).toBeInTheDocument() |
| 35 | + }) |
25 | 36 |
|
26 | | - it('applies correct color for compliant=true', () => { |
27 | | - const { container } = render(<GeneralCompliantComponent {...baseProps} compliant={true} />); |
28 | | - const svg = container.querySelector('svg'); // Find the SVG icon |
29 | | - expect(svg).toBeInTheDocument(); |
30 | | - expect(svg).toHaveClass('text-green-400/80'); // Check for the specific class |
31 | | -}); |
| 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 | + }) |
32 | 43 |
|
33 | 44 | it('applies correct color for compliant=false', () => { |
34 | | - const { container } = render(<GeneralCompliantComponent {...baseProps} compliant={false} />); |
35 | | - const svg = container.querySelector('svg'); // Find the SVG icon |
36 | | - expect(svg).toBeInTheDocument(); |
37 | | - expect(svg).toHaveClass('text-red-400/80'); // Check for the specific class |
38 | | - }); |
| 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 | + }) |
39 | 50 |
|
40 | | - it('renders the correct icon and title', () => { |
41 | | - render(<GeneralCompliantComponent {...baseProps} title="My Title" />); |
42 | | - expect(screen.getByText('My Title')).toBeInTheDocument(); |
43 | | - }); |
| 51 | + it('renders the correct icon structure', () => { |
| 52 | + const { container } = render(<GeneralCompliantComponent {...baseProps} />) |
| 53 | + const svgs = container.querySelectorAll('svg') |
| 54 | + expect(svgs).toHaveLength(2) |
| 55 | + }) |
44 | 56 |
|
45 | | - it('renders tooltip with the title', () => { |
46 | | - const { getByText } = render(<GeneralCompliantComponent {...baseProps} title="Tooltip Title" />); |
47 | | - // Tooltip content is rendered, but may require hover simulation in real DOM |
48 | | - expect(getByText('Tooltip Title')).toBeInTheDocument(); |
49 | | - }); |
| 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 | + }) |
50 | 63 |
|
51 | 64 | it('handles edge case: empty title', () => { |
52 | | - const { container } = render(<GeneralCompliantComponent {...baseProps} title="" />); |
53 | | - expect(container).toBeInTheDocument(); |
54 | | - }); |
| 65 | + const { container } = render(<GeneralCompliantComponent {...baseProps} title="" />) |
| 66 | + expect(container).toBeInTheDocument() |
| 67 | + }) |
55 | 68 |
|
56 | | - it('has accessible role and label', () => { |
57 | | - render(<GeneralCompliantComponent {...baseProps} />); |
58 | | - const iconElement = screen.getByText(baseProps.title); // Asserts the title text is visible |
59 | | - expect(iconElement).toBeInTheDocument(); |
60 | | - // Or, if the icon has a specific role, you can check for that |
61 | | - // expect(screen.getByRole('img', { hidden: true })).toBeInTheDocument(); |
62 | | -}); |
| 69 | + it('has accessible SVG icons', () => { |
| 70 | + const { container } = render(<GeneralCompliantComponent {...baseProps} />) |
| 71 | + const svgs = container.querySelectorAll('svg[role="img"]') |
| 72 | + expect(svgs).toHaveLength(2) |
| 73 | + expect(svgs[0]).toHaveAttribute('aria-hidden', 'true') |
| 74 | + expect(svgs[1]).toHaveAttribute('aria-hidden', 'true') |
| 75 | + }) |
63 | 76 |
|
64 | 77 | it('renders with custom icon', () => { |
65 | | - const customIcon = faCertificate; // Replace with another icon if needed |
66 | | - const { container } = render(<GeneralCompliantComponent {...baseProps} icon={customIcon} />); |
67 | | - expect(container.querySelector('svg')).toBeInTheDocument(); |
68 | | - }); |
69 | | - |
70 | | - // Add more tests as needed for event handling, state, etc. |
71 | | -}); |
72 | | - |
73 | | -// ...existing code... |
| 78 | + const customIcon = faCertificate |
| 79 | + const { container } = render(<GeneralCompliantComponent {...baseProps} icon={customIcon} />) |
| 80 | + expect(container.querySelector('svg')).toBeInTheDocument() |
| 81 | + }) |
| 82 | +}) |
0 commit comments