Skip to content

Commit

Permalink
test(ui): add Avatar test case
Browse files Browse the repository at this point in the history
  • Loading branch information
summericy authored and xiejay97 committed Nov 7, 2023
1 parent 12122b3 commit 6de6576
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions packages/ui/src/components/avatar/Avatar.test.tsx
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);
});
});

0 comments on commit 6de6576

Please sign in to comment.