Repository navigation
마이페이지 예문 난이도 설정과 생성 연동 #184
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
Yelihi
merged 1 commit into
main
from
fs/grammar-home-refresh-20261012/difficulty-setting
Oct 18, 2026
Merged
Changes from all commits
Commits
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
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
57 changes: 57 additions & 0 deletions
57
src/_tests/views/my-page/GrammarDifficultySetting.test.tsx
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,57 @@ | ||
| import { fireEvent, render, screen, waitFor } from "@testing-library/react"; | ||
| import { describe, expect, it, jest, beforeEach } from "@jest/globals"; | ||
|
|
||
| let GrammarDifficultySetting: typeof import("@/views/my-page/ui/GrammarDifficultySetting").GrammarDifficultySetting; | ||
| let saveGrammarDifficulty: typeof import("@/views/my-page/services/actions/saveGrammarDifficulty").saveGrammarDifficulty; | ||
|
|
||
| jest.mock("@/views/my-page/services/actions/saveGrammarDifficulty", () => ({ | ||
| saveGrammarDifficulty: jest.fn(), | ||
| })); | ||
|
|
||
| beforeEach(async () => { | ||
| ({ GrammarDifficultySetting } = await import("@/views/my-page/ui/GrammarDifficultySetting")); | ||
| ({ saveGrammarDifficulty } = | ||
| await import("@/views/my-page/services/actions/saveGrammarDifficulty")); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("마이페이지 예문 난이도 설정", () => { | ||
| it("저장된 난이도를 처음부터 선택해 보여준다", () => { | ||
| render(<GrammarDifficultySetting initialDifficulty={3} />); | ||
| expect(screen.getByRole("radio", { name: /3단계/ })).toBeChecked(); | ||
| }); | ||
|
|
||
| it("기본값 2에서 1~3 중 하나를 저장하고 요청 중에는 다시 변경하거나 저장할 수 없다", async () => { | ||
| let finish!: (result: { ok: true }) => void; | ||
| jest.mocked(saveGrammarDifficulty).mockImplementation( | ||
| () => | ||
| new Promise((resolve) => { | ||
| finish = resolve; | ||
| }), | ||
| ); | ||
| render(<GrammarDifficultySetting initialDifficulty={2} />); | ||
|
|
||
| expect(screen.getByRole("radio", { name: /2단계/ })).toBeChecked(); | ||
| fireEvent.click(screen.getByRole("radio", { name: /3단계/ })); | ||
| fireEvent.click(screen.getByRole("button", { name: /저장/ })); | ||
|
|
||
| expect(saveGrammarDifficulty).toHaveBeenCalledWith(3); | ||
| expect(screen.getByRole("button", { name: /저장/ })).toBeDisabled(); | ||
| expect(screen.getByRole("radio", { name: /1단계/ })).toBeDisabled(); | ||
| finish({ ok: true }); | ||
| await waitFor(() => expect(screen.getByRole("button", { name: /저장/ })).toBeEnabled()); | ||
| expect(screen.getByRole("radio", { name: /3단계/ })).toBeChecked(); | ||
| }); | ||
|
|
||
| it("저장 실패를 알리고 선택한 난이도를 유지해 재시도할 수 있다", async () => { | ||
| jest.mocked(saveGrammarDifficulty).mockResolvedValue({ ok: false, code: "FAILED" }); | ||
| render(<GrammarDifficultySetting initialDifficulty={1} />); | ||
|
|
||
| fireEvent.click(screen.getByRole("radio", { name: /2단계/ })); | ||
| fireEvent.click(screen.getByRole("button", { name: /저장/ })); | ||
|
|
||
| expect(await screen.findByRole("alert")).toHaveTextContent(/저장/); | ||
| expect(screen.getByRole("radio", { name: /2단계/ })).toBeChecked(); | ||
| expect(screen.getByRole("button", { name: /저장/ })).toBeEnabled(); | ||
| }); | ||
| }); |
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,93 @@ | ||
| import { beforeEach, describe, expect, it, jest } from "@jest/globals"; | ||
| import type { createSupabaseServerClient as CreateClient } from "@/shared/lib/supabase/server"; | ||
| import type { UserId } from "@/entities/value-object"; | ||
|
|
||
| let createSupabaseServerClient: typeof CreateClient; | ||
|
|
||
| jest.mock("server-only", () => ({})); | ||
| jest.mock("@/shared/lib/supabase/server", () => ({ createSupabaseServerClient: jest.fn() })); | ||
|
|
||
| const save = jest.fn<() => Promise<void>>(); | ||
|
|
||
| beforeEach(async () => { | ||
| ({ createSupabaseServerClient } = await import("@/shared/lib/supabase/server")); | ||
| jest.restoreAllMocks(); | ||
| jest.clearAllMocks(); | ||
| jest.mocked(createSupabaseServerClient).mockResolvedValue({ | ||
| auth: { getUser: async () => ({ data: { user: { id: "owner" } }, error: null }) }, | ||
| } as never); | ||
| save.mockResolvedValue(); | ||
| const { UserProfileRepository } = await import("@/entities/user-profile"); | ||
| jest.spyOn(UserProfileRepository.prototype, "saveDifficulty").mockImplementation(save); | ||
| }); | ||
|
|
||
| describe("예문 난이도 저장 액션", () => { | ||
| it.each([0, 4, "2", null])("범위 밖 입력 %p를 저장하지 않는다", async (value) => { | ||
| const { saveGrammarDifficulty } = | ||
| await import("@/views/my-page/services/actions/saveGrammarDifficulty"); | ||
| expect(await saveGrammarDifficulty(value)).toEqual({ ok: false, code: "INVALID" }); | ||
| expect(save).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("로그인하지 않은 사용자는 프로필을 수정할 수 없다", async () => { | ||
| jest.mocked(createSupabaseServerClient).mockResolvedValue({ | ||
| auth: { getUser: async () => ({ data: { user: null }, error: null }) }, | ||
| } as never); | ||
| const { saveGrammarDifficulty } = | ||
| await import("@/views/my-page/services/actions/saveGrammarDifficulty"); | ||
|
|
||
| expect(await saveGrammarDifficulty(3)).toEqual({ ok: false, code: "UNAUTHORIZED" }); | ||
| expect(save).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("인증된 본인의 난이도만 저장하고 저장 오류는 실패로 반환한다", async () => { | ||
| const { saveGrammarDifficulty } = | ||
| await import("@/views/my-page/services/actions/saveGrammarDifficulty"); | ||
| expect(await saveGrammarDifficulty(3)).toEqual({ ok: true }); | ||
| expect(save).toHaveBeenCalledWith("owner", 3); | ||
|
|
||
| save.mockRejectedValueOnce(new Error("database unavailable")); | ||
| expect(await saveGrammarDifficulty(1)).toEqual({ ok: false, code: "FAILED" }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("예문 난이도 저장소", () => { | ||
| it("기존 프로필의 다른 필드를 변경하지 않고 난이도만 갱신한다", async () => { | ||
| const { UserProfileRepository } = await import("@/entities/user-profile"); | ||
| jest.restoreAllMocks(); | ||
| const row = { | ||
| id: "owner", | ||
| display_name: "Yeli", | ||
| example_difficulty: 1, | ||
| created_at: "2026-10-12T00:00:00Z", | ||
| updated_at: "2026-10-12T00:00:00Z", | ||
| }; | ||
| const selectQuery = { | ||
| eq: jest.fn().mockReturnValue({ maybeSingle: async () => ({ data: row, error: null }) }), | ||
| }; | ||
| const updateQuery = { eq: jest.fn(async () => ({ error: null })) }; | ||
| const update = jest.fn().mockReturnValue(updateQuery); | ||
| const client = { from: jest.fn().mockReturnValue({ select: () => selectQuery, update }) }; | ||
| const repository = new UserProfileRepository(client as never); | ||
|
|
||
| await repository.saveDifficulty("owner" as UserId, 3); | ||
|
|
||
| expect(client.from).toHaveBeenCalledWith("user_profiles"); | ||
| expect(update).toHaveBeenCalledWith({ example_difficulty: 3 }); | ||
| expect(updateQuery.eq).toHaveBeenCalledWith("id", "owner"); | ||
| }); | ||
|
|
||
| it("프로필이 없는 사용자도 다른 필드를 덮어쓰지 않고 새 난이도를 저장한다", async () => { | ||
| const { UserProfileRepository } = await import("@/entities/user-profile"); | ||
| jest.restoreAllMocks(); | ||
| const selectQuery = { | ||
| eq: jest.fn().mockReturnValue({ maybeSingle: async () => ({ data: null, error: null }) }), | ||
| }; | ||
| const insert = jest.fn(async () => ({ error: null })); | ||
| const client = { from: jest.fn().mockReturnValue({ select: () => selectQuery, insert }) }; | ||
|
|
||
| await new UserProfileRepository(client as never).saveDifficulty("owner" as UserId, 2); | ||
|
|
||
| expect(insert).toHaveBeenCalledWith({ id: "owner", example_difficulty: 2 }); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -1,8 +1,14 @@ | ||
| import type { UserId } from "@/entities/value-object"; | ||
| import { z } from "zod"; | ||
|
|
||
| export const grammarDifficultySchema = z.union([z.literal(1), z.literal(2), z.literal(3)]); | ||
| export type GrammarDifficulty = z.infer<typeof grammarDifficultySchema>; | ||
| export const DEFAULT_GRAMMAR_DIFFICULTY: GrammarDifficulty = 2; | ||
|
|
||
| export interface EchoUserProfile { | ||
| readonly id: UserId; | ||
| readonly displayName: string | null; | ||
| readonly exampleDifficulty: GrammarDifficulty; | ||
| readonly createdAt: Date; | ||
| readonly updatedAt: Date; | ||
| } |
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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import type { EchoUserProfile } from "@/entities/user-profile/models/entity"; | ||
| import type { UserId } from "@/entities/value-object"; | ||
| import type { GrammarDifficulty } from "./entity"; | ||
|
|
||
| export interface UserProfileRepositoryPort { | ||
| findById(id: UserId): Promise<EchoUserProfile | null>; | ||
| saveDifficulty(id: UserId, difficulty: GrammarDifficulty): Promise<void>; | ||
| } |
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.
When a user without a profile submits their first setting concurrently from two tabs or devices, both calls can observe
data === nulland attempt this insert; one then fails on theuser_profilesprimary key and reports a save failure even though the input was valid. Use an atomic upsert that updates onlyexample_difficulty, or otherwise handle the duplicate-key race.Useful? React with 👍 / 👎.