diff --git a/examples/next/pages/ui/components/ai/ai-conversation/index.page.tsx b/examples/next/pages/ui/components/ai/ai-conversation/index.page.tsx index b71fbecebf9..9305e0bc491 100644 --- a/examples/next/pages/ui/components/ai/ai-conversation/index.page.tsx +++ b/examples/next/pages/ui/components/ai/ai-conversation/index.page.tsx @@ -7,6 +7,7 @@ import '@aws-amplify/ui-react-ai/ai-conversation-styles.css'; import outputs from './amplify_outputs'; import type { Schema } from '@environments/ai/gen2/amplify/data/resource'; import { Authenticator, Card } from '@aws-amplify/ui-react'; +import React from 'react'; const client = generateClient({ authMode: 'userPool' }); const { useAIConversation } = createAIHooks(client); @@ -29,24 +30,33 @@ function Chat() { sendMessage, ] = useAIConversation('pirateChat'); + const displayText = React.useMemo(() => { + return { getMessageTimestampText: formatDate }; + }, []); + + const suggestedPrompts = React.useMemo( + () => [ + { + inputText: 'hello', + header: 'hello', + }, + { + inputText: 'how are you?', + header: 'how are you?', + }, + ], + [] + ); + return ( diff --git a/packages/e2e/cypress/integration/common/ai.ts b/packages/e2e/cypress/integration/common/ai.ts new file mode 100644 index 00000000000..70e84daa9a5 --- /dev/null +++ b/packages/e2e/cypress/integration/common/ai.ts @@ -0,0 +1,23 @@ +import { Then } from '@badeball/cypress-cucumber-preprocessor'; +import { escapeRegExp } from 'lodash'; + +Then('I type {string}', (message: string) => { + cy.findByTestId('text-input').type(message); +}); + +Then('I click the send message button', () => { + cy.get("[type='submit']").click(); +}); + +Then('I see {string} in the text input', (message: string) => { + cy.get('[data-testid="text-input"]') + .find('textarea') + .should('have.value', message); +}); + +Then('I click the {string} prompt', (name: string) => { + cy.wait(2500); // todo: remove this once we figure out what's causing re-render + cy.findByRole('button', { + name: new RegExp(`^${escapeRegExp(name)}$`, 'i'), + }).click(); +}); diff --git a/packages/e2e/features/ui/components/ai/conversation.feature b/packages/e2e/features/ui/components/ai/conversation.feature new file mode 100644 index 00000000000..8b579119139 --- /dev/null +++ b/packages/e2e/features/ui/components/ai/conversation.feature @@ -0,0 +1,29 @@ +Feature: AI Conversation + + Test AI Conversation component + + Background: + Given I'm running the example "ui/components/ai/ai-conversation" + + @react + Scenario: See conversation window + When I type my "email" with status "CONFIRMED" + Then I type my password + Then I click the "Sign in" button + Then I see "how are you?" + Then I see "text-input" element + Then I type "What is your name?" + Then I click the send message button + Then I see "Argh the time be round" + + @react + Scenario: See conversation window and suggested prompts + When I type my "email" with status "CONFIRMED" + Then I type my password + Then I click the "Sign in" button + Then I see "how are you?" + Then I click the "how are you?" prompt + Then I see "how are you?" in the text input + Then I click the send message button + Then I see "Argh the time be round" + diff --git a/packages/e2e/package.json b/packages/e2e/package.json index a2dc9e82102..7f4e9cfd69d 100644 --- a/packages/e2e/package.json +++ b/packages/e2e/package.json @@ -6,6 +6,7 @@ "scripts": { "clean": "rimraf node_modules", "dev": "cypress open", + "test:ai": "cypress run --spec 'features/ui/components/ai/*.feature' --browser chrome", "test:examples": "cypress run --spec 'features/ui/hooks/*.feature','features/ui/components/**/*.feature' --browser chrome", "test:authenticator": "cypress run --spec 'features/ui/components/authenticator/*.feature' --browser chrome", "test:hooks": "cypress run --spec 'features/ui/hooks/*.feature' --browser chrome", diff --git a/packages/react-ai/src/components/AIConversation/AIConversation.tsx b/packages/react-ai/src/components/AIConversation/AIConversation.tsx index a5d74c0944a..02d5016b1a0 100644 --- a/packages/react-ai/src/components/AIConversation/AIConversation.tsx +++ b/packages/react-ai/src/components/AIConversation/AIConversation.tsx @@ -44,26 +44,37 @@ function AIConversationBase({ }, }; - const Provider = createProvider({ - elements: { - Text: React.forwardRef( - function _Text(props, ref) { - return ; - } - ), - }, - actions, - suggestedPrompts, - responseComponents, - variant, - controls: { - MessageList, - PromptList, - Form, - ...controls, - }, - displayText, - }); + const Provider = React.useMemo( + () => + createProvider({ + elements: { + Text: React.forwardRef( + function _Text(props, ref) { + return ; + } + ), + }, + actions, + suggestedPrompts, + responseComponents, + variant, + controls: { + MessageList, + PromptList, + Form, + ...controls, + }, + displayText, + }), + [ + actions, + controls, + displayText, + responseComponents, + suggestedPrompts, + variant, + ] + ); const providerProps = { messages,