forked from mogua-station/FE
-
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.
Test(mogua-station#123): Error 컴포넌트 테스트 코드 작성
- 에러 메세지 표시 테스트 - 기본 에러 메세지 표시 테스트 - 홈으로 돌아가기 버튼 동작 테스트
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 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,47 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import Error, { type ErrorProps } from "@/app/user/[id]/error"; | ||
import "@testing-library/jest-dom"; | ||
|
||
const mockReplace = jest.fn(); | ||
|
||
jest.mock("next/navigation", () => ({ | ||
useRouter: () => ({ | ||
replace: mockReplace, | ||
}), | ||
})); | ||
|
||
describe("유저페이지 Error 컴포넌트", () => { | ||
const errorMessage = "해당 유저는 존재하지 않습니다."; | ||
const mockError: ErrorProps["error"] = { | ||
name: "Error", | ||
message: "해당 유저는 존재하지 않습니다.", | ||
digest: "test-digest", | ||
} as const; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
render(<Error error={mockError} />); | ||
}); | ||
|
||
it("에러 메세지를 표시한다", () => { | ||
expect(screen.getByText(errorMessage)).toBeInTheDocument(); | ||
}); | ||
|
||
it("전달받은 에러 메세지가 없으면 기본 메세지를 표시한다", () => { | ||
const mockErrorWithoutMessage: ErrorProps["error"] = { | ||
name: "Error", | ||
message: "", | ||
} as const; | ||
|
||
render(<Error error={mockErrorWithoutMessage} />); | ||
expect(screen.getByText("오류가 발생했습니다.")).toBeInTheDocument(); | ||
}); | ||
|
||
it("홈으로 돌아가기 버튼 클릭시 홈으로 이동한다", async () => { | ||
const button = screen.getByText("홈으로 돌아가기"); | ||
await userEvent.click(button); | ||
|
||
expect(mockReplace).toHaveBeenCalledWith("/"); | ||
}); | ||
}); |