diff --git a/src/hooks/useNoticeTimer.ts b/src/hooks/useNoticeTimer.ts index d055f6f..714b6cd 100644 --- a/src/hooks/useNoticeTimer.ts +++ b/src/hooks/useNoticeTimer.ts @@ -48,12 +48,13 @@ export default function useNoticeTimer( // Reset when durationMs changed. React.useEffect(() => { passTimeRef.current = 0; + lastRafTimeRef.current = null; setWalking(durationMs > 0); }, [durationMs]); // Trigger update when walking changed. React.useEffect(() => { - if (!walking) { + if (!walking || durationMs <= 0) { return; } diff --git a/tests/index.test.tsx b/tests/index.test.tsx index ac34c84..ffb8602 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -267,6 +267,38 @@ describe('Notification.Basic', () => { expect(document.querySelectorAll('.updatable')).toHaveLength(0); }); + it('keeps notification open when updated from positive duration to 0', () => { + const onClose = vi.fn(); + const { instance } = renderDemo(); + + act(() => { + instance.open({ + key: 'keep-open', + description:

initial

, + duration: 1, + onClose, + }); + }); + + expect(document.querySelector('.keep-open')).toHaveTextContent('initial'); + + act(() => { + instance.open({ + key: 'keep-open', + description:

persistent

, + duration: 0, + onClose, + }); + }); + + act(() => { + vi.advanceTimersByTime(5000); + }); + + expect(onClose).not.toHaveBeenCalled(); + expect(document.querySelector('.keep-open')).toHaveTextContent('persistent'); + }); + it('freeze notification layer when mouse over', () => { const { instance } = renderDemo(); diff --git a/tests/useNoticeTimer.test.tsx b/tests/useNoticeTimer.test.tsx index ff499a2..ae18818 100644 --- a/tests/useNoticeTimer.test.tsx +++ b/tests/useNoticeTimer.test.tsx @@ -40,4 +40,53 @@ describe('useNoticeTimer', () => { unmount(); }); + + it.each([0, false, null] as const)( + 'does not close when duration changes from positive to %s', + (disabledDuration) => { + const onClose = vi.fn(); + const onUpdate = vi.fn(); + const { rerender, unmount } = renderHook( + ({ duration }) => useNoticeTimer(duration, onClose, onUpdate), + { + initialProps: { duration: 1 as number | false | null }, + }, + ); + + act(() => vi.advanceTimersByTime(200)); + expect(onClose).not.toHaveBeenCalled(); + + rerender({ duration: disabledDuration }); + + act(() => vi.advanceTimersByTime(2000)); + expect(onClose).not.toHaveBeenCalled(); + + unmount(); + }, + ); + + it('starts a fresh timer when duration changes from 0 back to positive', () => { + const onClose = vi.fn(); + const onUpdate = vi.fn(); + const { rerender, unmount } = renderHook( + ({ duration }) => useNoticeTimer(duration, onClose, onUpdate), + { + initialProps: { duration: 1 as number | false | null }, + }, + ); + + act(() => vi.advanceTimersByTime(200)); + rerender({ duration: 0 }); + act(() => vi.advanceTimersByTime(2000)); + expect(onClose).not.toHaveBeenCalled(); + + rerender({ duration: 1 }); + act(() => vi.advanceTimersByTime(500)); + expect(onClose).not.toHaveBeenCalled(); + + act(() => vi.advanceTimersByTime(600)); + expect(onClose).toHaveBeenCalledTimes(1); + + unmount(); + }); });