Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
} from "@/features/grammar-example-generation";
import { createEditorNote } from "@/_tests/features/grammar-note-editor/fixtures";
import { createExampleCandidates } from "@/_tests/features/grammar-example-generation/fixtures";

const note = createEditorNote();

const meta = {
title: "features/grammar-example-generation/GrammarExampleManager",
component: GrammarExampleManager,
Expand All @@ -31,12 +33,17 @@ const meta = {
),
],
} satisfies Meta<typeof GrammarExampleManager>;

export default meta;

type Story = StoryObj<typeof meta>;

export const Empty: Story = {};

export const SelectAndSave: Story = {
play: async ({ canvasElement, args }) => {
const canvas = within(canvasElement);

await userEvent.click(canvas.getByRole("button", { name: "예문 3개 생성" }));
await expect(await canvas.findAllByRole("checkbox")).toHaveLength(3);
await userEvent.click(canvas.getAllByRole("checkbox")[0]);
Expand All @@ -45,30 +52,36 @@ export const SelectAndSave: Story = {
await expect(canvas.getAllByRole("checkbox")).toHaveLength(2);
},
};

export const Retry: Story = {
args: {
generate: fn<GrammarExampleManagerProps["generate"]>(async () => ({
ok: true,
data: createExampleCandidates(),
})),
},

play: async ({ canvasElement, args }) => {
args.generate.mockResolvedValueOnce({
ok: false,
message: "생성에 실패했습니다. 다시 생성해 주세요.",
code: "FAILED",
});
const canvas = within(canvasElement);

await userEvent.click(canvas.getByRole("button", { name: "예문 3개 생성" }));
await expect(await canvas.findByRole("alert")).toBeVisible();
await userEvent.click(canvas.getByRole("button", { name: "예문 다시 생성" }));
await expect(await canvas.findAllByRole("checkbox")).toHaveLength(3);
},
};

export const EditCandidate: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

await userEvent.click(canvas.getByRole("button", { name: "예문 3개 생성" }));
const input = (await canvas.findAllByLabelText("한국어 뜻"))[0];

await userEvent.clear(input);
await userEvent.type(input, "그는 운전사가 아니라 교사입니다.");
await expect(input).toHaveValue("그는 운전사가 아니라 교사입니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import type { GrammarNoteEditorProps } from "@/features/grammar-note-editor";
import { GrammarNoteEditor } from "@/features/grammar-note-editor";
import { GrammarAnalysisEditor } from "@/features/grammar-analysis-edit";
import { createEditorNote } from "@/_tests/features/grammar-note-editor/fixtures";

const note = createEditorNote();

const meta = {
title: "features/grammar-note-editor/GrammarNoteEditor",
component: GrammarNoteEditor,
Expand All @@ -29,36 +31,46 @@ const meta = {
),
],
} satisfies Meta<typeof GrammarNoteEditor>;

export default meta;

type Story = StoryObj<typeof meta>;

export const EmptyInput: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

await userEvent.click(canvas.getByRole("button", { name: "문장 분석하기" }));
await expect(canvas.getAllByRole("alert")).toHaveLength(2);
await expect(canvas.queryByLabelText("제목")).not.toBeInTheDocument();
},
};

export const ReviewAndSave: Story = {
args: { initialNote: note },

play: async ({ canvasElement, args }) => {
const canvas = within(canvasElement);

await expect(canvas.getByRole("button", { name: "검토 완료 · 노트 저장" })).toBeDisabled();
await userEvent.click(canvas.getByRole("checkbox"));
await userEvent.click(canvas.getByRole("button", { name: "검토 완료 · 노트 저장" }));
await expect(args.onSaved).toHaveBeenCalled();
},
};

export const SaveFailure: Story = {
args: {
initialNote: note,
save: fn<GrammarNoteEditorProps["save"]>(async () => ({
ok: false as const,
message: "노트를 저장하지 못했습니다. 다시 저장해 주세요.",
code: "PERSISTENCE_FAILED",
})),
},

play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

await userEvent.click(canvas.getByRole("checkbox"));
await userEvent.click(canvas.getByRole("button", { name: "검토 완료 · 노트 저장" }));
await expect(await canvas.findByRole("alert")).toBeVisible();
Expand All @@ -70,6 +82,7 @@ export const SaveFailure: Story = {
export const AnalyzeThenReview: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

await userEvent.type(canvas.getByLabelText(/영어 문장/), note.source.sentence);
await userEvent.type(canvas.getByLabelText(/핵심 어법 설명/), note.source.learningNote);
await userEvent.click(canvas.getByRole("button", { name: "문장 분석하기" }));
Expand All @@ -78,10 +91,13 @@ export const AnalyzeThenReview: Story = {
await expect(canvas.getByLabelText(/영어 문장/)).toHaveValue(note.source.sentence);
},
};

export const UnappliedAnalysisBlocksSave: Story = {
args: { initialNote: note },

play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

await userEvent.click(canvas.getByRole("button", { name: "She" }));
await userEvent.click(canvas.getByRole("button", { name: "분석 수정" }));
await userEvent.type(canvas.getByLabelText("직독직해"), " 수정");
Expand Down
57 changes: 57 additions & 0 deletions src/_storybook/views/grammar-library/GrammarNoteList.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { Meta, StoryObj } from "@storybook/nextjs-vite";
import { expect, within } from "storybook/test";
import { GrammarNoteList } from "@/views/grammar-library";

const meta = {
title: "Views/Grammar/Note list",
component: GrammarNoteList,
args: { query: "", data: { items: [], total: 0, page: 1, pageSize: 20 } },
} satisfies Meta<typeof GrammarNoteList>;

export default meta;

type Story = StoryObj<typeof meta>;

export const Empty: Story = {
play: async ({ canvasElement }) => {
await expect(within(canvasElement).getByText("첫 어법 노트를 만들어보세요")).toBeVisible();
},
};

export const SearchEmpty: Story = { args: { query: "unknown" } };

export const Notes: Story = {
args: {
query: "not",
data: {
page: 2,
pageSize: 20,
total: 42,
items: [
{
id: "00000000-0000-4000-8000-000000000001",
ownerId: "00000000-0000-4000-8000-000000000002",
title: "not A but B",
sentence: "She is not a teacher but a doctor.",
tags: ["대조", "보어"],
version: 1,
createdAt: "2026-10-01T00:00:00Z",
updatedAt: "2026-10-01T00:00:00Z",
},
],
},
},

play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

await expect(canvas.getByRole("link", { name: /not A but B/ })).toHaveAttribute(
"href",
expect.stringContaining("returnTo=%2Fgrammar%3Fq%3Dnot%26page%3D2"),
);
await expect(canvas.getByRole("link", { name: "2페이지" })).toHaveAttribute(
"aria-current",
"page",
);
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe("grammar analysis review below the sentence", () => {
it("selects a chunk and edits its explanation without losing the source", async () => {
const user = userEvent.setup();
const onChange = jest.fn();

render(<GrammarAnalysisEditor initialAnalysis={createGrammarAnalysis()} onChange={onChange} />);
await user.click(screen.getByRole("button", { name: "She" }));
await user.click(screen.getByRole("button", { name: "분석 수정" }));
Expand All @@ -22,9 +23,11 @@ describe("grammar analysis review below the sentence", () => {
}),
);
});

it("keeps an invalid hierarchy draft visible with a recoverable error", async () => {
const user = userEvent.setup();
const onChange = jest.fn();

render(<GrammarAnalysisEditor initialAnalysis={createGrammarAnalysis()} onChange={onChange} />);
await user.click(screen.getByRole("button", { name: "분석 수정" }));
await user.click(screen.getByRole("button", { name: "절" }));
Expand All @@ -41,6 +44,7 @@ describe("grammar analysis review below the sentence", () => {

it("asks before leaving an unapplied edit and preserves input when continuing", async () => {
const user = userEvent.setup();

render(<GrammarAnalysisEditor initialAnalysis={createGrammarAnalysis()} onChange={jest.fn()} />);
await user.click(screen.getByRole("button", { name: "She" }));
await user.click(screen.getByRole("button", { name: "분석 수정" }));
Expand All @@ -57,6 +61,7 @@ it("asks before leaving an unapplied edit and preserves input when continuing",
it("shows AI chunks for reading and keeps correction controls behind edit mode", async () => {
const user = userEvent.setup();
const onChange = jest.fn();

render(<GrammarAnalysisEditor initialAnalysis={createGrammarAnalysis()} onChange={onChange} />);
expect(screen.queryByRole("button", { name: "절" })).not.toBeInTheDocument();
await user.click(screen.getByRole("button", { name: "She" }));
Expand All @@ -72,6 +77,7 @@ it("shows AI chunks for reading and keeps correction controls behind edit mode",
it("guards returning to reading and restores the selected chunk after discarding edits", async () => {
const user = userEvent.setup();
const onChange = jest.fn();

render(<GrammarAnalysisEditor initialAnalysis={createGrammarAnalysis()} onChange={onChange} />);
await user.click(screen.getByRole("button", { name: "She" }));
await user.click(screen.getByRole("button", { name: "분석 수정" }));
Expand All @@ -92,6 +98,7 @@ it("isolates selection and edits between mounted editors", async () => {
const user = userEvent.setup();
const firstChange = jest.fn();
const secondChange = jest.fn();

render(
<>
<section aria-label="첫 번째 노트">
Expand All @@ -104,6 +111,7 @@ it("isolates selection and edits between mounted editors", async () => {
);
const first = within(screen.getByRole("region", { name: "첫 번째 노트" }));
const second = within(screen.getByRole("region", { name: "두 번째 노트" }));

await user.click(first.getByRole("button", { name: "She" }));
expect(second.getByRole("button", { name: "She" })).toHaveAttribute("aria-pressed", "false");
await user.click(first.getByRole("button", { name: "분석 수정" }));
Expand All @@ -118,16 +126,32 @@ it("preserves drafts on parent rerender and notifies the current callback", asyn
const user = userEvent.setup();
const original = jest.fn();
const current = jest.fn();
const originalDirty = jest.fn();
const currentDirty = jest.fn();
const { rerender } = render(
<GrammarAnalysisEditor initialAnalysis={createGrammarAnalysis()} onChange={original} />,
<GrammarAnalysisEditor
initialAnalysis={createGrammarAnalysis()}
onChange={original}
onDirtyChange={originalDirty}
/>,
);

await user.click(screen.getByRole("button", { name: "분석 수정" }));
await user.type(screen.getByLabelText("직독직해"), " 수정");
rerender(<GrammarAnalysisEditor initialAnalysis={createGrammarAnalysis()} onChange={current} />);
expect(originalDirty.mock.calls).toEqual([[true]]);
rerender(
<GrammarAnalysisEditor
initialAnalysis={createGrammarAnalysis()}
onChange={current}
onDirtyChange={currentDirty}
/>,
);
expect(screen.getByLabelText("직독직해")).toHaveValue("그녀는 수정");
await user.click(screen.getByRole("button", { name: "풀이 적용" }));
expect(original).not.toHaveBeenCalled();
expect(current).toHaveBeenCalledTimes(1);
expect(originalDirty.mock.calls).toEqual([[true]]);
expect(currentDirty.mock.calls).toEqual([[false]]);
});

it("starts a fresh editor when the document key changes", async () => {
Expand All @@ -140,9 +164,11 @@ it("starts a fresh editor when the document key changes", async () => {
onChange={onChange}
/>,
);

await user.click(screen.getByRole("button", { name: "분석 수정" }));
await user.type(screen.getByLabelText("직독직해"), " 수정");
const next = createGrammarAnalysis();

rerender(
<GrammarAnalysisEditor
key="note-2"
Expand All @@ -161,6 +187,7 @@ it("starts a fresh editor when the document key changes", async () => {

it("keeps the editor draft when Escape dismisses the confirmation dialog", async () => {
const user = userEvent.setup();

render(<GrammarAnalysisEditor initialAnalysis={createGrammarAnalysis()} onChange={jest.fn()} />);
await user.click(screen.getByRole("button", { name: "분석 수정" }));
await user.type(screen.getByLabelText("직독직해"), " 수정");
Expand All @@ -174,9 +201,11 @@ it("keeps the editor draft when Escape dismisses the confirmation dialog", async
it("applies current text with a boundary edit and keeps input after validation failure", async () => {
const user = userEvent.setup();
const onChange = jest.fn();

render(<GrammarAnalysisEditor initialAnalysis={createGrammarAnalysis()} onChange={onChange} />);
await user.click(screen.getByRole("button", { name: "분석 수정" }));
const input = screen.getByLabelText("직독직해");

// 도메인 길이 제한을 넘는 입력에도 다른 편집 후보를 반영하지 않는다.
fireEvent.change(input, { target: { value: "x".repeat(4001) } });
await user.selectOptions(screen.getByLabelText("다음 구간과의 경계"), "3");
Expand Down
Loading