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
31 changes: 10 additions & 21 deletions tests/components/notes/NoteCreateContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from "@/hooks/queries/notes";
import NoteCreateContainer from "@/app/(protected)/notes/_components/NoteCreateContainer";
import { renderWithQueryClient } from "tests/test-utils";
import { draftNoteStorage } from "@/app/(protected)/notes/_utils/draft-note";

const replaceMock = jest.fn();

Expand Down Expand Up @@ -175,6 +174,16 @@ describe("NoteCreateContainer", () => {
});

describe("๋…ธํŠธ ๋“ฑ๋ก ๊ธฐ๋Šฅ", () => {
const mockNoteContent = JSON.stringify({
type: "doc",
content: [
{
type: "paragraph",
content: [{ type: "text", text: "์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ ๋‚ด์šฉ" }],
},
],
});

it("๋“ฑ๋ก ์„ฑ๊ณต ์‹œ toast ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค", () => {
const { mockCreateNoteMutation } = setup();

Expand All @@ -185,16 +194,6 @@ describe("NoteCreateContainer", () => {
});
});

const mockNoteContent = JSON.stringify({
type: "doc",
content: [
{
type: "paragraph",
content: [{ type: "text", text: "์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ ๋‚ด์šฉ" }],
},
],
});

mockCreateNoteMutation(
{
todoId: 1,
Expand All @@ -217,16 +216,6 @@ describe("NoteCreateContainer", () => {
onError(new Error("Network error"));
});

const mockNoteContent = JSON.stringify({
type: "doc",
content: [
{
type: "paragraph",
content: [{ type: "text", text: "์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ ๋‚ด์šฉ" }],
},
],
});

mockCreateNoteMutation(
{
todoId: 1,
Expand Down
276 changes: 276 additions & 0 deletions tests/components/notes/NoteEditContainer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
import { screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { toast } from "@/lib/toast";
import {
useNoteQuery,
useUpdateNoteMutation,
useLinkMetadataMutation,
} from "@/hooks/queries/notes";
import NoteEditContainer from "@/app/(protected)/notes/_components/NoteEditContainer";
import { renderWithQueryClient } from "tests/test-utils";

const replaceMock = jest.fn();

jest.mock("next/navigation", () => ({
useRouter: () => ({ replace: replaceMock }),
}));

jest.mock("@/lib/toast");
jest.mock("@/hooks/queries/notes");

const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: (key: string) => store[key] || null,
setItem: (key: string, value: string) => {
store[key] = value;
},
removeItem: (key: string) => {
delete store[key];
},
clear: () => {
store = {};
},
};
})();

Object.defineProperty(window, "localStorage", {
value: localStorageMock,
});

describe("NoteEditContainer", () => {
const setup = (noteId = 1) => {
const mockNote = {
id: 1,
title: "์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ",
content: JSON.stringify({
type: "doc",
content: [
{
type: "paragraph",
content: [{ type: "text", text: "์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ ๋‚ด์šฉ" }],
},
],
}),
linkUrl: "https://ko.wikipedia.org/wiki/์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ",
linkMetadata: {
url: "https://ko.wikipedia.org/wiki/์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ",
title: "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ - ์œ„ํ‚ค๋ฐฑ๊ณผ",
description:
"์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๊ฐ์ฒด ๊ธฐ๋ฐ˜์˜ ์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ž…๋‹ˆ๋‹ค.",
image: null,
},
todo: {
id: 1,
title: "ํ•  ์ผ 1",
done: false,
fileUrl: null,
linkUrl: null,
},
goal: {
id: 1,
title: "๋ชฉํ‘œ 1",
},
createdAt: "2026-01-18T09:00:00Z",
updatedAt: "2026-01-18T10:00:00Z",
teamId: "team-5",
userId: 1,
};

const mockUpdateMutation = jest.fn();
const mockLinkMetadataMutation = jest.fn();

jest.mocked(useNoteQuery).mockReturnValue({
data: mockNote,
} as unknown as ReturnType<typeof useNoteQuery>);

jest.mocked(useUpdateNoteMutation).mockReturnValue({
mutate: mockUpdateMutation,
isPending: false,
} as unknown as ReturnType<typeof useUpdateNoteMutation>);

jest.mocked(useLinkMetadataMutation).mockReturnValue({
mutate: mockLinkMetadataMutation,
} as unknown as ReturnType<typeof useLinkMetadataMutation>);

renderWithQueryClient(<NoteEditContainer noteId={noteId} />);

return {
mockNote,
mockUpdateMutation,
mockLinkMetadataMutation,
};
};

beforeEach(() => {
jest.clearAllMocks();
localStorageMock.clear();
});

describe("๊ธฐ๋ณธ UI ๋ Œ๋”๋ง", () => {
it("์ œ๋ชฉ ์ž…๋ ฅ ํ•„๋“œ์— ๊ธฐ์กด ์ œ๋ชฉ์ด ํ‘œ์‹œ๋œ๋‹ค", () => {
setup();

const titleInput =
screen.getByPlaceholderText("๋…ธํŠธ์˜ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
expect(titleInput).toBeInTheDocument();
expect(titleInput).toHaveValue("์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ");
});

it("์ˆ˜์ •ํ•˜๊ธฐ ๋ฒ„ํŠผ์ด ํ™œ์„ฑํ™”๋˜์–ด ์žˆ๋‹ค", () => {
setup();

const desktopButton = screen.getByRole("button", { name: "์ˆ˜์ •ํ•˜๊ธฐ" });
const mobileButton = screen.getByRole("button", { name: "์ˆ˜์ •" });

expect(desktopButton).toBeEnabled();
expect(mobileButton).toBeEnabled();
});
});

describe("์ž…๋ ฅ ๋™์ž‘", () => {
it("์ œ๋ชฉ ์ˆ˜์ • ์‹œ ๋ณ€๊ฒฝ๋œ ๊ฐ’์ด ์ •์ƒ ๋ฐ˜์˜๋œ๋‹ค", async () => {
const user = userEvent.setup();
setup();

const titleInput = screen.getByDisplayValue("์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ");

await user.clear(titleInput);
await user.type(titleInput, "์ˆ˜์ •๋œ ์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ");

expect(titleInput).toHaveValue("์ˆ˜์ •๋œ ์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ");
});

it("๋งํฌ ์ˆ˜์ • ์‹œ ๋ณ€๊ฒฝ๋œ URL๋กœ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ fetch๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค", async () => {
const user = userEvent.setup();
const { mockLinkMetadataMutation } = setup();

const linkButtons = screen.getAllByRole("button", {
name: "๋งํฌ ์—…๋กœ๋“œ",
});
await user.click(linkButtons[0]);

const urlInput = screen.getByDisplayValue(
"https://ko.wikipedia.org/wiki/์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ",
);

await user.clear(urlInput);
await user.type(
urlInput,
"https://developer.mozilla.org/ko/docs/Web/JavaScript",
);

const confirmButton = screen.getByRole("button", { name: "ํ™•์ธ" });
await user.click(confirmButton);

expect(mockLinkMetadataMutation).toHaveBeenCalledWith(
"https://developer.mozilla.org/ko/docs/Web/JavaScript",
expect.any(Object),
);
});
});

describe("์ž„์‹œ์ €์žฅ ๊ธฐ๋Šฅ", () => {
it("์ž„์‹œ์ €์žฅ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ localStorage์— ์ €์žฅ๋œ๋‹ค", async () => {
const user = userEvent.setup();
setup();

const titleInput = screen.getByDisplayValue("์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ");
await user.clear(titleInput);
await user.type(titleInput, "์ž„์‹œ์ €์žฅ ํ…Œ์ŠคํŠธ");

const draftButtons = screen.getAllByRole("button", { name: "์ž„์‹œ์ €์žฅ" });
await user.click(draftButtons[0]);

const saved = localStorageMock.getItem("draft_note_1");
expect(saved).toBeTruthy();

const draft = JSON.parse(saved!);
expect(draft.title).toBe("์ž„์‹œ์ €์žฅ ํ…Œ์ŠคํŠธ");
expect(draft.todoId).toBe(1);
});

it("์ž„์‹œ์ €์žฅ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ toast ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค", async () => {
const user = userEvent.setup();
setup();

const titleInput = screen.getByDisplayValue("์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ");
await user.clear(titleInput);
await user.type(titleInput, "์ž„์‹œ์ €์žฅ ํ…Œ์ŠคํŠธ");

const draftButtons = screen.getAllByRole("button", { name: "์ž„์‹œ์ €์žฅ" });
await user.click(draftButtons[0]);

expect(toast.success).toHaveBeenCalledWith("์ž„์‹œ ์ €์žฅ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค", {
hasTime: true,
});
});
});

describe("๋…ธํŠธ ์ˆ˜์ • ๊ธฐ๋Šฅ", () => {
const mockNoteContent = JSON.stringify({
type: "doc",
content: [
{
type: "paragraph",
content: [{ type: "text", text: "์ˆ˜์ •๋œ ๋…ธํŠธ ๋‚ด์šฉ" }],
},
],
});

it("์ˆ˜์ • ์„ฑ๊ณต ์‹œ toast ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค", () => {
const { mockUpdateMutation } = setup();

mockUpdateMutation.mockImplementation((data, { onSuccess }) => {
onSuccess({
id: 1,
...data,
});
});

mockUpdateMutation(
{
noteId: 1,
data: {
title: "์ˆ˜์ •๋œ ์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ",
content: mockNoteContent,
linkUrl: "https://developer.mozilla.org/ko/docs/Web/JavaScript",
},
},
{
onSuccess: () => {
toast.success("๋…ธํŠธ๊ฐ€ ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
},
},
);

expect(toast.success).toHaveBeenCalledWith("๋…ธํŠธ๊ฐ€ ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
});

it("์ˆ˜์ • ์‹คํŒจ ์‹œ ์—๋Ÿฌ toast๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค", () => {
const { mockUpdateMutation } = setup();

mockUpdateMutation.mockImplementation((data, { onError }) => {
onError(new Error("Network error"));
});

mockUpdateMutation(
{
noteId: 1,
data: {
title: "์ˆ˜์ •๋œ ์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ",
content: mockNoteContent,
linkUrl: "https://developer.mozilla.org/ko/docs/Web/JavaScript",
},
},
{
onError: () => {
toast.error("๋…ธํŠธ ์ˆ˜์ •์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.");
},
},
);

expect(toast.error).toHaveBeenCalledWith("๋…ธํŠธ ์ˆ˜์ •์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.");
});
});
});