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).