|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import Canvas from "../Canvas"; |
| 3 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 4 | + |
| 5 | +// Mock zustand stores |
| 6 | +const mockUseCurNodes = vi.fn(); |
| 7 | +const mockUseSelectedNodeId = vi.fn(); |
| 8 | +const mockUseSelectNode = vi.fn(); |
| 9 | +const mockUseUpdateNode = vi.fn(); |
| 10 | +const mockUseCanvas = vi.fn(); |
| 11 | +const mockUseSetCanvas = vi.fn(); |
| 12 | + |
| 13 | +//실제 파일을 가로채서 가짜 결과를 내뱉게 합니다. |
| 14 | +vi.mock("@/stores/useEditorStore", () => ({ |
| 15 | + useCurNodes: () => mockUseCurNodes(), //지연 실행 -> 이렇게 되면 다른 테스트에서 목함수의 반환값을 바꿔도 언제나 새롭게 해당 함수가 호출 되므로 다른 테스트의 영향을 받지 않는다. |
| 16 | + useSelectedNodeId: () => mockUseSelectedNodeId(), |
| 17 | + useSelectNode: () => mockUseSelectNode(), |
| 18 | + useUpdateNode: () => mockUseUpdateNode(), |
| 19 | + useCanvas: () => mockUseCanvas(), |
| 20 | + useSetCanvas: () => mockUseSetCanvas(), |
| 21 | +})); |
| 22 | + |
| 23 | +// Mock EditorNodeWrapper (Rnd 라이브러리는 jsdom환경에서 테스트 하기에 까다롭습니다.) |
| 24 | +vi.mock("@repo/ui/core/EditorNodeWrapper.jsx", () => ({ |
| 25 | + default: ({ |
| 26 | + children, |
| 27 | + node, |
| 28 | + }: { |
| 29 | + children: React.ReactNode; |
| 30 | + node: { id: string }; |
| 31 | + }) => ( |
| 32 | + <div data-testid={`wrapper-${node.id}`} className="mock-wrapper"> |
| 33 | + {children} |
| 34 | + </div> |
| 35 | + ), |
| 36 | +})); |
| 37 | + |
| 38 | +// Mock NodeRenderer |
| 39 | +vi.mock("@repo/ui/core/NodeRenderer.jsx", () => ({ |
| 40 | + default: ({ |
| 41 | + children, |
| 42 | + node, |
| 43 | + }: { |
| 44 | + children: React.ReactNode; |
| 45 | + node: { id: string }; |
| 46 | + }) => ( |
| 47 | + <div data-testid={`renderer-${node.id}`} className="mock-renderer"> |
| 48 | + {children} |
| 49 | + </div> |
| 50 | + ), |
| 51 | +})); |
| 52 | + |
| 53 | +describe("Canvas Component", () => { |
| 54 | + beforeEach(() => { |
| 55 | + vi.clearAllMocks(); |
| 56 | + mockUseSelectedNodeId.mockReturnValue(null); |
| 57 | + mockUseCanvas.mockReturnValue({ scale: 1 }); |
| 58 | + }); |
| 59 | + |
| 60 | + it("Case 1: Should render empty when there are no nodes", () => { |
| 61 | + mockUseCurNodes.mockReturnValue([]); |
| 62 | + const { container } = render(<Canvas />); |
| 63 | + expect(container.querySelector(".canvas-root")).toBeInTheDocument(); |
| 64 | + expect(container.querySelectorAll(".mock-wrapper")).toHaveLength(0); |
| 65 | + }); |
| 66 | + |
| 67 | + it("Case 2: Should render flat nodes (Siblings)", () => { |
| 68 | + const flatNodes = [ |
| 69 | + { id: "1", type: "Container", parent_id: null, layout: {} }, |
| 70 | + { id: "2", type: "Hero", parent_id: null, layout: {} }, |
| 71 | + ]; |
| 72 | + mockUseCurNodes.mockReturnValue(flatNodes); |
| 73 | + |
| 74 | + const { container } = render(<Canvas />); |
| 75 | + |
| 76 | + expect(screen.getByTestId("wrapper-1")).toBeInTheDocument(); |
| 77 | + expect(screen.getByTestId("wrapper-2")).toBeInTheDocument(); |
| 78 | + expect(screen.getByTestId("renderer-1")).toBeInTheDocument(); |
| 79 | + expect(container.querySelectorAll(".mock-wrapper")).toHaveLength(2); |
| 80 | + }); |
| 81 | + |
| 82 | + it("Case 3: Should render nested nodes (Parent-Child)", () => { |
| 83 | + const nestedNodes = [ |
| 84 | + { id: "parent", type: "Container", parent_id: null, layout: {} }, |
| 85 | + { id: "child1", type: "Button", parent_id: "parent", layout: {} }, |
| 86 | + { id: "child2", type: "Button", parent_id: "parent", layout: {} }, |
| 87 | + ]; |
| 88 | + mockUseCurNodes.mockReturnValue(nestedNodes); |
| 89 | + |
| 90 | + render(<Canvas />); |
| 91 | + |
| 92 | + // 존재 확인 |
| 93 | + const parentWrapper = screen.getByTestId("wrapper-parent"); |
| 94 | + const child1Wrapper = screen.getByTestId("wrapper-child1"); |
| 95 | + const child2Wrapper = screen.getByTestId("wrapper-child2"); |
| 96 | + |
| 97 | + expect(parentWrapper).toBeInTheDocument(); |
| 98 | + expect(child1Wrapper).toBeInTheDocument(); |
| 99 | + expect(child2Wrapper).toBeInTheDocument(); |
| 100 | + |
| 101 | + // 핵심 검증: 계층 구조 확인 |
| 102 | + // 부모의 렌더러 안에 자식의 래퍼가 들어있는가? |
| 103 | + // Canvas가 재귀를 제대로 돌리지 않았으면, 자식이 부모 밖에 튀어 나와 있었을 것. |
| 104 | + const parentRenderer = screen.getByTestId("renderer-parent"); |
| 105 | + expect(parentRenderer).toContainElement(child1Wrapper); |
| 106 | + expect(parentRenderer).toContainElement(child2Wrapper); |
| 107 | + }); |
| 108 | + |
| 109 | + it("Case 4: 부모-자식1-자식2-손자 관계의 트리도 렌더링이 되야 합니다.", () => { |
| 110 | + const nestedNodes = [ |
| 111 | + { id: "parent", type: "Container", parent_id: null, layout: {} }, |
| 112 | + { id: "child1", type: "Button", parent_id: "parent", layout: {} }, |
| 113 | + { id: "child2", type: "Button", parent_id: "parent", layout: {} }, |
| 114 | + { id: "grandson", type: "Button", parent_id: "child1", layout: {} }, |
| 115 | + ]; |
| 116 | + |
| 117 | + mockUseCurNodes.mockReturnValue(nestedNodes); |
| 118 | + |
| 119 | + render(<Canvas />); |
| 120 | + const parentRenderer = screen.getByTestId("renderer-parent"); |
| 121 | + const child1Wrapper = screen.getByTestId("wrapper-child1"); |
| 122 | + const child1Renderer = screen.getByTestId("renderer-child1"); // 👈 child1의 "렌더러(속)"를 찾아야 합니다. |
| 123 | + const grandsonWrapper = screen.getByTestId("wrapper-grandson"); |
| 124 | + |
| 125 | + expect(parentRenderer).toContainElement(child1Wrapper); |
| 126 | + expect(child1Renderer).toContainElement(grandsonWrapper); |
| 127 | + |
| 128 | + expect(child1Wrapper.parentElement).toBe(parentRenderer); |
| 129 | + expect(grandsonWrapper.parentElement).toBe(child1Renderer); |
| 130 | + |
| 131 | + expect(grandsonWrapper.parentElement).not.toBe(parentRenderer); |
| 132 | + }); |
| 133 | + |
| 134 | + it("Case 5: Should NOT render wrapper for root (id: null)", () => { |
| 135 | + // This test specifically verifies that the root pseudo-node doesn't get a wrapper |
| 136 | + // If the bug exists, this might crash or render a weird wrapper |
| 137 | + mockUseCurNodes.mockReturnValue([ |
| 138 | + { id: "1", type: "Container", parent_id: null, layout: {} }, |
| 139 | + ]); |
| 140 | + |
| 141 | + render(<Canvas />); |
| 142 | + |
| 143 | + // We expect wrapper-1 to exist |
| 144 | + expect(screen.getByTestId("wrapper-1")).toBeInTheDocument(); |
| 145 | + |
| 146 | + // We expect NO "wrapper-null" or similar |
| 147 | + const wrappers = screen.getAllByTestId(/wrapper-/); |
| 148 | + expect(wrappers).toHaveLength(1); |
| 149 | + }); |
| 150 | +}); |
0 commit comments