Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ログインページのテストコード作成と、actの記述方法を変更 #24

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions __test__/LandingSection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ import { mockData } from "./mock/mock_CmsData";

describe("<LandingSection />", () => {
test("LandingSectionコンポーネントが正しく表示されるかのテスト", async () => {
await act(async () => {
render(<LandingSection data={mockData} />);
});
render(<LandingSection data={mockData} />);

await act(async () => {});

expect(screen.getByText(mockData.title || "")).toBeInTheDocument();
});

test("アプリケーション名がレンダリングされる", async () => {
await act(async () => {
render(<LandingSection data={mockData} />);
});
render(<LandingSection data={mockData} />);

await act(async () => {});

expect(
screen.getByText(mockData.applicationName || "")
).toBeInTheDocument();
});

test("各サブタイトルとテキストがレンダリングされる", async () => {
await act(async () => {
render(<LandingSection data={mockData} />);
});
render(<LandingSection data={mockData} />);

await act(async () => {});

expect(screen.getByText(mockData.subTitle1 || "")).toBeInTheDocument();
expect(screen.getByText(mockData.text1)).toBeInTheDocument();
expect(screen.getByText(mockData.subTitle2 || "")).toBeInTheDocument();
Expand All @@ -40,15 +42,15 @@ describe("<LandingSection />", () => {
});

test("画像が正しいsrc属性でレンダリングされる", async () => {
await act(async () => {
render(<LandingSection data={mockData} />);
});
render(<LandingSection data={mockData} />);

const imageElement = screen.getByAltText("Home");
const imageElement_img1 = screen.getByAltText("Image 1");
const imageElement_img2 = screen.getByAltText("Image 2");
const imageElement_img3 = screen.getByAltText("Image 3");

await act(async () => {});

expect(imageElement).toHaveAttribute("src");
expect(imageElement.getAttribute("src")).toContain(
encodeURIComponent(mockData.icon?.url ?? "")
Expand Down
76 changes: 76 additions & 0 deletions __test__/LoginPage.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { act, render, screen } from "@testing-library/react";
import LoginPage from "app/user/login/page";
import user from "@testing-library/user-event";

// モック化したuseRouterをインポート
jest.mock("next/navigation", () => ({
useRouter: jest.fn(),
}));

describe("<Page />", () => {
test("LoginPageコンポーネントが正しく表示されるかのテスト", async () => {
render(<LoginPage />);

await act(async () => {});

expect(screen.getByText("ログイン画面")).toBeInTheDocument();
expect(screen.getByPlaceholderText("メールアドレス")).toBeInTheDocument();
expect(screen.getByPlaceholderText("パスワード")).toBeInTheDocument();
expect(screen.getByRole("button")).toBeInTheDocument();
});

test("メールアドレスとパスワード欄が未入力の場合、エラーメッセージが表示されるかのテスト", async () => {
render(<LoginPage />);

const submitButton = screen.getByRole("button");
await user.click(submitButton);

await act(async () => {});

expect(
screen.getByText("メールアドレスを入力してください")
).toBeInTheDocument();

expect(
screen.getByText("パスワードを入力してください")
).toBeInTheDocument();
});

test("適切なメールアドレス,適切なパスワードを入力していない場合、エラーメッセージが表示されるテスト", async () => {
render(<LoginPage />);

const emailInput = screen.getByPlaceholderText("メールアドレス");
const passwordInput = screen.getByPlaceholderText("パスワード");
const submitButton = screen.getByRole("button");

await user.type(emailInput, "test");
await user.type(passwordInput, "pass");
await user.click(submitButton);

await act(async () => {});

expect(
screen.getByText("有効なメールアドレスを入力してください")
).toBeInTheDocument();

expect(
screen.getByText("パスワードは6文字以上にしてください")
).toBeInTheDocument();
});

test("正しいメールアドレスを入力した時、ログイン成功メッセージが表示され、ルートにリダイレクトされるかのテスト", async () => {
render(<LoginPage />);

const emailInput = screen.getByPlaceholderText("メールアドレス");
const passwordInput = screen.getByPlaceholderText("パスワード");
const submitButton = screen.getByRole("button");

await user.type(emailInput, "adidas827068@gmail.com");
await user.type(passwordInput, "123456");
await user.click(submitButton);

await act(async () => {});

expect(screen.getByText("ログイン中・・・")).toBeInTheDocument();
});
});
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.5.0",
"@testing-library/react": "^16.0.1",
"@testing-library/user-event": "^14.5.2",
"@types/jest": "^29.5.13",
"@types/node": "^20",
"@types/react": "^18",
Expand Down
Loading