Prerequisite

Create new interfaces and types that will be utilized throughout the hero and villain groups of components.

// src/models/Villain.ts
export interface Villain {
  id: string;
  name: string;
  description: string;
}
// src/models/types.ts
import { Hero } from "./Hero";
import { Villain } from "./Villain";

export type HeroProperty = Hero["name"] | Hero["description"] | Hero["id"];
export type VillainProperty =
  | Villain["name"]
  | Villain["description"]
  | Villain["id"];

export type EntityRoute = "heroes" | "villains";

export type EntityType = "hero" | "villain";

Update the hooks

We want to make our hooks more generic so that they can seamlessly be used in the villains group of components. In short, we will replace useCRUDhero hooks with useCRUDentity.

useDeleteEntity replaces useDeleteHero.

useEntityParams replaces useHeroParams.

useGetEntities replaces useGetHeroes.

usePostEntity replaces usePostHero.

usePutEntity replaces usePutHero.

Update the hero components

Having changed the hooks, the hero group of components need slight modifications.

Note about testing implementation details

There are no modifications needed to Cypress e2e, CT, or RTL tests with MSW because we did not test implementation details in any of them. They will all work as they are at the moment.

With cy.intercept and MSW we checked that a network request goes out versus checking that the operation caused a hook to be called. Consequently changing the hooks had no impact on the tests, or the functionality. This is why we want to test at a slightly higher level of abstraction, and why we want to verify the consequences of the implementation vs the implementation details themselves.

Modify the e2e commands to be more generic

The e2e tests for villains will look exactly the same, however our commands are a specific to heroes. They can also be more generic so that mirroring the hero group of tests into villains is easier.

In the commands file we will change most references to hero to entity, and for types we will include the villain varieties next to hero. The change will require small updates to type definitions and e2e tests.

Create a villains mirror of the heroes

Aligned with the theme of the book, we will first create villain related tests.

If your local copy of heroes versions of the test are slightly different, you can optionally tweak them.

Mirror the e2e tests

We create 3 new e2e tests, which are villain mirrors of the hero versions. We also enhance the remaining tests to check for villain related features.

The backend test needs a new block to cover villains.

The routes-nav needs a new test to cover villains route.

Mirror the Cypress component tests

Create a new fixture for villains at cypress/fixtures/villains.json.

Mirror the RTL tests

Enhance App.cy.tsx and App.test.tsx

Mirror the 3 hero components to villain components

We are creating 3 components for villains, mirroring heroes group as they are.

Add /villains route to App.tsx.

Last updated