|
| 1 | +import { render, screen } from '@testing-library/preact'; |
| 2 | +import { fireEvent } from '@testing-library/preact'; |
| 3 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 4 | + |
| 5 | +// Helper to flush timers when using setTimeout |
| 6 | +const advanceTimers = async (ms: number) => { |
| 7 | + await vi.advanceTimersByTimeAsync(ms); |
| 8 | +}; |
| 9 | + |
| 10 | +describe('Alert component & showAlert', () => { |
| 11 | + beforeEach(() => { |
| 12 | + vi.useFakeTimers(); |
| 13 | + window.scrollTo = vi.fn(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('renders an alert with heading and text', async () => { |
| 17 | + const real = await vi.importActual<typeof import('../Alert')>('../Alert'); |
| 18 | + real.showAlert('Some text', 'danger', 'abc', 'Heading Text'); |
| 19 | + render(<real.ErrorAlert />); |
| 20 | + expect(screen.getByText('Some text')).toBeTruthy(); |
| 21 | + expect(screen.getByTestId('alert-heading')).toHaveTextContent('Heading Text'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('suppresses alert containing Failed to fetch', async () => { |
| 25 | + const real = await vi.importActual<typeof import('../Alert')>('../Alert'); |
| 26 | + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 27 | + real.showAlert('Failed to fetch resource', 'danger'); |
| 28 | + render(<real.ErrorAlert />); |
| 29 | + expect(screen.queryByText('Failed to fetch resource')).toBeNull(); |
| 30 | + expect(warnSpy).toHaveBeenCalled(); |
| 31 | + warnSpy.mockRestore(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('auto dismisses alert after timeout', async () => { |
| 35 | + const real = await vi.importActual<typeof import('../Alert')>('../Alert'); |
| 36 | + real.showAlert('Autoclose', 'success', 'auto', 'Auto Heading', 2000); |
| 37 | + const { rerender } = render(<real.ErrorAlert />); |
| 38 | + expect(screen.getByText('Autoclose')).toBeTruthy(); |
| 39 | + await advanceTimers(2000); |
| 40 | + rerender(<real.ErrorAlert />); |
| 41 | + expect(screen.queryByText('Autoclose')).toBeNull(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('replaces alert with same id and clears previous timeout', async () => { |
| 45 | + const real = await vi.importActual<typeof import('../Alert')>('../Alert'); |
| 46 | + const clearSpy = vi.spyOn(window, 'clearTimeout'); |
| 47 | + real.showAlert('First', 'warning', 'same', 'First Heading', 5000); |
| 48 | + const utils = render(<real.ErrorAlert />); |
| 49 | + expect(screen.getByText('First')).toBeTruthy(); |
| 50 | + real.showAlert('Second', 'warning', 'same', 'Second Heading'); |
| 51 | + utils.rerender(<real.ErrorAlert />); |
| 52 | + expect(screen.getByText('Second')).toBeTruthy(); |
| 53 | + expect(screen.queryByText('First')).toBeNull(); |
| 54 | + expect(clearSpy).toHaveBeenCalled(); |
| 55 | + clearSpy.mockRestore(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('manual dismiss via close button triggers onClose and removes alert', async () => { |
| 59 | + const real = await vi.importActual<typeof import('../Alert')>('../Alert'); |
| 60 | + real.showAlert('Dismiss me', 'danger', 'dismiss', 'Dismiss Heading'); |
| 61 | + const { rerender } = render(<real.ErrorAlert />); |
| 62 | + expect(screen.getByText('Dismiss me')).toBeTruthy(); |
| 63 | + const closeButtons = screen.getAllByTestId('close-alert'); |
| 64 | + fireEvent.click(closeButtons[closeButtons.length - 1]); |
| 65 | + rerender(<real.ErrorAlert />); |
| 66 | + expect(screen.queryByText('Dismiss me')).toBeNull(); |
| 67 | + }); |
| 68 | +}); |
0 commit comments