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
95 changes: 95 additions & 0 deletions tests/components/notes/NoteList.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import NoteList from "@/app/(protected)/notes/_components/NoteList";
import { Note } from "@/hooks/queries/notes";

describe("NoteList", () => {
const mockNotes: Note[] = [
{
id: 1,
title: "์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ",
todo: { id: 1, title: "ํ•  ์ผ 1", done: false },
updatedAt: "2026-01-15T10:00:00Z",
},
{
id: 2,
title: "๋‘ ๋ฒˆ์งธ ๋…ธํŠธ",
todo: { id: 2, title: "ํ•  ์ผ 2", done: true },
updatedAt: "2026-01-16T10:00:00Z",
},
];

const mockOnEditNote = jest.fn();
const mockOnDeleteNote = jest.fn();

const renderNoteList = () => {
return render(
<NoteList
notes={mockNotes}
onEditNote={mockOnEditNote}
onDeleteNote={mockOnDeleteNote}
/>,
);
};

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

describe("๋ Œ๋”๋ง", () => {
it("๋…ธํŠธ ๋ชฉ๋ก์ด ํ™”๋ฉด์— ๋ Œ๋”๋ง๋œ๋‹ค", () => {
renderNoteList();

expect(screen.getByText("์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ")).toBeInTheDocument();
expect(screen.getByText("๋‘ ๋ฒˆ์งธ ๋…ธํŠธ")).toBeInTheDocument();
});

it("๋…ธํŠธ๊ฐ€ ์ตœ์‹ ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค", () => {
renderNoteList();

const articles = screen.getAllByRole("article");
expect(articles[0]).toHaveTextContent("๋‘ ๋ฒˆ์งธ ๋…ธํŠธ");
expect(articles[1]).toHaveTextContent("์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ");
});

it("๋…ธํŠธ ํด๋ฆญ ์‹œ ์ƒ์„ธ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋Š” ๋งํฌ๊ฐ€ ์žˆ๋‹ค", () => {
renderNoteList();

const links = screen.getAllByRole("link");
expect(links[0]).toHaveAttribute("href", "/notes/2");
expect(links[1]).toHaveAttribute("href", "/notes/1");
});
});

describe("์‚ฌ์šฉ์ž ์ธํ„ฐ๋ž™์…˜", () => {
it("์ˆ˜์ • ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ onEditNote๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค", async () => {
const user = userEvent.setup();

renderNoteList();

const dropdownButtons = screen.getAllByLabelText("๋…ธํŠธ ์˜ต์…˜ ๋ฉ”๋‰ด");
await user.click(dropdownButtons[0]);

const editButton = screen.getByText("์ˆ˜์ •ํ•˜๊ธฐ");
await user.click(editButton);

expect(mockOnEditNote).toHaveBeenCalledWith(2);
expect(mockOnEditNote).toHaveBeenCalledTimes(1);
});

it("์‚ญ์ œ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ onDeleteNote๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค", async () => {
const user = userEvent.setup();

renderNoteList();

const dropdownButtons = screen.getAllByLabelText("๋…ธํŠธ ์˜ต์…˜ ๋ฉ”๋‰ด");
await user.click(dropdownButtons[0]);

const deleteButton = screen.getByText("์‚ญ์ œํ•˜๊ธฐ");
await user.click(deleteButton);

expect(mockOnDeleteNote).toHaveBeenCalledWith(2);
expect(mockOnDeleteNote).toHaveBeenCalledTimes(1);
});
});
});
66 changes: 66 additions & 0 deletions tests/components/notes/NoteListContainer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { screen } from "@testing-library/react";
import { useNotesQuery, useDeleteNoteMutation } from "@/hooks/queries/notes";
import NoteListContainer from "@/app/(protected)/notes/_components/NoteListContainer";
import { renderWithQueryClient } from "tests/test-utils";

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

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

describe("NoteListContainer", () => {
const goalId = 1;

beforeEach(() => {
jest.clearAllMocks();

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

describe("๋…ธํŠธ๊ฐ€ ์—†์„ ๋•Œ", () => {
it("๋…ธํŠธ๊ฐ€ ์—†์œผ๋ฉด EmptyState๋ฅผ ๋ Œ๋”๋งํ•œ๋‹ค", () => {
jest.mocked(useNotesQuery).mockReturnValue({
data: {
totalCount: 0,
nextCursor: null,
goal: { id: goalId, title: "๋ชฉํ‘œ 1" },
notes: [],
},
} as unknown as ReturnType<typeof useNotesQuery>);

renderWithQueryClient(<NoteListContainer goalId={goalId} />);

expect(screen.getByText("์•„์ง ๋“ฑ๋ก๋œ ๋…ธํŠธ๊ฐ€ ์—†์–ด์š”")).toBeInTheDocument();
});
});

describe("๋…ธํŠธ๊ฐ€ ์žˆ์„ ๋•Œ", () => {
it("GoalBanner์™€ NoteList๋ฅผ ๋ Œ๋”๋งํ•œ๋‹ค", () => {
jest.mocked(useNotesQuery).mockReturnValue({
data: {
totalCount: 1,
nextCursor: null,
goal: { id: goalId, title: "๋ชฉํ‘œ 1" },
notes: [
{
id: 1,
title: "์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ",
todo: { id: 1, title: "ํ•  ์ผ 1", done: false },
updatedAt: "2026-01-17T10:00:00Z",
},
],
},
} as unknown as ReturnType<typeof useNotesQuery>);

renderWithQueryClient(<NoteListContainer goalId={goalId} />);

expect(screen.getByText("๋ชฉํ‘œ 1")).toBeInTheDocument();
expect(screen.getByText("์ฒซ ๋ฒˆ์งธ ๋…ธํŠธ")).toBeInTheDocument();
});
});
});