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.

TextField `multiline` with no `rows` throws error when using ReactTestRenderer

See original GitHub issue
  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

When a renderer for <TextField multiline /> is created by ReactTestRenderer.create, no errors should be thrown:

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

import TextField from '@material-ui/core/TextField'

it('should not throw errors', () => {
  ReactTestRenderer.create(<TextField multiline />)
})

Current Behavior 😯

Running a test containing such a request produces this error:

    Error: Uncaught [TypeError: Cannot read property 'style' of null]

This is what appears to be the relevant part of the stack trace:

    The above error occurred in the <ForwardRef(Textarea)> component:
        in ForwardRef(Textarea) (created by ForwardRef(InputBase))
        in div (created by ForwardRef(InputBase))
        in ForwardRef(InputBase) (created by WithStyles(ForwardRef(InputBase)))
        in WithStyles(ForwardRef(InputBase)) (created by ForwardRef(Input))
        in ForwardRef(Input) (created by WithStyles(ForwardRef(Input)))
        in WithStyles(ForwardRef(Input)) (created by ForwardRef(TextField))
        in div (created by ForwardRef(FormControl))
        in ForwardRef(FormControl) (created by WithStyles(ForwardRef(FormControl)))
        in WithStyles(ForwardRef(FormControl)) (created by ForwardRef(TextField))
        in ForwardRef(TextField) (created by WithStyles(ForwardRef(TextField)))

Steps to Reproduce 🕹

Link: https://codesandbox.io/s/create-react-app-jl67r

  1. Go to the Tests tab
  2. Note the failing test (the error is actually different in the sandbox—maybe this helps diagnose the problem?)
  3. Note the other variants of rendering TextField that do not fail
  4. Note the workaround (fourth test): using createNodeMock to return a textarea element

Context 🔦

I have test suites which render React components that have the Material UI TextField component with multiline set to true (but no rows attribute). These tests passed in v1.x (and the app works fine as well) but are now throwing this error in v4.1.3 (I skipped v3.x so I don’t know if they would have passed then).

I can use the createNodeMock workaround for now, but it feels unwieldy to add this to every usage of ReactTestRenderer for a component that happens to have a TextField multiline somewhere in its tree.

Your Environment 🌎

Tech Version
Material-UI v4.1.3
React v16.8.6
Browser n/a
TypeScript n/a
etc.

Thanks in advance for any help that you might be able to provide!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
dondicommented, Jul 5, 2019

Thanks for the quick analysis! It’ll take a little longer to digest everything but sure, once I’ve gotten myself situated, I can submit something.

1reaction
BobrImperatorcommented, Jul 22, 2021

Just stumbled upon this, my solution for this was to not use react-test-renderer, but render from @testing-library/react

it("renders", () => {
  const { asFragment } = render(<Header text="Hello!" />);
  expect(asFragment()).toMatchSnapshot();
});

Shoutout to https://www.leighhalliday.com/introduction-react-testing-library where I was able to find this API for generating snapshots.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TextField `multiline` with no `rows` throws error when using ...
I have test suites which render React components that have the Material UI TextField component with multiline set to true (but no rows...
Read more >
Material UI TextField multiline fixed quantity of rows
It does work correctly in the way it was intended. The props maxRows and minRows control how many visible rows are displayed.
Read more >
Test Renderer - React
Find a single descendant test instance with the provided props . If there is not exactly one test instance with the provided props...
Read more >
react-native-elements | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
The React Handbook – Learn React for Beginners
Inside a function, any variable defined in it is visible throughout all the function code, even if the variable is declared at the...
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