|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { AddTodo } from './AddTodo'; |
| 5 | + |
| 6 | +describe('AddTodo', () => { |
| 7 | + it('should render input and button', () => { |
| 8 | + render(<AddTodo onAdd={vi.fn()} />); |
| 9 | + |
| 10 | + expect(screen.getByPlaceholderText('Add a new task...')).toBeInTheDocument(); |
| 11 | + expect(screen.getByRole('button', { name: /add task/i })).toBeInTheDocument(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should disable button when input is empty', () => { |
| 15 | + render(<AddTodo onAdd={vi.fn()} />); |
| 16 | + |
| 17 | + const button = screen.getByRole('button', { name: /add task/i }); |
| 18 | + expect(button).toBeDisabled(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should enable button when input has text', async () => { |
| 22 | + const user = userEvent.setup(); |
| 23 | + render(<AddTodo onAdd={vi.fn()} />); |
| 24 | + |
| 25 | + const input = screen.getByPlaceholderText('Add a new task...'); |
| 26 | + await user.type(input, 'New task'); |
| 27 | + |
| 28 | + const button = screen.getByRole('button', { name: /add task: new task/i }); |
| 29 | + expect(button).not.toBeDisabled(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should call onAdd with trimmed text on submit', async () => { |
| 33 | + const user = userEvent.setup(); |
| 34 | + const onAdd = vi.fn(); |
| 35 | + render(<AddTodo onAdd={onAdd} />); |
| 36 | + |
| 37 | + const input = screen.getByPlaceholderText('Add a new task...'); |
| 38 | + await user.type(input, ' Buy groceries '); |
| 39 | + await user.keyboard('{Enter}'); |
| 40 | + |
| 41 | + expect(onAdd).toHaveBeenCalledWith('Buy groceries'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should clear input after submission', async () => { |
| 45 | + const user = userEvent.setup(); |
| 46 | + render(<AddTodo onAdd={vi.fn()} />); |
| 47 | + |
| 48 | + const input = screen.getByPlaceholderText('Add a new task...'); |
| 49 | + await user.type(input, 'New task'); |
| 50 | + await user.keyboard('{Enter}'); |
| 51 | + |
| 52 | + expect(input).toHaveValue(''); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should not submit empty input', async () => { |
| 56 | + const user = userEvent.setup(); |
| 57 | + const onAdd = vi.fn(); |
| 58 | + render(<AddTodo onAdd={onAdd} />); |
| 59 | + |
| 60 | + const input = screen.getByPlaceholderText('Add a new task...'); |
| 61 | + await user.type(input, ' '); |
| 62 | + await user.keyboard('{Enter}'); |
| 63 | + |
| 64 | + expect(onAdd).not.toHaveBeenCalled(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should show keyboard hint when focused with text', async () => { |
| 68 | + const user = userEvent.setup(); |
| 69 | + render(<AddTodo onAdd={vi.fn()} />); |
| 70 | + |
| 71 | + const input = screen.getByPlaceholderText('Add a new task...'); |
| 72 | + await user.type(input, 'Task'); |
| 73 | + |
| 74 | + expect(screen.getByText(/press/i)).toBeInTheDocument(); |
| 75 | + expect(screen.getByText('Enter')).toBeInTheDocument(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should have proper accessibility attributes', () => { |
| 79 | + render(<AddTodo onAdd={vi.fn()} />); |
| 80 | + |
| 81 | + const form = screen.getByRole('search', { name: /add new task/i }); |
| 82 | + expect(form).toBeInTheDocument(); |
| 83 | + |
| 84 | + const input = screen.getByLabelText(/add a new task/i); |
| 85 | + expect(input).toBeInTheDocument(); |
| 86 | + }); |
| 87 | +}); |
0 commit comments