Fix issue #5341: [Bug]: When an image is attached in the chat, the chat text is not cleared

This commit is contained in:
openhands
2024-12-01 14:43:04 +00:00
parent eb5f4f5ebc
commit faa78e9951
2 changed files with 36 additions and 1 deletions

View File

@@ -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/interactive-chat-box";
@@ -98,6 +98,33 @@ describe("InteractiveChatBox", () => {
expect(screen.queryAllByTestId("image-preview")).toHaveLength(0);
});
it("should clear text input after submitting with images via paste", async () => {
const user = userEvent.setup();
render(<InteractiveChatBox onSubmit={onSubmitMock} onStop={onStopMock} />);
const textarea = within(screen.getByTestId("chat-input")).getByRole(
"textbox",
);
await user.type(textarea, "Hello, world!");
const file = new File(["(⌐□_□)"], "chucknorris.png", { type: "image/png" });
// Get the UploadImageInput component and simulate file upload
const input = screen.getByTestId("upload-image-input");
const changeEvent = {
target: {
files: [file],
},
};
fireEvent.change(input, changeEvent);
// Submit the message
await user.keyboard("{Enter}");
expect(onSubmitMock).toHaveBeenCalledWith("Hello, world!", [file]);
expect(textarea).toHaveValue("");
});
it("should disable the submit button", async () => {
const user = userEvent.setup();
render(

View File

@@ -51,6 +51,10 @@ export function ChatInput({
if (files.length > 0) {
event.preventDefault();
onImagePaste(files);
// Submit the current message along with the image
if (textareaRef.current?.value) {
handleSubmitMessage();
}
}
}
// For text paste, let the default behavior handle it
@@ -77,6 +81,10 @@ export function ChatInput({
);
if (files.length > 0) {
onImagePaste(files);
// Submit the current message along with the image
if (textareaRef.current?.value) {
handleSubmitMessage();
}
}
}
};