|
| 1 | +import { describe, it, expect, vi } from 'vitest' |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react' |
| 3 | +import TutorialCard from '../TutorialCard' |
| 4 | +import { Terminal } from 'lucide-react' |
| 5 | + |
| 6 | +// Mock the scrollToSection utility |
| 7 | +vi.mock('../../utils/scrollToSection', () => ({ |
| 8 | + scrollToSection: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +import { scrollToSection } from '../../utils/scrollToSection' |
| 12 | + |
| 13 | +describe('TutorialCard', () => { |
| 14 | + const defaultProps = { |
| 15 | + icon: Terminal, |
| 16 | + title: 'Linux Basics', |
| 17 | + description: 'Learn the command line.', |
| 18 | + topics: ['Shell', 'Files'], |
| 19 | + color: 'from-blue-500 to-blue-600', |
| 20 | + onSelect: vi.fn(), |
| 21 | + buttonLabel: 'Start Now', |
| 22 | + } |
| 23 | + |
| 24 | + it('renders title and description', () => { |
| 25 | + render(<TutorialCard {...defaultProps} />) |
| 26 | + expect(screen.getByText('Linux Basics')).toBeInTheDocument() |
| 27 | + expect(screen.getByText('Learn the command line.')).toBeInTheDocument() |
| 28 | + }) |
| 29 | + |
| 30 | + it('renders topics', () => { |
| 31 | + render(<TutorialCard {...defaultProps} />) |
| 32 | + expect(screen.getByText('Shell')).toBeInTheDocument() |
| 33 | + expect(screen.getByText('Files')).toBeInTheDocument() |
| 34 | + }) |
| 35 | + |
| 36 | + it('calls onSelect when button is clicked', () => { |
| 37 | + render(<TutorialCard {...defaultProps} />) |
| 38 | + const button = screen.getByRole('button') |
| 39 | + fireEvent.click(button) |
| 40 | + expect(defaultProps.onSelect).toHaveBeenCalled() |
| 41 | + }) |
| 42 | + |
| 43 | + it('calls scrollToSection if onSelect is not provided', () => { |
| 44 | + const propsWithoutSelect = { ...defaultProps, onSelect: undefined } |
| 45 | + render(<TutorialCard {...propsWithoutSelect} />) |
| 46 | + const button = screen.getByRole('button') |
| 47 | + fireEvent.click(button) |
| 48 | + expect(scrollToSection).toHaveBeenCalledWith('tutorials') |
| 49 | + }) |
| 50 | + |
| 51 | + it('renders custom button label', () => { |
| 52 | + render(<TutorialCard {...defaultProps} />) |
| 53 | + expect(screen.getByText('Start Now')).toBeInTheDocument() |
| 54 | + }) |
| 55 | +}) |
0 commit comments