react-sizeme icon indicating copy to clipboard operation
react-sizeme copied to clipboard

How do you write tests for components using react-sizeme?

Open jeznag opened this issue 7 years ago • 8 comments

I'm using react-sizeme within create-react-app and am trying to write tests using jest.

When I try and do a snapshot test, this is the JSON.

{ type: 'div',
      props: { style: { width: '100%', height: '100%' } },
      children: null }

How can I write tests that wait until the initial sizing render has finished? Would also be helpful to see how people write tests that adjust to different viewport widths (see unanswered question: https://stackoverflow.com/questions/46221210/jest-enzyme-how-to-test-at-different-viewports)

jeznag avatar Aug 21 '18 08:08 jeznag

I have the same problem. The children are not rendered yet so the snapshot is not really that useful. Currently, I'm setting noPlaceholder to true only when running the tests. I'm using this library with react-grid-layout and we can still find some oddities like style="height: 150px; position: absolute; transform: translate(NaNpx,0px);" in the snapshots, but that's better than an empty div I guess. 💭

It would be great to have a solution to this issue.

nunofmaia avatar Jan 25 '19 17:01 nunofmaia

I have the following code in my jest setup file so it's configured globally and only effects the test environment:

import sizeMe from "react-sizeme";

sizeMe.noPlaceholders = true;

kaitmore avatar Apr 11 '19 18:04 kaitmore

Good tip @kaitmore - we should add this to the docs.

ctrlplusb avatar May 01 '19 13:05 ctrlplusb

I am using a library that's using sizeme, and unfortunately it doesn't seem to be picking up the noPlaceholders global if I try and set it in my setupTests.js. Any ideas?

scottdickerson avatar Sep 27 '19 19:09 scottdickerson

thanks @kaitmore, works like a charm.

spiritwalker-jiang avatar Dec 02 '19 06:12 spiritwalker-jiang

@scottdickerson , I'm not using react-sizeme directly either, it is loaded through another component. And I don't have such a problem. Maybe the lib you're using gets loaded first before you test kicks in; therefore it is too late to set the global flag? just guess

spiritwalker-jiang avatar Dec 02 '19 07:12 spiritwalker-jiang

@scottdickerson check where your application's sizeme version and the library's sizeme version is the same. If not sizeMe.noPlaceholders = true; will not work. I also ran in to the same problem and after updating the version in to a one version, issue got fixed.

rumeshwick avatar Jan 23 '20 11:01 rumeshwick

Workaround from @kaitmore should be added to the documentation.

keul avatar Oct 01 '20 08:10 keul