Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 43 of the Intermediate WordPress Plugins: REST API & React Admin course
WordPressReactJune 26, 20263 min read

Unit Testing React Components: Jest & React Testing Library

Learn how to test React components in WordPress. We'll set up Jest, mock API services, and verify UI behavior to ensure your plugin's admin dashboard is stable.

WordPressReactJestTestingUnit TestingJavaScriptphpplugin-development

Previously in this course, we explored Unit Testing API Endpoints in WordPress: A Practical Guide to ensure our backend logic remains sound. Now, we shift our focus to the frontend, where we'll implement unit testing for our React admin dashboard to prevent regressions in our UI.

Testing isn't just about catching bugs; it’s about defining the expected behavior of your components. When you write tests, you're building a safety net that allows you to refactor your code with confidence. In the WordPress ecosystem, we rely on Jest and React Testing Library (RTL) to simulate user interactions and verify rendered output.

Setting Up the Testing Environment

Since we are using @wordpress/scripts, most of the heavy lifting for our testing environment is already handled. The wp-scripts test command is pre-configured to use Jest and the necessary Babel transforms for WordPress.

First, ensure you have the required dependencies installed:

Bash
npm install --save-dev @testing-library/react @testing-library/jest-dom

Next, add a test script to your package.json if it doesn't already exist:

JSON
"scripts": {
    "test": "wp-scripts test-unit-js"
}

This command runs Jest in "watch" mode by default, which is perfect for development. It scans your directory for files ending in .test.js or .spec.js.

Writing Your First Component Test

Let's test a simple component from our Knowledge Base plugin: a status message indicator. We want to verify that when we pass a status prop, the component renders the correct text.

Create a file named StatusIndicator.test.js alongside your component:

JAVASCRIPT
import { render, screen } from CE9178">'@testing-library/react';
import StatusIndicator from CE9178">'./StatusIndicator';

test(CE9178">'renders the correct status message', () => {
    render(<StatusIndicator status="success" />);
    const messageElement = screen.getByText(/success/i);
    expect(messageElement).toBeInTheDocument();
});

Here, render mounts the component into a virtual DOM, and screen provides methods to query elements. We use expect to assert that the element exists.

Mocking the API Service Layer

Real-world components often fetch data using our Building the Knowledge Base Service Layer. We don't want to make actual network requests during unit tests. Instead, we "mock" the service.

If your component uses a service file, you can mock it using jest.mock.

JAVASCRIPT
// KnowledgeBaseList.test.js
import { render, screen, waitFor } from CE9178">'@testing-library/react';
import KnowledgeBaseList from CE9178">'./KnowledgeBaseList';
import * as api from CE9178">'./api';

jest.mock(CE9178">'./api');

test(CE9178">'displays loading state and then data', async () => {
    // Mock the API response
    api.fetchEntries.mockResolvedValue([{ id: 1, title: CE9178">'Test Entry' }]);

    render(<KnowledgeBaseList />);
    
    // Verify loading state
    expect(screen.getByText(/loading/i)).toBeInTheDocument();

    // Verify data appears after promise resolves
    await waitFor(() => {
        expect(screen.getByText(CE9178">'Test Entry')).toBeInTheDocument();
    });
});

This pattern ensures that your tests are deterministic and fast, regardless of the state of your WordPress database.

Hands-on Exercise: Testing a Form

In our previous lesson, Creating a React Form for Submissions in WordPress, we built a form. Your task is to:

  1. Create a KnowledgeBaseForm.test.js file.
  2. Render the form.
  3. Use @testing-library/user-event to simulate typing into the input field.
  4. Verify that the "Submit" button triggers the expected mock function.

Common Pitfalls

  • Testing Implementation Details: Avoid testing internal state or methods. Instead, test what the user sees. If you change your internal logic but the output remains the same, your test shouldn't break.
  • Forgetting to Cleanup: While RTL usually cleans up automatically, if you're using global mocks, ensure you call jest.clearAllMocks() in an afterEach block to prevent test pollution.
  • Asynchronous Race Conditions: Always use findBy* queries or waitFor when waiting for API responses. Using getBy* will fail immediately if the element isn't present during the initial render.

Recap

We've covered the basics of bringing professional testing habits to your React admin UI. By leveraging Jest and React Testing Library, you can isolate components, mock external dependencies, and verify that your UI behaves exactly as expected. This approach is essential for any Introduction to Testing: Quality Assurance in React with Jest workflow.

Up next: We will explore how to manage large datasets efficiently using GraphQL, moving beyond simple REST API patterns.

Previous lessonUnit Testing API EndpointsNext lesson Handling Large Datasets with GraphQL
Back to Blog

Similar Posts

WordPressReactJune 25, 20263 min read

Performance Optimization with Selectors in WordPress React Apps

Master React Performance Optimization in WordPress. Learn how to implement shallow comparison and memoized selectors to prevent unnecessary re-renders.

Read more
WordPressJune 26, 20263 min read

Implementing Real-time Updates with Web: WordPress Strategies

Part of the course

Intermediate WordPress Plugins: REST API & React Admin

intermediate · Lesson 43 of 45

  1. 1

    Setting up the WordPress Development Environment

    3 min
  2. 2

    Introduction to @wordpress/scripts

    3 min
  3. 3

    Configuring ESLint and Prettier

    3 min

Learn to implement real-time updates in your WordPress plugin using efficient polling strategies to keep your React admin dashboard data perfectly synced.

Read more
WordPressJune 26, 20263 min read

Unit Testing API Endpoints in WordPress: A Practical Guide

Learn how to use PHPUnit to test your WordPress REST API endpoints. We cover setting up WP_UnitTestCase, mocking requests, and verifying secure responses.

Read more
4

Localizing Data for JavaScript

3 min
  • 5

    Anatomy of a REST API Endpoint

    3 min
  • 6

    Implementing REST API Permission Callbacks

    3 min
  • 7

    Handling GET Requests in REST API

    3 min
  • 8

    Validating and Sanitizing API Arguments

    4 min
  • 9

    Creating POST Endpoints for Data Submission

    3 min
  • 10

    Updating Existing API Resources

    3 min
  • 11

    Handling Asynchronous State in React

    3 min
  • 12

    Building the Knowledge Base Service Layer

    3 min
  • 13

    Scaffolding the React Admin Dashboard

    3 min
  • 14

    Working with @wordpress/components

    3 min
  • 15

    Creating a React Form for Submissions

    3 min
  • 16

    Implementing CRUD in the Admin UI

    3 min
  • 17

    Understanding WordPress Data Store Architecture

    4 min
  • 18

    Registering a Custom Data Store

    3 min
  • 19

    Writing Selectors for Data Access

    3 min
  • 20

    Defining Actions and Reducers

    3 min
  • 21

    Implementing Resolvers for Data Fetching

    3 min
  • 22

    Optimizing Performance with Selectors

    3 min
  • 23

    Handling Complex State Dependencies

    4 min
  • 24

    Implementing Nonce Verification

    4 min
  • 25

    Advanced Sanitization Techniques

    3 min
  • 26

    Input Validation and Error Handling

    3 min
  • 27

    Protecting Admin Screens

    3 min
  • 28

    Production Build Pipeline

    3 min
  • 29

    Debugging React in the WordPress Admin

    4 min
  • 30

    Building Search and Filter Functionality

    3 min
  • 31

    Internationalization in React

    3 min
  • 32

    Managing File Uploads via REST API

    3 min
  • 33

    Optimizing API Response Times

    3 min
  • 34

    Working with Date and Time in React

    3 min
  • 35

    Implementing Drag-and-Drop Sorting

    3 min
  • 36

    Creating Custom Hooks for API Logic

    3 min
  • 37

    Integrating with Gutenberg Blocks

    4 min
  • 38

    Handling Conflict Resolution

    4 min
  • 39

    Building a Modal Confirmation System

    3 min
  • 40

    Implementing Activity Logging

    3 min
  • 41

    Using Webpack Aliases

    3 min
  • 42

    Unit Testing API Endpoints

    3 min
  • 43

    Unit Testing React Components

    3 min
  • 44

    Handling Large Datasets with GraphQL

    3 min
  • 45

    Implementing Real-time Updates with Web

    3 min
  • View full course