From b7f0a046e30916545cc7bf62238f48285b6699c2 Mon Sep 17 00:00:00 2001 From: KatiaLiu Date: Wed, 25 Mar 2026 23:23:26 +0800 Subject: Add Vitest testing setup and Button component tests (#11) * Add Vitest testing setup and Button component tests - Install Vitest and related dependencies - Configure Vitest with React support - Create Button component test cases - Add test scripts to package.json - Create TESTING.md documentation - Update test comments to English for consistency * Update package.json Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Bertrand Yuan * Update package.json Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Bertrand Yuan * remove TESTING.md for the moment There are many parts not matched with current content. Signed-off-by: Bertrand Yuan --------- Signed-off-by: Bertrand Yuan Signed-off-by: Bertrand Yuan Co-authored-by: Bertrand Yuan Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Bertrand Yuan --- src/components/ui/button.test.tsx | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/components/ui/button.test.tsx (limited to 'src/components/ui/button.test.tsx') diff --git a/src/components/ui/button.test.tsx b/src/components/ui/button.test.tsx new file mode 100644 index 0000000..314e9bf --- /dev/null +++ b/src/components/ui/button.test.tsx @@ -0,0 +1,62 @@ +import { describe, test, vi, expect } from 'vitest'; +import { render, screen } from '@testing-library/react'; +import { Button } from './button'; + +// Test Button component rendering +describe('Button', () => { + // Test default button rendering + test('renders default button with text', () => { + render(); + const button = screen.getByText('Click Me'); + expect(button).toBeInTheDocument(); + expect(button).toHaveAttribute('data-slot', 'button'); + }); + + // Test buttons with different variants + test('renders button with different variants', () => { + const variants = ['default', 'destructive', 'outline', 'secondary', 'ghost', 'link']; + variants.forEach((variant) => { + render(); + const button = screen.getByText(`${variant} Variant`); + expect(button).toBeInTheDocument(); + }); + }); + + // Test buttons with different sizes + test('renders button with different sizes', () => { + const sizes = ['default', 'sm', 'lg', 'icon']; + sizes.forEach((size) => { + render(); + const button = screen.getByText(`${size} Size`); + expect(button).toBeInTheDocument(); + }); + }); + + // Test button click events + test('handles click events', () => { + const handleClick = vi.fn(); + render(); + const button = screen.getByText('Click Test'); + button.click(); + expect(handleClick).toHaveBeenCalledTimes(1); + }); + + // Test disabled state + test('renders disabled button', () => { + render(); + const button = screen.getByText('Disabled Button'); + expect(button).toBeDisabled(); + }); + + // Test asChild property + test('renders as a link when asChild is true', () => { + render( + + ); + const link = screen.getByText('As Child Link'); + expect(link).toBeInTheDocument(); + expect(link.tagName).toBe('A'); + }); +}); -- cgit v1.2.3