import { describe, it, expect, vi } from 'vitest';
import { render, fireEvent, waitFor } from '@testing-library/svelte';
import Toaster from './Toaster.svelte';
describe('m-toaster component', () => {
    it('renders with description and proper classes', () => {
        const { container } = render(Toaster, {
            props: {
                open: true,
                description: 'This is a toaster message',
                status: 'success',
                position: 'top',
            },
        });
        const toaster = container.querySelector('.mc-toaster');
        expect(toaster).toBeTruthy();
        expect(toaster.textContent).toContain('This is a toaster message');
        expect(toaster.classList.contains('is-open')).toBe(true);
        expect(toaster.classList.contains('mc-toaster--success')).toBe(true);
        expect(toaster.classList.contains('mc-toaster--top')).toBe(true);
    });
    it('emits "update:open" when closed via close button', async () => {
        const { container } = render(Toaster, {
            props: {
                open: true,
                description: 'Closable toaster',
                closable: true,
            },
        });
        const toaster = container.querySelector('section');
        const mockHandler = vi.fn();
        toaster?.addEventListener('update:open', mockHandler);
        const closeButton = container.querySelector('button');
        expect(closeButton).toBeTruthy();
        await fireEvent.click(closeButton);
        expect(mockHandler).toHaveBeenCalledTimes(1);
        expect(mockHandler.mock.calls[0][0].detail).toBe(false);
    });
    it('automatically closes after timeout', async () => {
        vi.useFakeTimers();
        const { container } = render(Toaster, {
            props: {
                open: true,
                description: 'Timeout toaster',
                timeout: 2000,
            },
        });
        const toaster = container.querySelector('section');
        const mockHandler = vi.fn();
        toaster?.addEventListener('update:open', mockHandler);
        vi.advanceTimersByTime(2000);
        await waitFor(() => {
            expect(mockHandler).toHaveBeenCalled();
            expect(mockHandler.mock.calls[0][0].detail).toBe(false);
        });
        vi.useRealTimers();
    });
    it('shows progress bar when enabled', async () => {
        vi.useFakeTimers();
        const { container } = render(Toaster, {
            props: {
                open: true,
                description: 'Progress toaster',
                timeout: 3000,
                progress: true,
            },
        });
        const progressBar = container.querySelector('.mc-linear-progressbar-buffer__indicator');
        expect(progressBar).toBeTruthy();
        vi.advanceTimersByTime(1500);
        await waitFor(() => {
            const now = parseFloat(progressBar.getAttribute('aria-valuenow') || '0');
            expect(now).toBeGreaterThan(0);
            expect(now).toBeLessThanOrEqual(100);
        });
        vi.useRealTimers();
    });
    it('does not render close button when closable is false', () => {
        const { container } = render(Toaster, {
            props: {
                open: true,
                description: 'Non-closable toaster',
                closable: false,
            },
        });
        const closeButton = container.querySelector('button');
        expect(closeButton).toBeNull();
    });
    it('does not show progress bar if progress is false', () => {
        const { container } = render(Toaster, {
            props: {
                open: true,
                description: 'No progress bar',
                progress: false,
                timeout: 3000,
            },
        });
        const progressBar = container.querySelector('.mc-linear-progressbar-buffer');
        expect(progressBar).toBeNull();
    });
});
