diff --git a/ui/src/chatbot.test.tsx b/ui/src/chatbot.test.tsx index 3b8c2b1c08..e25a4c0d9a 100644 --- a/ui/src/chatbot.test.tsx +++ b/ui/src/chatbot.test.tsx @@ -267,6 +267,29 @@ describe('XChatbot', () => { expect(emitMock).toHaveBeenCalledTimes(1) }) + it('Preserve feedback on data change', () => { + const { container, getByRole, getByTestId, rerender } = render() + const likeButton = container.querySelector("i[data-icon-name='Like']") as HTMLLIElement + expect(likeButton).toBeInTheDocument() + + fireEvent.click(likeButton) + const likeSolidButton = container.querySelector("i[data-icon-name='LikeSolid']") as HTMLLIElement + expect(likeSolidButton).toBeInTheDocument() + + const input = getByRole('textbox') + fireEvent.change(input, { target: { value: 'Woohoo' } }) + fireEvent.click(getByTestId(`${name}-submit`)) + + // TODO: "rerender" does not append new message + rerender() + + expect(container.querySelector("i[data-icon-name='LikeSolid']") as HTMLLIElement).toBeInTheDocument() + }) + it('Renders suggestions when specified', () => { const { getByText } = render() diff --git a/ui/src/chatbot.tsx b/ui/src/chatbot.tsx index ef51146213..a13960034f 100644 --- a/ui/src/chatbot.tsx +++ b/ui/src/chatbot.tsx @@ -191,7 +191,9 @@ export const XChatbot = (props: Chatbot) => { setIsInfiniteLoading(true) } }, [props.events, props.name]), - onDataChange = React.useCallback(() => { if (props.data) setMsgs(processData(props.data)) }, [props.data]), + onDataChange = React.useCallback(() => { + if (props.data) setMsgs(prevMsgs => processData(props.data).map((msg, idx) => ({ ...msg, positive: prevMsgs?.[idx]?.positive }))) + }, [props.data]), handlePositive = (id: I) => { setMsgs(messages => { if (messages[id]?.positive) return messages