-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat: add AIChat stories and tests #36700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/client/packages/design-system/widgets/src/components/AIChat/src/ChatTitle/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import type { HTMLProps } from "react"; | ||
|
|
||
| export interface ChatTitleProps extends HTMLProps<HTMLDivElement> { | ||
| title: string; | ||
| title?: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
app/client/packages/design-system/widgets/src/components/AIChat/stories/AIChat.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { AIChat } from "@appsmith/wds"; | ||
| import type { Meta, StoryObj } from "@storybook/react"; | ||
|
|
||
| const meta: Meta<typeof AIChat> = { | ||
| component: AIChat, | ||
| title: "WDS/Widgets/AIChat", | ||
| }; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof AIChat>; | ||
|
|
||
| export const Main: Story = { | ||
| args: { | ||
| thread: [], | ||
| prompt: "", | ||
| username: "John Doe", | ||
| promptInputPlaceholder: "Type your message here", | ||
| chatTitle: "Chat with Assistant", | ||
| assistantName: "", | ||
| isWaitingForResponse: false, | ||
| }, | ||
| }; | ||
|
|
||
| export const EmptyHistory: Story = { | ||
| args: { | ||
| thread: [], | ||
| prompt: "", | ||
| username: "John Doe", | ||
| promptInputPlaceholder: "Type your message here", | ||
| chatTitle: "Chat with Assistant", | ||
| }, | ||
| }; | ||
|
|
||
| export const Loading: Story = { | ||
| args: { | ||
| thread: [], | ||
| prompt: "", | ||
| username: "John Doe", | ||
| promptInputPlaceholder: "Type your message here", | ||
| chatTitle: "Chat with Assistant", | ||
| isWaitingForResponse: true, | ||
| }, | ||
| }; | ||
|
|
||
| export const WithHistory: Story = { | ||
| args: { | ||
| thread: [ | ||
| { | ||
| id: "1", | ||
| content: "Hi, how can I help you?", | ||
| isAssistant: true, | ||
| }, | ||
| { | ||
| id: "2", | ||
| content: "Find stuck support requests", | ||
| isAssistant: false, | ||
| }, | ||
| { | ||
| id: "3", | ||
| content: `Certainly! Here's what I can do to help you: | ||
| * Search our customers database | ||
| * Find stuck support requests | ||
| * Provide advice on how to resolve customer issues | ||
| `, | ||
| isAssistant: true, | ||
| }, | ||
| { | ||
| id: "4", | ||
| content: "Thank you", | ||
| isAssistant: false, | ||
| }, | ||
| ], | ||
| prompt: "", | ||
| username: "John Doe", | ||
| promptInputPlaceholder: "Type your message here", | ||
| chatTitle: "Chat with Assistant", | ||
| }, | ||
| }; |
113 changes: 113 additions & 0 deletions
113
app/client/packages/design-system/widgets/src/components/AIChat/tests/AIChat.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import React from "react"; | ||
| import "@testing-library/jest-dom"; | ||
| import { render, screen, within } from "@testing-library/react"; | ||
| import { faker } from "@faker-js/faker"; | ||
|
|
||
| import { AIChat, type AIChatProps } from ".."; | ||
| import userEvent from "@testing-library/user-event"; | ||
|
|
||
| const renderComponent = (props: Partial<AIChatProps> = {}) => { | ||
| const defaultProps: AIChatProps = { | ||
| username: "", | ||
| thread: [], | ||
| prompt: "", | ||
| onPromptChange: jest.fn(), | ||
| }; | ||
|
|
||
| return render(<AIChat {...defaultProps} {...props} />); | ||
| }; | ||
|
|
||
| describe("@appsmith/wds/AIChat", () => { | ||
| it("should render chat's title", () => { | ||
| const chatTitle = faker.lorem.words(2); | ||
|
|
||
| renderComponent({ chatTitle }); | ||
|
|
||
| expect(screen.getByTestId("t--aichat-chat-title")).toHaveTextContent( | ||
| chatTitle, | ||
| ); | ||
| }); | ||
|
|
||
| it("should render username", () => { | ||
| const username = faker.name.firstName(); | ||
|
|
||
| renderComponent({ username }); | ||
|
|
||
| expect(screen.getByTestId("t--aichat-username")).toHaveTextContent( | ||
| username, | ||
| ); | ||
| }); | ||
|
|
||
| it("should render thread", () => { | ||
| const thread = [ | ||
| { | ||
| id: faker.datatype.uuid(), | ||
| content: faker.lorem.paragraph(1), | ||
| isAssistant: false, | ||
| }, | ||
| { | ||
| id: faker.datatype.uuid(), | ||
| content: faker.lorem.paragraph(2), | ||
| isAssistant: true, | ||
| }, | ||
| ]; | ||
|
|
||
| renderComponent({ thread }); | ||
|
|
||
| const messages = within( | ||
| screen.getByTestId("t--aichat-thread"), | ||
| ).getAllByRole("listitem"); | ||
|
|
||
| expect(messages).toHaveLength(thread.length); | ||
| expect(messages[0]).toHaveTextContent(thread[0].content); | ||
| expect(messages[1]).toHaveTextContent(thread[1].content); | ||
| }); | ||
|
|
||
| it("should render prompt input placeholder", () => { | ||
| const promptInputPlaceholder = faker.lorem.words(3); | ||
|
|
||
| renderComponent({ | ||
| promptInputPlaceholder, | ||
| }); | ||
|
|
||
| expect(screen.getByRole("textbox")).toHaveAttribute( | ||
| "placeholder", | ||
| promptInputPlaceholder, | ||
| ); | ||
| }); | ||
|
|
||
| it("should render prompt input value", () => { | ||
| const prompt = faker.lorem.words(3); | ||
|
|
||
| renderComponent({ | ||
| prompt, | ||
| }); | ||
|
|
||
| expect(screen.getByRole("textbox")).toHaveValue(prompt); | ||
| }); | ||
|
|
||
| it("should trigger user's prompt", async () => { | ||
| const onPromptChange = jest.fn(); | ||
|
|
||
| renderComponent({ | ||
| onPromptChange, | ||
| }); | ||
|
|
||
| await userEvent.type(screen.getByRole("textbox"), "A"); | ||
|
|
||
| expect(onPromptChange).toHaveBeenCalledWith("A"); | ||
| }); | ||
|
|
||
| it("should submit user's prompt", async () => { | ||
| const onSubmit = jest.fn(); | ||
|
|
||
| renderComponent({ | ||
| prompt: "ABCD", | ||
| onSubmit, | ||
| }); | ||
|
|
||
| await userEvent.click(screen.getByRole("button", { name: "Send" })); | ||
|
|
||
| expect(onSubmit).toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import React from "react"; | ||
|
|
||
| jest.mock("react-markdown", () => (props: { children: unknown }) => { | ||
| return <>{props.children}</>; | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to do the check directly in the component, since the consumer does not have to validate edge cases.