react-input icon indicating copy to clipboard operation
react-input copied to clipboard

Case where timeouts are not cleared

Open petewins opened this issue 11 months ago • 1 comments

In specific testing Environments, it is possible for setTimeout to not clear after each test. Causing my test to fail with a window not defined error

Error I'm recieving:

ReferenceError: window is not defined ❯ Timeout.e [as onTimeout] node_modules/.pnpm/@[email protected]@[email protected][email protected][email protected][email protected]/node_modules/@react-input/core/dist/module/hooks/useInput.js:1:1751

I believe the fix is to always clearTimeout when component is unmounted / unregistered.

Currently, I believe timeouts are only cleared onBlur of the input.

https://github.com/GoncharukOrg/react-input/blob/7ca98d5cb97103ad49568355fb64b5f589ac93e4/packages/core/src/Input.ts#L106

Workaround

For those Running into a similar error, I was able to avoid this issue by invoking the blur() function after each test so the timeouts are cleared.

afterEach(() => {
  const activeElement = global.document?.activeElement
  if (activeElement && activeElement instanceof HTMLElement) {
    activeElement.blur()
  }
})

petewins avatar Jan 02 '25 19:01 petewins

Thanks heaps @petewins for the work around!! Worked like a charm~~

Annie-Huang avatar Mar 18 '25 04:03 Annie-Huang