# Component testing [Unit testing your Redux actions and reducers](unit-testing.md) is nice, but you can do even more to make sure nothing breaks your application. Since React is the _view_ layer of your app, let's see how to test Components too! - [Shallow rendering](#shallow-rendering) - [react-testing-library](#react-testing-library) - [Snapshot testing](#snapshot-testing) - [Behavior testing](#behavior-testing) ## Shallow rendering React provides us with a nice add-on called the Shallow Renderer. This renderer will render a React component **one level deep**. Lets take a look at what that means with a simple ` ); } export default Button; ``` _Note: This is a [state**less** ("dumb") component](../js/README.md#architecture-components-and-containers)_ It might be used in another component like this: ```javascript // HomePage.js import Button from './Button'; function HomePage() { return ; } ``` _Note: This is a [state**ful** ("smart") component](../js/README.md#architecture-components-and-containers)!_ When rendered normally with the standard `ReactDOM.render` function, this will be the HTML output (_Comments added in parallel to compare structures in HTML from JSX source_): ```html ``` Conversely, when rendered with the shallow renderer, we'll get a String containing this "HTML": ```html ``` If we test our `Button` with the normal renderer and there's a problem with the `CheckmarkIcon` then the test for the `Button` will fail as well. This makes it harder to find the culprit. Using the _shallow_ renderer, we isolate the problem's cause since we don't render any other components other than the one we're testing! The problem with the shallow renderer is that all assertions have to be done manually, and you cannot do anything that needs the DOM. ## react-testing-library In order to write more maintainable tests which also resemble more closely the way our component is used in real life, we have included [react-testing-library](https://github.com/kentcdodds/react-testing-library). This library renders our component within an actual DOM and provides utilities for querying it. Let's give it a go with our `); expect(container.firstChild).toMatchSnapshot(); }); ``` `render` returns an object that has a property `container` and yes, this is the container our `); fireEvent.click(getByText(text)); expect(onClickSpy).toHaveBeenCalledTimes(1); }); ``` Our finished test file looks like this: ```javascript import React from 'react'; import { render, fireEvent } from 'react-testing-library'; import Button from '../Button'; describe('); expect(container.firstChild).toMatchSnapshot(); }); it('handles clicks', () => { const onClickMock = jest.fn(); const text = 'Click me!'; const { getByText } = render(); fireEvent.click(getByText(text)); expect(onClickSpy).toHaveBeenCalledTimes(1); }); }); ``` And that's how you unit test your components and make sure they work correctly! Also have a look at our example application. It deliberately shows some variations of implementing tests with react-testing-library. _Continue to learn how to test your components [remotely](remote-testing.md)!_