|
| 1 | +import { render, screen } from '@testing-library/react' |
| 2 | +import { AnimatedNumber } from '../../src/components/Common/AnimatedNumber' |
| 3 | + |
| 4 | +// Mock framer-motion to avoid complex animation testing |
| 5 | +let mockValue = 1000 |
| 6 | +jest.mock('framer-motion', () => ({ |
| 7 | + motion: { |
| 8 | + span: ({ children, className, ...props }: React.ComponentProps<'span'>) => ( |
| 9 | + <span className={className} {...props}> |
| 10 | + {children} |
| 11 | + </span> |
| 12 | + ), |
| 13 | + }, |
| 14 | + useSpring: jest.fn((value) => { |
| 15 | + mockValue = value |
| 16 | + return { |
| 17 | + set: jest.fn((newValue) => { |
| 18 | + mockValue = newValue |
| 19 | + }), |
| 20 | + get: jest.fn(() => mockValue), |
| 21 | + } |
| 22 | + }), |
| 23 | + useTransform: jest.fn((_, transform) => { |
| 24 | + return transform(mockValue) |
| 25 | + }), |
| 26 | +})) |
| 27 | + |
| 28 | +describe('AnimatedNumber Component', () => { |
| 29 | + beforeEach(() => { |
| 30 | + jest.clearAllMocks() |
| 31 | + }) |
| 32 | + |
| 33 | + it('should render with default formatting', () => { |
| 34 | + render(<AnimatedNumber value={1000} />) |
| 35 | + |
| 36 | + // The component should render the formatted value |
| 37 | + expect(screen.getByText('1,000')).toBeInTheDocument() |
| 38 | + }) |
| 39 | + |
| 40 | + it('should apply custom className', () => { |
| 41 | + render(<AnimatedNumber value={1000} className="custom-class" />) |
| 42 | + |
| 43 | + const element = screen.getByText('1,000') |
| 44 | + expect(element).toHaveClass('custom-class') |
| 45 | + }) |
| 46 | + |
| 47 | + it('should use custom formatValue function', () => { |
| 48 | + const customFormat = (value: number) => `$${value.toFixed(2)}` |
| 49 | + render(<AnimatedNumber value={1000} formatValue={customFormat} />) |
| 50 | + |
| 51 | + expect(screen.getByText('$1000.00')).toBeInTheDocument() |
| 52 | + }) |
| 53 | + |
| 54 | + it('should handle zero value', () => { |
| 55 | + render(<AnimatedNumber value={0} />) |
| 56 | + |
| 57 | + expect(screen.getByText('0')).toBeInTheDocument() |
| 58 | + }) |
| 59 | + |
| 60 | + it('should handle negative values', () => { |
| 61 | + render(<AnimatedNumber value={-500} />) |
| 62 | + |
| 63 | + expect(screen.getByText('-500')).toBeInTheDocument() |
| 64 | + }) |
| 65 | + |
| 66 | + it('should handle decimal values with default rounding', () => { |
| 67 | + render(<AnimatedNumber value={1234.56} />) |
| 68 | + |
| 69 | + expect(screen.getByText('1,235')).toBeInTheDocument() |
| 70 | + }) |
| 71 | + |
| 72 | + it('should handle large numbers', () => { |
| 73 | + render(<AnimatedNumber value={1000000} />) |
| 74 | + |
| 75 | + expect(screen.getByText('1,000,000')).toBeInTheDocument() |
| 76 | + }) |
| 77 | + |
| 78 | + it('should use custom duration prop', () => { |
| 79 | + const { rerender } = render(<AnimatedNumber value={1000} duration={2.5} />) |
| 80 | + |
| 81 | + // Test that component renders without error with custom duration |
| 82 | + expect(screen.getByText('1,000')).toBeInTheDocument() |
| 83 | + |
| 84 | + // Rerender with different value to test duration effect |
| 85 | + rerender(<AnimatedNumber value={2000} duration={0.5} />) |
| 86 | + expect(screen.getByText('2,000')).toBeInTheDocument() |
| 87 | + }) |
| 88 | + |
| 89 | + it('should handle percentage formatting', () => { |
| 90 | + const percentFormat = (value: number) => `${(value * 100).toFixed(1)}%` |
| 91 | + render(<AnimatedNumber value={0.85} formatValue={percentFormat} />) |
| 92 | + |
| 93 | + expect(screen.getByText('85.0%')).toBeInTheDocument() |
| 94 | + }) |
| 95 | + |
| 96 | + it('should handle currency formatting', () => { |
| 97 | + const currencyFormat = (value: number) => |
| 98 | + new Intl.NumberFormat('en-US', { |
| 99 | + style: 'currency', |
| 100 | + currency: 'USD', |
| 101 | + }).format(value) |
| 102 | + |
| 103 | + render(<AnimatedNumber value={1234.56} formatValue={currencyFormat} />) |
| 104 | + |
| 105 | + expect(screen.getByText('$1,234.56')).toBeInTheDocument() |
| 106 | + }) |
| 107 | + |
| 108 | + it('should render as motion.span element', () => { |
| 109 | + render(<AnimatedNumber value={1000} />) |
| 110 | + |
| 111 | + const element = screen.getByText('1,000') |
| 112 | + expect(element.tagName).toBe('SPAN') |
| 113 | + }) |
| 114 | +}) |
0 commit comments