Comments (9)
Hi @loicpw
You're right, ReactResizeDetector works only in a browser environment.
What values do you expect to see?
You should probably mock the ReactResizeDetector library https://jestjs.io/docs/en/manual-mocks and create your own logic to test components, e.g. pass different values to the mock and test how your components react to these values...
from react-resize-detector.
Hi and thanks for your reply,
well this is what I'm currently doing in some tests because not everything depends on the resolution...
beforeEach(() => {
MediaQuery.defaultProps = {
values: { width: 800, height: 800 },
};
})
EDIT: And actually this is useful for testing viewport related behavior as well (mocking things is maybe more maintainable in the long run but it requires much more efforts not always worth the use case).
from react-resize-detector.
Cool!
To be honest I'm not an expert in unit testing and I didn't even know that you can set width and height using MediaQuery.
I think it can be very useful. Thanks for pointing this out!
from react-resize-detector.
Do you think you could implement something similar in ReactResizeDetector
?
I really don't know if it's a common use case, as far as I'm concerned I find this useful. Maybe I'll try to implement it by myself later on as I'm still a beginner in js and React...
from react-resize-detector.
I don't think we should add any extra logic to the library in order to reduce unit tests by 3 rows.
@loicpw you provided a beautiful solution to mock RRD width and height - #67 (comment)
I think it's the best approach to test your components with react-resize-detector.
Closing this issue.
from react-resize-detector.
For few hours now I am trying set width in my Jest test so that I do not get undefined value in my render function.
Can someone help me with clean solution for typescript?
MediaQuery is not defined, and I cannot figure out how to mock react-resize-detector, this does not work:
jest.mock("react-resize-detector", () => {
const ComponentToMock = () => <div />;
return ComponentToMock;
});
I am using react-test-renderer. Maybe it is impossible with that, I do not know.
from react-resize-detector.
I do this currently:
let containerWidth: number;
jest.mock("react-resize-detector", () => {
return {
default: props => <div>
{props.children({ width: containerWidth })}
</div>
}
});
beforeEach(() => {
containerWidth = 1000;
});
from react-resize-detector.
@Hogan, did you figure it out? I'm having the same problem. Your last snippet doesn't work for me:
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.
from react-resize-detector.
The code I posted must be immediately after imports. I tried to wrap it in function and it did not work. I do not understand why, but I am glad it somehow works.
Oh the weirdness of JS.
from react-resize-detector.
Related Issues (20)
- `onFinishResize`? HOT 2
- Allow React 18 as peerDependency HOT 10
- ReactResizeDetector Component is not working with React18 HOT 1
- Wrong build folder in NPM packages 7.1.1 and 7.0.2 HOT 2
- react resize hook doesn't detect vertical resize HOT 1
- TypeError: window.ResizeObserver is not a constructor HOT 3
- Return width according to provided observerOptions HOT 4
- SourceMap loader complains "Missing source files" in the new v8 build HOT 12
- Debug `console.log` shows up in the code published to NPM HOT 4
- useResizeDetector targetRef cannot be null TS2322 HOT 5
- React errors in DEV mode HOT 12
- useResizeDetector returns width = undefined using forwardRef HOT 10
- dont'work with ReactjS scheduler
- ResizeObserver loop limit exceeded in v9.1.0 HOT 6
- (Request) pass ref to callback HOT 4
- onResize doesn't work when min-height is set HOT 4
- Package not compatible with Edge Functions HOT 4
- Issue with Next.js ^14.0.2 in DEV environment HOT 5
- Best practice for using react-resize-detector v10 in class components HOT 2
- Incorrect peer dependencies HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-resize-detector.