import { describe, it, expect, vi } from 'vitest';
import { render, fireEvent } from '@testing-library/svelte';
import Modal from './Modal.svelte';
describe('m-modal component', () => {
    const title = 'Test Modal';
    const description = 'Modal description';
    it('renders the title and description', () => {
        const { getByText } = render(Modal, {
            props: {
                open: true,
                title,
                description,
            },
        });
        expect(getByText(title)).toBeTruthy();
        expect(getByText(description)).toBeTruthy();
    });
    it('applies is-open class when open is true', () => {
        const { container } = render(Modal, {
            props: { open: true, title },
        });
        expect(container.querySelector('.mc-modal')).toHaveClass('is-open');
    });
    it('does not show close button if closable is false', () => {
        const { queryByRole } = render(Modal, {
            props: { open: true, title, closable: false },
        });
        expect(queryByRole('button', { name: 'Close' })).toBeNull();
    });
    it('emits update:open event with false when close button is clicked', async () => {
        const onUpdateOpen = vi.fn();
        const { container } = render(Modal, {
            props: { open: true, title, onUpdateOpen },
        });
        const closeButton = container.querySelector('button[aria-label="Close"]');
        const handler = vi.fn();
        container.addEventListener('update:open', handler);
        await fireEvent.click(closeButton);
        expect(onUpdateOpen).toHaveBeenCalledTimes(1);
        expect(handler).toHaveBeenCalledTimes(1);
        expect(handler.mock.calls[0][0].detail).toBe(false);
    });
    it('sets aria-hidden to false when open and true when closed', async () => {
        const { container, rerender } = render(Modal, {
            props: { open: false, title },
        });
        const modal = container.querySelector('.mc-modal');
        expect(modal.getAttribute('aria-hidden')).toBe('true');
        await rerender({ open: true, title });
        expect(modal.getAttribute('aria-hidden')).toBe('false');
    });
});
