React component tests should be interesting, straightforward, and easy for a human to build and maintain.
Yet, the current state of the testing library ecosystem is not sufficient to motivate developers to write consistent JavaScript tests for React components. Testing React components—and the DOM in general—often require some kind of higher-level wrapper around popular testing frameworks like Jest or Mocha.
Here’s the problem
Writing component tests with the tools available today is boring, and even when you get to writing them, it takes lots of hassle. Expressing test logic following a jQuery-like style (chaining) is confusing. It doesn’t jive with how React components are usually built.
The Enzyme code below is readable, but a bit too bulky because it uses too many words to express something that is ultimately simple markup.
expect(screen.find(".view").hasClass("technologies")).to.equal(true);
expect(screen.find("h3").text()).toEqual("Technologies:");
expect(screen.find("ul").children()).to.have.lengthOf(4);
expect(screen.contains([
<li>JavaScript</li>,
<li>ReactJs</li>,
<li>NodeJs</li>,
<li>Webpack</li>
])).to.equal(true);
expect(screen.find("button").text()).toEqual("Back");
expect(screen.find("button").hasClass("small")).to.equal(true);
The DOM representation is just this:
<div className="view technologies">
<h3>Technologies:</h3>
<ul>
<li>JavaScript</li>
<li>ReactJs</li>
<li>NodeJs</li>
<li>Webpack</li>
</ul>
<button className="small">Back</button>
</div>
What if you need to test heavier components? While the syntax is still bearable, it doesn’t help your brain grasp the structure and logic. Reading and writing several tests like this is bound to wear you out—it certainly wears me out. That’s because React components follow certain principles to generate HTML code at the end. Tests that express the same principles, on the other hand, are not straightforward. Simply using JavaScript chaining won’t help in the long run.
There are two main issues with testing in React:
- How to even approach writing tests specifically for components
- How to avoid all the unnecessary noise
Let’s further expand those before jumping into the real examples.
Approaching React component tests
A simple React component may look like this:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
This is a function that accepts a props
object and returns a DOM node using the JSX syntax.
Since a component can be represented by a function, it is all about testing functions. We need to account for arguments and how they influence the returned result. Applying that logic to React components, the focus in the tests should be on setting up props and testing for the DOM rendered in the UI. Since user actions like mouseover
, click
, typing, etc. may also lead to UI changes, you will need to find a way to programmatically trigger those too.
Hiding the unnecessary noise in tests
Tests require a certain level of readability achieved by both slimming the wording down and following a certain pattern to describe each scenario.
Component tests flow through three phases:
- Preparation (setup): The component props are prepared.
- Render (action): The component needs to render its DOM to the UI before either triggering any actions on it or testing for certain texts and attributes. That’s when actions can be programmatically triggered.
- Validation (verify): The expectations are set, verifying certain side effects over the component markup.
Here is an example:
it("should click a large button", () => {
// 1️⃣ Preparation
// Prepare component props
props.size = "large";
// 2️⃣ Render
// Render the Button's DOM and click on it
const component = mount(<Button {...props}>Send</Button>);
simulate(component, { type: "click" });
// 3️⃣ Validation
// Verify a .clicked class is added
expect(component, "to have class", "clicked");
});
For simpler tests, the phases can merge:
it("should render with a custom text", () => {
// Mixing up all three phases into a single expect() call
expect(
// 1️⃣ Preparation
<Button>Send</Button>,
// 2️⃣ Render
"when mounted",
// 3️⃣ Validation
"to have text",
"Send"
);
});
Writing component tests today
Those two examples above look logical but are anything but trivial. Most of the testing tools do not provide such a level of abstraction, so we have to handle it ourselves. Perhaps the code below looks more familiar.
it("should display the technologies view", () => {
const container = document.createElement("div");
document.body.appendChild(container);
act(() => {
ReactDOM.render(<ProfileCard {...props} />, container);
});
const button = container.querySelector("button");
act(() => {
button.dispatchEvent(new window.MouseEvent("click", { bubbles: true }));
});
const details = container.querySelector(".details");
expect(details.classList.contains("technologies")).toBe(true);
expect(details.querySelector("h3").textContent, "to be", "Technologies");
expect(details.querySelector("button").textContent, "to be", "View Bio");
});
Compare that with the same test, only with an added layer of abstraction:
it("should display the technologies view", () => {
const component = mount(<ProfileCard {...props} />);
simulate(component, {
type: "click",
target: "button",
});
expect(
component,
"queried for first",
".details",
"to exhaustively satisfy",
<div className="details technologies">
<h3>Technologies</h3>
<div>
<button>View Bio</button>
</div>
</div>
);
});
It does look much better. Less code, obvious flow, and more DOM instead of JavaScript. This is not a fiction test, but something you can achieve with UnexpectedJS today.
The following section is a deep dive into testing React components without getting too deep into UnexpectedJS. Its documentation more than does the job. Instead, we’ll focus on usage, examples, and possibilities.
Writing React Tests with UnexpectedJS
UnexpectedJS is an extensible assertion toolkit compatible with all test frameworks. It can be extended with plugins, and some of those plugins are used in the test project below. Probably the best thing about this library is the handy syntax it provides to describe component test cases in React.
The example: A Profile Card component
The subject of the tests is a Profile card component.
And here is the full component code of ProfileCard.js
:
// ProfileCard.js
export default function ProfileCard({
data: {
name,
posts,
isOnline = false,
bio = "",
location = "",
technologies = [],
creationDate,
onViewChange,
},
}) {
const [isBioVisible, setIsBioVisible] = useState(true);
const handleBioVisibility = () => {
setIsBioVisible(!isBioVisible);
if (typeof onViewChange === "function") {
onViewChange(!isBioVisible);
}
};
return (
<div className="ProfileCard">
<div className="avatar">
<h2>{name}</h2>
<i className="photo" />
<span>{posts} posts</span>
<i className={`status ${isOnline ? "online" : "offline"}`} />
</div>
<div className={`details ${isBioVisible ? "bio" : "technologies"}`}>
{isBioVisible ? (
<>
<h3>Bio</h3>
<p>{bio !== "" ? bio : "No bio provided yet"}</p>
<div>
<button onClick={handleBioVisibility}>View Skills</button>
<p className="joined">Joined: {creationDate}</p>
</div>
</>
) : (
<>
<h3>Technologies</h3>
{technologies.length > 0 && (
<ul>
{technologies.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
)}
<div>
<button onClick={handleBioVisibility}>View Bio</button>
{!!location && <p className="location">Location: {location}</p>}
</div>
</>
)}
</div>
</div>
);
}
We will work with the component’s desktop version. You can read more about device-driven code split in React but note that testing mobile components is still pretty straightforward.
Setting up the example project
Not all tests are covered in this article, but we will certainly look at the most interesting ones. If you want to follow along, view this component in the browser, or check all its tests, go ahead and clone the GitHub repo.
## 1. Clone the project:
git clone git@github.com:moubi/profile-card.git
## 2. Navigate to the project folder:
cd profile-card
## 3. Install the dependencies:
yarn
## 4. Start and view the component in the browser:
yarn start
## 5. Run the tests:
yarn test
Here’s how the <ProfileCard />
component and UnexpectedJS tests are structured once the project has spun up:
/src
└── /components
├── /ProfileCard
| ├── ProfileCard.js
| ├── ProfileCard.scss
| └── ProfileCard.test.js
└── /test-utils
└── unexpected-react.js
Component tests
Let’s take a look at some of the component tests. These are located in src/components/ProfileCard/ProfileCard.test.js
. Note how each test is organized by the three phases we covered earlier.
- Setting up required component props for each test.
beforeEach(() => {
props = {
data: {
name: "Justin Case",
posts: 45,
creationDate: "01.01.2021",
},
};
});
Before each test, a props
object with the required <ProfileCard />
props is composed, where props.data
contains the minimum info for the component to render.
- Render with a default set of props.
This test checks the whole DOM produced by the component when passing name
, posts
, and creationDate
fields.
Here’s what the result produces in the UI:
And here’s the test case for it:
it("should render default", () => {
// "to exhaustively satisfy" ensures all classes/attributes are also matching
expect(
<ProfileCard {...props} />,
"when mounted",
"to exhaustively satisfy",
<div className="ProfileCard">
<div className="avatar">
<h2>Justin Case</h2>
<i className="photo" />
<span>45{" posts"}</span>
<i className="status offline" />
</div>
<div className="details bio">
<h3>Bio</h3>
<p>No bio provided yet</p>
<div>
<button>View Skills</button>
<p className="joined">{"Joined: "}01.01.2021</p>
</div>
</div>
</div>
);
});
- Render with status online.
Now we check if the profile renders with the “online” status icon.
And the test case for that:
it("should display online icon", () => {
// Set the isOnline prop
props.data.isOnline = true;
// The minimum to test for is the presence of the .online class
expect(
<ProfileCard {...props} />,
"when mounted",
"queried for first",
".status",
"to have class",
"online"
);
});
- Render with bio text.
<ProfileCard />
accepts any arbitrary string for its bio.
So, let’s write a test case for that:
it("should display online icon", () => {
// Set the isOnline prop
props.data.isOnline = true;
// The minimum to test for is the presence of the .online class
expect(
<ProfileCard {...props} />,
"when mounted",
"queried for first",
".status",
"to have class",
"online"
);
});
- Render “Technologies” view with an empty list.
Clicking on the “View Skills” link should switch to a list of technologies for this user. If no data is passed, then the list should be empty.
Here’s that test case:
it("should display the technologies view", () => {
// Mount <ProfileCard /> and obtain a ref
const component = mount(<ProfileCard {...props} />);
// Simulate a click on the button element ("View Skills" link)
simulate(component, {
type: "click",
target: "button",
});
// Check if the .details element contains the technologies view
expect(
component,
"queried for first",
".details",
"to exhaustively satisfy",
<div className="details technologies">
<h3>Technologies</h3>
<div>
<button>View Bio</button>
</div>
</div>
);
});
- Render a list of technologies.
If a list of technologies is passed, it will display in the UI when clicking on the “View Skills” link.
Yep, another test case:
it("should display list of technologies", () => {
// Set the list of technologies
props.data.technologies = ["JavaScript", "React", "NodeJs"];
// Mount ProfileCard and obtain a ref
const component = mount(<ProfileCard {...props} />);
// Simulate a click on the button element ("View Skills" link)
simulate(component, {
type: "click",
target: "button",
});
// Check if the list of technologies is present and matches prop values
expect(
component,
"queried for first",
".technologies ul",
"to exhaustively satisfy",
<ul>
<li>JavaScript</li>
<li>React</li>
<li>NodeJs</li>
</ul>
);
});
- Render a user location.
That information should render in the DOM only if it was provided as a prop.
The test case:
it("should display location", () => {
// Set the location
props.data.location = "Copenhagen, Denmark";
// Mount <ProfileCard /> and obtain a ref
const component = mount(<ProfileCard {...props} />);
// Simulate a click on the button element ("View Skills" link)
// Location render only as part of the Technologies view
simulate(component, {
type: "click",
target: "button",
});
// Check if the location string matches the prop value
expect(
component,
"queried for first",
".location",
"to have text",
"Location: Copenhagen, Denmark"
);
});
- Calling a callback when switching views.
This test does not compare DOM nodes but does check if a function prop passed to <ProfileCard />
is executed with the correct argument when switching between the Bio and Technologies views.
it("should call onViewChange prop", () => {
// Create a function stub (dummy)
props.data.onViewChange = sinon.stub();
// Mount ProfileCard and obtain a ref
const component = mount(<ProfileCard {...props} />);
// Simulate a click on the button element ("View Skills" link)
simulate(component, {
type: "click",
target: "button",
});
// Check if the stub function prop is called with false value for isBioVisible
// isBioVisible is part of the component's local state
expect(
props.data.onViewChange,
"to have a call exhaustively satisfying",
[false]
);
});
Running all the tests
Now, all of the tests for <ProfileCard />
can be executed with a simple command:
yarn test
Notice that tests are grouped. There are two independent tests and two groups of tests for each of the <ProfileCard />
views—bio and technologies. Grouping makes test suites easier to follow and is a nice way to organize logically-related UI units.
Some final words
Again, this is meant to be a fairly simple example of how to approach React component tests. The essence is to look at components as simple functions that accept props and return a DOM. From that point on, choosing a testing library should be based on the usefulness of the tools it provides for handling component renders and DOM comparisons. UnexpectedJS happens to be very good at that in my experience.
What should be your next steps? Look at the GitHub project and give it a try if you haven’t already! Check all the tests in ProfileCard.test.js
and perhaps try to write a few of your own. You can also look at src/test-utils/unexpected-react.js
which is a simple helper function exporting features from the third-party testing libraries.
And lastly, here are a few additional resources I’d suggest checking out to dig even deeper into React component testing:
- UnexpectedJS – The official page and docs for UnexpectedJS. See the Plugins section as well.
- UnexpectedJS Gitter room – Perfect for when you need help or have a specific question for the maintainers.
- Testing Overview – You can test React components similar to testing other JavaScript code.
- React Testing Library – The recommended tooling for writing component tests in React.
- How Are Function Components Different from Classes – Dan Abramov describes the two programming models for creating React components.
The post React Component Tests for Humans appeared first on CSS-Tricks.
You can support CSS-Tricks by being an MVP Supporter.
from CSS-Tricks https://ift.tt/3pOQuzw
via IFTTT
No comments:
Post a Comment