Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a className with the Jest and React testing library

I am totally new to JavaScript testing and am working in a new codebase. I would like to write a test that is checking for a className on the element. I am working with Jest and React Testing Library. Below I have a test that will render a button based on the variant prop. It also contains a className and I would like to test that.

it('Renders with a className equal to the variant', () => {     const { container } = render(<Button variant="default" />)     expect(container.firstChild) // Check for className here }) 

I tried to google for a property like Enzyme has with hasClass, but I couldn't find anything. How can I solve this with the current libraries (React Testing Library and Jest)?

like image 513
Giesburts Avatar asked Nov 20 '18 09:11

Giesburts


People also ask

How do I test the className in React test library?

To find elements by className in React testing library: Render a component and destructure the container object from the result. Use the getElementsByClassName() method on the container to find elements by class name.

Does React testing library use Jest?

React Testing Library is not specific to any testing framework; we can use it with any other testing library, although Jest is recommended and preferred by many developers. create-react-app uses both Jest and React Testing Library by default.

Can you test React with Jest?

Jest is a JavaScript testing framework that allows developers to run tests on JavaScript and TypeScript code and can be easily integrated with React JS. Open the package. json, and you will find that when you use create-react-app for creating a react project, it has default support for jest and react testing library.


2 Answers

You can easily do that with react-testing-library.

First, you have to understand that container or the result of getByText etc. are merely DOM nodes. You can interact with them in the same way you would do in a browser.

So, if you want to know what class is applied to container.firstChild you can just do it like this container.firstChild.className.

If you read more about className in MDN you'll see that it returns all the classes applied to your element separated by a space, that is:

<div class="foo">     => className === 'foo' <div class="foo bar"> => className === 'foo bar' 

This might not be the best solution depending on your case. No worries, you can use another browser API, for example classList.

expect(container.firstChild.classList.contains('foo')).toBe(true) 

That's it! No need to learn a new API that works only for tests. It's just as in the browser.

If checking for a class is something you do often you can make the tests easier by adding jest-dom to your project.

The test then becomes:

expect(container.firstChild).toHaveClass('foo') 

There are a bunch of other handy methods like toHaveStyle that could help you.


As a side note, react-testing-library is a proper JavaScript testing utility. It has many advantages over other libraries. I encourage you to join the spectrum forum if you're new to JavaScript testing.

like image 76
Giorgio Polvara - Gpx Avatar answered Oct 24 '22 08:10

Giorgio Polvara - Gpx


The library gives access to normal DOM selectors, so we can also simply do this:

it('Renders with a className equal to the variant', () => {     const { container } = render(<Button variant="default" />)     expect(container.getElementsByClassName('default').length).toBe(1); }); 
like image 24
jbmilgrom Avatar answered Oct 24 '22 07:10

jbmilgrom