Home-Blog-

Content

How to test components with React.createElement in React using Enzyme?

May 30, 2025

Hey there! If you're into React development, you've probably come across the need to test your components at some point. And if you're using React.createElement to create components in React, you might be wondering how to test these components effectively. That's where Enzyme comes in, and as an Enzyme supplier, I'm here to walk you through the process step by step.

Why Enzyme and React.createElement?

First off, let's quickly talk about why we use React.createElement and Enzyme. React.createElement is a fundamental way to create React elements in React. It might seem old - fashioned with the introduction of JSX, but there are still plenty of scenarios where you'd want to use it, like in older codebases or when you need more control over the element creation process.

Enzyme, on the other hand, is an awesome testing utility for React developed by Airbnb. It provides a set of flexible APIs for interacting with and testing React components. It makes it super easy to mount components, simulate events, and assert on the output.

Setting Up the Test Environment

Before we start testing our components created with React.createElement, we need to set up our test environment. First, you'll need to install Enzyme and its adapter for your version of React. For example, if you're using React 16, you'll use enzyme-adapter-react-16.

npm install enzyme enzyme-adapter-react-16 --save-dev

Once you've installed these packages, you'll need to configure the adapter in your test setup file. Typically, you'd create a setupTests.js file in your src directory.

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

Let's Create a Component with React.createElement

Let's start by creating a simple component using React.createElement. Here's a basic HelloWorld component:

import React from 'react';

const HelloWorld = () => {
    return React.createElement('div', null, 'Hello, World!');
};

export default HelloWorld;

This component is pretty straightforward. It creates a div element with the text "Hello, World!" inside it.

Testing the Component with Enzyme

Now that we have our component, let's test it using Enzyme. We'll use the shallow rendering method, which renders a component "shallowly", meaning it only renders one level deep and doesn't render any child components.

import React from 'react';
import { shallow } from 'enzyme';
import HelloWorld from './HelloWorld';

describe('HelloWorld component', () => {
    it('should render the correct text', () => {
        const wrapper = shallow(<HelloWorld />);
        expect(wrapper.text()).toBe('Hello, World!');
    });
});

In this test, we're using the shallow function from Enzyme to render our HelloWorld component. Then, we're using the text method to get the text content of the rendered component and asserting that it matches "Hello, World!".

Working with Components that Accept Props

Often, components created with React.createElement accept props. Let's create a new component that accepts a name prop and uses it to display a personalized greeting.

import React from 'react';

const Greeting = (props) => {
    return React.createElement('div', null, `Hello, ${props.name}!`);
};

export default Greeting;

Now, let's test this component. We'll pass a name prop to the component when we render it using Enzyme.

import React from 'react';
import { shallow } from 'enzyme';
import Greeting from './Greeting';

describe('Greeting component', () => {
    it('should render the correct greeting', () => {
        const wrapper = shallow(<Greeting name="John" />);
        expect(wrapper.text()).toBe('Hello, John!');
    });
});

Simulating Events

Another important aspect of testing React components is simulating events. Let's create a component with a button that, when clicked, updates some state.

import React, { useState } from 'react';

const Clickable = () => {
    const [clicked, setClicked] = useState(false);
    const handleClick = () => {
        setClicked(true);
    };

    return React.createElement('button', { onClick: handleClick }, clicked? 'Clicked' : 'Click me');
};

export default Clickable;

To test the click event, we'll use Enzyme's simulate method.

High Quality Wholesale Hemp ProteinArabic Gum

import React from 'react';
import { shallow } from 'enzyme';
import Clickable from './Clickable';

describe('Clickable component', () => {
    it('should update the text when clicked', () => {
        const wrapper = shallow(<Clickable />);
        expect(wrapper.text()).toBe('Click me');
        wrapper.find('button').simulate('click');
        expect(wrapper.text()).toBe('Clicked');
    });
});

Some Additional Resources for Testing Ingredients

If you're into natural ingredients for projects or just curious, check out these links. You can find Curcuma Longa Turmeric Extract/95%curcumin HPLC, High Quality Wholesale Hemp Protein, and Arabic Gum at AB.com. These could be great for various applications!

Conclusion and Call to Action

Testing components created with React.createElement using Enzyme is a powerful way to ensure the reliability and functionality of your React applications. By following the steps outlined in this blog, you can set up your test environment, create components, and write effective tests.

If you're interested in using Enzyme for your projects or have any questions about our Enzyme products, don't hesitate to reach out for a procurement discussion. We're here to offer the right solutions for your React testing needs.

References

  • React official documentation
  • Enzyme GitHub repository
SEND INQUIRY

SEND INQUIRY