From ef43d2967e27f3213a77cf2266249a722a35e3ba Mon Sep 17 00:00:00 2001 From: summericy <671412430@qq.com> Date: Mon, 6 Nov 2023 18:22:09 +0800 Subject: [PATCH] test(ui): add Avatar test case --- .../ui/src/components/avatar/Avatar.test.tsx | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 packages/ui/src/components/avatar/Avatar.test.tsx diff --git a/packages/ui/src/components/avatar/Avatar.test.tsx b/packages/ui/src/components/avatar/Avatar.test.tsx new file mode 100644 index 00000000..61ce7629 --- /dev/null +++ b/packages/ui/src/components/avatar/Avatar.test.tsx @@ -0,0 +1,43 @@ +import { render, fireEvent } from '../../__tests__/utils'; +import { DAvatar } from './Avatar'; + +describe('DAvatar', () => { + it('renders without crashing', () => { + const { getByRole } = render(); + expect(getByRole('img')).toBeInTheDocument(); + }); + + it('renders with an image when dImg is provided', () => { + const testImageUrl = 'test-image-url'; + const { getByRole } = render(); + const image = getByRole('img'); + expect(image).toHaveAttribute('src', testImageUrl); + }); + + it('switches to icon type when dImg is not provided but dIcon is', () => { + const TestIcon = () => Test Icon; + const { getByText } = render(} />); + expect(getByText('Test Icon')).toBeInTheDocument(); + }); + + it('switches to text type when neither dImg nor dIcon is provided', () => { + const { getByText } = render(); + expect(getByText('A')).toBeInTheDocument(); + }); + it('handles image load errors', () => { + const { getByRole } = render(); + 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(); + 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); + }); +});