Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-test-renderer does not work with ref methods #5462

Closed
mildrenben opened this issue Feb 5, 2018 · 6 comments
Closed

react-test-renderer does not work with ref methods #5462

mildrenben opened this issue Feb 5, 2018 · 6 comments

Comments

@mildrenben
Copy link

mildrenben commented Feb 5, 2018

Do you want to request a feature or report a bug?
Report a bug

What is the current behavior?
Ref methods cause an error to be thrown

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

import React from 'react'
import renderer from 'react-test-renderer'

it('Test refs', () => {
  class Comp extends React.Component {
    componentDidMount () {
      console.log(this.myRef)
      this.myRef.addEventListener('click', () => console.log('click'))
    }
    render () {
      return (
        <div ref={div => { this.myRef = div }}>foo</div>
      )
    }
  }

  const tree = renderer.create(
    <Comp />
  ).toJSON()

  expect(tree).toMatchSnapshot()
})

Cannot read property 'addEventListener' of null

What is the expected behavior?

For refs to work and have usable mock methods. I did some digging through other issues and it looks like this was fixed here facebook/react#7740 but I'm not sure if ref DOM methods were ever fixed.

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

MacOS
react-test-renderer @ 16.2.0
jest @ 21.2.1
react @ 16.1.1
node @ 8.2.1
yarn @ 0.27.5

@thymikee
Copy link
Collaborator

thymikee commented Feb 5, 2018

Do you have a repro of that? Does Cannot read property 'addEventListener' of null comes from Jest or react-test-renderer? I'd assume the latter, but who knows without a repo and full error message 🤷‍♂️

@mildrenben
Copy link
Author

@thymikee Here is a repo https://github.com/mildrenben/jest-ref-issue

I believe it's being thrown by react-test-renderer

@SimenB
Copy link
Member

SimenB commented Feb 6, 2018

You've linked to the correct issue in react's tracker - react-test-renderer does not support ref. You can either use ReactDOM or enzyme to do a real render.

@SimenB SimenB closed this as completed Feb 6, 2018
@SimenB
Copy link
Member

SimenB commented Feb 6, 2018

Also see React docs about createNodeMock: https://reactjs.org/docs/test-renderer.html#ideas for an alternative.

Either way, this is not a jest issue

@mildrenben
Copy link
Author

createNodeMock seems perfect, thanks!

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants