Skip to content

Commit

Permalink
Make the returned clearTimeout conditional
Browse files Browse the repository at this point in the history
+ add unit test for that as welll
  • Loading branch information
cee-chen committed Jan 30, 2024
1 parent 0f3389c commit 48c0e3e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/components/text_truncate/text_truncate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<EuiTextTruncate {...props} width={0} calculationDelayMs={0} />
);

expect(setTimeoutSpy).not.toHaveBeenCalled();
unmount();
expect(clearTimeoutSpy).not.toHaveBeenCalled();
});
});

describe('resize observer', () => {
Expand Down
8 changes: 2 additions & 6 deletions src/components/text_truncate/text_truncate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 48c0e3e

Please sign in to comment.