import { describe, it, expect } from 'vitest';
import { render, fireEvent } from '@testing-library/svelte';
import Textarea from './Textarea.svelte';
describe('m-textarea component', () => {
    const baseProps = {
        id: 'test-textarea',
        name: 'comments',
        value: 'Initial text',
    };
    it('renders textarea with correct initial value', () => {
        const { container } = render(Textarea, { props: baseProps });
        const textarea = container.querySelector('textarea');
        expect(textarea).toBeTruthy();
        expect(textarea.value).toBe('Initial text');
        expect(textarea.getAttribute('name')).toBe('comments');
        expect(textarea.getAttribute('id')).toBe('test-textarea');
    });
    it('updates value on user input', async () => {
        const { container } = render(Textarea, { props: baseProps });
        const textarea = container.querySelector('textarea');
        await fireEvent.input(textarea, { target: { value: 'Updated text' } });
        expect(textarea.value).toBe('Updated text');
    });
    it('applies placeholder text', () => {
        const { container } = render(Textarea, {
            props: { ...baseProps, placeholder: 'Enter text here' },
        });
        const textarea = container.querySelector('textarea');
        expect(textarea.placeholder).toBe('Enter text here');
    });
    it('applies disabled prop', () => {
        const { container } = render(Textarea, {
            props: { ...baseProps, disabled: true },
        });
        const textarea = container.querySelector('textarea');
        expect(textarea.disabled).toBe(true);
    });
    it('applies readonly prop', () => {
        const { container } = render(Textarea, {
            props: { ...baseProps, readonly: true },
        });
        const textarea = container.querySelector('textarea');
        expect(textarea.readOnly).toBe(true);
    });
    it('applies is-invalid class when isinvalid is true', () => {
        const { container } = render(Textarea, {
            props: { ...baseProps, isinvalid: true },
        });
        const textarea = container.querySelector('textarea');
        expect(textarea.classList.contains('is-invalid')).toBe(true);
        expect(textarea.getAttribute('aria-invalid')).toBe('true');
    });
    it('applies rows, minlength and maxlength props', () => {
        const { container } = render(Textarea, {
            props: { ...baseProps, rows: 5, minlength: 10, maxlength: 100 },
        });
        const textarea = container.querySelector('textarea');
        expect(textarea.rows).toBe(5);
        expect(textarea.minLength).toBe(10);
        expect(textarea.maxLength).toBe(100);
    });
    it('defaults to 2 rows if not specified', () => {
        const { container } = render(Textarea, { props: baseProps });
        const textarea = container.querySelector('textarea');
        expect(textarea.rows).toBe(2);
    });
});
