A way to get at top level rendered element
See original GitHub issueDescribe the Feature
At my company, we have a few uses case where we want to make assertion on the top level rendered element. Pre v7, we were able to do const element = view.container.children?.[0] || null
. Is there a way to get the same result for post v7 without adding additional testID
?
Trivial use cases example:
const Component = (render=true) => (render ? <View></View> : null);
// assert that component render when render is true
// assert that component render nothing when render is false
Possible Implementations
I see that we have renderer.root here. Maybe just have to expose that element
Related Issues
https://github.com/callstack/react-native-testing-library/issues/477#issuecomment-699096451
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (9 by maintainers)
Top Results From Across the Web
How can I access current component's top-level element ref ...
While it seems to make sense, unfortunately, I'd have to do this with each component that wants this behavior (which could be many)....
Read more >ReactDOM.render and the Top Level React API
ReactDOM.render and the Top Level React API ... To update the properties of an existing component, you call render again with a new...
Read more >Top-Level API | React
A component implements a render method which returns one single child. ... The resulting element will have the original element's props with the...
Read more >The stacking context - CSS: Cascading Style Sheets | MDN
An easy way to figure out the rendering order of stacked elements along the z-axis is to think of it as a "version...
Read more >Meet the top layer: a solution to z-index:10000
Elements in the top layer are stacked in the order they appear in the set. The last one in, appears on top. If...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
IMHO this can be added, but together with exporting the root component 😃 Trying to hide the element that is needed to work with custom queries, and it is also returned from the query is just not logical. I can prepare workaround to have this root container either using
wrapper
option or suggested by youextendWithQueries
.I believe that either we agree that root/container and custom queries are allowed or not. I understand that the goal is to prevent testing internal things and creating more stable tests. But not exporting the root is not doing that. Additionally there are exported queries that brakes the rules.
In react-testing-library you have access to the DOM of rendered components. With that you can of course use
.parentNode
,children
or check the attributes. But is not recommended. The similar situation is here and not exporting root does not change anything, besides forcing users to do workarounds 😃Together with @pvinis we found a way to expose root ReactTestInstance without making it too easy to use – through an API designed to extend default queries. Here’s a rough idea: https://github.com/callstack/react-native-testing-library/pull/569#issuecomment-705494579