kmenu icon indicating copy to clipboard operation
kmenu copied to clipboard

[BUG] Testing with jest/vitest (scrollIntoView is not available)

Open cguedes opened this issue 1 year ago • 2 comments

Describe the bug When using kmenu in unit tests, with Testing Library and jest/vitest, the tests fail because scrollIntoView is not available. I've identified that the cause is that the function isn't implemented by jsdom.

To Reproduce Steps to reproduce the behavior:

  1. Create a project with kmenu
  2. Create a unit test using testing library
  3. Create a test that renders the kmenu
  4. Ensure that the kmenu renders (open the menu by default for example)
  5. See error "scrollIntoView is not available"

Expected behavior The component should render it's content inside the test runner.

Screenshots

TypeError: _bottomRef$current.scrollIntoView is not a function
 ❯ node_modules/kmenu/src/Command.tsx:25:26
 ❯ commitHookEffectListMount node_modules/react-dom/cjs/react-dom.development.js:23150:26
 ❯ commitPassiveMountOnFiber node_modules/react-dom/cjs/react-dom.development.js:24931:11
 ❯ commitPassiveMountEffects_complete node_modules/react-dom/cjs/react-dom.development.js:24891:9
 ❯ commitPassiveMountEffects_begin node_modules/react-dom/cjs/react-dom.development.js:24878:7
 ❯ commitPassiveMountEffects node_modules/react-dom/cjs/react-dom.development.js:24866:3
 ❯ flushPassiveEffectsImpl node_modules/react-dom/cjs/react-dom.development.js:27039:3
 ❯ flushPassiveEffects node_modules/react-dom/cjs/react-dom.development.js:26984:14
 ❯ node_modules/react-dom/cjs/react-dom.development.js:26769:9
 ❯ flushActQueue node_modules/react/cjs/react.development.js:2667:24

Desktop (please complete the following information):

  • OS: osx
  • Browser: Tauri

Additional context This can be fixed by adding the missing function without any implementation.

window.HTMLElement.prototype.scrollIntoView = () => { };

cguedes avatar Jul 13 '23 13:07 cguedes

Hey, @cguedes, I'm unsure on how I should go about helping you here? You gave me all the details I asked for but I'm unsure if I fully understand how this can be fixed.

haaarshsingh avatar Jul 13 '23 20:07 haaarshsingh

Hi. That's a good point. Maybe you can add some init tests to identify the best way of fixing this. Additionally you can have a note in README.md

cguedes avatar Jul 17 '23 12:07 cguedes