Skip to content

Commit

Permalink
Merge pull request #65 from dfe-analytical-services/EES-4784/reset-er…
Browse files Browse the repository at this point in the history
…ror-state-on-sending-new-message

EES-4784: Reset error state on sending new message
  • Loading branch information
benoutram authored Jan 11, 2024
2 parents 7873bcb + 9dfe9c2 commit f66a0eb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
9 changes: 2 additions & 7 deletions chatbot-ui/components/UserInputDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describe('Message history', () => {
<UserInputDialog
sendMessage={() => Promise.resolve()}
fetching={false}
error=""
/>,
);

Expand All @@ -20,9 +19,7 @@ describe('Message history', () => {

it('Calls `sendMessage` when successfully submitted', async () => {
const handleSend = jest.fn();
render(
<UserInputDialog sendMessage={handleSend} fetching={false} error="" />,
);
render(<UserInputDialog sendMessage={handleSend} fetching={false} />);

fireEvent.change(screen.getByLabelText('What is your question?'), {
target: { value: 'Is the ski village on fire?' },
Expand All @@ -37,9 +34,7 @@ describe('Message history', () => {

it('Shows an error message when submitted without a question', async () => {
const handleSend = jest.fn();
render(
<UserInputDialog sendMessage={handleSend} fetching={false} error="" />,
);
render(<UserInputDialog sendMessage={handleSend} fetching={false} />);

await userEvent.click(screen.getByRole('button', { name: 'Send' }));

Expand Down
6 changes: 3 additions & 3 deletions chatbot-ui/components/UserInputDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { UseChatbotState } from '@/hooks/useChatbot';
import ErrorSummary from '@/components/ErrorSummary';
import classNames from 'classnames';

const UserInputDialog = ({ sendMessage, error: APIError, fetching }: Props) => {
const UserInputDialog = ({ sendMessage, fetching }: Props) => {
const [query, setQuery] = useState<string>('');
const [userInputError, setUserInputError] = useState<string | null>(null);
const textAreaRef = useRef<HTMLTextAreaElement>(null);
Expand Down Expand Up @@ -59,7 +59,7 @@ const UserInputDialog = ({ sendMessage, error: APIError, fetching }: Props) => {
{userInputError && <ErrorSummary error={userInputError} />}
<textarea
className="govuk-textarea"
disabled={fetching || APIError !== null}
disabled={fetching}
onKeyDown={handleKeyDown}
ref={textAreaRef}
rows={3}
Expand Down Expand Up @@ -90,6 +90,6 @@ const UserInputDialog = ({ sendMessage, error: APIError, fetching }: Props) => {
);
};

interface Props extends Omit<UseChatbotState, 'messages'> {}
interface Props extends Omit<UseChatbotState, 'messages' | 'error'> {}

export default UserInputDialog;
31 changes: 31 additions & 0 deletions chatbot-ui/hooks/useChatbot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,35 @@ describe('useChatbot', () => {
'An error occurred while fetching the data. Please try again.',
);
});

it('Resets the error state after sending a new message', async () => {
ChatBotService.sendUserMessage.mockRejectedValue('I am the error');
const { result } = renderHook(() => useChatbot());

await act(() => result.current.sendMessage('I am a message'));

await waitFor(() => {
expect(ChatBotService.sendUserMessage).toHaveBeenCalledWith(
'I am a message',
);
});

const { error, messages: updatedMessages } = result.current;
expect(updatedMessages).toHaveLength(2);

expect(error).toBe(
'An error occurred while fetching the data. Please try again.',
);

ChatBotService.sendUserMessage.mockResolvedValue({
content: 'I am a future successful message',
type: 'apiMessage',
});

await act(() => result.current.sendMessage('I am a second message'));

const { error: errorTwo, messages: updatedMessagesTwo } = result.current;
expect(updatedMessagesTwo).toHaveLength(4);
expect(errorTwo).toBeNull();
});
});
1 change: 1 addition & 0 deletions chatbot-ui/hooks/useChatbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function useChatbot(): UseChatbotState {
};

const sendMessage = async (userInput: string) => {
setError(null);
const userMessage: Message = { content: userInput, type: 'userMessage' };
recordMessageInHistory([userMessage]);
setFetching(true);
Expand Down
6 changes: 1 addition & 5 deletions chatbot-ui/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ export default function Home({

<MessageHistory messages={messages} loading={fetching} />

<UserInputDialog
sendMessage={sendMessage}
fetching={fetching}
error={error}
/>
<UserInputDialog sendMessage={sendMessage} fetching={fetching} />
</>
</Page>
);
Expand Down

0 comments on commit f66a0eb

Please sign in to comment.