In order to make better sense out of the new features, we start by adding 3 components - ErrorComp, PageSpinner, Spinner - and a new search filter feature for HeroList. We will need the components for Suspense and ErrorBoundary use cases.
We want a new feature for HeroList which will search and filter the heroes by their names or descriptions, so that later we can have a use case for the new React 18 hooks useTransition and useDeferredValue. Let's add a new test for it to HeroList.cy.tsx. When we type a hero name or description to search, we should only get that hero in the list. We can also move the mount to a beforeEach test hook since it is the same to all tests in this file (Red 1).
When typing into the search field, we want to filter the heroes data to see if a name or description exists in the list. We already get the heroes data as a prop, which we can manage as state with useState:
Now we have to set that state with the filtering logic. Here are two functions that help us do that:
typeHeroProperty=Hero["name"] |Hero["description"] |Hero["id"];/** returns a boolean whether the hero properties exist in the search field */constsearchExists= (searchProperty:HeroProperty, searchField:string) =>String(searchProperty).toLowerCase().indexOf(searchField.toLowerCase()) !==-1;/** given the data and the search field, returns the data in which the search field exists */constsearchProperties= (searchField:string, data:Hero[]) => [...data].filter((item:Hero) =>Object.values(item).find((property:HeroProperty) =>searchExists(property, searchField) ) );/** filters the heroes data to see if the name or the description exists in the list */consthandleSearch= (data:Hero[]) => (event:ChangeEvent<HTMLInputElement>) => {constsearchField=event.target.value;returnsetFilteredHeroes(searchProperties(searchField, data)); };
Instead of rendering the list by heroes.map, we use filteredHeroes, which will get set by the handleSearch upon a change event.
// src/heroes/HeroList.tsximport { useNavigate } from"react-router-dom";import CardContent from"components/CardContent";import ButtonFooter from"components/ButtonFooter";import { FaEdit, FaRegSave } from"react-icons/fa";import { ChangeEvent, MouseEvent, useState } from"react";import { Hero } from"models/Hero";typeHeroListProps= { heroes:Hero[];handleDeleteHero: (hero:Hero) => (e:MouseEvent<HTMLButtonElement>) =>void;};exportdefaultfunctionHeroList({ heroes, handleDeleteHero }:HeroListProps) {const [filteredHeroes,setFilteredHeroes] =useState(heroes);constnavigate=useNavigate();// currying: the outer fn takes our custom arg and returns a fn that takes the eventconsthandleSelectHero= (heroId:string) => () => {consthero=heroes.find((h:Hero) =>h.id === heroId);navigate(`/heroes/edit-hero/${hero?.id}?name=${hero?.name}&description=${hero?.description}` ); };typeHeroProperty=Hero["name"] |Hero["description"] |Hero["id"];/** returns a boolean whether the hero properties exist in the search field */constsearchExists= (searchProperty:HeroProperty, searchField:string) =>String(searchProperty).toLowerCase().indexOf(searchField.toLowerCase()) !==-1;/** given the data and the search field, returns the data in which the search field exists */constsearchProperties= (searchField:string, data:Hero[]) => [...data].filter((item:Hero) =>Object.values(item).find((property:HeroProperty) =>searchExists(property, searchField) ) );/** filters the heroes data to see if the name or the description exists in the list */consthandleSearch= (data:Hero[]) => (event:ChangeEvent<HTMLInputElement>) => {constsearchField=event.target.value;returnsetFilteredHeroes(searchProperties(searchField, data)); };return ( <div> <divclassName="card-content"> <span>Search </span> <inputdata-cy="search"onChange={handleSearch(heroes)} /> </div> <uldata-cy="hero-list"className="list"> {filteredHeroes.map((hero, index) => ( <lidata-cy={`hero-list-item-${index}`} key={hero.id}> <divclassName="card"> <CardContentname={hero.name} description={hero.description} /> <footerclassName="card-footer"> <ButtonFooterlabel="Delete"IconClass={FaRegSave}onClick={handleDeleteHero(hero)} /> <ButtonFooterlabel="Edit"IconClass={FaEdit}onClick={handleSelectHero(hero.id)} /> </footer> </div> </li> ))} </ul> </div> );}
We added a feature to a component that get used in other components. When adding major features, it is important to execute the CT as well as e2e test suites entirely to ensure there are no regressions; yarn cy:run-ct, yarn cy:run-e2e. In theory, nothing should go wrong. There are no component errors. delete-hero e2e test however is not clearing the newly added hero upon delete; we have to refresh to render the updated hero list. Although they have a reputation of being "brittle", well-written, stable e2e tests have a high fault-finding capability, catching the defects that are not realized in a smaller focus.
To address the defect, we have to render the HeroList whenever heroes change. That is achieved with useEffect and the state we rely on - heroes - in the dependency array (Green 1).
// src/heroes/HeroList.tsximport { useNavigate } from"react-router-dom";import CardContent from"components/CardContent";import ButtonFooter from"components/ButtonFooter";import { FaEdit, FaRegSave } from"react-icons/fa";import { ChangeEvent, MouseEvent, startTransition, useEffect, useState,} from"react";import { Hero } from"models/Hero";typeHeroListProps= { heroes:Hero[];handleDeleteHero: (hero:Hero) => (e:MouseEvent<HTMLButtonElement>) =>void;};exportdefaultfunctionHeroList({ heroes, handleDeleteHero }:HeroListProps) {const [filteredHeroes,setFilteredHeroes] =useState(heroes);constnavigate=useNavigate();// needed to refresh the list after deleting a herouseEffect(() =>setFilteredHeroes(heroes), [heroes]);// currying: the outer fn takes our custom arg and returns a fn that takes the eventconsthandleSelectHero= (heroId:string) => () => {consthero=heroes.find((h:Hero) =>h.id === heroId);navigate(`/heroes/edit-hero/${hero?.id}?name=${hero?.name}&description=${hero?.description}` ); };typeHeroProperty=Hero["name"] |Hero["description"] |Hero["id"];/** returns a boolean whether the hero properties exist in the search field */constsearchExists= (searchProperty:HeroProperty, searchField:string) =>String(searchProperty).toLowerCase().indexOf(searchField.toLowerCase()) !==-1;/** given the data and the search field, returns the data in which the search field exists */constsearchProperties= (searchField:string, data:Hero[]) => [...data].filter((item:Hero) =>Object.values(item).find((property:HeroProperty) =>searchExists(property, searchField) ) );/** filters the heroes data to see if the name or the description exists in the list */consthandleSearch= (data:Hero[]) => (event:ChangeEvent<HTMLInputElement>) => {constsearchField=event.target.value;returnsetFilteredHeroes(searchProperties(searchField, data)); };return ( <div> <divclassName="card-content"> <span>Search </span> <inputdata-cy="search"onChange={handleSearch(heroes)} /> </div> <uldata-cy="hero-list"className="list"> {filteredHeroes.map((hero, index) => ( <lidata-cy={`hero-list-item-${index}`} key={hero.id}> <divclassName="card"> <CardContentname={hero.name} description={hero.description} /> <footerclassName="card-footer"> <ButtonFooterlabel="Delete"IconClass={FaRegSave}onClick={handleDeleteHero(hero)} /> <ButtonFooterlabel="Edit"IconClass={FaEdit}onClick={handleSelectHero(hero.id)} /> </footer> </div> </li> ))} </ul> </div> );}
Concurrency with useDeferredValue & useTransition
The concept of Concurrency is new in React 18. While multiple state updates are occurring simultaneously, Concurrency refers to certain state updates having less priority over others, for the purpose of optimizing UI responsiveness. useDeferredValue & useTransition hooks are new in React 18. They are not needed in our application, but we will show where they may fit if we were loading vast amounts of data on a slow connection.
useTransition() can be used to specify which state updates have a lower priority than all other state updates.
isPending is a boolean value, signifying if the low-priority state update is still pending.
startTransition is a function that we wrap around the low-priority state update.
In our HeroList component, setFilteredHeroes can be treated as a low priority state update. This would make the user experience so that the search filter input stays responsive while the list is still loading, in case the hero list is very large and the network is very slow.
The first change is in the return segment of handleSearch. startTransition wraps a function that returns setFilteredHeroes.
Here are the useTransition updates to the HeroList component.
// src/heroes/HeroList.tsximport { useNavigate } from"react-router-dom";import CardContent from"components/CardContent";import ButtonFooter from"components/ButtonFooter";import { FaEdit, FaRegSave } from"react-icons/fa";import { ChangeEvent, MouseEvent, useTransition, useEffect, useState,} from"react";import { Hero } from"models/Hero";typeHeroListProps= { heroes:Hero[];handleDeleteHero: (hero:Hero) => (e:MouseEvent<HTMLButtonElement>) =>void;};exportdefaultfunctionHeroList({ heroes, handleDeleteHero }:HeroListProps) {const [filteredHeroes,setFilteredHeroes] =useState(heroes);constnavigate=useNavigate();const [isPending,startTransition] =useTransition();// needed to refresh the list after deleting a herouseEffect(() =>setFilteredHeroes(heroes), [heroes]);// currying: the outer fn takes our custom arg and returns a fn that takes the eventconsthandleSelectHero= (heroId:string) => () => {consthero=heroes.find((h:Hero) =>h.id === heroId);navigate(`/heroes/edit-hero/${hero?.id}?name=${hero?.name}&description=${hero?.description}` ); };typeHeroProperty=Hero["name"] |Hero["description"] |Hero["id"];/** returns a boolean whether the hero properties exist in the search field */constsearchExists= (searchProperty:HeroProperty, searchField:string) =>String(searchProperty).toLowerCase().indexOf(searchField.toLowerCase()) !==-1;/** given the data and the search field, returns the data in which the search field exists */constsearchProperties= (searchField:string, data:Hero[]) => [...data].filter((item:Hero) =>Object.values(item).find((property:HeroProperty) =>searchExists(property, searchField) ) );/** filters the heroes data to see if the name or the description exists in the list */consthandleSearch= (data:Hero[]) => (event:ChangeEvent<HTMLInputElement>) => {constsearchField=event.target.value;returnstartTransition(() =>setFilteredHeroes(searchProperties(searchField, data)) ); };return ( <divstyle={{ opacity: isPending ?0.5:1, }} > <divclassName="card-content"> <span>Search </span> <inputdata-cy="search"onChange={handleSearch(heroes)} /> </div> <uldata-cy="hero-list"className="list"> {filteredHeroes.map((hero, index) => ( <lidata-cy={`hero-list-item-${index}`} key={hero.id}> <divclassName="card"> <CardContentname={hero.name} description={hero.description} /> <footerclassName="card-footer"> <ButtonFooterlabel="Delete"IconClass={FaRegSave}onClick={handleDeleteHero(hero)} /> <ButtonFooterlabel="Edit"IconClass={FaEdit}onClick={handleSelectHero(hero.id)} /> </footer> </div> </li> ))} </ul> </div> );}
With useTransition we have full control over the low priority code. Sometimes, we might not, for example if the data is coming in from the outside as a prop or if it is coming from external code. In such cases we can utilize useDeferredValue. In contrast to wrapping the state updating code with useTransition , with useDeferredValue we wrap the final value that got impacted. The end results of useTransition & useDeferredValue are the same; we tell React what the lower priority state updates are.
If you have access to the state updating code, prefer useTransition. If you do not have access to the code but only to the final value, utilize useDeferredValue.
In our HeroList component, the hero data is coming in as a prop, which is a good candidate for useDeferredValue.
Here is the updated HeroList component (Refactor 1):
// src/heroes/HeroList.tsximport { useNavigate } from"react-router-dom";import CardContent from"components/CardContent";import ButtonFooter from"components/ButtonFooter";import { FaEdit, FaRegSave } from"react-icons/fa";import { ChangeEvent, MouseEvent, useTransition, useEffect, useState, useDeferredValue,} from"react";import { Hero } from"models/Hero";typeHeroListProps= { heroes:Hero[];handleDeleteHero: (hero:Hero) => (e:MouseEvent<HTMLButtonElement>) =>void;};exportdefaultfunctionHeroList({ heroes, handleDeleteHero }:HeroListProps) {constdeferredHeroes=useDeferredValue(heroes);constisStale= deferredHeroes !== heroes;const [filteredHeroes,setFilteredHeroes] =useState(deferredHeroes);constnavigate=useNavigate();const [isPending,startTransition] =useTransition();// needed to refresh the list after deleting a herouseEffect(() =>setFilteredHeroes(deferredHeroes), [deferredHeroes]);// currying: the outer fn takes our custom arg and returns a fn that takes the eventconsthandleSelectHero= (heroId:string) => () => {consthero=deferredHeroes.find((h:Hero) =>h.id === heroId);navigate(`/heroes/edit-hero/${hero?.id}?name=${hero?.name}&description=${hero?.description}` ); };typeHeroProperty=Hero["name"] |Hero["description"] |Hero["id"];/** returns a boolean whether the hero properties exist in the search field */constsearchExists= (searchProperty:HeroProperty, searchField:string) =>String(searchProperty).toLowerCase().indexOf(searchField.toLowerCase()) !==-1;/** given the data and the search field, returns the data in which the search field exists */constsearchProperties= (searchField:string, data:Hero[]) => [...data].filter((item:Hero) =>Object.values(item).find((property:HeroProperty) =>searchExists(property, searchField) ) );/** filters the heroes data to see if the name or the description exists in the list */consthandleSearch= (data:Hero[]) => (event:ChangeEvent<HTMLInputElement>) => {constsearchField=event.target.value;returnstartTransition(() =>setFilteredHeroes(searchProperties(searchField, data)) ); };return ( <divstyle={{ opacity: isPending ?0.5:1, color: isStale ?"dimgray":"black", }} > <divclassName="card-content"> <span>Search </span> <inputdata-cy="search"onChange={handleSearch(deferredHeroes)} /> </div> <uldata-cy="hero-list"className="list"> {filteredHeroes.map((hero, index) => ( <lidata-cy={`hero-list-item-${index}`} key={hero.id}> <divclassName="card"> <CardContentname={hero.name} description={hero.description} /> <footerclassName="card-footer"> <ButtonFooterlabel="Delete"IconClass={FaRegSave}onClick={handleDeleteHero(hero)} /> <ButtonFooterlabel="Edit"IconClass={FaEdit}onClick={handleSelectHero(hero.id)} /> </footer> </div> </li> ))} </ul> </div> );}
The conditional rendering gives another clue; do we need a search bar when there is no data? Let's add that feature, starting with a failing test. We will rearrange HeroList.cy.tsx a bit so that we can capture the test in two contexts; mount without hero data, and mount with hero data (Red 2).
// src/heroes/HeroList.cy.tsximport { BrowserRouter } from"react-router-dom";import HeroList from"./HeroList";import"../styles.scss";import heroes from"../../cypress/fixtures/heroes.json";describe("HeroList", () => {it("no heroes should not display a list nor search bar", () => {cy.mount( <BrowserRouter> <HeroListheroes={[]}handleDeleteHero={cy.stub().as("handleDeleteHero")} /> </BrowserRouter> );cy.getByCy("hero-list").should("exist");cy.getByCyLike("hero-list-item").should("not.exist");cy.getByCy("search").should("not.exist"); });context("with heroes in the list", () => {beforeEach(() => {cy.mount( <BrowserRouter> <HeroListheroes={heroes}handleDeleteHero={cy.stub().as("handleDeleteHero")} /> </BrowserRouter> ); });it("should render the hero layout", () => {cy.getByCyLike("hero-list-item").should("have.length",heroes.length);cy.getByCy("card-content");cy.contains(heroes[0].name);cy.contains(heroes[0].description);cy.get("footer").first().within(() => {cy.getByCy("delete-button");cy.getByCy("edit-button"); }); });it("should search and filter hero by name and description", () => {cy.getByCy("search").type(heroes[0].name);cy.getByCyLike("hero-list-item").should("have.length",1).contains(heroes[0].name);cy.getByCy("search").clear().type(heroes[2].description);cy.getByCyLike("hero-list-item").should("have.length",1).contains(heroes[2].description); });it("should handle delete", () => {cy.getByCy("delete-button").first().click();cy.get("@handleDeleteHero").should("have.been.called"); });it("should handle edit", () => {cy.getByCy("edit-button").first().click();cy.location("pathname").should("eq","/heroes/edit-hero/"+ heroes[0].id); }); });});
To satisfy the test, all we need is conditional rendering for the search bar.
Here is the HeroList component in its final form (Green 2):
// src/heroes/HeroList.tsximport { useNavigate } from"react-router-dom";import CardContent from"components/CardContent";import ButtonFooter from"components/ButtonFooter";import { FaEdit, FaRegSave } from"react-icons/fa";import { ChangeEvent, MouseEvent, useTransition, useEffect, useState, useDeferredValue,} from"react";import { Hero } from"models/Hero";typeHeroListProps= { heroes:Hero[];handleDeleteHero: (hero:Hero) => (e:MouseEvent<HTMLButtonElement>) =>void;};exportdefaultfunctionHeroList({ heroes, handleDeleteHero }:HeroListProps) {constdeferredHeroes=useDeferredValue(heroes);constisStale= deferredHeroes !== heroes;const [filteredHeroes,setFilteredHeroes] =useState(deferredHeroes);constnavigate=useNavigate();const [isPending,startTransition] =useTransition();// needed to refresh the list after deleting a herouseEffect(() =>setFilteredHeroes(deferredHeroes), [deferredHeroes]);// currying: the outer fn takes our custom arg and returns a fn that takes the eventconsthandleSelectHero= (heroId:string) => () => {consthero=deferredHeroes.find((h:Hero) =>h.id === heroId);navigate(`/heroes/edit-hero/${hero?.id}?name=${hero?.name}&description=${hero?.description}` ); };typeHeroProperty=Hero["name"] |Hero["description"] |Hero["id"];/** returns a boolean whether the hero properties exist in the search field */constsearchExists= (searchProperty:HeroProperty, searchField:string) =>String(searchProperty).toLowerCase().indexOf(searchField.toLowerCase()) !==-1;/** given the data and the search field, returns the data in which the search field exists */constsearchProperties= (searchField:string, data:Hero[]) => [...data].filter((item:Hero) =>Object.values(item).find((property:HeroProperty) =>searchExists(property, searchField) ) );/** filters the heroes data to see if the any of the properties exist in the list */consthandleSearch= (data:Hero[]) => (event:ChangeEvent<HTMLInputElement>) => {constsearchField=event.target.value;returnstartTransition(() =>setFilteredHeroes(searchProperties(searchField, data)) ); };return ( <divstyle={{ opacity: isPending ?0.5:1, color: isStale ?"dimgray":"black", }} > {deferredHeroes.length>0&& ( <divclassName="card-content"> <span>Search </span> <inputdata-cy="search"onChange={handleSearch(deferredHeroes)} /> </div> )} <uldata-cy="hero-list"className="list"> {filteredHeroes.map((hero, index) => ( <lidata-cy={`hero-list-item-${index}`} key={hero.id}> <divclassName="card"> <CardContentname={hero.name} description={hero.description} /> <footerclassName="card-footer"> <ButtonFooterlabel="Delete"IconClass={FaRegSave}onClick={handleDeleteHero(hero)} /> <ButtonFooterlabel="Edit"IconClass={FaEdit}onClick={handleSelectHero(hero.id)} /> </footer> </div> </li> ))} </ul> </div> );}
Suspense & ErrorBoundary
The setup
To manage the amount of code loaded upon application startup - the initial bundle - we can use code splitting which aims to load the app's code in chunks in order to improve UI responsiveness. In React, Suspense and lazy loading are used to accomplish code splitting. They are usually talked about together with ErrorBoundary, because Suspense and ErrorBoundary components let us decouple the loading and error UI from individual components. Here are the key ideas:
While loading show the Suspense component, if error show the ErrorBoundary component, if success show the component we want to render.
Use React.lazy to load components only when they are first rendered.
Converting a component to a lazy component with the lazy function:
Use the Suspense and ErrorBoundary components to wrap UI that contains one or more lazy components in its tree. Here is how they work together at a high level:
Now we can begin writing failing tests for error edge cases. Where do we start? Any component test that is covering the positive cases, spying on or stubbing the network with cy.intercept() is a good candidate . Those are HeroDetail and Heroes component tests.
Add a test to HeroList component test for a non-200 scenario. We use a delay option to be able to see the spinner (Red 3).
Looking at the HeroDetail component, we have status from usePostHero and isUpdating from usePutHero hooks, which we can utilize. In case these are seen, render the PageSpinner (Green 2).
Running the component test, we can verify the spinner by hovering over the Cypress time travel debug. We also realize the stubbed POST request going out, which we can verify with a cy.wait() (Refactor 2). Being able to see all the transitions of a component via Cypress time travel debugger can hep us improve our tests
We do not have any way to check the update scenario in the component test, because we are not able to setup such state that would trigger a back-end modification. Any time we are not able to cover a test at a low level with component tests, move up to ui-integration tests. Most the time a ui-integration test will suffice, and when it is not enough we can use a true e2e that hits the backend. In our case ui-integration is preferred because it would be hard to have the backend respond with a 500 response. We also do not need a response from a real network to render the error . Therefore we can add a ui-integration test to edit-hero.cy.ts e2e test that covers the update scenario. We see the boundaries between test types begin to get thinner; we use the least costly kind of test to gain the highest confidence. Where they are in the pyramid is only relevant by the ability to perform that kind of test in the given context (Refactor 4).
The new test is similar to other ui-integration tests; we stub the network and visit the main route. We go to the edit page for any random hero. We setup the network stub that will happen on update via cy.intercept. Finally we repeat a similar spinner -> wait on network -> error flow from the component test. The only distinction here is PUT vs POST.
it("should go through the PUT error flow (ui-integration)", () => {// Arrange: beginning statecy.visitStubbedHeroes();// verify that we are editing data already populatedcy.fixture("heroes").then((heroes) => {constheroIndex=randomHeroIndex(heroes);cy.getByCy("edit-button").eq(heroIndex).click();verifyHero(heroes, heroIndex); });// setup network stubcy.intercept("PUT",`${Cypress.env("API_URL")}/heroes/*`, { statusCode:500, delay:100, }).as("isUpdateError");// Actcy.getByCy("save-button").click();// Assertcy.getByCy("spinner");cy.wait("@isUpdateError");cy.getByCy("error");});
Here is the side by side with the component test for comparison. The Act and Assert are the same, the network stub is POST vs PUT with less of a need to specify the url. Predominantly setting up the Arrange is different.
By default, cy.intecept accepts the status code as 200. What happens when the status code is not 200?
Heroes component is using HeroDetail. Before we begin, we will slightly refactor the test to have 2 contexts; one for 200 flows, and the new test for the non-200 flow. The non-200 test we will create will come before the 200 flows.
// src/heroes/Heroes.cy.tsximport Heroes from"./Heroes";import { BrowserRouter } from"react-router-dom";import { QueryClient, QueryClientProvider } from"react-query";import"../styles.scss";describe("Heroes", () => {constmounter= (queryClient:QueryClient) =>cy.mount( <QueryClientProviderclient={queryClient}> <BrowserRouter> <Heroes /> </BrowserRouter> </QueryClientProvider> );context("200 flows", () => {beforeEach(() => {cy.intercept("GET",`${Cypress.env("API_URL")}/heroes`, { fixture:"heroes.json", }).as("getHeroes");mounter(newQueryClient()); });it("should display the hero list on render, and go through hero add & refresh flow", () => {cy.wait("@getHeroes");cy.getByCy("list-header").should("be.visible");cy.getByCy("hero-list").should("be.visible");cy.getByCy("add-button").click();cy.location("pathname").should("eq","/heroes/add-hero");cy.getByCy("refresh-button").click();cy.location("pathname").should("eq","/heroes"); });constinvokeHeroDelete= () => {cy.getByCy("delete-button").first().click();cy.getByCy("modal-yes-no").should("be.visible"); };it("should go through the modal flow", () => {cy.getByCy("modal-yes-no").should("not.exist");cy.log("do not delete flow");invokeHeroDelete();cy.getByCy("button-no").click();cy.getByCy("modal-yes-no").should("not.exist");cy.log("delete flow");invokeHeroDelete();cy.intercept("DELETE","*", { statusCode:200 }).as("deleteHero");cy.getByCy("button-yes").click();cy.wait("@deleteHero");cy.getByCy("modal-yes-no").should("not.exist"); }); });});
Let's start with the new test. We setup the network setup, mount the component, and expect to see an error.
// src/heroes/Heroes.cy.tsximport Heroes from"./Heroes";import { BrowserRouter } from"react-router-dom";import { QueryClient, QueryClientProvider } from"react-query";import"../styles.scss";describe("Heroes", () => {constmounter= (queryClient:QueryClient) =>cy.mount( <QueryClientProviderclient={queryClient}> <BrowserRouter> <Heroes /> </BrowserRouter> </QueryClientProvider> );it.only("should go through the error flow", () => {cy.intercept("GET",`${Cypress.env("API_URL")}/heroes`, { statusCode:400, delay:100, }).as("notFound");mounter(newQueryClient());cy.getByCy("error"); });context("200 flows", () => {beforeEach(() => {cy.intercept("GET",`${Cypress.env("API_URL")}/heroes`, { fixture:"heroes.json", }).as("getHeroes");mounter(newQueryClient()); });it("should display the hero list on render, and go through hero add & refresh flow", () => {cy.wait("@getHeroes");cy.getByCy("list-header").should("be.visible");cy.getByCy("hero-list").should("be.visible");cy.getByCy("add-button").click();cy.location("pathname").should("eq","/heroes/add-hero");cy.getByCy("refresh-button").click();cy.location("pathname").should("eq","/heroes"); });constinvokeHeroDelete= () => {cy.getByCy("delete-button").first().click();cy.getByCy("modal-yes-no").should("be.visible"); };it("should go through the modal flow", () => {cy.getByCy("modal-yes-no").should("not.exist");cy.log("do not delete flow");invokeHeroDelete();cy.getByCy("button-no").click();cy.getByCy("modal-yes-no").should("not.exist");cy.log("delete flow");invokeHeroDelete();cy.intercept("DELETE","*", { statusCode:200 }).as("deleteHero");cy.getByCy("button-yes").click();cy.wait("@deleteHero");cy.getByCy("modal-yes-no").should("not.exist"); }); });});
Running the component test, we see that nothing renders, Axios retrying multiple times and throws an error (Red 5).
We can mirror the improvement that was done to HeroDetail in Heroes.
Still those Axios retries are taking long and nothing renders. We can speed up the network errors by using cy.clock and cy.tick. We also tell Cypress that uncaught exceptions are expected using Cypress.on('uncaught:exception', () => false).
// src/heroes/Heroes.cy.tsxit.only("should go through the error flow", () => {Cypress.on("uncaught:exception", () =>false);cy.clock();cy.intercept("GET",`${Cypress.env("API_URL")}/heroes`, { statusCode:400, delay:100, }).as("notFound");mounter(newQueryClient());Cypress._.times(3, () => {cy.tick(5000);cy.wait("@notFound"); });cy.tick(5000);cy.getByCy("error");});
After that change, the only remaining failure is data-cy not rendering
We need to remember that a component test is an independent, small scale app. Our application is being wrapped at the base level, and with that ErrorBoundary and Suspense are able to apply to every component under the App. Therefore we also need to wrap our mounted component (Green 5).
It is optimal to make that a Cypress command which we can use in any component without having to import. We could replace most cy.mounts in the component test suite, with the exception of App.cy.tsx . Even when the custom mount is not needed, the additional wrappers will not hurt. Change ./cypress/support/component.ts to a tsx file. We align the command better with cy.mount in command version of the wrappedMount.
/** Mounts the component wrapped by all the providers:* QueryClientProvider, ErrorBoundary, Suspense, BrowserRouter* @param component React Node to mount* @param options Additional options to pass into mount */wrappedMount( component: React.ReactNode, options?: MountOptions,): Cypress.Chainable<MountReturn>
Here is the final version of the test with cy.wrappedMount. We included a check for the spinner before the error as a bonus (Refactor 5). You can optionally apply cy.wrappedMount refactor to a few of the component tests:
src/components/Heroes.cy.tsx
src/components/HeroList.cy.tsx
src/components/HeroDetail.cy.tsx
Not as useful but still possible (they only use BrowserRouter as the wrapper):
src/components/HeaderBar.cy.tsx
src/components/HeaderBarBrand.cy.tsx
src/components/ListHeader.cy.tsx
src/components/NavBar.cy.tsx
// src/heroes/Heroes.cy.tsx
import Heroes from "./Heroes";
import "../styles.scss";
describe("Heroes", () => {
it("should see error on initial load with GET", () => {
Cypress.on("uncaught:exception", () => false);
cy.clock();
cy.intercept("GET", `${Cypress.env("API_URL")}/heroes`, {
statusCode: 400,
delay: 100,
}).as("notFound");
cy.wrappedMount(<Heroes />);
cy.getByCy("page-spinner").should("be.visible");
Cypress._.times(3, () => {
cy.tick(5000);
cy.wait("@notFound");
});
cy.tick(5000);
cy.getByCy("error");
});
context("200 flows", () => {
beforeEach(() => {
cy.intercept("GET", `${Cypress.env("API_URL")}/heroes`, {
fixture: "heroes.json",
}).as("getHeroes");
cy.wrappedMount(<Heroes />);
});
it("should display the hero list on render, and go through hero add & refresh flow", () => {
cy.wait("@getHeroes");
cy.getByCy("list-header").should("be.visible");
cy.getByCy("hero-list").should("be.visible");
cy.getByCy("add-button").click();
cy.location("pathname").should("eq", "/heroes/add-hero");
cy.getByCy("refresh-button").click();
cy.location("pathname").should("eq", "/heroes");
});
const invokeHeroDelete = () => {
cy.getByCy("delete-button").first().click();
cy.getByCy("modal-yes-no").should("be.visible");
};
it("should go through the modal flow, and cover error on DELETE", () => {
cy.getByCy("modal-yes-no").should("not.exist");
cy.log("do not delete flow");
invokeHeroDelete();
cy.getByCy("button-no").click();
cy.getByCy("modal-yes-no").should("not.exist");
cy.log("delete flow");
invokeHeroDelete();
cy.intercept("DELETE", "*", { statusCode: 500 }).as("deleteHero");
cy.getByCy("button-yes").click();
cy.wait("@deleteHero");
cy.getByCy("modal-yes-no").should("not.exist");
cy.getByCy("error").should("be.visible");
});
});
});
// src/heroes/HeroList.cy.tsx
import HeroList from "./HeroList";
import "../styles.scss";
import heroes from "../../cypress/fixtures/heroes.json";
describe("HeroList", () => {
it("no heroes should not display a list nor search bar", () => {
cy.wrappedMount(
<HeroList
heroes={[]}
handleDeleteHero={cy.stub().as("handleDeleteHero")}
/>
);
cy.getByCy("hero-list").should("exist");
cy.getByCyLike("hero-list-item").should("not.exist");
cy.getByCy("search").should("not.exist");
});
context("with heroes in the list", () => {
beforeEach(() => {
cy.wrappedMount(
<HeroList
heroes={heroes}
handleDeleteHero={cy.stub().as("handleDeleteHero")}
/>
);
});
it("should render the hero layout", () => {
cy.getByCyLike("hero-list-item").should("have.length", heroes.length);
cy.getByCy("card-content");
cy.contains(heroes[0].name);
cy.contains(heroes[0].description);
cy.get("footer")
.first()
.within(() => {
cy.getByCy("delete-button");
cy.getByCy("edit-button");
});
});
it("should search and filter hero by name and description", () => {
cy.getByCy("search").type(heroes[0].name);
cy.getByCyLike("hero-list-item")
.should("have.length", 1)
.contains(heroes[0].name);
cy.getByCy("search").clear().type(heroes[2].description);
cy.getByCyLike("hero-list-item")
.should("have.length", 1)
.contains(heroes[2].description);
});
it("should handle delete", () => {
cy.getByCy("delete-button").first().click();
cy.get("@handleDeleteHero").should("have.been.called");
});
it("should handle edit", () => {
cy.getByCy("edit-button").first().click();
cy.location("pathname").should("eq", "/heroes/edit-hero/" + heroes[0].id);
});
});
});
In RTL, being able to handle the spinner in the beginning is a bit different. We have to use an act to asynchronously wait. Here is the updated unit test:
// src/App.test.tsx
import { act, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import App from "./App";
import { heroes } from "../db.json";
import { rest } from "msw";
import { setupServer } from "msw/node";
describe("200 flow", () => {
const handlers = [
rest.get(
`${process.env.REACT_APP_API_URL}/heroes`,
async (_req, res, ctx) => res(ctx.status(200), ctx.json(heroes))
),
];
const server = setupServer(...handlers);
beforeAll(() => {
server.listen({
onUnhandledRequest: "warn",
});
});
afterEach(server.resetHandlers);
afterAll(server.close);
test("renders tour of heroes", async () => {
render(<App />);
await act(() => new Promise((r) => setTimeout(r, 0))); // spinner
await userEvent.click(screen.getByText("About"));
expect(screen.getByTestId("about")).toBeVisible();
await userEvent.click(screen.getByText("Heroes"));
expect(screen.getByTestId("heroes")).toBeVisible();
});
});
To mirror cy.wrappedMount in RTL, create a custom render at src/test-utils.tsx. This file also exports '@testing-library/react', so we can import screen, userEvent, waitFor from here in case we are using the wrappedRender. Similar to the component tests, wrappedRender is the most useful in 3 components under heroes folder.
// src/test-utils.tsx
import React, { FC, Suspense } from "react";
import { render, RenderOptions } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";
import { QueryClient, QueryClientProvider } from "react-query";
import { ErrorBoundary } from "react-error-boundary";
import ErrorComp from "components/ErrorComp";
import PageSpinner from "components/PageSpinner";
const AllTheProviders: FC<{ children: React.ReactNode }> = ({ children }) => {
return (
<QueryClientProvider client={new QueryClient()}>
<ErrorBoundary fallback={<ErrorComp />}>
<Suspense fallback={<PageSpinner />}>
<BrowserRouter>{children}</BrowserRouter>
</Suspense>
</ErrorBoundary>
</QueryClientProvider>
);
};
/** Renders the component wrapped by all the providers:
* QueryClientProvider, ErrorBoundary, Suspense, BrowserRouter.
*/
const wrappedRender = (
ui: React.ReactNode,
options?: Omit<RenderOptions, "wrapper">
// @ts-expect-error - ok to ignore
) => render(ui, { wrapper: AllTheProviders, ...options });
export * from "@testing-library/react";
export { wrappedRender };
HeroList.test.tsx is the RTL mirror of HeroList.cy.tsx
With msw - think of cy.intercept for RTL use - it is not recommended to verify XHR calls going out of the app. Instead, the advice is the verify the changes in the UI. Alas, sometimes there are no changes in the component itself therefore we cannot mirror every single Cypress component testing 1:1 with RTL. Here is the RTL mirror of HeroDetail.cy.tsx.
Alternatively we could spy on the react-query hooks and verify they are called. While that is what most developers have been used to, it is an implementation detail because changes to our state management approach would break the tests.
// src/heroes/HeroDetail.test.tsx
import HeroDetail from "./HeroDetail";
import { wrappedRender, act, screen, waitFor } from "test-utils";
import userEvent from "@testing-library/user-event";
describe("HeroDetail", () => {
beforeEach(() => {
wrappedRender(<HeroDetail />);
});
// should handle Save and should handle non-200 Save have no RTL mirrors
// because of difference between msw and cy.intercept
it("should handle Cancel", async () => {
// code that causes React state updates (ex: BrowserRouter)
// should be wrapped into act(...):
// userEvent.click(await screen.findByTestId('cancel-button')) // won't work
act(() => screen.getByTestId("cancel-button").click());
expect(window.location.pathname).toBe("/heroes");
});
it("should handle name change", async () => {
const newHeroName = "abc";
const inputDetailName = await screen.findByPlaceholderText("e.g. Colleen");
userEvent.type(inputDetailName, newHeroName);
await waitFor(async () =>
expect(inputDetailName).toHaveDisplayValue(newHeroName)
);
});
const inputDetailDescription = async () =>
screen.findByPlaceholderText("e.g. dance fight!");
it("should handle description change", async () => {
const newHeroDescription = "123";
userEvent.type(await inputDetailDescription(), newHeroDescription);
await waitFor(async () =>
expect(await inputDetailDescription()).toHaveDisplayValue(
newHeroDescription
)
);
});
it("id: false, name: false - should verify the minimal state of the component", async () => {
expect(await screen.findByTestId("input-detail-name")).toBeVisible();
expect(await screen.findByTestId("input-detail-description")).toBeVisible();
expect(screen.queryByTestId("input-detail-id")).not.toBeInTheDocument();
expect(await inputDetailDescription()).toBeVisible();
expect(await screen.findByTestId("save-button")).toBeVisible();
expect(await screen.findByTestId("cancel-button")).toBeVisible();
});
});
Heroes.test.tsx is the RTL mirror of Heroes.cy.tsx.
// src/heroes/Heroes.test.tsx
import Heroes from "./Heroes";
import { wrappedRender, screen, waitForElementToBeRemoved } from "test-utils";
import userEvent from "@testing-library/user-event";
import { rest } from "msw";
import { setupServer } from "msw/node";
import { heroes } from "../../db.json";
describe("Heroes", () => {
// mute the expected console.error message, because we are mocking non-200 responses
// eslint-disable-next-line @typescript-eslint/no-empty-function
jest.spyOn(console, "error").mockImplementation(() => {});
beforeEach(() => wrappedRender(<Heroes />));
it("should see error on initial load with GET", async () => {
const handlers = [
rest.get(
`${process.env.REACT_APP_API_URL}/heroes`,
async (_req, res, ctx) => res(ctx.status(500))
),
];
const server = setupServer(...handlers);
server.listen({
onUnhandledRequest: "warn",
});
jest.useFakeTimers();
expect(await screen.findByTestId("page-spinner")).toBeVisible();
jest.advanceTimersByTime(25000);
await waitForElementToBeRemoved(
() => screen.queryByTestId("page-spinner"),
{
timeout: 25000,
}
);
expect(await screen.findByTestId("error")).toBeVisible();
jest.useRealTimers();
server.resetHandlers();
server.close();
});
describe("200 flows", () => {
const handlers = [
rest.get(
`${process.env.REACT_APP_API_URL}/heroes`,
async (_req, res, ctx) => res(ctx.status(200), ctx.json(heroes))
),
rest.delete(
`${process.env.REACT_APP_API_URL}/heroes/${heroes[0].id}`, // use /.*/ for all requests
async (_req, res, ctx) =>
res(ctx.status(400), ctx.json("expected error"))
),
];
const server = setupServer(...handlers);
beforeAll(() => {
server.listen({
onUnhandledRequest: "warn",
});
});
afterEach(server.resetHandlers);
afterAll(server.close);
it("should display the hero list on render, and go through hero add & refresh flow", async () => {
expect(await screen.findByTestId("list-header")).toBeVisible();
expect(await screen.findByTestId("hero-list")).toBeVisible();
await userEvent.click(await screen.findByTestId("add-button"));
expect(window.location.pathname).toBe("/heroes/add-hero");
await userEvent.click(await screen.findByTestId("refresh-button"));
expect(window.location.pathname).toBe("/heroes");
});
const deleteButtons = async () => screen.findAllByTestId("delete-button");
const modalYesNo = async () => screen.findByTestId("modal-yes-no");
const maybeModalYesNo = () => screen.queryByTestId("modal-yes-no");
const invokeHeroDelete = async () => {
userEvent.click((await deleteButtons())[0]);
expect(await modalYesNo()).toBeVisible();
};
it("should go through the modal flow, and cover error on DELETE", async () => {
expect(screen.queryByTestId("modal-dialog")).not.toBeInTheDocument();
await invokeHeroDelete();
await userEvent.click(await screen.findByTestId("button-no"));
expect(maybeModalYesNo()).not.toBeInTheDocument();
await invokeHeroDelete();
await userEvent.click(await screen.findByTestId("button-yes"));
expect(maybeModalYesNo()).not.toBeInTheDocument();
expect(await screen.findByTestId("error")).toBeVisible();
expect(screen.queryByTestId("modal-dialog")).not.toBeInTheDocument();
});
});
});
Summary
We added new components to be used in the chapter.
We added a new test for a hero search / filter feature (Red 1).
We added the implementation to HeroList component, and ensured that there are no CT or e2e regressions (Green 1).
We enhanced the component with useTransition to wrap the code we have control over (setFilteredHeroes) and useDeferredValue to wrap the value we do not have control over (heroes value being passed in as a prop) (Refactor 1).
We added conditional rendering for the search-filter (Red 2, Green 2)
We configured the application for Suspense and ErrorBoundary
We wrote a non-200 / network error edge case for HeroDetail component which also hits the Suspense code using a cy.intercept delay option (Red 3, Red 4).
We added conditional rendering to HeroDetail for loading and error conditions that may occur with a POST request (Green 3, Green 4)
In order to cover PUT request loading and error condition, we utilized a ui-integration test, since it is aware of a state that can trigger a back-end modification, but doesn't necessarily have to receive a 500 response from a real network to render the error (Refactor 4)
We wrote a non-200 / network error edge case for Heroes component which is HeroDetail's parent.' It uses GET request to get the data (Red 5).
We wrapped the component test mount in the fashion the root app is wrapped by ErrorBoundary & Suspense. We took advantage of cy.clock , cy.tick and turned off test failure on expected error throws (Green 5).
We improved the component test to check for the spinner. Similar to the POST request error case, we covered the network error case for DELETE in a ui-integration test since it is aware of a state that can trigger a back-end modification, but doesn't necessarily have to receive a 500 response from a real network to render the error (Refactor 5).
We modified the RTL unit test to work with Suspense
Takeaways
When adding major features, it is important to execute the CT as well as e2e test suites to ensure there are no regressions. Small incremental steps coupled by confident tests make error diagnosis easier.
Although they have a reputation of being "brittle", well-written, stable e2e or ui-integration tests have a high fault-finding capability, catching the defects that are not realized in an isolated component, or unit test.
While multiple state updates are occurring simultaneously, Concurrency refers to certain state updates having less priority over others, for the purpose of optimizing UI responsiveness. useTransition & useDeferredValue can be used to specify what is of lower priority. If you have access to the state updating code, prefer useTransition and wrap that code. If you do not have access to the code but only to the final value, utilize useDeferredValue to wrap that value.
Suspense with lazy loading is used to code-split the initial application bundle, in order to improve UI responsiveness. Together with ErrorBoundary , they de-couple the loading and error UI from individual components. While loading show the Suspense component, if error show the ErrorBoundary component, if success show the component we want to render.
When beginning to write tests for error cases, any test that is covering the positive flows to spy on or stub the network with cy.intercept() is a good candidate to begin with. Start at the component level and move up to ui-integration when further tests are not possible.
Any time we are not able to cover a test at a low level with component tests, move up to ui-integration tests. Most the time a ui-integration test will suffice, and when it is not enough we can use a true e2e that hits the backend. Use the least costly kind of test to gain the highest confidence; where they are in the pyramid is only relevant by the ability to perform that kind of test in the given context.
We can speed up the network errors by using cy.clock and cy.tick. When covering error case we also tell Cypress that uncaught exceptions are expected using Cypress.on('uncaught:exception', () => false).
Remember that a component test is an independent, small scale application. Whatever is wrapping the base App component may need to be wrapping a component test mount as well (Providers, ErrorBoundary, Suspense, Router etc.).