diff --git a/src/components/text_truncate/text_truncate.test.tsx b/src/components/text_truncate/text_truncate.test.tsx index 024fe925f71..52c6af87a61 100644 --- a/src/components/text_truncate/text_truncate.test.tsx +++ b/src/components/text_truncate/text_truncate.test.tsx @@ -70,6 +70,19 @@ describe('EuiTextTruncate', () => { expect(clearTimeoutSpy).toHaveBeenCalledTimes(2); expect(clearTimeoutSpy).toHaveBeenLastCalledWith(expect.any(Number)); }); + + it('does not set or clear a timeout if a duration is not passed', () => { + const setTimeoutSpy = jest.spyOn(window, 'setTimeout'); + const clearTimeoutSpy = jest.spyOn(window, 'clearTimeout'); + + const { unmount } = render( + + ); + + expect(setTimeoutSpy).not.toHaveBeenCalled(); + unmount(); + expect(clearTimeoutSpy).not.toHaveBeenCalled(); + }); }); describe('resize observer', () => { diff --git a/src/components/text_truncate/text_truncate.tsx b/src/components/text_truncate/text_truncate.tsx index 4b4aa0c3f16..30022a43a8d 100644 --- a/src/components/text_truncate/text_truncate.tsx +++ b/src/components/text_truncate/text_truncate.tsx @@ -138,14 +138,10 @@ const EuiTextTruncateWithWidth: FunctionComponent< // If necessary, wait a tick on mount before truncating const [ready, setReady] = useState(!calculationDelayMs); useEffect(() => { - let timerId: NodeJS.Timeout; if (calculationDelayMs) { - timerId = setTimeout(() => setReady(true), calculationDelayMs); + const timerId = setTimeout(() => setReady(true), calculationDelayMs); + return () => clearTimeout(timerId); } - - return () => { - clearTimeout(timerId); - }; }, [calculationDelayMs]); // Handle exceptions where we need to override the passed props