|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { render, waitFor } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | +import { CodeEditor } from "./code-editor"; |
| 5 | + |
| 6 | +// Mock next-themes |
| 7 | +vi.mock("next-themes", () => ({ |
| 8 | + useTheme: () => ({ theme: "light" }), |
| 9 | +})); |
| 10 | + |
| 11 | +describe("CodeEditor", () => { |
| 12 | + it("renders with default props", () => { |
| 13 | + const { container } = render(<CodeEditor />); |
| 14 | + expect(container.firstChild).toBeInTheDocument(); |
| 15 | + }); |
| 16 | + |
| 17 | + it("displays initial value", async () => { |
| 18 | + const initialValue = "console.log('Hello, World!');"; |
| 19 | + render(<CodeEditor value={initialValue} />); |
| 20 | + |
| 21 | + await waitFor(() => { |
| 22 | + const content = document.querySelector(".cm-content"); |
| 23 | + expect(content?.textContent).toContain("Hello, World!"); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it("calls onChange when content is modified", async () => { |
| 28 | + const handleChange = vi.fn(); |
| 29 | + const user = userEvent.setup(); |
| 30 | + |
| 31 | + render(<CodeEditor onChange={handleChange} />); |
| 32 | + |
| 33 | + await waitFor(() => { |
| 34 | + const editor = document.querySelector(".cm-content"); |
| 35 | + expect(editor).toBeInTheDocument(); |
| 36 | + }); |
| 37 | + |
| 38 | + const editor = document.querySelector(".cm-content") as HTMLElement; |
| 39 | + |
| 40 | + // Focus and type in the editor |
| 41 | + await user.click(editor); |
| 42 | + await user.type(editor, "test"); |
| 43 | + |
| 44 | + await waitFor(() => { |
| 45 | + expect(handleChange).toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it("respects readOnly prop", async () => { |
| 50 | + const handleChange = vi.fn(); |
| 51 | + |
| 52 | + render( |
| 53 | + <CodeEditor value="readonly content" readOnly onChange={handleChange} /> |
| 54 | + ); |
| 55 | + |
| 56 | + await waitFor(() => { |
| 57 | + const content = document.querySelector(".cm-content"); |
| 58 | + expect(content).toBeInTheDocument(); |
| 59 | + expect(content?.getAttribute("aria-readonly")).toBe("true"); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("shows placeholder when empty", async () => { |
| 64 | + const placeholderText = "Type your code..."; |
| 65 | + |
| 66 | + render(<CodeEditor placeholder={placeholderText} value="" />); |
| 67 | + |
| 68 | + await waitFor(() => { |
| 69 | + const placeholder = document.querySelector(".cm-placeholder"); |
| 70 | + expect(placeholder?.textContent).toBe(placeholderText); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it("applies custom className", () => { |
| 75 | + const { container } = render( |
| 76 | + <CodeEditor className="custom-editor-class" /> |
| 77 | + ); |
| 78 | + |
| 79 | + expect(container.firstChild).toHaveClass("custom-editor-class"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("respects height prop", () => { |
| 83 | + const { container } = render(<CodeEditor height="600px" />); |
| 84 | + |
| 85 | + const editorContainer = container.firstChild as HTMLElement; |
| 86 | + expect(editorContainer).toBeInTheDocument(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("shows line numbers when enabled", async () => { |
| 90 | + render(<CodeEditor showLineNumbers={true} value="line 1\nline 2" />); |
| 91 | + |
| 92 | + await waitFor(() => { |
| 93 | + const lineNumbers = document.querySelector(".cm-lineNumbers"); |
| 94 | + expect(lineNumbers).toBeInTheDocument(); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it("hides line numbers when disabled", async () => { |
| 99 | + render(<CodeEditor showLineNumbers={false} value="line 1\nline 2" />); |
| 100 | + |
| 101 | + await waitFor(() => { |
| 102 | + const lineNumbers = document.querySelector(".cm-lineNumbers"); |
| 103 | + expect(lineNumbers).not.toBeInTheDocument(); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("supports different languages", async () => { |
| 108 | + const pythonCode = "def hello():\n print('Hello, World!')"; |
| 109 | + |
| 110 | + render(<CodeEditor language="python" value={pythonCode} />); |
| 111 | + |
| 112 | + await waitFor(() => { |
| 113 | + const content = document.querySelector(".cm-content"); |
| 114 | + expect(content?.textContent).toContain("Hello, World!"); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it("updates when value prop changes", async () => { |
| 119 | + const { rerender } = render(<CodeEditor value="initial" />); |
| 120 | + |
| 121 | + await waitFor(() => { |
| 122 | + const content = document.querySelector(".cm-content"); |
| 123 | + expect(content?.textContent).toContain("initial"); |
| 124 | + }); |
| 125 | + |
| 126 | + rerender(<CodeEditor value="updated" />); |
| 127 | + |
| 128 | + await waitFor(() => { |
| 129 | + const content = document.querySelector(".cm-content"); |
| 130 | + expect(content?.textContent).toContain("updated"); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it("applies theme override", async () => { |
| 135 | + render(<CodeEditor theme="dark" />); |
| 136 | + |
| 137 | + await waitFor(() => { |
| 138 | + const editor = document.querySelector(".cm-editor"); |
| 139 | + expect(editor).toBeInTheDocument(); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + it("renders loading state during SSR", () => { |
| 144 | + // The component should render during SSR with loading state |
| 145 | + const { container } = render(<CodeEditor />); |
| 146 | + |
| 147 | + // Should initially show the container div |
| 148 | + expect(container.firstChild).toBeInTheDocument(); |
| 149 | + |
| 150 | + // After mount, it should show the editor |
| 151 | + waitFor(() => { |
| 152 | + const editor = document.querySelector(".cm-editor"); |
| 153 | + expect(editor).toBeInTheDocument(); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments