1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】テストデータをモックデータに置き換える

1
Last updated at Posted at 2025-06-09

やりたいこと

モックデータをセットし、データが置き換わっていることを確認したい

実装

App.spec.tsx
// 本番データはここのgetAllReocrds関数で取得してくる
import * as api from "../../utils/supabaseFunction";

jest.mock("../../utils/supabaseFunction");

describe("App", () => {
  test("モックで取得したデータが表示される", async () => {
    // getAllRecordsで取得するデータをモックデータに置き換える
    (api.getAllRecords as jest.Mock).mockResolvedValue([
      { title: "てすと", time: "33" },
      { title: "てすと2", time: "44" },
    ]);
  
    render(<App />);

    // モックデータは複数取れるので、find「All」ByTestIdで指定する
    const items = await screen.findAllByTestId("item");
    expect(items[0]).toHaveTextContent("てすと");
  });

App.tsx
{records.map((item, key) => {
  return (
    <>
      <p key={key} data-testid="item">{item.title}:{item.time}</p>
      <button onClick={() => handleDelete(item.id)}>×</button>
    </>
  )
})}

詰まっていたところ

await waitFor(()=>{})で取ろうとしてやり方がわからなかったり、screen.findAllByTestIdで取得しようとした。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?