From 08b1031666611a598cb5b4ea65cc4f1c1be8303e Mon Sep 17 00:00:00 2001 From: "sp.wack" <83104063+amanape@users.noreply.github.com> Date: Mon, 16 Dec 2024 11:33:26 +0400 Subject: [PATCH] fix(frontend): Prevent from submitting empty characters (#5545) --- .../components/chat/chat-input.test.tsx | 16 ++++++++++++++++ .../src/components/features/chat/chat-input.tsx | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/frontend/__tests__/components/chat/chat-input.test.tsx b/frontend/__tests__/components/chat/chat-input.test.tsx index 51cbe4df66..7947b67d41 100644 --- a/frontend/__tests__/components/chat/chat-input.test.tsx +++ b/frontend/__tests__/components/chat/chat-input.test.tsx @@ -51,6 +51,22 @@ describe("ChatInput", () => { expect(onSubmitMock).not.toHaveBeenCalled(); }); + it("should not call onSubmit when the message is only whitespace", async () => { + const user = userEvent.setup(); + render(); + const textarea = screen.getByRole("textbox"); + + await user.type(textarea, " "); + await user.keyboard("{Enter}"); + + expect(onSubmitMock).not.toHaveBeenCalled(); + + await user.type(textarea, " \t\n"); + await user.keyboard("{Enter}"); + + expect(onSubmitMock).not.toHaveBeenCalled(); + }); + it("should disable submit", async () => { const user = userEvent.setup(); render(); diff --git a/frontend/src/components/features/chat/chat-input.tsx b/frontend/src/components/features/chat/chat-input.tsx index 6cec5f42e0..43011aafdf 100644 --- a/frontend/src/components/features/chat/chat-input.tsx +++ b/frontend/src/components/features/chat/chat-input.tsx @@ -84,7 +84,7 @@ export function ChatInput({ const handleSubmitMessage = () => { const message = value || textareaRef.current?.value || ""; - if (message) { + if (message.trim()) { onSubmit(message); onChange?.(""); if (textareaRef.current) {