Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to check that a component is not rendered

Let's assume that a component returns null in render method, based on some prop.

What is the best way to use expect in order to ensure that component is not rendered?

Example:

import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import Pagination from '../Pagination';

it('should not render if totaPages is 0', () => {
  const { container } = render(<Pagination activePage={1} totalPages={0} />);
  expect(container.firstChild).toBeNull();
});

Is the above code enough?

like image 393
Antonios Providakis Avatar asked Jun 13 '19 18:06

Antonios Providakis


1 Answers

If you use jest-dom you can do expect(container).toBeEmptyDOMElement(). I find it a bit more readable, but your solution works too

like image 164
Giorgio Polvara - Gpx Avatar answered Nov 03 '22 05:11

Giorgio Polvara - Gpx