133 lines
4.3 KiB
TypeScript
133 lines
4.3 KiB
TypeScript
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
|
import { describe, it, expect, vi, beforeAll, afterAll, beforeEach } from "vitest";
|
|
import { LoginPage } from "@/pages/auth/login";
|
|
import { authService } from "@/entities/auth";
|
|
|
|
// next/image 모킹
|
|
vi.mock("next/image", () => ({
|
|
default: (props: any) => {
|
|
// eslint-disable-next-line @next/next/no-img-element
|
|
return <img {...props} />;
|
|
},
|
|
}));
|
|
|
|
// @/entities/auth 모킹
|
|
vi.mock("@/entities/auth", () => ({
|
|
authService: {
|
|
login: vi.fn(),
|
|
},
|
|
AuthWrapper: ({ children }: { children: React.ReactNode }) => children,
|
|
tokenManager: {
|
|
getToken: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
// @common/assets 모킹
|
|
vi.mock("@common/assets", () => ({
|
|
images: {
|
|
logo: "/logo.png",
|
|
},
|
|
}));
|
|
|
|
describe("LoginPage 단위 테스트 (Vitest)", () => {
|
|
const originalLocation = window.location;
|
|
|
|
beforeAll(() => {
|
|
// window.location.href 변경 감지를 위해 window.location 모킹
|
|
// @ts-expect-error
|
|
delete window.location;
|
|
window.location = { ...originalLocation, href: "" } as never;
|
|
});
|
|
|
|
afterAll(() => {
|
|
// @ts-expect-error
|
|
window.location = originalLocation;
|
|
});
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
window.location.href = "";
|
|
});
|
|
|
|
it("1. 로그인 화면 UI 요소가 정상 렌더링되는지 검증", () => {
|
|
render(<LoginPage />);
|
|
|
|
// 타이틀 렌더링 확인
|
|
expect(screen.getByText("유역하수도통합플랫폼")).toBeInTheDocument();
|
|
expect(screen.getByText("하수도플랫폼 통합관리(CMS)시스템")).toBeInTheDocument();
|
|
|
|
// 입력 필드 존재 확인
|
|
const idInput = screen.getByTitle("아이디를 입력하세요.");
|
|
const passwordInput = screen.getByTitle("비밀번호를 입력하세요.");
|
|
expect(idInput).toBeInTheDocument();
|
|
expect(passwordInput).toBeInTheDocument();
|
|
|
|
// 체크박스 존재 확인
|
|
const saveIdCheckbox = screen.getByLabelText("아이디저장");
|
|
expect(saveIdCheckbox).toBeInTheDocument();
|
|
expect(saveIdCheckbox).not.toBeChecked();
|
|
|
|
// 로그인 버튼 존재 확인
|
|
const submitButton = screen.getByRole("button", { name: "LOGIN" });
|
|
expect(submitButton).toBeInTheDocument();
|
|
});
|
|
|
|
it("2. 로그인 실패 시 에러 메시지가 화면에 노출되는지 검증", async () => {
|
|
// authService.login이 에러를 던지도록 모크 구현
|
|
const errorMessage = "아이디와 비밀번호가 일치하지 않습니다.";
|
|
vi.mocked(authService.login).mockRejectedValueOnce(new Error(errorMessage));
|
|
|
|
render(<LoginPage />);
|
|
|
|
const idInput = screen.getByTitle("아이디를 입력하세요.");
|
|
const passwordInput = screen.getByTitle("비밀번호를 입력하세요.");
|
|
const submitButton = screen.getByRole("button", { name: "LOGIN" });
|
|
|
|
// 값 입력 및 제출
|
|
fireEvent.change(idInput, { target: { value: "testuser" } });
|
|
fireEvent.change(passwordInput, { target: { value: "wrongpassword" } });
|
|
fireEvent.click(submitButton);
|
|
|
|
// 에러 메시지 렌더링 대기 및 확인
|
|
await waitFor(() => {
|
|
expect(screen.getByText(errorMessage)).toBeInTheDocument();
|
|
});
|
|
|
|
// authService.login 호출 확인
|
|
expect(authService.login).toHaveBeenCalledWith({
|
|
userId: "testuser",
|
|
password: "wrongpassword",
|
|
});
|
|
|
|
// 리다이렉트가 발생하지 않아야 함
|
|
expect(window.location.href).toBe("");
|
|
});
|
|
|
|
it("3. 로그인 성공 시 /dashboard 로 리다이렉트되는지 검증", async () => {
|
|
// authService.login 성공 모크 구현
|
|
vi.mocked(authService.login).mockResolvedValueOnce(undefined as never);
|
|
|
|
render(<LoginPage />);
|
|
|
|
const idInput = screen.getByTitle("아이디를 입력하세요.");
|
|
const passwordInput = screen.getByTitle("비밀번호를 입력하세요.");
|
|
const submitButton = screen.getByRole("button", { name: "LOGIN" });
|
|
|
|
// 값 입력 및 제출
|
|
fireEvent.change(idInput, { target: { value: "admin1gmail" } });
|
|
fireEvent.change(passwordInput, { target: { value: "test1234!" } });
|
|
fireEvent.click(submitButton);
|
|
|
|
// 리다이렉트 주소값 변경 대기 및 확인
|
|
await waitFor(() => {
|
|
expect(window.location.href).toBe("/dashboard");
|
|
});
|
|
|
|
// authService.login 호출 확인
|
|
expect(authService.login).toHaveBeenCalledWith({
|
|
userId: "admin1gmail",
|
|
password: "test1234!",
|
|
});
|
|
});
|
|
});
|