84f488a253
- 修复 animations.test.tsx 中的 Variant 类型访问问题 - 清理 9 个测试文件中的未使用导入 - 使用可选链操作符处理可能为 undefined 的属性访问 - 修复 mock 组件缺少 displayName 的 ESLint 错误
93 lines
2.5 KiB
TypeScript
93 lines
2.5 KiB
TypeScript
import { render, screen, fireEvent } from '@testing-library/react';
|
|
import '@testing-library/jest-dom';
|
|
|
|
const MockRippleButton = ({
|
|
children,
|
|
onClick,
|
|
disabled,
|
|
variant,
|
|
size
|
|
}: {
|
|
children: React.ReactNode;
|
|
onClick?: () => void;
|
|
disabled?: boolean;
|
|
variant?: string;
|
|
size?: string;
|
|
}) => {
|
|
return (
|
|
<button
|
|
onClick={onClick}
|
|
disabled={disabled}
|
|
className={`${variant || 'default'} ${size || 'default'}`}
|
|
>
|
|
{children}
|
|
</button>
|
|
);
|
|
};
|
|
|
|
jest.mock('framer-motion', () => ({
|
|
motion: {
|
|
div: ({ children, onClick }: { children: React.ReactNode; onClick?: () => void }) => (
|
|
<div onClick={onClick}>{children}</div>
|
|
),
|
|
},
|
|
AnimatePresence: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
|
}));
|
|
|
|
describe('RippleButton', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
describe('Rendering', () => {
|
|
it('should render button', () => {
|
|
render(<MockRippleButton>Click me</MockRippleButton>);
|
|
const button = screen.getByRole('button', { name: /click me/i });
|
|
expect(button).toBeInTheDocument();
|
|
});
|
|
|
|
it('should render children', () => {
|
|
render(<MockRippleButton>Test Button</MockRippleButton>);
|
|
expect(screen.getByText('Test Button')).toBeInTheDocument();
|
|
});
|
|
|
|
it('should apply variant classes', () => {
|
|
render(<MockRippleButton variant="secondary">Secondary</MockRippleButton>);
|
|
const button = screen.getByRole('button');
|
|
expect(button).toHaveClass('secondary');
|
|
});
|
|
|
|
it('should apply size classes', () => {
|
|
render(<MockRippleButton size="lg">Large</MockRippleButton>);
|
|
const button = screen.getByRole('button');
|
|
expect(button).toHaveClass('lg');
|
|
});
|
|
});
|
|
|
|
describe('Functionality', () => {
|
|
it('should handle click events', () => {
|
|
const handleClick = jest.fn();
|
|
render(<MockRippleButton onClick={handleClick}>Click me</MockRippleButton>);
|
|
|
|
const button = screen.getByRole('button');
|
|
fireEvent.click(button);
|
|
|
|
expect(handleClick).toHaveBeenCalled();
|
|
});
|
|
|
|
it('should be disabled when disabled prop is true', () => {
|
|
render(<MockRippleButton disabled>Disabled</MockRippleButton>);
|
|
const button = screen.getByRole('button');
|
|
expect(button).toBeDisabled();
|
|
});
|
|
});
|
|
|
|
describe('Accessibility', () => {
|
|
it('should be focusable', () => {
|
|
render(<MockRippleButton>Focus me</MockRippleButton>);
|
|
const button = screen.getByRole('button');
|
|
expect(button).not.toHaveAttribute('tabindex', '-1');
|
|
});
|
|
});
|
|
});
|