|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 3 | +import { render, fireEvent, cleanup } from "@testing-library/react"; |
| 4 | +import { AutoTagSuggestions } from "../auto-tag-suggestions"; |
| 5 | + |
| 6 | +// ───────────────────────────────────────────── |
| 7 | +// Mock: tRPC |
| 8 | +// ───────────────────────────────────────────── |
| 9 | +const mockUseQuery = vi.fn(); |
| 10 | + |
| 11 | +vi.mock("@/lib/trpc", () => ({ |
| 12 | + trpc: { |
| 13 | + item: { |
| 14 | + suggestMetadata: { |
| 15 | + useQuery: (...args: unknown[]) => mockUseQuery(...args), |
| 16 | + }, |
| 17 | + }, |
| 18 | + }, |
| 19 | +})); |
| 20 | + |
| 21 | +// ───────────────────────────────────────────── |
| 22 | +// Mock: shared constants (Node ESM 호환) |
| 23 | +// ───────────────────────────────────────────── |
| 24 | +vi.mock("@math-item-os/shared/constants/index", () => ({ |
| 25 | + BLOOM_LEVEL: { |
| 26 | + 1: { value: 1, label: "기억", order: 1 }, |
| 27 | + 2: { value: 2, label: "이해", order: 2 }, |
| 28 | + 3: { value: 3, label: "적용", order: 3 }, |
| 29 | + 4: { value: 4, label: "분석", order: 4 }, |
| 30 | + 5: { value: 5, label: "평가", order: 5 }, |
| 31 | + 6: { value: 6, label: "창조", order: 6 }, |
| 32 | + }, |
| 33 | +})); |
| 34 | + |
| 35 | +// ───────────────────────────────────────────── |
| 36 | +// 테스트 데이터 |
| 37 | +// ───────────────────────────────────────────── |
| 38 | +const mockData = { |
| 39 | + skills: [{ id: "sk1", title: "일차방정식", similarity: 0.92 }], |
| 40 | + standards: [{ id: "std1", code: "M8-01", title: "표준1" }], |
| 41 | + misconceptions: [{ id: "mc1", title: "부호 오류", typicalError: "부호 반전" }], |
| 42 | + bloomLevel: 3, |
| 43 | +}; |
| 44 | + |
| 45 | +function defaultProps(overrides: Partial<Parameters<typeof AutoTagSuggestions>[0]> = {}) { |
| 46 | + return { |
| 47 | + bodyLatex: "x + 1 = 0", |
| 48 | + schoolLevel: "middle" as const, |
| 49 | + grade: 2, |
| 50 | + selectedSkillIds: [] as string[], |
| 51 | + selectedStandardIds: [] as string[], |
| 52 | + selectedMisconceptionIds: [] as string[], |
| 53 | + onSkillSelect: vi.fn(), |
| 54 | + onStandardSelect: vi.fn(), |
| 55 | + onMisconceptionSelect: vi.fn(), |
| 56 | + ...overrides, |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +// ───────────────────────────────────────────── |
| 61 | +// 테스트 |
| 62 | +// ───────────────────────────────────────────── |
| 63 | +describe("AutoTagSuggestions", () => { |
| 64 | + afterEach(cleanup); |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + vi.clearAllMocks(); |
| 68 | + mockUseQuery.mockReturnValue({ data: undefined, isLoading: false, error: null }); |
| 69 | + }); |
| 70 | + |
| 71 | + it("bodyLatex 빈 문자열 → null 렌더링", () => { |
| 72 | + mockUseQuery.mockReturnValue({ data: undefined, isLoading: false, error: null }); |
| 73 | + const { container } = render( |
| 74 | + <AutoTagSuggestions {...defaultProps({ bodyLatex: "" })} />, |
| 75 | + ); |
| 76 | + expect(container.innerHTML).toBe(""); |
| 77 | + }); |
| 78 | + |
| 79 | + it("isLoading → 스켈레톤(animate-pulse) 표시", () => { |
| 80 | + mockUseQuery.mockReturnValue({ data: undefined, isLoading: true, error: null }); |
| 81 | + const { container } = render( |
| 82 | + <AutoTagSuggestions {...defaultProps()} />, |
| 83 | + ); |
| 84 | + expect(container.querySelector(".animate-pulse")).not.toBeNull(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("data 반환 → 스킬/성취기준/오개념 칩 렌더링", () => { |
| 88 | + mockUseQuery.mockReturnValue({ data: mockData, isLoading: false, error: null }); |
| 89 | + const { getByText } = render( |
| 90 | + <AutoTagSuggestions {...defaultProps()} />, |
| 91 | + ); |
| 92 | + expect(getByText("일차방정식")).toBeDefined(); |
| 93 | + expect(getByText("M8-01 표준1")).toBeDefined(); |
| 94 | + expect(getByText("부호 오류")).toBeDefined(); |
| 95 | + // Bloom 레벨 배지 |
| 96 | + expect(getByText("적용(3)")).toBeDefined(); |
| 97 | + }); |
| 98 | + |
| 99 | + it("칩 클릭 → onSkillSelect 콜백 호출", () => { |
| 100 | + mockUseQuery.mockReturnValue({ data: mockData, isLoading: false, error: null }); |
| 101 | + const onSkillSelect = vi.fn(); |
| 102 | + const { getByText } = render( |
| 103 | + <AutoTagSuggestions {...defaultProps({ onSkillSelect })} />, |
| 104 | + ); |
| 105 | + fireEvent.click(getByText("일차방정식")); |
| 106 | + expect(onSkillSelect).toHaveBeenCalledWith("sk1"); |
| 107 | + }); |
| 108 | + |
| 109 | + it("선택된 항목 → 체크마크(✓) 표시", () => { |
| 110 | + mockUseQuery.mockReturnValue({ data: mockData, isLoading: false, error: null }); |
| 111 | + const { container } = render( |
| 112 | + <AutoTagSuggestions {...defaultProps({ selectedSkillIds: ["sk1"] })} />, |
| 113 | + ); |
| 114 | + // sk1 칩의 버튼 내부에 ✓ 문자 존재 |
| 115 | + const buttons = container.querySelectorAll("button"); |
| 116 | + const sk1Button = Array.from(buttons).find((b) => b.textContent?.includes("일차방정식")); |
| 117 | + expect(sk1Button?.textContent).toContain("\u2713"); |
| 118 | + }); |
| 119 | +}); |
0 commit comments