Skip to content

Commit

Permalink
Fix chat input not clearing after image paste/drop (All-Hands-AI#5342)
Browse files Browse the repository at this point in the history
Co-authored-by: openhands <[email protected]>
  • Loading branch information
neubig and openhands-agent authored Dec 12, 2024
1 parent 425ccb0 commit e979f51
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
58 changes: 57 additions & 1 deletion frontend/__tests__/components/interactive-chat-box.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen, within } from "@testing-library/react";
import { render, screen, within, fireEvent } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { InteractiveChatBox } from "#/components/features/chat/interactive-chat-box";
Expand Down Expand Up @@ -131,4 +131,60 @@ describe("InteractiveChatBox", () => {
await user.click(stopButton);
expect(onStopMock).toHaveBeenCalledOnce();
});

it("should handle image upload and message submission correctly", async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
const onStop = vi.fn();
const onChange = vi.fn();

const { rerender } = render(
<InteractiveChatBox
onSubmit={onSubmit}
onStop={onStop}
onChange={onChange}
value="test message"
/>
);

// Upload an image via the upload button - this should NOT clear the text input
const file = new File(["dummy content"], "test.png", { type: "image/png" });
const input = screen.getByTestId("upload-image-input");
await user.upload(input, file);

// Verify text input was not cleared
expect(screen.getByRole("textbox")).toHaveValue("test message");
expect(onChange).not.toHaveBeenCalledWith("");

// Submit the message with image
const submitButton = screen.getByRole("button", { name: "Send" });
await user.click(submitButton);

// Verify onSubmit was called with the message and image
expect(onSubmit).toHaveBeenCalledWith("test message", [file]);

// Verify onChange was called to clear the text input
expect(onChange).toHaveBeenCalledWith("");

// Simulate parent component updating the value prop
rerender(
<InteractiveChatBox
onSubmit={onSubmit}
onStop={onStop}
onChange={onChange}
value=""
/>
);

// Verify the text input was cleared
expect(screen.getByRole("textbox")).toHaveValue("");

// Upload another image - this should NOT clear the text input
onChange.mockClear();
await user.upload(input, file);

// Verify text input is still empty and onChange was not called
expect(screen.getByRole("textbox")).toHaveValue("");
expect(onChange).not.toHaveBeenCalled();
});
});
10 changes: 7 additions & 3 deletions frontend/src/components/features/chat/chat-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ export function ChatInput({
};

const handleSubmitMessage = () => {
if (textareaRef.current?.value) {
onSubmit(textareaRef.current.value);
textareaRef.current.value = "";
if (value || (textareaRef.current?.value && !value)) {
onSubmit(value || textareaRef.current?.value || "");
if (value) {
onChange?.("");
} else if (textareaRef.current) {
textareaRef.current.value = "";
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export function InteractiveChatBox({
const handleSubmit = (message: string) => {
onSubmit(message, images);
setImages([]);
if (message) {
onChange?.("");
}
};

return (
Expand Down

0 comments on commit e979f51

Please sign in to comment.