Skip to content

Commit 48c0e3e

Browse files
committed
Make the returned clearTimeout conditional
+ add unit test for that as welll
1 parent 0f3389c commit 48c0e3e

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/components/text_truncate/text_truncate.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ describe('EuiTextTruncate', () => {
7070
expect(clearTimeoutSpy).toHaveBeenCalledTimes(2);
7171
expect(clearTimeoutSpy).toHaveBeenLastCalledWith(expect.any(Number));
7272
});
73+
74+
it('does not set or clear a timeout if a duration is not passed', () => {
75+
const setTimeoutSpy = jest.spyOn(window, 'setTimeout');
76+
const clearTimeoutSpy = jest.spyOn(window, 'clearTimeout');
77+
78+
const { unmount } = render(
79+
<EuiTextTruncate {...props} width={0} calculationDelayMs={0} />
80+
);
81+
82+
expect(setTimeoutSpy).not.toHaveBeenCalled();
83+
unmount();
84+
expect(clearTimeoutSpy).not.toHaveBeenCalled();
85+
});
7386
});
7487

7588
describe('resize observer', () => {

src/components/text_truncate/text_truncate.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,10 @@ const EuiTextTruncateWithWidth: FunctionComponent<
138138
// If necessary, wait a tick on mount before truncating
139139
const [ready, setReady] = useState(!calculationDelayMs);
140140
useEffect(() => {
141-
let timerId: NodeJS.Timeout;
142141
if (calculationDelayMs) {
143-
timerId = setTimeout(() => setReady(true), calculationDelayMs);
142+
const timerId = setTimeout(() => setReady(true), calculationDelayMs);
143+
return () => clearTimeout(timerId);
144144
}
145-
146-
return () => {
147-
clearTimeout(timerId);
148-
};
149145
}, [calculationDelayMs]);
150146

151147
// Handle exceptions where we need to override the passed props

0 commit comments

Comments
 (0)