-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { render, fireEvent } from '../../__tests__/utils'; | ||
import { DAvatar } from './Avatar'; | ||
|
||
describe('DAvatar', () => { | ||
it('renders without crashing', () => { | ||
const { getByRole } = render(<DAvatar />); | ||
expect(getByRole('img')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders with an image when dImg is provided', () => { | ||
const testImageUrl = 'test-image-url'; | ||
const { getByRole } = render(<DAvatar dImg={{ src: testImageUrl }} />); | ||
const image = getByRole('img'); | ||
expect(image).toHaveAttribute('src', testImageUrl); | ||
}); | ||
|
||
it('switches to icon type when dImg is not provided but dIcon is', () => { | ||
const TestIcon = () => <span>Test Icon</span>; | ||
const { getByText } = render(<DAvatar dIcon={<TestIcon />} />); | ||
expect(getByText('Test Icon')).toBeInTheDocument(); | ||
}); | ||
|
||
it('switches to text type when neither dImg nor dIcon is provided', () => { | ||
const { getByText } = render(<DAvatar dText="A" />); | ||
expect(getByText('A')).toBeInTheDocument(); | ||
}); | ||
it('handles image load errors', () => { | ||
const { getByRole } = render(<DAvatar dImg={{ src: 'test-image.jpg', alt: 'Test Image' }} />); | ||
const image = getByRole('img'); | ||
fireEvent.error(image); | ||
expect(image).toHaveAttribute('src', 'test-image.jpg'); | ||
}); | ||
it('should scale the text properly when necessary', () => { | ||
const dSize = 40; | ||
Object.defineProperty(HTMLElement.prototype, 'scrollWidth', { configurable: true, value: 100 }); | ||
Object.defineProperty(HTMLElement.prototype, 'scrollHeight', { configurable: true, value: 20 }); | ||
const { getByText } = render(<DAvatar dSize={dSize} dText="Test Text" />); | ||
const textElement = getByText('Test Text'); | ||
const expectedScaleValue = (Math.sqrt(Math.pow(dSize / 2, 2) - Math.pow(20 / 2, 2)) * 2) / 100; | ||
const expectedScale = `scale(${expectedScaleValue})`; | ||
expect(textElement.style.transform).toBe(expectedScale); | ||
}); | ||
}); |