Skip to content

Commit

Permalink
ChatInput: add a handler for when user send a message
Browse files Browse the repository at this point in the history
J=CLIP-573
TEST=auto,manual

verified that if provided, onSend is executed after the user sends a message
  • Loading branch information
anguyen-yext2 committed Jan 10, 2024
1 parent bb525e7 commit f929153
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/components/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ export function ChatInput({
? chat.streamNextMessage(input)
: chat.getNextMessage(input);
setInput("");
if (onSend) res.then(() => {onSend(input)});
res.catch((e) => (handleError ? handleError(e) : defaultHandleApiError(e)));
res.then(() => {
onSend?.(input)
}).catch((e) => (handleError ? handleError(e) : defaultHandleApiError(e)));
}, [chat, input, handleError, defaultHandleApiError, stream, onSend]);

const handleKeyDown = useCallback(
Expand Down
6 changes: 3 additions & 3 deletions tests/components/ChatInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ it("executes custom handleError if provided", async () => {
});

it("executes onSend if provided", async () => {
console.log = jest.fn();
render(<ChatInput onSend={message => console.log("Message: ", message)} />);
const onSendCb = jest.fn();
render(<ChatInput onSend={message => onSendCb(message)} />);
await act(() => userEvent.type(screen.getByRole("textbox"), "test"));
const sendButton = screen.getByRole("button");
await act(() => userEvent.click(sendButton));
expect(console.log).toBeCalledWith("Message: ", "test");
expect(onSendCb).toBeCalledWith("test");
});

0 comments on commit f929153

Please sign in to comment.