|
| 1 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 2 | +import { describe, it, expect, vi } from 'vitest'; |
| 3 | +import { MobileTray } from './MobileTray'; |
| 4 | + |
| 5 | +describe('MobileTray', () => { |
| 6 | + const defaultProps = { |
| 7 | + isOpen: true, |
| 8 | + onOverlayClick: vi.fn(), |
| 9 | + onEscKeyPress: vi.fn(), |
| 10 | + onAnimationEnd: vi.fn(), |
| 11 | + children: <div>Test Content</div>, |
| 12 | + }; |
| 13 | + |
| 14 | + it('renders children when open', () => { |
| 15 | + render(<MobileTray {...defaultProps} />); |
| 16 | + expect(screen.getByText('Test Content')).toBeInTheDocument(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('renders overlay when open', () => { |
| 20 | + render(<MobileTray {...defaultProps} />); |
| 21 | + expect(screen.getByRole('presentation')).toHaveClass('bg-black bg-opacity-20'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('does not render overlay when closed', () => { |
| 25 | + render(<MobileTray {...defaultProps} isOpen={false} />); |
| 26 | + expect(screen.queryByRole('presentation')).not.toBeInTheDocument(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('calls onOverlayClick when overlay is clicked', () => { |
| 30 | + render(<MobileTray {...defaultProps} />); |
| 31 | + fireEvent.click(screen.getByRole('presentation')); |
| 32 | + expect(defaultProps.onOverlayClick).toHaveBeenCalled(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('calls onEscKeyPress when Escape key is pressed on overlay', () => { |
| 36 | + render(<MobileTray {...defaultProps} />); |
| 37 | + fireEvent.keyDown(screen.getByRole('presentation'), { key: 'Escape' }); |
| 38 | + expect(defaultProps.onEscKeyPress).toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('calls onAnimationEnd when animation completes', () => { |
| 42 | + render(<MobileTray {...defaultProps} />); |
| 43 | + fireEvent.animationEnd(screen.getByTestId('ockMobileTray')); |
| 44 | + expect(defaultProps.onAnimationEnd).toHaveBeenCalled(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('applies custom animation class when provided', () => { |
| 48 | + render(<MobileTray {...defaultProps} animation="custom-animation" />); |
| 49 | + expect(screen.getByTestId('ockMobileTray')).toHaveClass('custom-animation'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('applies default translation classes when no animation prop is provided', () => { |
| 53 | + const { rerender } = render(<MobileTray {...defaultProps} />); |
| 54 | + expect(screen.getByTestId('ockMobileTray')).toHaveClass('translate-y-0'); |
| 55 | + |
| 56 | + rerender(<MobileTray {...defaultProps} isOpen={false} />); |
| 57 | + expect(screen.getByTestId('ockMobileTray')).toHaveClass('translate-y-full'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('applies custom className when provided', () => { |
| 61 | + render(<MobileTray {...defaultProps} className="custom-class" />); |
| 62 | + expect(screen.getByTestId('ockMobileTray')).toHaveClass('custom-class'); |
| 63 | + }); |
| 64 | +}); |
0 commit comments