jsdom / jsdom

A JavaScript implementation of various web standards, for use with Node.js
MIT License
20.56k stars 1.7k forks source link

scrollIntoView is not a function #1695

Open JakeDluhy opened 7 years ago

JakeDluhy commented 7 years ago

I'm running a test using a library (grommet) that uses scrollIntoView on an a tag. jsdom is throwing errors when hitting that line of code. I was able to get it to work by adding window.HTMLElement.prototype.scrollIntoView = function() {}; in my testHelper file to setup the tests, but I thought I'd report it to see if it's something you want to fix.

domenic commented 7 years ago

Yeah this might be worth adding a not-implemented-warning stub for. We can't really implemented it since we don't do layout.

quantuminformation commented 6 years ago

Any movement on this?

Trying to use in a Jest test, I'm trying to mount a component that calls scrollIntoView on an element, and the test always fails with

TypeError: node.scrollIntoView is not a function
        at Object.scrollInToView src/component/shared/foo.tsx:7
        at User.Object.<anonymous>.User.componentDidMount
enessoylu commented 5 years ago

@QuantumInformation You can use this for the Jest:

Element.prototype.scrollIntoView = jest.fn();

qmg-JamesT commented 5 years ago

I see this fix everywhere, but were are you importing Element from? I get 'Element is not defined' I am using enzyme with chai.

quantuminformation commented 5 years ago

You need Element in your tsconfig libs:

ie 'dom'

enessoylu commented 5 years ago

@qmg-JamesT It is the global element class where the other elements are inherited scrollIntoView method from.

ado-astpos commented 5 years ago

Hi, is there any update on this without using jest?

maksymsm commented 4 years ago

hello, you can use window.HTMLElement.prototype.scrollIntoView = jest.fn()

thisconnect commented 4 years ago

I need the same for Element.prototype.scroll = jest.fn()

@enessoylu and @msmetana where do you recommend putting Element.prototype.scrollIntoView = jest.fn(), in each .test.

When using CRA there is a setupTest.ts, seems to work but idk

import '@testing-library/jest-dom/extend-expect'

Element.prototype.scroll = jest.fn()
ghost commented 4 years ago

For ava or any other unit test runner you can use window.HTMLElement.prototype.scrollIntoView = () => {} and set it in test.beforeEach() or inside the test function

tobiloeb commented 4 years ago

For me it was enough to set:

Element.prototype.scrollIntoView` = () => {};

once into setupJest.ts

jcampbell05 commented 3 years ago

This also seems to be the case with scroll

ccloveak commented 3 years ago

Element.prototype.scrollIntoView = jest.fn();

Thanks! it worked.

asharuDheen-code commented 3 years ago

try this one.. window.HTMLElement.prototype.scrollIntoView = function() {};

dobradovic commented 3 years ago

How can I mock value for srcollIntoView? I'm using enzyme shallow / jest.

I'm getting "TypeError: Cannot read property 'scrollIntoView' of null. "

Any advice would be helpful, I don't need to test anything regarding to scrollIntoView, I just need to mock some values, because my test failing and can not pass because scrollIntoView is null.

Thanks for your time!

gabrielgaraujo commented 3 years ago

Anyone have an idea why it happens?

safi28 commented 2 years ago

@quantuminformation You can use this for the Jest:

Element.prototype.scrollIntoView = jest.fn();

Thank you it works!

ledenis commented 1 year ago

Why not add the not-implemented-warning stub as suggested in the second comment?

Is there a reason why it has been done for scrollTo but not scrollIntoView? https://github.com/jsdom/jsdom/blob/65ef06f3771a33dcbf7bef442fdc7e07816d008d/lib/jsdom/browser/Window.js#L931

carlos-monti commented 1 year ago

It would be nice to have a proper way to restore it after all tests were run but I guess that if we don't have it implemented in jsdom it doesn't care very much.

trajano commented 3 months ago

FWIW it happy-dom appears to handle this.