-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- LoginForm.test.tsx에 테스트 추가 - accessToken 추출 로직을 정규식 대신 getCookieValue 함수로 리팩토링 - getCookieValue 함수를 통해 쿠키 값 가져오는 방식 개선
- Loading branch information
Showing
6 changed files
with
103 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { render, screen, fireEvent, waitFor } from "@testing-library/react"; | ||
import { vi, Mock } from "vitest"; | ||
|
||
import { useAuth } from "@/hooks/useAuth"; | ||
|
||
import LoginForm from "./LoginForm"; | ||
|
||
// Mock the useAuth hook | ||
vi.mock("@/hooks/useAuth"); | ||
|
||
describe("LoginForm", () => { | ||
const mockLoginMutation = { | ||
mutate: vi.fn(), | ||
isLoading: false, | ||
}; | ||
|
||
beforeEach(() => { | ||
(useAuth as Mock).mockReturnValue({ | ||
loginMutation: mockLoginMutation, | ||
}); | ||
}); | ||
|
||
it("모든 입력 필드와 로그인 버튼이 렌더링되어야 한다.", () => { | ||
render(<LoginForm />); | ||
|
||
expect(screen.getByPlaceholderText("이메일")).toBeInTheDocument(); | ||
expect(screen.getByPlaceholderText("비밀번호")).toBeInTheDocument(); | ||
expect(screen.getByRole("button", { name: "로그인" })).toBeInTheDocument(); | ||
}); | ||
|
||
describe("유효성 검사", () => { | ||
it("이메일 및 비밀번호 필드 유효성 검사", async () => { | ||
render(<LoginForm />); | ||
fireEvent.blur(screen.getByPlaceholderText("이메일")); | ||
fireEvent.blur(screen.getByPlaceholderText("비밀번호")); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("유효한 이메일 주소를 입력해주세요.")).toBeInTheDocument(); | ||
expect(screen.getByText("비밀번호는 최소 8자 이상이어야 합니다.")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it("이메일과 비밀번호로 로그인합니다.", async () => { | ||
render(<LoginForm />); | ||
|
||
fireEvent.change(screen.getByPlaceholderText("이메일"), { | ||
target: { value: "[email protected]" }, | ||
}); | ||
fireEvent.change(screen.getByPlaceholderText("비밀번호"), { | ||
target: { value: "password123" }, | ||
}); | ||
fireEvent.click(screen.getByRole("button", { name: "로그인" })); | ||
|
||
await waitFor(() => { | ||
expect(mockLoginMutation.mutate).toHaveBeenCalledWith({ | ||
email: "[email protected]", | ||
password: "password123", | ||
}); | ||
}); | ||
}); | ||
|
||
it("제출 시 로딩 중인 스피너를 표시합니다.", async () => { | ||
mockLoginMutation.isLoading = true; | ||
render(<LoginForm />); | ||
|
||
fireEvent.change(screen.getByPlaceholderText("이메일"), { | ||
target: { value: "[email protected]" }, | ||
}); | ||
fireEvent.change(screen.getByPlaceholderText("비밀번호"), { | ||
target: { value: "password123" }, | ||
}); | ||
|
||
fireEvent.click(screen.getByRole("button", { name: "로그인" })); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByTestId("spinner")).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters