CLOVER🍀

That was when it all began.

Learn ReactをVitest、React Testing Libraryを使ってやってみる(State: A Component's Memory)

これは、なにをしたくて書いたもの?

こちらの「Learn React」をやってみるシリーズの続きです。

Learn ReactをVitest、React Testing Library、jest-domを使ってやってみる(Describing the UI) - CLOVER🍀

Learn ReactをVitest、React Testing Library、jest-domを使ってやってみる(Keeping Components Pure) - CLOVER🍀

Learn ReactをVitest、React Testing Libraryを使ってやってみる(Understanding Your UI as a Tree) - CLOVER🍀

Learn ReactをVitest、React Testing Libraryを使ってやってみる(Responding to Events) - CLOVER🍀

今回は、「Learn React」の「Adding Interactivity」から「State: A Component's Memory」をやってみます。

State: A Component's Memory – React

基本的には写経のシリーズです。

Learn Reactをやってみる

Learn Reactをただ写経するのではなく、以下の条件で進めていきます。再掲です。

  • 環境はViteで構築する
  • TypeScriptで書く
  • Vitest、React Testing Library、jest-domを使ったテストを書く

今回の対象はこちらのページです。

State: A Component's Memory – React

環境

今回の環境はこちら。

$ node --version
v22.15.0


$ npm --version
10.9.2

準備

Viteを使って、Node.jsプロジェクトを作成します。

$ npm create vite@latest state-a-components-memory -- --template react-ts
$ cd state-a-components-memory

依存ライブラリー、テスト用のライブラリーをインストール。

$ npm i
$ npm i -D vitest jsdom @testing-library/react @testing-library/dom @testing-library/user-event @testing-library/jest-dom @types/react @types/react-dom
$ npm i -D prettier

インストールしたライブラリーの一覧とバージョンはこちら。

$ npm ls
state-a-components-memory@0.0.0 /path/to/state-a-components-memory
├── @eslint/js@9.25.1
├── @testing-library/dom@10.4.0
├── @testing-library/jest-dom@6.6.3
├── @testing-library/react@16.3.0
├── @testing-library/user-event@14.6.1
├── @types/react-dom@19.1.2
├── @types/react@19.1.2
├── @vitejs/plugin-react@4.4.1
├── eslint-plugin-react-hooks@5.2.0
├── eslint-plugin-react-refresh@0.4.20
├── eslint@9.25.1
├── globals@16.0.0
├── jsdom@26.1.0
├── prettier@3.5.3
├── react-dom@19.1.0
├── react@19.1.0
├── typescript-eslint@8.31.1
├── typescript@5.7.3
├── vite@6.3.3
└── vitest@3.1.2

自動生成された不要なファイルは削除しておきます。

$ find src -type f | grep -vE 'main.tsx|vite-env.d.ts' | xargs rm -rf
$ rmdir src/assets

Viteで作成したプロジェクトから、追加・変更した設定ファイルを載せておきます。

tsconfig.json

{
  "files": [],
  "references": [
    { "path": "./tsconfig.app.json" },
    { "path": "./tsconfig.node.json" },
    { "path": "./tsconfig.vitest.json" }
  ]
}

tsconfig.vitest.json

{
  "extends": "./tsconfig.app.json",
  "compilerOptions": {
    "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.vitest.tsbuildinfo",
    "types": ["@testing-library/jest-dom"]
  },
  "include": [
    "src",
    "test",
    "vitest.config.ts",
    "vitest.setup.ts"
  ]
}

vitest.config.ts

import { defineConfig } from 'vite'

export default defineConfig({
  test: {
    environment: 'jsdom',
    setupFiles: ['./vitest.setup.ts'],
  },
})

vitest.setup.ts

import '@testing-library/jest-dom/vitest';

.prettierrc.json

{
  "singleQuote": true,
  "printWidth": 100
}

package.jsonscriptsの内容。

  "scripts": {
    "dev": "vite",
    "build": "tsc -b && vite build",
    "lint": "eslint .",
    "preview": "vite preview",
    "typecheck": "tsc -b",
    "typecheck:watch": "tsc -b --w",
    "format": "prettier --write src/**/*.{ts,tsx,css} test/**/*.{ts,tsx}",
    "test": "vitest run",
    "test:update": "vitest run -u",
    "test:watch": "vitest watch"
  },

Learn ReactのAdding InteractivityのState: A Component's Memoryをやってみる

Testing Libraryのuser-eventも使いますが、セットアップ部分は共通化しておきます。

test/setup.ts

import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ReactNode } from 'react';

export function setup(jsx: ReactNode) {
  return {
    user: userEvent.setup(),
    ...render(jsx),
  };
}
When a regular variable isn’t enough

こちらは悪い例ですが、せっかくなので写経してテストも書いておきます。

State: A Component's Memory / When a regular variable isn’t enough

src/data.ts

type Sculpture = {
  name: string;
  artist: string;
  description: string;
  url: string;
  alt: string;
};

export const sculptures: Sculpture[] = [
  {
    name: 'Homenaje a la Neurocirugía',
    artist: 'Marta Colvin Andrade',
    description:
      'Although Colvin is predominantly known for abstract themes that allude to pre-Hispanic symbols, this gigantic sculpture, an homage to neurosurgery, is one of her most recognizable public art pieces.',
    url: 'https://i.imgur.com/Mx7dA2Y.jpg',
    alt: 'A bronze statue of two crossed hands delicately holding a human brain in their fingertips.',
  },
  {
    name: 'Floralis Genérica',
    artist: 'Eduardo Catalano',
    description:
      'This enormous (75 ft. or 23m) silver flower is located in Buenos Aires. It is designed to move, closing its petals in the evening or when strong winds blow and opening them in the morning.',
    url: 'https://i.imgur.com/ZF6s192m.jpg',
    alt: 'A gigantic metallic flower sculpture with reflective mirror-like petals and strong stamens.',
  },
  {
    name: 'Eternal Presence',
    artist: 'John Woodrow Wilson',
    description:
      'Wilson was known for his preoccupation with equality, social justice, as well as the essential and spiritual qualities of humankind. This massive (7ft. or 2,13m) bronze represents what he described as "a symbolic Black presence infused with a sense of universal humanity."',
    url: 'https://i.imgur.com/aTtVpES.jpg',
    alt: 'The sculpture depicting a human head seems ever-present and solemn. It radiates calm and serenity.',
  },
  {
    name: 'Moai',
    artist: 'Unknown Artist',
    description:
      'Located on the Easter Island, there are 1,000 moai, or extant monumental statues, created by the early Rapa Nui people, which some believe represented deified ancestors.',
    url: 'https://i.imgur.com/RCwLEoQm.jpg',
    alt: 'Three monumental stone busts with the heads that are disproportionately large with somber faces.',
  },
  {
    name: 'Blue Nana',
    artist: 'Niki de Saint Phalle',
    description:
      'The Nanas are triumphant creatures, symbols of femininity and maternity. Initially, Saint Phalle used fabric and found objects for the Nanas, and later on introduced polyester to achieve a more vibrant effect.',
    url: 'https://i.imgur.com/Sd1AgUOm.jpg',
    alt: 'A large mosaic sculpture of a whimsical dancing female figure in a colorful costume emanating joy.',
  },
  {
    name: 'Ultimate Form',
    artist: 'Barbara Hepworth',
    description:
      'This abstract bronze sculpture is a part of The Family of Man series located at Yorkshire Sculpture Park. Hepworth chose not to create literal representations of the world but developed abstract forms inspired by people and landscapes.',
    url: 'https://i.imgur.com/2heNQDcm.jpg',
    alt: 'A tall sculpture made of three elements stacked on each other reminding of a human figure.',
  },
  {
    name: 'Cavaliere',
    artist: 'Lamidi Olonade Fakeye',
    description:
      "Descended from four generations of woodcarvers, Fakeye's work blended traditional and contemporary Yoruba themes.",
    url: 'https://i.imgur.com/wIdGuZwm.png',
    alt: 'An intricate wood sculpture of a warrior with a focused face on a horse adorned with patterns.',
  },
  {
    name: 'Big Bellies',
    artist: 'Alina Szapocznikow',
    description:
      'Szapocznikow is known for her sculptures of the fragmented body as a metaphor for the fragility and impermanence of youth and beauty. This sculpture depicts two very realistic large bellies stacked on top of each other, each around five feet (1,5m) tall.',
    url: 'https://i.imgur.com/AlHTAdDm.jpg',
    alt: 'The sculpture reminds a cascade of folds, quite different from bellies in classical sculptures.',
  },
  {
    name: 'Terracotta Army',
    artist: 'Unknown Artist',
    description:
      'The Terracotta Army is a collection of terracotta sculptures depicting the armies of Qin Shi Huang, the first Emperor of China. The army consisted of more than 8,000 soldiers, 130 chariots with 520 horses, and 150 cavalry horses.',
    url: 'https://i.imgur.com/HMFmH6m.jpg',
    alt: '12 terracotta sculptures of solemn warriors, each with a unique facial expression and armor.',
  },
  {
    name: 'Lunar Landscape',
    artist: 'Louise Nevelson',
    description:
      'Nevelson was known for scavenging objects from New York City debris, which she would later assemble into monumental constructions. In this one, she used disparate parts like a bedpost, juggling pin, and seat fragment, nailing and gluing them into boxes that reflect the influence of Cubism’s geometric abstraction of space and form.',
    url: 'https://i.imgur.com/rN7hY6om.jpg',
    alt: 'A black matte sculpture where the individual elements are initially indistinguishable.',
  },
  {
    name: 'Aureole',
    artist: 'Ranjani Shettar',
    description:
      'Shettar merges the traditional and the modern, the natural and the industrial. Her art focuses on the relationship between man and nature. Her work was described as compelling both abstractly and figuratively, gravity defying, and a "fine synthesis of unlikely materials."',
    url: 'https://i.imgur.com/okTpbHhm.jpg',
    alt: 'A pale wire-like sculpture mounted on concrete wall and descending on the floor. It appears light.',
  },
  {
    name: 'Hippos',
    artist: 'Taipei Zoo',
    description: 'The Taipei Zoo commissioned a Hippo Square featuring submerged hippos at play.',
    url: 'https://i.imgur.com/6o5Vuyu.jpg',
    alt: 'A group of bronze hippo sculptures emerging from the sett sidewalk as if they were swimming.',
  },
];

src/BadGallery.tsx

import { sculptures } from './data';

export function BadGallery() {
  let index = 0;

  const handleClick = () => {
    index += 1;
  };

  const sculpture = sculptures[index];

  return (
    <>
      <button onClick={handleClick}>Next</button>
      <h2>
        <i>{sculpture.name}</i>
        by {sculpture.artist}
      </h2>
      <h3>
        ({index + 1} of {sculptures.length})
      </h3>
      <img src={sculpture.url} alt={sculpture.alt} />
      <p>{sculpture.description}</p>
    </>
  );
}

test/BadGallery.test.tsx

import { expect, test } from 'vitest';
import { setup } from './setup';
import { BadGallery } from '../src/BadGallery';
import { screen } from '@testing-library/react';

test("State: A Component's Memory, BadGallery test", async () => {
  const { user, container } = setup(<BadGallery />);

  expect(screen.getByRole('button')).toHaveTextContent('Next');
  expect(screen.getAllByRole('heading')).toHaveLength(2);
  expect(screen.getByText('Homenaje a la Neurocirugía')).toBeInTheDocument();

  const beforeHTML = container.innerHTML;

  await user.click(screen.getByRole('button'));

  expect(screen.getByText('Homenaje a la Neurocirugía')).toBeInTheDocument(); // not changed

  expect(container.innerHTML).toStrictEqual(beforeHTML); // not changed
});

手動での動作確認は、こんな感じでAppコンポーネントに組み込むコンポーネントを変えながら行っています。

src/main.tsx

import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { App } from './App.tsx';

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <App />
  </StrictMode>,
);

src/App.tsx

import { BadGallery } from './BadGallery';

export function App() {
  return <BadGallery />;
}
Giving a component multiple state variables

State: A Component's Memory / Giving a component multiple state variables

src/Gallery.tsx

import { useState } from 'react';
import { sculptures } from './data';

export function Gallery() {
  const [index, setIndex] = useState(0);

  const handleClick = () => setIndex((index + 1) % sculptures.length);

  const sculpture = sculptures[index];

  return (
    <>
      <button onClick={handleClick}>Next</button>
      <h2>
        <i>{sculpture.name} </i>
        by {sculpture.artist}
      </h2>
      <h3>
        ({index + 1} of {sculptures.length})
      </h3>
      <img src={sculpture.url} alt={sculpture.alt} />
      <p>{sculpture.description}</p>
    </>
  );
}

test/Gallery.test.tsx

import { screen } from '@testing-library/react';
import { expect, test } from 'vitest';
import { Gallery } from '../src/Gallery';
import { setup } from './setup';

test("State: A Component's Memory, Gallery test", async () => {
  const { user, container } = setup(<Gallery />);

  expect(screen.getByRole('button')).toHaveTextContent('Next');
  expect(screen.getAllByRole('heading')).toHaveLength(2);
  expect(screen.getByText('Homenaje a la Neurocirugía')).toBeInTheDocument();

  expect(container).toMatchSnapshot();

  const beforeHTML = container.innerHTML;

  await user.click(screen.getByRole('button'));

  expect(screen.getByText('Floralis Genérica')).toBeInTheDocument();

  expect(container.innerHTML).not.toStrictEqual(beforeHTML);

  expect(container).toMatchSnapshot();
});
State is isolated and private

State: A Component's Memory / State is isolated and private

src/Gallery2.tsx

import { useState } from 'react';
import { sculptures } from './data';

export function Gallery2() {
  const [index, setIndex] = useState(0);
  const [showMore, setShowMore] = useState(false);

  const handleNextClick = () => setIndex((index + 1) % sculptures.length);
  const handleMoreClick = () => setShowMore(!showMore);

  const sculpture = sculptures[index];

  return (
    <>
      <button onClick={handleNextClick}>Next</button>
      <h2>
        <i>{sculpture.name} </i>
        by {sculpture.artist}
      </h2>
      <h3>
        ({index + 1} of {sculptures.length})
      </h3>
      <button onClick={handleMoreClick}>{showMore ? 'Hide' : 'Show'} details</button>
      {showMore && <p>{sculpture.description}</p>}
      <img src={sculpture.url} alt={sculpture.alt} />
    </>
  );
}

test/Gallery2.test.tsx

import { expect, test } from 'vitest';
import { setup } from './setup';
import { Gallery2 } from '../src/Gallery2';
import { screen } from '@testing-library/react';

test("State: A Component's Memory, Gallery2 test", async () => {
  const { user, container } = setup(<Gallery2 />);

  expect(screen.getAllByRole('button')).toHaveLength(2);
  expect(screen.getAllByRole('heading')).toHaveLength(2);
  expect(screen.getByText('Next')).toBeInTheDocument();
  expect(screen.getByText('Show details')).toBeInTheDocument();
  expect(screen.getByText('Homenaje a la Neurocirugía')).toBeInTheDocument();

  expect(screen.queryByText('Hide details')).toBeNull();
  expect(screen.queryByRole('paragraph')).toBeNull();

  expect(container).toMatchSnapshot();

  await user.click(screen.getByText('Show details'));

  expect(screen.queryByText('Show details')).toBeNull();
  expect(screen.getByText('Hide details')).toBeInTheDocument();
  expect(screen.getByRole('paragraph')).toBeInTheDocument();

  expect(screen.getByRole('paragraph')).toHaveTextContent(
    'Although Colvin is predominantly known for abstract themes that allude to pre-Hispanic symbols, this gigantic sculpture, an homage to neurosurgery, is one of her most recognizable public art pieces.',
  );

  expect(container).toMatchSnapshot();

  await user.click(screen.getByText('Next'));

  expect(screen.getByText('Floralis Genérica')).toBeInTheDocument();

  expect(screen.queryByText('Show details')).toBeNull();
  expect(screen.getByText('Hide details')).toBeInTheDocument();
  expect(screen.getByRole('paragraph')).toBeInTheDocument();

  expect(container).toMatchSnapshot();

  await user.click(screen.getByText('Hide details'));

  expect(screen.getByText('Show details')).toBeInTheDocument();
  expect(screen.queryByText('Hide details')).toBeNull();
  expect(screen.queryByRole('paragraph')).toBeNull();

  expect(container).toMatchSnapshot();
});

src/Page.tsx

import { Gallery2 } from './Gallery2';

export function Page() {
  return (
    <div className="Page">
      <Gallery2 />
      <Gallery2 />
    </div>
  );
}

test/Page.test.tsx

import { expect, test } from 'vitest';
import { setup } from './setup';
import { Page } from '../src/Page';
import { screen } from '@testing-library/react';

test("State: A Component's Memory, Page test", async () => {
  const { user, container } = setup(<Page />);

  expect(screen.getAllByRole('button')).toHaveLength(4);
  expect(screen.getAllByRole('heading')).toHaveLength(4);
  expect(screen.getAllByText('Next')).toHaveLength(2);
  expect(screen.getAllByText('Show details')).toHaveLength(2);

  expect(screen.queryByRole('paragraph')).toBeNull();

  expect(screen.getAllByText('Homenaje a la Neurocirugía')).toHaveLength(2);

  expect(container).toMatchSnapshot();

  await user.click(screen.getAllByText('Next')[0]);

  expect(screen.getAllByText('Next')).toHaveLength(2);
  expect(screen.getAllByText('Show details')).toHaveLength(2);

  expect(screen.getAllByText('Floralis Genérica')).toHaveLength(1);
  expect(screen.getAllByText('Homenaje a la Neurocirugía')).toHaveLength(1);

  expect(screen.queryByRole('paragraph')).toBeNull();

  expect(container).toMatchSnapshot();

  await user.click(screen.getAllByText('Show details')[1]);

  expect(screen.getAllByText('Next')).toHaveLength(2);
  expect(screen.getAllByText('Show details')).toHaveLength(1);
  expect(screen.getAllByText('Hide details')).toHaveLength(1);

  expect(screen.getAllByRole('paragraph')).toHaveLength(1);

  expect(screen.getByRole('paragraph')).toHaveTextContent(
    'Although Colvin is predominantly known for abstract themes that allude to pre-Hispanic symbols, this gigantic sculpture, an homage to neurosurgery, is one of her most recognizable public art pieces.',
  );

  expect(container).toMatchSnapshot();
});

おわりに

今回は「Learn React」の「Adding Interactivity」から「State: A Component's Memory」をやってみました。

新しい話は出てこなかったのですが、コンポーネントが複数のステートを持ったり、その変化をテストしていくのにけっこう
苦労しました。

もうちょっと複雑になるとコンポーネントをどう作るかといったところから悩むと思うので、こうやって小さなコンポーネントから
積み重ねて慣れていきたいですね。