Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(ui): add Avatar test case #286

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading