question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React - Jest - Enzyme: How to mock ref properties

See original GitHub issue

I’m writing test for a component with ref. I’d like to mock the ref element and change some properties but have no idea how to. Any suggestions?

// MyComp.jsx
class MyComp extends React.Component {
  constructor(props) {
    super(props);
    this.getRef = this.getRef.bind(this);
  }
  componentDidMount() {
    this.setState({elmHeight: this.elm.offsetHeight});
  }
  getRef(elm) {
    this.elm = elm;
  }
  render() {
    return <div>
      <span ref={getRef}>
        Stuff inside 
      </span>
    </div>
  }
}

// MyComp.test.jsx
const comp = mount(<MyComp />);
// Since it is not in browser, offsetHeight is 0
// mock ref offsetHeight to be 100 here... How do I do it?
expect(comp.state('elmHeight')).toEqual(100);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
gabor-ottlik-epamcommented, Feb 19, 2021

This didn’t work for me when the variable of useRef is in useEffect

const test = useRef()

useEffect(() => {
  console.log(test.current)
})

This logs out the underlying dom element instead of the mock function since useEffect runs when the dom is rendered hence the dom element is attached to ref.

Any idea how to get around this?

4reactions
mrdulincommented, May 14, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

React - Jest - Enzyme: How to mock ref properties
I'd like to mock the ref element and change some properties but have no idea how to. Any suggestions? // MyComp.jsx class MyComp...
Read more >
React - Jest - Enzyme: How to mock ref properties-Reactjs
It is possible to monkey-patch the class with a non-arrow function, where "this" keyword is passed to the right scope. function mockGetRef(ref:any) {...
Read more >
Reactjs – React – Jest – Enzyme: How to mock ref properties
I'm writing test for a component with ref. I'd like to mock the ref element and change some properties but have no idea...
Read more >
How to mock React refs in unit tests using Jest - YouTube
Today I learned how to mock a React ref with Jest. In the past I might have used a full render (mount) in...
Read more >
Continuous integration for React applications using Jest and ...
This test shows how you can easily use enzyme to query nested components, (in this case, Picker ), and assert that it is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found