Skip to content

Commit fc9ae2b

Browse files
authored
fix: export code editor handle (#80)
1 parent 293a438 commit fc9ae2b

File tree

2 files changed

+278
-206
lines changed

2 files changed

+278
-206
lines changed

components/ui/code-editor.test.tsx

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("CodeEditor", () => {
2424
});
2525
});
2626

27-
it("calls onChange when content is modified", async () => {
27+
it("calls onChange on blur after content is modified", async () => {
2828
const handleChange = vi.fn();
2929
const user = userEvent.setup();
3030

@@ -41,16 +41,22 @@ describe("CodeEditor", () => {
4141
await user.click(editor);
4242
await user.type(editor, "test");
4343

44+
// onChange shouldn't be called yet
45+
expect(handleChange).not.toHaveBeenCalled();
46+
47+
// Blur the editor
48+
await user.tab();
49+
4450
await waitFor(() => {
45-
expect(handleChange).toHaveBeenCalled();
51+
expect(handleChange).toHaveBeenCalledWith("test");
4652
});
4753
});
4854

4955
it("respects readOnly prop", async () => {
5056
const handleChange = vi.fn();
5157

5258
render(
53-
<CodeEditor value="readonly content" readOnly onChange={handleChange} />
59+
<CodeEditor value="readonly content" readOnly onChange={handleChange} />,
5460
);
5561

5662
await waitFor(() => {
@@ -73,7 +79,7 @@ describe("CodeEditor", () => {
7379

7480
it("applies custom className", () => {
7581
const { container } = render(
76-
<CodeEditor className="custom-editor-class" />
82+
<CodeEditor className="custom-editor-class" />,
7783
);
7884

7985
expect(container.firstChild).toHaveClass("custom-editor-class");
@@ -140,6 +146,26 @@ describe("CodeEditor", () => {
140146
});
141147
});
142148

149+
it("exposes an imperative API to get the current value", async () => {
150+
const ref = { current: null } as React.MutableRefObject<any>;
151+
const user = userEvent.setup();
152+
153+
render(<CodeEditor ref={ref} />);
154+
155+
await waitFor(() => {
156+
const editor = document.querySelector(".cm-content");
157+
expect(editor).toBeInTheDocument();
158+
});
159+
160+
const editor = document.querySelector(".cm-content") as HTMLElement;
161+
await user.click(editor);
162+
await user.type(editor, "value");
163+
164+
await user.tab();
165+
166+
expect(ref.current?.getValue()).toBe("value");
167+
});
168+
143169
it("renders loading state during SSR", () => {
144170
// The component should render during SSR with loading state
145171
const { container } = render(<CodeEditor />);

0 commit comments

Comments
 (0)