Skip to content

Instantly share code, notes, and snippets.

@koba04
Last active September 24, 2017 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koba04/7a3ba84c7ebd61faee2fd2a52c72e6b0 to your computer and use it in GitHub Desktop.
Save koba04/7a3ba84c7ebd61faee2fd2a52c72e6b0 to your computer and use it in GitHub Desktop.
import React from 'react';
import TestRenderer from 'react-test-renderer';
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.input = null;
}
componentDidMount() {
this.input.focus();
}
render() {
return <input type="text" ref={el => this.input = el} />
}
}
let focused = false;
TestRenderer.create(
<MyComponent />,
{
createNodeMock: (element) => {
if (element.type === 'input') {
// mock a focus function
return {
focus: () => {
focused = true;
}
};
}
return null;
}
}
);
console.assert(focused === true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment