diff --git a/src/Notice.tsx b/src/Notice.tsx index 80c379a..266de3b 100644 --- a/src/Notice.tsx +++ b/src/Notice.tsx @@ -1,9 +1,16 @@ import { clsx } from 'clsx'; import KeyCode from '@rc-component/util/lib/KeyCode'; +import warning from '@rc-component/util/lib/warning'; import * as React from 'react'; import type { NoticeConfig } from './interface'; import pickAttrs from '@rc-component/util/lib/pickAttrs'; +/** + * Maximum delay value for setTimeout in seconds (2^31 - 1 ms). + * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#maximum_delay_value + */ +const MAX_DURATION = 2147483647 / 1000; + export interface NoticeProps extends Omit { prefixCls: string; className?: string; @@ -38,7 +45,9 @@ const Notify = React.forwardRef 0 && showProgress; // ======================== Close ========================= @@ -52,6 +61,14 @@ const Notify = React.forwardRef { + warning( + rawDuration <= MAX_DURATION, + `\`duration\` exceeds the maximum supported value (${MAX_DURATION}s) and has been clamped.`, + ); + }, [rawDuration]); + // ======================== Effect ======================== React.useEffect(() => { if (!mergedHovering && mergedDuration > 0) { diff --git a/tests/index.test.tsx b/tests/index.test.tsx index 6b47858..7beddb7 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -1018,4 +1018,51 @@ describe('Notification.Basic', () => { 'xxx', ); }); + + describe('MAX_DURATION', () => { + it('should not warn when duration is within limit', () => { + const { instance } = renderDemo(); + const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); + + act(() => { + instance.open({ + content:

1

, + duration: 0.1, + }); + }); + + expect(document.querySelector('.test')).toBeTruthy(); + expect(errSpy).not.toHaveBeenCalled(); + }); + + it('should warn and clamp when duration exceeds limit', () => { + const { instance } = renderDemo(); + const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); + + const MAX_DURATION = 2147483647 / 1000; + const EXCEEDED_DURATION = (2147483647 + 1) / 1000; + + act(() => { + instance.open({ + content:

1

, + duration: EXCEEDED_DURATION, + }); + }); + + expect(document.querySelector('.test')).toBeTruthy(); + expect(errSpy).toHaveBeenCalledWith( + `Warning: \`duration\` exceeds the maximum supported value (${MAX_DURATION}s) and has been clamped.`, + ); + + act(() => { + vi.advanceTimersByTime(MAX_DURATION * 1000 - 1); + }); + expect(document.querySelector('.test')).toBeTruthy(); + + act(() => { + vi.advanceTimersByTime(1); + }); + expect(document.querySelector('.test')).toBeFalsy(); + }); + }); });