|
| 1 | +import React from 'react'; |
| 2 | +import { BrowserRouter } from 'react-router-dom'; |
| 3 | +import { render, screen, fireEvent } from '../test-utils'; |
| 4 | +import Button from './Button'; |
| 5 | + |
| 6 | +const MockIcon = (props: React.SVGProps<SVGSVGElement>) => ( |
| 7 | + <svg data-testid="mock-icon" {...props} /> |
| 8 | +); |
| 9 | + |
| 10 | +describe('Button', () => { |
| 11 | + it('renders children', () => { |
| 12 | + render(<Button>Click Me</Button>); |
| 13 | + expect(screen.getByText('Click Me')).toBeInTheDocument(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('calls onClick handler when clicked', () => { |
| 17 | + const handleClick = jest.fn(); |
| 18 | + render(<Button onClick={handleClick}>Click</Button>); |
| 19 | + fireEvent.click(screen.getByText('Click')); |
| 20 | + expect(handleClick).toHaveBeenCalledTimes(1); |
| 21 | + }); |
| 22 | + |
| 23 | + it('renders as an anchor when href is provided', () => { |
| 24 | + render(<Button href="https://example.com">Link</Button>); |
| 25 | + const anchor = screen.getByRole('link'); |
| 26 | + expect(anchor).toHaveAttribute('href', 'https://example.com'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('renders as a React Router link when `to` is provided', () => { |
| 30 | + render(<Button to="/dashboard">Go</Button>); |
| 31 | + const link = screen.getByRole('link'); |
| 32 | + expect(link).toHaveAttribute('href', '/dashboard'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('renders with iconBefore', () => { |
| 36 | + render( |
| 37 | + <Button iconBefore={<MockIcon aria-label="github" />}>Create</Button> |
| 38 | + ); |
| 39 | + expect(screen.getByLabelText('github')).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('renders disabled state', () => { |
| 43 | + render(<Button disabled>Disabled</Button>); |
| 44 | + expect(screen.getByRole('button')).toBeDisabled(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('uses aria-label when provided', () => { |
| 48 | + render(<Button aria-label="Upload" iconOnly />); |
| 49 | + expect(screen.getByLabelText('Upload')).toBeInTheDocument(); |
| 50 | + }); |
| 51 | +}); |
0 commit comments