Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Render Method
2.1.
Syntax
3.
Render Result
3.1.
…queries
3.1.1.
Example
3.2.
container
3.3.
baseElement
3.4.
debug
3.4.1.
Example
3.5.
rerender
3.5.1.
Example
3.6.
unmount
3.6.1.
Example
3.7.
asFragment
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Render Result in React

Introduction

Many times we need to test our React apps in the way in which our users will be accessing the app. This is where we will make the best use of the render method in React testing library. By using the render method we will be able to access the actual DOM tree rendered by React in the browser.

In this blog, we will learn about how we can utilize the object returned by the render method that has a few properties that make it possible for us to get the most out of the testing library.

Render Method

When we use the render method on any component created in React it wraps it in a container and appends it to the document.body. Also, one of the most important features of render is that the queries from DOM Testing Library are automatically returned with their first argument bound to the baseElement, which contains the default value as the document.

The component passed in the render method is described as a view to be rendered in the browser window as a DOM tree. 

Thus, when we couple it with how virtual DOM works in React there can be many ways in which the method can be used and manipulated.

Syntax

Let’s first create a simple component in react and name it Test.

import React from 'react';
function Test() {
	return <div>Hello</div>
}
Export default Test;

Then we import the component and the library to the folder where we want to test the DOM tree created by the component.

import React from 'react';
import {render} from '@testing-library/react';
import Test from './Test';

describe('Test', () => {
	test('renders Test component', () => {
		render(<Test/>); //This will show us the HTML output of the component.
	});
});

Render Result

The render method usually returns an object that has a few properties: 

…queries

Whenever a render method is called it returns the queries from the DOM Testing Library with the first argument bound to the baseElement which is defaulted to be document.body.

Example

const {getByLabelText, queryAllByTestId} = render(<Test/>);

container

The render method essentially wraps the DOM tree created out of the component in the method in a container. As this is a DOM node it is basically a div element. Thus, just like a normal DOM Node, we can use container.querySelector to inspect the children wrapped and returned by the render method.

However, this must be avoided when using a query for rendered elements. As the other queries are more resilient to changes made to the component we are testing. 

baseElement

Let’s say we need to test render where we test something outside the container div. Here we can simply use When we don’t mention the baseElement the default value is set to document.body. However, we can change it in the options of render.

debug

When we are creating a prettyDOM for a particular component in the render method, we can use a shortcut returned by the render function itself. This is the debug property that acts like a simple wrapper around prettyDOM which is exposed and comes from the testing library.

Example

Import React from 'react'
Import {render} from '@testing-library/react'

const {debug} = render(<Test/>);
debug();
//<div>
// <div>Hello</div>
//</div>

rerender

Sometimes the components we are testing are taking parameters that may need to be updated. In cases like these, we need to re-render our components with the updated props and then test the components and their renders. 

Example

import {render} from '@testing-library/react'
const {rerender} = render(<Test number = {30}/>)
rerender(<Test number = {50}/>) //rerenders the same component with different props

unmount

In many cases where we use a lot of event handlers, we eventually clear it up after a certain condition is met. In scenarios like these, the component that has been rendered as of now has to be unmounted. Another case can be when we need to check what happens when a particular component is removed from the page. This can be done using the unmount function and helps in mitigating memory leaks that may occur in the above cases.

Example

import {render} from '@testing-library/react'

const {container, unmount} = render(<Test/>)
unmount()
//the component has been unmounted

asFragment

When this function is called it returns a DocumentFragment of our rendered component. This is used when we need to check how our component reacts to particular events all while avoiding live bindings.

Frequently Asked Questions

1. What is prettyDOM?

Ans: PrettyDOM is a helper function that can be used to print a readable representation of the DOM tree of a node.

 

2. Difference between Enzyme and React testing library.

Ans: While Enzyme allows you to access the internal working of the component rendered the testing library doesn't give any such access to the implementation details.

 

3. What is a DOM Tree?

Ans: A DOM(Document Object Model) tree is basically a representation of all the objects in the XML or HTML document in the form of nodes that are connected to each other to form a tree-like structure.
 

4. What is a Virtual DOM?

Ans: A Virtual DOM is an ideal copy of the actual DOM which is kept in memory and synced with the real DOM every time a change occurs, thus making it comparatively faster.

Conclusion

In this blog, we discussed the different methods as a result of using the render function of the React testing library on a component.

You may want to learn more about react testing utilities hereWe hope that this blog has helped you enhance your knowledge regarding render functions in the React testing library. Do upvote our blog to help other ninjas grow.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Learning!

Live masterclass