{ keburukan"/> { keburukan">

Rumah  >  Artikel  >  hujung hadapan web  >  Asas Bertindakbalas~ujian unit/ujian huraikan

Asas Bertindakbalas~ujian unit/ujian huraikan

Linda Hamilton
Linda Hamiltonasal
2024-10-23 06:24:02267semak imbas

React Basics~unit test/describe test

  • Apabila saya menguji beberapa kod semasa, saya boleh menggunakan fungsi huraikan untuk membahagikan kod kepada kumpulan.

  • Dalam kes ini, saya mencipta apl kaunter.

・src/Example.jsx

import Counter from "./components/Counter";

const Example = () => {
  const originCount = 0;

  return <Counter originCount={originCount}></Counter>;
};

export default Example;

・src/components/Counter.jsx

import { useState } from "react";

const Counter = (props) => {
  const { originCount } = props;
  const [count, setCount] = useState(originCount);

  const countUp = () => {
    setCount(count + 1);
  };

  const countDown = () => {
    setCount(count - 1);
  };

  const countClear = () => {
    setCount(0);
  };

  return (
    <div>
      <h2>Counter test</h2>
      <div>
        <span>Current count:{count}</span>
      </div>
      <div>
        <button onClick={countUp}>Countup</button>
        <button onClick={countDown}>Countdown</button>
        <button onClick={countClear}>Clear</button>
      </div>
    </div>
  );
};

export default Counter;

・src/components/Counter.test.jsx

import { fireEvent, render, screen } from "@testing-library/react";
import Counter from "./Counter";

//A group for initial test
describe("Counter", () => {
 describe("Check the initial display", () => {

  // ① A Confirming the Initial State
    test("Whether 
    test("Whether the current count is 0 or not", () => {
      render(<Counter originCount={0} />);

      const spanElBeforeUpdate = screen.getByText("Current count:0");
      expect(spanElBeforeUpdate).toBeInTheDocument();
    });
  });

  //A group for actions tests
  describe("Control buttons", () => {

    // ① count up
    test("Whether the current count changes into 1 or not, in case the 
      countup button is clicked", () => {
      render(<Counter originCount={0} />);

      const spanElBeforeUpdate = screen.getByText("Current count:0");
      expect(spanElBeforeUpdate).toBeInTheDocument();

      const btn = screen.getByRole("button", { name: "Countup" });
      fireEvent.click(btn);

      const spanEl = screen.getByText("Current count:1");
      expect(spanEl).toBeInTheDocument();
    });
  
  // ② count down 
    test("Whether the current count  changes into -1 or not, in case the 
      countdown button is clicked ", () => {
      render(<Counter originCount={0} />);

      const spanElBeforeUpdate = screen.getByText("Current count:0");
      expect(spanElBeforeUpdate).toBeInTheDocument();

      const btn = screen.getByRole("button", { name: "Countdown" });
      fireEvent.click(btn);

      const spanEl = screen.getByText("Current count:-1");
      expect(spanEl).toBeInTheDocument();
    });

  // ③ count clear 
    test("Whether the current count changes into 0 or not, in case the 
      clear button is clicked ", () => {
      render(<Counter originCount={0} />);

      const spanElBeforeUpdate = screen.getByText("Current count:0");
      expect(spanElBeforeUpdate).toBeInTheDocument();

      const btn = screen.getByRole("button", { name: "Clear" });
      fireEvent.click(btn);

      const spanEl = screen.getByText("Current count:0");
      expect(spanEl).toBeInTheDocument();
    });
  });
});

・Mengira sehingga

・Mengira mundur
・Mengira mundur
・Kejayaan
・Kegagalan

Atas ialah kandungan terperinci Asas Bertindakbalas~ujian unit/ujian huraikan. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn