Ad
Enzyme: How Can I Test A Component With DOM Side-effect?
Say I have a component like so -
// @flow
import React, { PureComponent } from 'react';
export default class ReplaceLink extends Component {
containerRef = React.createRef();
componentDidMount() {
const links =
Array.from(this.containerRef.current.getElementsByTagName('a'));
links.forEach(a => a.setAttribute('href', 'dummylink'));
}
render = () => <div ref={this.containerRef}>{this.props.children}</div>;
}
which replaces href of links placed within it. But even when doing full dom rendering in enzyme, when I do a wrapper.debug()
to see the result of the render, I still see original links only.
I've tried doing a force wrapper.update
and using setTimeouts, but it just doesn't reflect the expected link.
Ad
Answer
Found the best way to test something like this is through the getDOMNode
method.
First, make sure to use
mount
to render the wrapper, so we have a simulated DOM environment to query against.Next, use
wrapper.getDOMNode()
to get the underlying DOM node.- Any changes made during the lifecycle methods to the underlying DOM will be reflected in this DOM reference.
Use
.querySelector
, or<insert-dom-query-method>
to make assertions.const wrapper = mount( <ReplaceLink> <a target="_blank" rel="nofollow noreferrer" href="gogole.com"> Google</a> </ReplaceLink> ); const linkTags = wrapper.getDOMNode().querySelectorAll('a'); linkTags.forEach(tag => { expect(tag.getAttribute('href')).toBe('dummy'); });
Ad
source: stackoverflow.com
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad