|
| 1 | +import { act, render, screen } from "@testing-library/react"; |
| 2 | +import user from "@testing-library/user-event"; |
| 3 | +import RegisterPage from "app/user/register/page"; |
| 4 | + |
| 5 | +// モック化したuseRouterをインポート |
| 6 | +jest.mock("next/navigation", () => ({ |
| 7 | + useRouter: jest.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +describe("<Page />", () => { |
| 11 | + test("RegisterPageコンポーネントが正しく表示されるかのテスト", async () => { |
| 12 | + render(<RegisterPage />); |
| 13 | + |
| 14 | + await act(async () => {}); |
| 15 | + |
| 16 | + expect(screen.getByText("新規登録画面")).toBeInTheDocument(); |
| 17 | + expect(screen.getByPlaceholderText("メールアドレス")).toBeInTheDocument(); |
| 18 | + expect(screen.getByPlaceholderText("パスワード")).toBeInTheDocument(); |
| 19 | + expect(screen.getByRole("button")).toBeInTheDocument(); |
| 20 | + }); |
| 21 | + |
| 22 | + test("メールアドレスとパスワード欄が未入力の場合、エラーメッセージが表示されるかのテスト", async () => { |
| 23 | + render(<RegisterPage />); |
| 24 | + |
| 25 | + const submitButton = screen.getByRole("button"); |
| 26 | + await user.click(submitButton); |
| 27 | + |
| 28 | + await act(async () => {}); |
| 29 | + |
| 30 | + expect( |
| 31 | + screen.getByText("メールアドレスを入力してください") |
| 32 | + ).toBeInTheDocument(); |
| 33 | + |
| 34 | + expect( |
| 35 | + screen.getByText("パスワードを入力してください") |
| 36 | + ).toBeInTheDocument(); |
| 37 | + }); |
| 38 | + |
| 39 | + test("適切なメールアドレス,適切なパスワードを入力していない場合、エラーメッセージが表示されるテスト", async () => { |
| 40 | + render(<RegisterPage />); |
| 41 | + |
| 42 | + const emailInput = screen.getByPlaceholderText("メールアドレス"); |
| 43 | + const passwordInput = screen.getByPlaceholderText("パスワード"); |
| 44 | + const submitButton = screen.getByRole("button"); |
| 45 | + |
| 46 | + await user.type(emailInput, "test"); |
| 47 | + await user.type(passwordInput, "pass"); |
| 48 | + await user.click(submitButton); |
| 49 | + |
| 50 | + await act(async () => {}); |
| 51 | + |
| 52 | + expect( |
| 53 | + screen.getByText("有効なメールアドレスを入力してください") |
| 54 | + ).toBeInTheDocument(); |
| 55 | + |
| 56 | + expect( |
| 57 | + screen.getByText("パスワードは6文字以上にしてください") |
| 58 | + ).toBeInTheDocument(); |
| 59 | + }); |
| 60 | +}); |
0 commit comments