|
| 1 | +// __tests__/components/StudentBehaviorContent.test.tsx |
| 2 | +import React from "react"; |
| 3 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 4 | +import BehaviorContent from "@/components/behavior/StudentBehaviorContent"; |
| 5 | +import * as getBehaviorApi from "@/api/student/getBehavior"; |
| 6 | +import useStudentFilterStore from "@/store/student-filter-store"; |
| 7 | +import useSelectedDate from "@/store/selected-date-store"; |
| 8 | + |
| 9 | +// store mock |
| 10 | +jest.mock("@/store/student-filter-store"); |
| 11 | +jest.mock("@/store/selected-date-store"); |
| 12 | +// API mock |
| 13 | +jest.mock("@/api/student/getBehavior"); |
| 14 | + |
| 15 | +describe("<BehaviorContent />", () => { |
| 16 | + beforeEach(() => { |
| 17 | + jest.clearAllMocks(); |
| 18 | + // store mock 리턴값 |
| 19 | + (useStudentFilterStore as unknown as jest.Mock).mockReturnValue({ |
| 20 | + grade: "1", |
| 21 | + classNumber: "2", |
| 22 | + studentId: "3", |
| 23 | + }); |
| 24 | + (useSelectedDate as unknown as jest.Mock).mockReturnValue({ |
| 25 | + year: "2025", |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + it("행동특성/종합의견이 API 데이터로 렌더링된다", async () => { |
| 30 | + (getBehaviorApi.GetBehavior as jest.Mock).mockResolvedValueOnce({ |
| 31 | + behavior: "성실하게 생활함", |
| 32 | + generalComment: "종합적으로 우수함", |
| 33 | + }); |
| 34 | + |
| 35 | + render(<BehaviorContent />); |
| 36 | + expect(screen.getByText("행동특성")).toBeInTheDocument(); |
| 37 | + expect(screen.getByText("종합의견")).toBeInTheDocument(); |
| 38 | + |
| 39 | + // 비동기 데이터가 textarea에 반영되는지 확인 |
| 40 | + await waitFor(() => { |
| 41 | + expect(screen.getByDisplayValue("성실하게 생활함")).toBeInTheDocument(); |
| 42 | + expect(screen.getByDisplayValue("종합적으로 우수함")).toBeInTheDocument(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("API 에러 시 콘솔에 에러가 찍혀도 렌더링은 된다", async () => { |
| 47 | + const errorSpy = jest.spyOn(console, "error").mockImplementation(() => {}); |
| 48 | + (getBehaviorApi.GetBehavior as jest.Mock).mockRejectedValueOnce(new Error("API 실패")); |
| 49 | + |
| 50 | + render(<BehaviorContent />); |
| 51 | + expect(screen.getByText("행동특성")).toBeInTheDocument(); |
| 52 | + expect(screen.getByText("종합의견")).toBeInTheDocument(); |
| 53 | + |
| 54 | + await waitFor(() => { |
| 55 | + expect(errorSpy).toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + errorSpy.mockRestore(); |
| 59 | + }); |
| 60 | +}); |
0 commit comments