score:8
Error message clarification
Using mount
like in the sample code above gives this error:
TypeError: _reactDom2.default.findDOMNode(...).scrollIntoView is not a function
Using shallow
gives the error listed above:
TypeError: Cannot read property 'scrollIntoView' of null
shallow
Issue
shallow
does not do DOM rendering so there will never be a DOM node on which to call scrollIntoView()
.
Solution
Any code that does DOM manipulation needs to be tested using the full DOM rendering provided by mount
.
mount
"The default environment in Jest is a browser-like environment through jsdom".
Issue
jsdom
implements much of the browser environment but it does not implement everything. Of particular note for this question is that it does not implement scrollIntoView
since jsdom
does not do layout and would therefore not be able to provide an accurate implementation.
Because jsdom
does not implement scrollIntoView
it will be undefined on elements provided by jsdom
.
Solution
The recommended approach from this Google dev is to add the following line to your test code:
Element.prototype.scrollIntoView = () => {};
That line will add a noop implementation of scrollIntoView
to the jsdom
-provided Element
.
For your test you could take it a step further and set scrollIntoView
to a spy
to make sure it is called:
it('should render component correctly', () => {
const props = {
...defaultProps,
};
Element.prototype.scrollIntoView = jest.fn(); // set scrollIntoView to a spy
const wrapper = mount(<PageView {...props} />);
expect(wrapper).toMatchSnapshot();
expect(Element.prototype.scrollIntoView).toHaveBeenCalled(); // PASSES
});
Also, Antonio is correct that you shouldn't need to use ReactDOM.findDOMNode()
, you should be able to use this.topOfPageRef.current
directly:
componentDidMount() {
this.topOfPageRef.current.scrollIntoView();
}
score:0
You can set in window
it('should call paginator component', () => {
window.HTMLElement.prototype.scrollIntoView = jest.fn();
// arrange
// act
// assertion
})
score:1
The error you get is because ReactDOM.findDOMNode is giving you back null.
As the React doc says:
When a component renders to null or false, findDOMNode returns null
also
In most cases, you can attach a ref to the DOM node and avoid using findDOMNode at all.
You should not use React.findDOMNode
componentDidMount() {
ReactDOM.findDOMNode(this.topOfPageRef.current).scrollIntoView();
}
but:
componentDidMount() {
this.topOfPageRef.current.scrollIntoView();
}
Hope it will help.
Source: stackoverflow.com
Related Query
- Cannot read property 'selectionEnd' of null using React + Jest + Datepicker + TextMask
- TypeError: Cannot read property 'scrollIntoView' of null - react. jest enzyme
- React testing with Jest and Enzyme @react-google-maps/api returns TypeError: Cannot read property 'maps' of undefined
- React Facebook Login with Enzyme mount - TypeError cannot read property 'parentNode' of undefined
- TypeError: Cannot read property 'setTransform' of null when using Openlayers and Jest + Enzyme
- React enzyme testing, Cannot read property 'have' of undefined
- Cannot read property 'refs' of null react error react js
- Uncaught TypeError: Cannot read property 'state' of null in react
- How to test form submission in React with Jest and Enzyme? Cannot read property 'preventDefault' of undefined
- React component returning cannot read property '__reactInternalInstance$ of null after trying to access array
- React Jest test Cannot read property 'pathname' of undefined
- React - Cannot read property 'setState' of null
- Jest test Cannot read property 'Object.<anonymous>' of null
- How to get over Cannot read property 'removeEventListener' of null in react
- TypeError: Cannot read property 'createEvent' of null React Apollo testing
- React tutorial: Uncaught TypeError: Cannot read property 'data' of null
- React test with enzyme TypeError: Cannot read property 'state' of undefined
- TypeError: Cannot read property 'value' of null in React Form
- React refs: Cannot read property 'focus' of null
- Cannot read property 'style' of null React
- React JS and Google Spreadsheets: Cannot Read Property '2' of Null
- "TypeError: cannot read property 'prototype' of undefined" when Jest testing react component that includes c3 chart
- TypeError: Cannot read property 'name' of null in react
- TypeError: Cannot read property 'substr' of null - React
- TypeError: Cannot read property 'length' of null in React Project
- react jest unit test case TypeError: Cannot read property 'then' of undefined
- Cannot read property 'setState' of null react
- TypeError: Cannot read property 'foo' of null - Object, React
- Jest and Enzyme unit test: TypeError: Cannot read property 'push' of undefined
- cannot read property of null using state in react
More Query from same tag
- html5 canvas fill circle with pinstripe
- How can I access current component’s top-level element ref inside a hook?
- React.useCallback : it doesn't prevent continuos re-rendering
- How to we simulate a file upload in jest
- How to in React return array without duplicates or sum duplicate objects
- Pasing fetched data as props (a JSON) to state in order to render data
- django built-in authentication system not working with react
- How to remove the padding top and bottom in menu drop-down react-select?
- how to test react-saga axios post
- React router not rendering path component
- JSX mapping element within mapping element, Adjacent JSX elements must be wrapping in enclosing tags
- Permissions error when trying to install jquery-ui/data
- Enforce prop should be either of two functional components
- Async with useEffect and loading data into variables
- Cannot read property 'state' of undefined
- How do I pass data from one sibling to another using React Router?
- why should I not use CDN for react & babel?
- how to use a function from another js file in reactjs?
- Webpack + React : reusable component is not defined
- Access input name attribute using template literals (React)
- Ref react dynamically
- Electron/React: How to avoid blocking main thread with recursive file/folder enumeration
- React JS TypeError: Cannot read properties of undefined (reading 'length')
- Reactjs - toggling class indipendently with right way
- Filter with startsWith with multiple object keys
- Correct syntax of printing array objects inside array React
- Follow user location on React Native AirBnb's MapView [Android]
- How to check the entire state of a form in react, in order to enable a disabled button submit
- Trigger onSubmit event
- Updating state from the data that was read from redux in React