summaryrefslogtreecommitdiff
path: root/src/components/ui/input.test.tsx
blob: 036352b64abaf4a875d65dbec1665b1f983e0594 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { render, screen } from '@testing-library/react';
import { describe, expect, test } from 'vitest';
import { Input } from './input';
import userEvent from '@testing-library/user-event';
import { vi } from 'vitest';

describe('Input', () => {
  test('renders an input with the expected slot attribute', () => {
    render(<Input aria-label='Email' />);

    const input = screen.getByLabelText('Email');
    expect(input).toBeInTheDocument();
    expect(input).toHaveAttribute('data-slot', 'input');
  });

  test('supports input type and disabled props', () => {
    render(<Input aria-label='Password' type='password' disabled />);

    const input = screen.getByLabelText('Password');
    expect(input).toHaveAttribute('type', 'password');
    expect(input).toBeDisabled();
  });

  test('handles user typing and triggers onChange event', async () => {
    const user = userEvent.setup();
    const handleChange = vi.fn();

    render(<Input aria-label="Username" onChange={handleChange} />);
    const input = screen.getByLabelText('Username');

    await user.type(input, 'hello world');

    expect(input).toHaveValue('hello world');
    expect(handleChange).toHaveBeenCalled();
    expect(handleChange).toHaveBeenCalledTimes(11);
  });
});