|
| 1 | +import '@testing-library/jest-dom' |
| 2 | +import { fireEvent, render, screen } from '@testing-library/react' |
| 3 | + |
| 4 | +import { Link } from './Link' |
| 5 | + |
| 6 | +describe('Link Component', () => { |
| 7 | + test('renders the link with correct href', () => { |
| 8 | + render(<Link href='/test' label='Test Link' />) |
| 9 | + const linkElement = screen.getByRole('link', { name: /test link/i }) |
| 10 | + expect(linkElement).toHaveAttribute('href', '/test') |
| 11 | + }) |
| 12 | + |
| 13 | + test('prevents click when disabled', () => { |
| 14 | + const handleClick = jest.fn() |
| 15 | + render( |
| 16 | + <Link href='/test' label='Disabled Link' disabled onClick={handleClick} /> |
| 17 | + ) |
| 18 | + |
| 19 | + const linkElement = screen.getByRole('link', { name: /disabled link/i }) |
| 20 | + fireEvent.click(linkElement) |
| 21 | + expect(handleClick).not.toHaveBeenCalled() |
| 22 | + expect(linkElement).toHaveAttribute('aria-disabled', 'true') |
| 23 | + }) |
| 24 | + |
| 25 | + test('calls onClick when not disabled', () => { |
| 26 | + const handleClick = jest.fn() |
| 27 | + render(<Link href='/test' label='Enabled Link' onClick={handleClick} />) |
| 28 | + |
| 29 | + const linkElement = screen.getByRole('link', { name: /enabled link/i }) |
| 30 | + fireEvent.click(linkElement) |
| 31 | + expect(handleClick).toHaveBeenCalledTimes(1) |
| 32 | + }) |
| 33 | + |
| 34 | + test('renders Clickable component with proper props', () => { |
| 35 | + render(<Link href='/test' label='Clickable Test' />) |
| 36 | + const clickableElement = screen.getByText(/clickable test/i) |
| 37 | + expect(clickableElement).toBeInTheDocument() |
| 38 | + }) |
| 39 | +}) |
0 commit comments