From 90ac52482c9eb1afda5839d87005532831879506 Mon Sep 17 00:00:00 2001 From: twwu Date: Tue, 17 Jun 2025 16:48:52 +0800 Subject: [PATCH] test: add unit tests for ActionButton component with various states and sizes --- .../base/action-button/index.spec.tsx | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 web/app/components/base/action-button/index.spec.tsx diff --git a/web/app/components/base/action-button/index.spec.tsx b/web/app/components/base/action-button/index.spec.tsx new file mode 100644 index 0000000000..76c8eebda0 --- /dev/null +++ b/web/app/components/base/action-button/index.spec.tsx @@ -0,0 +1,76 @@ +import { render, screen } from '@testing-library/react' +import { ActionButton, ActionButtonState } from './index' + +describe('ActionButton', () => { + test('renders button with default props', () => { + render(Click me) + const button = screen.getByRole('button', { name: 'Click me' }) + expect(button).toBeInTheDocument() + expect(button.classList.contains('action-btn')).toBe(true) + expect(button.classList.contains('action-btn-m')).toBe(true) + }) + + test('renders button with xs size', () => { + render(Small Button) + const button = screen.getByRole('button', { name: 'Small Button' }) + expect(button.classList.contains('action-btn-xs')).toBe(true) + }) + + test('renders button with l size', () => { + render(Large Button) + const button = screen.getByRole('button', { name: 'Large Button' }) + expect(button.classList.contains('action-btn-l')).toBe(true) + }) + + test('renders button with xl size', () => { + render(Extra Large Button) + const button = screen.getByRole('button', { name: 'Extra Large Button' }) + expect(button.classList.contains('action-btn-xl')).toBe(true) + }) + + test('applies correct state classes', () => { + const { rerender } = render( + Destructive, + ) + let button = screen.getByRole('button', { name: 'Destructive' }) + expect(button.classList.contains('action-btn-destructive')).toBe(true) + + rerender(Active) + button = screen.getByRole('button', { name: 'Active' }) + expect(button.classList.contains('action-btn-active')).toBe(true) + + rerender(Disabled) + button = screen.getByRole('button', { name: 'Disabled' }) + expect(button.classList.contains('action-btn-disabled')).toBe(true) + + rerender(Hover) + button = screen.getByRole('button', { name: 'Hover' }) + expect(button.classList.contains('action-btn-hover')).toBe(true) + }) + + test('applies custom className', () => { + render(Custom Class) + const button = screen.getByRole('button', { name: 'Custom Class' }) + expect(button.classList.contains('custom-class')).toBe(true) + }) + + test('applies custom style', () => { + render( + + Custom Style + , + ) + const button = screen.getByRole('button', { name: 'Custom Style' }) + expect(button).toHaveStyle({ + color: 'red', + backgroundColor: 'blue', + }) + }) + + test('forwards additional button props', () => { + render(Disabled Button) + const button = screen.getByRole('button', { name: 'Disabled Button' }) + expect(button).toBeDisabled() + expect(button).toHaveAttribute('data-testid', 'test-button') + }) +})