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.

A way to get at top level rendered element

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
klekowskimcommented, Oct 8, 2020

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 you extendWithQueries.

  1. Workaround using new extendWithQueries:
const getRoot = (container) => { return container }
const {getRoot} = render(<Comp />, {extendWithQueries: {getByTextWithType}});
const root = getRoot()
  1. Workaround using wrapper
function render(component, options) {
const result = render(component, {...options, wrapper: <View testId='root'>{children}</View>}});
const root = getByTestId('root');
return {...result, root };
}

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 😃

1reaction
thymikeecommented, Oct 8, 2020

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

const getByTextWithType = (root) => { ... }
const customRender = render<typeof getByTextWithType>(<Comp />, {extendWithQueries: {getByTextWithType}});
const {getByTextWithType} = customRender(<Comp />)
Read more comments on GitHub >

github_iconTop 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 >

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