import { act, renderHook } from '@testing-library/react' import { type PropsWithChildren } from 'react' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { BANNER_ID, BannerStackProvider, useBannerStack } from './BannerStackProvider' const wrapper = ({ children }: PropsWithChildren) => ( {children} ) describe('BannerStackProvider', () => { beforeEach(() => { vi.useFakeTimers() }) afterEach(() => { vi.useRealTimers() }) it('keeps a banner that is revived before the dismiss animation finishes', () => { const { result } = renderHook(() => useBannerStack(), { wrapper }) act(() => { result.current.addBanner({ id: BANNER_ID.EXPLORER, isDismissed: false, content: null, }) }) act(() => { result.current.dismissBanner(BANNER_ID.EXPLORER) }) act(() => { result.current.addBanner({ id: BANNER_ID.EXPLORER, isDismissed: false, content: null, }) }) act(() => { vi.advanceTimersByTime(300) }) expect(result.current.banners).toEqual([ expect.objectContaining({ id: BANNER_ID.EXPLORER, isDismissed: false, }), ]) }) it('removes a banner after the dismiss animation when it stays dismissed', () => { const { result } = renderHook(() => useBannerStack(), { wrapper }) act(() => { result.current.addBanner({ id: BANNER_ID.EXPLORER, isDismissed: false, content: null, }) }) act(() => { result.current.dismissBanner(BANNER_ID.EXPLORER) }) expect(result.current.banners[0]?.isDismissed).toBe(true) act(() => { vi.advanceTimersByTime(300) }) expect(result.current.banners).toEqual([]) }) it('cancels the pending removal when the provider unmounts', () => { const { result, unmount } = renderHook(() => useBannerStack(), { wrapper }) act(() => { result.current.addBanner({ id: BANNER_ID.EXPLORER, isDismissed: false, content: null, }) }) act(() => { result.current.dismissBanner(BANNER_ID.EXPLORER) }) expect(vi.getTimerCount()).toBe(1) unmount() expect(vi.getTimerCount()).toBe(0) }) })