Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/hooks/useNoticeTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: <p className="keep-open">initial</p>,
duration: 1,
onClose,
});
});

expect(document.querySelector('.keep-open')).toHaveTextContent('initial');

act(() => {
instance.open({
key: 'keep-open',
description: <p className="keep-open">persistent</p>,
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();

Expand Down
49 changes: 49 additions & 0 deletions tests/useNoticeTimer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Loading