Skip to content
Merged
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
246 changes: 246 additions & 0 deletions tests/components/notes/NoteCreateContainer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
import { screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { toast } from "@/lib/toast";
import { useTodoQuery } from "@/hooks/queries/todos";
import {
useCreateNoteMutation,
useLinkMetadataMutation,
} 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();

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

jest.mock("@/lib/toast");
jest.mock("@/hooks/queries/todos");
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("NoteCreateContainer", () => {
const setup = (todoId = 1) => {
const mockTodo = {
goal: {
id: 1,
title: "๋ชฉํ‘œ 1",
},
title: "ํ•  ์ผ 1",
done: false,
updatedAt: "2025-01-18T10:00:00Z",
};

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

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

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

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

renderWithQueryClient(<NoteCreateContainer todoId={todoId} />);

return {
mockTodo,
mockCreateNoteMutation,
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).toBeDisabled();
expect(mobileButton).toBeDisabled();
});
});

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

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

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

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

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

const urlInput = screen.getByPlaceholderText("https://www.example.com");
await user.type(urlInput, "https://ko.wikipedia.org/wiki/์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ");

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

expect(mockLinkMetadataMutation).toHaveBeenCalledWith(
"https://ko.wikipedia.org/wiki/์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ",
expect.any(Object),
);
});
});

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

const titleInput =
screen.getByPlaceholderText("๋…ธํŠธ์˜ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
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.getByPlaceholderText("๋…ธํŠธ์˜ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
await user.type(titleInput, "์ž„์‹œ์ €์žฅ ํ…Œ์ŠคํŠธ");

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

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

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

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

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

mockCreateNoteMutation(
{
todoId: 1,
title: "์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ",
content: mockNoteContent,
},
{
onSuccess: () => {
toast.success("๋…ธํŠธ๊ฐ€ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
},
},
);

expect(toast.success).toHaveBeenCalledWith("๋…ธํŠธ๊ฐ€ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
});
it("๋“ฑ๋ก ์‹คํŒจ ์‹œ ์—๋Ÿฌ toast๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค", () => {
const { mockCreateNoteMutation } = setup();

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

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

mockCreateNoteMutation(
{
todoId: 1,
title: "์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ",
content: mockNoteContent,
},
{
onError: () => {
toast.error("๋…ธํŠธ ๋“ฑ๋ก์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.");
},
},
);

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