For a complete example of usage, see `client/components/themes-list/test/index.jsx`.
The render function basically just draws a bunch of Theme sub-components:
```javascript
class ThemesList extends React.Component {
// …
render() {
return (
{ this.props.themes.map( function ( theme ) {
return ;
} ) }
);
}
}
```
So we test it like this:
```javascript
import { render, screen } from '@testing-library/react';
import EmptyContent from 'calypso/components/empty-content';
import Theme from 'calypso/components/theme';
import { ThemesList } from '../';
const defaultProps = deepFreeze( {
themes: [
{ id: '1', name: 'kubrick', screenshot: '/theme/kubrick/screenshot.png' },
{ id: '2', name: 'picard', screenshot: '/theme/picard/screenshot.png' },
],
// …
} );
test( 'should render a div with a className of "themes-list"', () => {
const { container } = render( );
expect( container ).toMatchSnapshot();
expect( container.firstChild ).toHaveClass( 'themes-list' );
expect( container.querySelectorAll( '.theme' ) ).toHaveLength( defaultProps.themes.length );
} );
test( 'should render a child for each provided theme', () => {
const { container } = render( );
expect( container.querySelectorAll( '.theme' ) ).toHaveLength( defaultProps.themes.length );
} );
test( 'should display the EmptyContent component when no themes are provided', () => {
const { container } = render( );
expect( container ).toBeEmptyDOMElement();
} );
```
By using `shallow`, we avoid rendering the `Theme` components when testing `ThemesList`.
## Troubleshooting
- Valid tests can fail if a component is wrapped in a higher order component, like `localize()` or `connect()`. This is because a shallow render only results in the higher component being rendered, not its children.
The best practice is to test the unwrapped component, with external dependencies mocked, so that the results aren't influenced by anything outside the component being tested:
```javascript
// Bad. Tests cannot access the unwrapped component.
export default localize(
class SomeComponent extends React.Component {
// ...
}
);
```
```javascript
// Good! This component can imported for testing.
export class SomeComponent extends React.Component {
// ...
}
// The default export wrapped component can be imported for use elsewhere.
export default localize( SomeComponent );
```
See [#18064](https://github.com/Automattic/wp-calypso/pull/18064) for full examples of using ES6 classes.
## Enzyme support
Historically, we used to support [`enzyme`](https://github.com/enzymejs/enzyme), but support was dropped in favor of `@testing-library/react`, the primary reason being the fact that it was incompatible with React 18, and we are aiming at unblocking the upgrade to React 18. There were additional motivations, like being able to write more accessible tests and being able to test closer to what the user actually experiences.
Previously, `enzyme` was provided by the `@automattic/calypso-jest` package as part of the testing infrastructure. Nowadays, in the Calypso monorepo, it's recommended to use `@testing-library/react` for component tests.
If you wish to use `enzyme` in your project that uses a Calypso package, you can still use it by manually providing the React 17 adapter, by following the steps below. Note that it's likely that Enzyme still doesn't support React 18 yet.
To install the enzyme dependency, run:
```bash
npm install --save enzyme
```
To install the React 17 adapter dependency, run:
```bash
npm install --save @wojtekmaj/enzyme-adapter-react-17
```
To use the React 17 adapter, use this in your [`setupFilesAfterEnv`](https://jestjs.io/docs/configuration#setupfilesafterenv-array) configuration:
```javascript
// It "mocks" enzyme, so that we can delay loading of
// the utility functions until enzyme is imported in tests.
// Props to @gdborton for sharing this technique in his article:
// https://medium.com/airbnb-engineering/unlocking-test-performance-migrating-from-mocha-to-jest-2796c508ec50.
let mockEnzymeSetup = false;
jest.mock( 'enzyme', () => {
const actualEnzyme = jest.requireActual( 'enzyme' );
if ( ! mockEnzymeSetup ) {
mockEnzymeSetup = true;
// Configure enzyme 3 for React, from docs: http://airbnb.io/enzyme/docs/installation/index.html
const Adapter = jest.requireActual( '@wojtekmaj/enzyme-adapter-react-17' );
actualEnzyme.configure( { adapter: new Adapter() } );
}
return actualEnzyme;
} );
```
If you also use snapshot tests with `enzyme`, you might want to add support for serializing them, through the `enzyme-to-json` package.
To install the dependency, run:
```bash
npm install --save enzyme-to-json
```
Finally, you should add `enzyme-to-json/serializer` to the array of [`snapshotSerializers`](https://jestjs.io/docs/configuration#snapshotserializers-arraystring) in your `jest` configuration:
```
{
snapshotSerializers: [ 'enzyme-to-json/serializer' ]
}
```