import { act } from 'react'; import { fireEvent, render } from '@testing-library/react'; import '@testing-library/jest-dom'; import { describe, expect, it, vi } from 'vitest'; import { Toast } from './Toast'; describe('Toast component', () => { it('should render bottom-right with default animation correctly', () => { const handleClose = vi.fn(); const { getByTestId } = render(
Test
, ); const toastContainer = getByTestId('ockToastViewport'); expect(toastContainer).toBeInTheDocument(); expect(toastContainer).toHaveClass('bottom-5 left-3/4'); const toast = getByTestId('ockToast'); expect(toast).toBeInTheDocument(); expect(toast).toHaveClass('animate-enterRight'); const closeButton = getByTestId('ockCloseButton'); expect(closeButton).toBeInTheDocument(); }); it('should render bottom-right with custom animation correctly', () => { const handleClose = vi.fn(); const { getByTestId } = render(
Test
, ); const toastContainer = getByTestId('ockToastViewport'); expect(toastContainer).toBeInTheDocument(); expect(toastContainer).toHaveClass('bottom-5 left-3/4'); const toast = getByTestId('ockToast'); expect(toast).toBeInTheDocument(); expect(toast).toHaveClass('animate-enterRight'); const closeButton = getByTestId('ockCloseButton'); expect(closeButton).toBeInTheDocument(); }); it('should render top-right with default animation correctly', () => { const handleClose = vi.fn(); const { getByTestId } = render(
Test
, ); const toastContainer = getByTestId('ockToastViewport'); expect(toastContainer).toBeInTheDocument(); expect(toastContainer).toHaveClass('top-[100px] left-3/4'); const toast = getByTestId('ockToast'); expect(toast).toBeInTheDocument(); expect(toast).toHaveClass('animate-enterRight'); const closeButton = getByTestId('ockCloseButton'); expect(closeButton).toBeInTheDocument(); }); it('should render top-right with custom animation correctly', () => { const handleClose = vi.fn(); const { getByTestId } = render(
Test
, ); const toastContainer = getByTestId('ockToastViewport'); expect(toastContainer).toBeInTheDocument(); expect(toastContainer).toHaveClass('top-[100px] left-3/4'); const toast = getByTestId('ockToast'); expect(toast).toBeInTheDocument(); expect(toast).toHaveClass('animate-enterRight'); const closeButton = getByTestId('ockCloseButton'); expect(closeButton).toBeInTheDocument(); }); it('should render top-center with default animation correctly', () => { const handleClose = vi.fn(); const { getByTestId } = render(
Test
, ); const toastContainer = getByTestId('ockToastViewport'); expect(toastContainer).toBeInTheDocument(); expect(toastContainer).toHaveClass('top-[100px] left-2/4'); const toast = getByTestId('ockToast'); expect(toast).toBeInTheDocument(); expect(toast).toHaveClass('animate-enterDown'); const closeButton = getByTestId('ockCloseButton'); expect(closeButton).toBeInTheDocument(); }); it('should render top-center with custom animation correctly', () => { const handleClose = vi.fn(); const { getByTestId } = render(
Test
, ); const toastContainer = getByTestId('ockToastViewport'); expect(toastContainer).toBeInTheDocument(); expect(toastContainer).toHaveClass('top-[100px] left-2/4'); const toast = getByTestId('ockToast'); expect(toast).toBeInTheDocument(); expect(toast).toHaveClass('animate-enterDown'); const closeButton = getByTestId('ockCloseButton'); expect(closeButton).toBeInTheDocument(); }); it('should render bottom-center with default animation correctly', () => { const handleClose = vi.fn(); const { getByTestId } = render(
Test
, ); const toastContainer = getByTestId('ockToastViewport'); expect(toastContainer).toBeInTheDocument(); expect(toastContainer).toHaveClass('bottom-5 left-2/4'); const toast = getByTestId('ockToast'); expect(toast).toBeInTheDocument(); expect(toast).toHaveClass('animate-enterUp'); const closeButton = getByTestId('ockCloseButton'); expect(closeButton).toBeInTheDocument(); }); it('should render bottom-center with custom animation correctly', () => { const handleClose = vi.fn(); const { getByTestId } = render(
Test
, ); const toastContainer = getByTestId('ockToastViewport'); expect(toastContainer).toBeInTheDocument(); expect(toastContainer).toHaveClass('bottom-5 left-2/4'); const toast = getByTestId('ockToast'); expect(toast).toBeInTheDocument(); expect(toast).toHaveClass('animate-enterUp'); const closeButton = getByTestId('ockCloseButton'); expect(closeButton).toBeInTheDocument(); }); it('should apply custom className correctly', () => { const handleClose = vi.fn(); const { getByTestId } = render(
Test
, ); const toast = getByTestId('ockToast'); expect(toast).toHaveClass('custom-class'); }); it('should not be visible when open is false', () => { const handleClose = vi.fn(); const { queryByTestId } = render(
Test
, ); const toastContainer = queryByTestId('ockToast'); expect(toastContainer).not.toBeInTheDocument(); }); it('should close when close button is clicked', () => { const handleClose = vi.fn(); const { getByTestId } = render(
Test
, ); const closeButton = getByTestId('ockCloseButton'); fireEvent.click(closeButton); expect(handleClose).toHaveBeenCalled(); }); it('should render children correctly', () => { const handleClose = vi.fn(); const { getByText } = render(
Test
, ); const text = getByText('Test'); expect(text).toBeInTheDocument(); }); it('should disappear after durationMs', async () => { vi.useFakeTimers(); const handleClose = vi.fn(); const durationMs = 2000; render(
Test
, ); expect(handleClose).not.toHaveBeenCalled(); await act(async () => { vi.advanceTimersByTime(durationMs); }); expect(handleClose).toHaveBeenCalled(); vi.useRealTimers(); }); });