|
| 1 | +import { render, fireEvent } from '../../__tests__/utils'; |
| 2 | +import { DAvatar } from './Avatar'; |
| 3 | + |
| 4 | +describe('DAvatar', () => { |
| 5 | + it('renders without crashing', () => { |
| 6 | + const { getByRole } = render(<DAvatar />); |
| 7 | + expect(getByRole('img')).toBeInTheDocument(); |
| 8 | + }); |
| 9 | + |
| 10 | + it('renders with an image when dImg is provided', () => { |
| 11 | + const testImageUrl = 'test-image-url'; |
| 12 | + const { getByRole } = render(<DAvatar dImg={{ src: testImageUrl }} />); |
| 13 | + const image = getByRole('img'); |
| 14 | + expect(image).toHaveAttribute('src', testImageUrl); |
| 15 | + }); |
| 16 | + |
| 17 | + it('switches to icon type when dImg is not provided but dIcon is', () => { |
| 18 | + const TestIcon = () => <span>Test Icon</span>; |
| 19 | + const { getByText } = render(<DAvatar dIcon={<TestIcon />} />); |
| 20 | + expect(getByText('Test Icon')).toBeInTheDocument(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('switches to text type when neither dImg nor dIcon is provided', () => { |
| 24 | + const { getByText } = render(<DAvatar dText="A" />); |
| 25 | + expect(getByText('A')).toBeInTheDocument(); |
| 26 | + }); |
| 27 | + it('handles image load errors', () => { |
| 28 | + const { getByRole } = render(<DAvatar dImg={{ src: 'test-image.jpg', alt: 'Test Image' }} />); |
| 29 | + const image = getByRole('img'); |
| 30 | + fireEvent.error(image); |
| 31 | + expect(image).toHaveAttribute('src', 'test-image.jpg'); |
| 32 | + }); |
| 33 | + it('should scale the text properly when necessary', () => { |
| 34 | + const dSize = 40; |
| 35 | + Object.defineProperty(HTMLElement.prototype, 'scrollWidth', { configurable: true, value: 100 }); |
| 36 | + Object.defineProperty(HTMLElement.prototype, 'scrollHeight', { configurable: true, value: 20 }); |
| 37 | + const { getByText } = render(<DAvatar dSize={dSize} dText="Test Text" />); |
| 38 | + const textElement = getByText('Test Text'); |
| 39 | + const expectedScaleValue = (Math.sqrt(Math.pow(dSize / 2, 2) - Math.pow(20 / 2, 2)) * 2) / 100; |
| 40 | + const expectedScale = `scale(${expectedScaleValue})`; |
| 41 | + expect(textElement.style.transform).toBe(expectedScale); |
| 42 | + }); |
| 43 | +}); |
0 commit comments