-
Notifications
You must be signed in to change notification settings - Fork 0
회원가입 테스트 작성 #35
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
Merged
Merged
회원가입 테스트 작성 #35
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
37485b2
chore: useRouter push 모킹
mung96 0eda30c
test: 회원가입 페이지 필수필드 렌더링 테스트
mung96 6db9976
chore: cleanup 자동 실행하도록 변경
mung96 00c4716
feat: 이메일, 닉네임 중복 체크 api mock 구현
mung96 4012709
test: 필수필드 입력시 버튼 활성화 테스트 추가
mung96 5d7b73b
test: 회원가입시 API 호출 테스트 추가
mung96 d8bbc52
test: 회원가입 성공 플로우 e2e테스트
mung96 3323e0a
fix: email, nickname worker별 다르게 설정
mung96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export const handlers = []; | ||
| import { signupHandlers } from "./signup-handlers"; | ||
|
|
||
| export const handlers = [...signupHandlers]; |
This file contains hidden or 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,9 @@ | ||
| import { checkEmailHandler } from "@pages/auth/signup/api/check-email/index.mock"; | ||
| import { checkNicknameHandler } from "@pages/auth/signup/api/check-nickname/index.mock"; | ||
| import { signupHandler } from "@pages/auth/signup/api/signup/index.mock"; | ||
|
|
||
| export const signupHandlers = [ | ||
| checkEmailHandler, | ||
| checkNicknameHandler, | ||
| signupHandler, | ||
| ]; |
This file contains hidden or 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,16 @@ | ||
| import { CHECK_EMAIL_ENDPOINT } from "@pages/auth/signup/api/check-email"; | ||
| import { http, HttpResponse } from "msw"; | ||
|
|
||
| export const checkEmailHandler = http.get( | ||
| CHECK_EMAIL_ENDPOINT, | ||
| ({ request }) => { | ||
| const url = new URL(request.url); | ||
| const email = url.searchParams.get("email"); | ||
|
|
||
| return HttpResponse.json({ | ||
| success: true, | ||
| available: true, | ||
| message: "사용 가능한 이메일입니다.", | ||
| }); | ||
| }, | ||
| ); |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| import { apiRequester } from "@shared/api/api-requester"; | ||
|
|
||
| export const CHECK_EMAIL_ENDPOINT = `/api/signup/check-email`; | ||
|
|
||
| export const checkEmail = (email: string) => { | ||
| return apiRequester<{ | ||
| success: boolean; | ||
| available: boolean; | ||
| message: string; | ||
| }>(`/api/signup/check-email?email=${email}`, { | ||
| }>(`${CHECK_EMAIL_ENDPOINT}?email=${email}`, { | ||
| method: "GET", | ||
| }); | ||
| }; |
This file contains hidden or 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,16 @@ | ||
| import { CHECK_NICKNAME_ENDPOINT } from "@pages/auth/signup/api/check-nickname"; | ||
| import { http, HttpResponse } from "msw"; | ||
|
|
||
| export const checkNicknameHandler = http.get( | ||
| CHECK_NICKNAME_ENDPOINT, | ||
| ({ request }) => { | ||
| const url = new URL(request.url); | ||
| const nickname = url.searchParams.get("nickname"); | ||
|
|
||
| return HttpResponse.json({ | ||
| success: true, | ||
| available: true, | ||
| message: "사용 가능한 닉네임입니다.", | ||
| }); | ||
| }, | ||
| ); |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| import { apiRequester } from "@shared/api/api-requester"; | ||
|
|
||
| export const CHECK_NICKNAME_ENDPOINT = `/api/signup/check-nickname`; | ||
|
|
||
| export const checkNickname = (nickname: string) => { | ||
| return apiRequester<{ | ||
| success: boolean; | ||
| available: boolean; | ||
| message: string; | ||
| }>(`/api/signup/check-nickname?nickname=${nickname}`, { | ||
| }>(`${CHECK_NICKNAME_ENDPOINT}?nickname=${nickname}`, { | ||
| method: "GET", | ||
| }); | ||
| }; |
This file contains hidden or 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,8 @@ | ||
| import { SIGNUP_ENDPOINT } from "@pages/auth/signup/api/signup"; | ||
| import { http, HttpResponse } from "msw"; | ||
|
|
||
| export const signupHandler = http.post(SIGNUP_ENDPOINT, ({}) => { | ||
| return HttpResponse.json({ | ||
| success: true, | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,92 @@ | ||
| import { SignupPage } from "@pages/auth/signup/ui/signup-page"; | ||
| import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
| import { render, screen, waitFor } from "@testing-library/react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { userEvent } from "@testing-library/user-event"; | ||
| import { http, HttpResponse } from "msw"; | ||
| import { server } from "@app/mocks/server"; | ||
|
|
||
| const TEST_EMAIL = "test@example.com"; | ||
| const TEST_NICKNAME = "데품타"; | ||
| const TEST_PASSWORD = "abcd1234"; | ||
| const TEST_CONFIRM_PASSWORD = "abcd1234"; | ||
| const TEST_WRONG_CONFIRM_PASSWORD = "abc123123"; | ||
|
|
||
| describe("회원가입 폼", () => { | ||
| beforeEach(() => { | ||
| render( | ||
| <QueryClientProvider client={new QueryClient()}> | ||
| <SignupPage /> | ||
| </QueryClientProvider>, | ||
| ); | ||
| }); | ||
|
|
||
| it("모든 필수 필드를 렌더링한다", () => { | ||
| expect(screen.getByLabelText("아이디")).toBeInTheDocument(); | ||
| expect(screen.getByLabelText("닉네임")).toBeInTheDocument(); | ||
| expect(screen.getByLabelText("비밀번호")).toBeInTheDocument(); | ||
| expect(screen.getByLabelText("비밀번호 확인")).toBeInTheDocument(); | ||
| expect(screen.getByLabelText("동의함")).toBeInTheDocument(); //이용약관 | ||
| }); | ||
|
|
||
| //복잡한 제출 로직 테스트 | ||
| it("모든 필수 필드 입력 시 제출을 허용한다", async () => { | ||
| const signupSpy = vi.fn(); | ||
| server.use( | ||
| http.post("/api/signup", async ({ request }) => { | ||
| signupSpy(await request.json()); | ||
| return HttpResponse.json({ | ||
| success: true, | ||
| message: "회원가입이 완료되었습니다.", | ||
| }); | ||
| }), | ||
| ); | ||
| const user = userEvent.setup(); | ||
| expect(screen.getByRole("button", { name: "회원가입" })).toBeDisabled(); | ||
|
|
||
| // 이메일 검증 | ||
| await user.type(screen.getByLabelText("아이디"), TEST_EMAIL); | ||
| await user.tab(); | ||
| await user.click(screen.getByRole("button", { name: "이메일 중복 확인" })); | ||
| expect( | ||
| await screen.findByText("사용 가능한 이메일입니다."), | ||
| ).toBeInTheDocument(); | ||
|
|
||
| // 닉네임 검증 | ||
| await user.type(screen.getByLabelText("닉네임"), TEST_NICKNAME); | ||
| await user.tab(); | ||
| await user.click(screen.getByRole("button", { name: "닉네임 중복 확인" })); | ||
| expect( | ||
| await screen.findByText("사용 가능한 닉네임입니다."), | ||
| ).toBeInTheDocument(); | ||
|
|
||
| // 비밀번호, 비밀번호 확인 | ||
| await user.type(screen.getByLabelText("비밀번호"), TEST_PASSWORD); | ||
| await user.type( | ||
| screen.getByLabelText("비밀번호 확인"), | ||
| TEST_CONFIRM_PASSWORD, | ||
| ); | ||
|
|
||
| // 약관 동의 체크 | ||
| await user.click(screen.getByLabelText("동의함")); | ||
|
|
||
| // 버튼 활성화 테스트 | ||
| expect(screen.getByRole("button", { name: "회원가입" })).toBeEnabled(); | ||
|
|
||
| // 제출 | ||
| await user.click(screen.getByRole("button", { name: "회원가입" })); | ||
| await waitFor(() => { | ||
| expect(signupSpy).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| email: TEST_EMAIL, | ||
| nickname: TEST_NICKNAME, | ||
| password: TEST_PASSWORD, | ||
| confirmPassword: TEST_CONFIRM_PASSWORD, | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
| // 유효성검사 테스트 | ||
|
|
||
| // 조건부렌더링 테스트 | ||
| }); |
This file contains hidden or 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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,9 +9,9 @@ import { PATH } from "@shared/routes"; | |||||||||||
| import { Button } from "@shared/ui"; | ||||||||||||
|
|
||||||||||||
| import Link from "next/link"; | ||||||||||||
| import { FormProvider, useForm } from "react-hook-form"; | ||||||||||||
| import { FormProvider, useController, useForm } from "react-hook-form"; | ||||||||||||
|
|
||||||||||||
| import { useState } from "react"; | ||||||||||||
| import { ChangeEvent, useState } from "react"; | ||||||||||||
| import { useRouter } from "next/navigation"; | ||||||||||||
|
|
||||||||||||
| import { EmailField } from "@pages/auth/signup/ui/email-field"; | ||||||||||||
|
|
@@ -22,6 +22,8 @@ import { TermsAgreementField } from "@pages/auth/signup/ui/term-agreement-field" | |||||||||||
| import { useSignup } from "@pages/auth/signup/model/use-signup"; | ||||||||||||
|
|
||||||||||||
| import { SideBanner } from "@widgets/side-banner"; | ||||||||||||
| import { checkEmail } from "@pages/auth/signup/api/check-email"; | ||||||||||||
| import { ApiError, HttpStatus } from "@shared/api"; | ||||||||||||
|
|
||||||||||||
| export const SignupPage = () => { | ||||||||||||
| const methods = useForm<SignUpFormValues>({ | ||||||||||||
|
|
@@ -38,7 +40,9 @@ export const SignupPage = () => { | |||||||||||
| const router = useRouter(); | ||||||||||||
| const { | ||||||||||||
| handleSubmit, | ||||||||||||
| formState: { isValid }, | ||||||||||||
| control, | ||||||||||||
| setError, | ||||||||||||
| formState: { isValid, errors }, | ||||||||||||
| } = methods; | ||||||||||||
| const [isValidDuplicateEmail, setIsValidDuplicateEmail] = useState(false); | ||||||||||||
| const [isValidDuplicateNickname, setIsValidDuplicateNickname] = | ||||||||||||
|
|
@@ -49,6 +53,35 @@ export const SignupPage = () => { | |||||||||||
| const isFormValid = | ||||||||||||
| isValidDuplicateEmail && isValidDuplicateNickname && isValid; | ||||||||||||
|
|
||||||||||||
| const { field: emailField } = useController({ | ||||||||||||
| control, | ||||||||||||
| name: "email", | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| const checkDuplicateEmail = () => { | ||||||||||||
| const email = emailField.value; | ||||||||||||
| checkEmail(email) | ||||||||||||
| .then((response) => { | ||||||||||||
| if (response.available) { | ||||||||||||
| setIsValidDuplicateEmail(true); | ||||||||||||
| } else { | ||||||||||||
| setError("email", { | ||||||||||||
| type: "duplicate", | ||||||||||||
| message: response.message, | ||||||||||||
| }); | ||||||||||||
| } | ||||||||||||
| }) | ||||||||||||
| .catch((error) => { | ||||||||||||
| if (error instanceof ApiError) { | ||||||||||||
| if (error.status === HttpStatus.BAD_REQUEST) { | ||||||||||||
| setError("email", { | ||||||||||||
| type: "invalid_format", | ||||||||||||
| message: error.message, | ||||||||||||
| }); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| }); | ||||||||||||
| }; | ||||||||||||
| return ( | ||||||||||||
| <div className="flex h-full"> | ||||||||||||
| <SideBanner /> | ||||||||||||
|
|
@@ -72,7 +105,14 @@ export const SignupPage = () => { | |||||||||||
| {/* 이메일 */} | ||||||||||||
| <EmailField | ||||||||||||
| isValidDuplicateEmail={isValidDuplicateEmail} | ||||||||||||
| setIsValidDuplicateEmail={setIsValidDuplicateEmail} | ||||||||||||
| value={emailField.value} | ||||||||||||
| errorMessage={errors.email?.message} | ||||||||||||
| successMessage={"사용 가능한 이메일입니다."} | ||||||||||||
| onChange={(e) => emailField.onChange(e.target.value)} | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이메일 입력값이 변경될 때 중복 확인 상태(
Suggested change
|
||||||||||||
| isCheckDisabled={ | ||||||||||||
| !emailField.value || errors.email?.type === "invalid_format" | ||||||||||||
| } | ||||||||||||
| checkDuplicateEmail={checkDuplicateEmail} | ||||||||||||
| /> | ||||||||||||
| {/* 닉네임 */} | ||||||||||||
| <NicknameField | ||||||||||||
|
|
||||||||||||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!!value조건문으로 인해 이메일 필드가 비어있을 때 발생하는 에러 메시지(예: '이메일을 입력해주세요')가 화면에 표시되지 않는 문제가 발생할 수 있습니다.errorMessage가 존재한다면 값의 유무와 상관없이 표시되도록 수정하는 것이 좋습니다.