Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 minLESSON 08 / 8

Testing components

01

Understand the idea

A good component test renders the component with known props and checks the text a person would see — not the private names of useState .

Test what a person can see

You do not need a huge app to learn component testing. Greeting from the first lesson takes a name and returns a heading. That is a clear contract: given Ada, show Hello, Ada.

A testable is one you can render by passing . If it only works when a global variable, a hidden context, and a network call are all perfect, testing becomes setting up a miniature product.

What the live example does

The example is a tiny test runner inside the preview:

  1. Create a temporary DOM node.
  2. Render Greeting with name Ada into it.
  3. flushSync makes sure the render finishes before we read the result.
  4. Read textContent.
  5. Check that it equals Hello, Ada.
  6. Log pass or fail.

Teams often use React Testing Library for the same idea: render, then find by role or text. The assertion here is that idea without a separate test runner.

What the test should know
KnowsDoes not needWhy
Props you passedInternal useState namesThe public contract
Visible textHow many times it renderedPeople read text
A button’s role or labelExact CSS class stringsClasses change in redesigns

Arrange, act, assert

  • Arrange: choose props (name set to Ada).
  • Act: render the component.
  • Assert: check the visible text.

When this fails in a pull request, the message should say the greeting was wrong, not dump an opaque snapshot.

Keep components easy to call

Prefer props over hidden globals. If the component truly needs context, the test can wrap it in a Provider on purpose. Make that choice visible.

What you can defend after this lesson

You can point at Greeting and at one clear assertion. That is the bar: a real component from earlier, rendered, and checked against what a person would read.

02

Mental model

FOLLOW THE VALUE

props
render
DOM text

Arrange, act, assert.

01 / 03Trace the transformation

03

Live example

The example renders Greeting with name Ada into a temporary node, then logs pass when the text is Hello, Ada.

import { flushSync } from "react-dom"; // Finish the render before asserting.
import { createRoot } from "react-dom/client";
import { Greeting } from "./Greeting"; // Component from the first lesson.
export default function App() {
  const host = document.createElement("div"); // Stand-in for a test DOM node.
  const root = createRoot(host);
  flushSync(() => {
    root.render(<Greeting name="Ada" />); // Act: render with known props.
  });
  const text = host.textContent; // Read what the screen shows.
  const ok = text === "Hello, Ada"; // Assert the visible contract.
  console.log(ok ? "pass" : "fail"); // Result of the assertion.
  console.log(text); // The text under test.
  console.log("greeting"); // The component under test.
  console.log("assert"); // This run is an assertion.
  console.log("tested"); // Marker that the check finished.
  return <p>{ok ? "pass" : "fail"}: {text}</p>;
}

04

Guided challenge

Expect the greeting that rendered

The test logs fail. Change it to pass when the heading text is Hello, Ada.

import { flushSync } from "react-dom";
import { createRoot } from "react-dom/client";
import { Greeting } from "./Greeting";
export default function App() {
  const host = document.createElement("div");
  const root = createRoot(host);
  flushSync(() => {
    root.render(<Greeting name="Ada" />); // Render still produces Hello, Ada.
  });
  console.log("fail"); // Wrong - change to pass when the text matches.
  console.log(host.textContent); // Keep the text log.
  console.log("greeting"); // Keep the greeting log.
  console.log("assert"); // Keep the assert log.
  console.log("tested"); // Keep the tested log.
  return <p>fail</p>;
}

EXPECTED CONSOLE OUTPUT

pass
Hello, Ada
greeting
assert
tested

Hints 0/2

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    import { Greeting } from "./Greeting";
    export default function App() {
      return <Greeting name="Ada" />; // Try a different name and watch the heading.
    }
    
    
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What should a component test look at?
    02Why reuse Greeting from the first lesson?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress