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.

Usage with react-test-renderer

See original GitHub issue

I’m trying to unit test components that render RV components with the react-test-renderer, and running into this error: https://github.com/facebook/react/issues/7371.

It seems like an issue with the usage of findDOMNode. What is the recommended way to unit test RV components?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

67reactions
thieliumcommented, Dec 13, 2018

Noting the above caveats about mocking the DOM, I adapted the AutoSizer’s mock, though it’s a little white-boxy.

describe("My Test", () => {
  const originalOffsetHeight = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetHeight');
  const originalOffsetWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetWidth');

  beforeAll(() => {
    Object.defineProperty(HTMLElement.prototype, 'offsetHeight', { configurable: true, value: 50 });
    Object.defineProperty(HTMLElement.prototype, 'offsetWidth', { configurable: true, value: 50 });
  });

  afterAll(() => {
    Object.defineProperty(HTMLElement.prototype, 'offsetHeight', originalOffsetHeight);
    Object.defineProperty(HTMLElement.prototype, 'offsetWidth', originalOffsetWidth);
  });
  // Your tests
})

I’ve noticed some issues that are pretty challenging to reproduce by defaulting the value of AutoSizer to the grid (column sizes aren’t recalculated though the width has changed). That’s the only real reason to mock rather than to default. If I can repro, I’ll make a bug.

33reactions
izakfilmaltercommented, Jan 28, 2018

Even easier solution:

<AutoSizer>
  {({ width, height }) => (
    <List
      height={height || 100}
      width={width || 100}
    />
  )}
</AutoSizer>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Test Renderer - React
This package provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM...
Read more >
TDD with React Test Renderer - LogRocket Blog
Although React Test Renderer is usually associated with snapshot testing, it can still be used to make specific assertions against your ...
Read more >
Testing React Native components in Node with react ... - Medium
The react-test-renderer package makes it convenient to test components outside of their native environment (e.g. on an iOS/Android device for React Native ...
Read more >
Testing React Components with react-test-renderer, and the ...
react -test-renderer is a library for rendering React components to pure JavaScript objects, but it can do a lot more than creating objects....
Read more >
react-test-renderer - npm
React package for snapshot testing.. Latest version: 18.2.0, last published: 6 months ago. Start using react-test-renderer in your project ...
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