|
|
@@ -1,6 +1,6 @@
|
|
|
import React from "react";
|
|
|
import userEvent from "@testing-library/user-event";
|
|
|
-import { act, render, fireEvent } from "@testing-library/react";
|
|
|
+import { render, screen } from "@testing-library/react";
|
|
|
import ChatInput from "./ChatInput";
|
|
|
|
|
|
describe("ChatInput", () => {
|
|
|
@@ -11,109 +11,104 @@ describe("ChatInput", () => {
|
|
|
const onSendMessage = vi.fn();
|
|
|
|
|
|
it("should render a textarea", () => {
|
|
|
- const { getByRole } = render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
- const textarea = getByRole("textbox");
|
|
|
- expect(textarea).toBeInTheDocument();
|
|
|
+ render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
+ expect(screen.getByRole("textbox")).toBeInTheDocument();
|
|
|
});
|
|
|
|
|
|
- it("should be able to be set as disabled", () => {
|
|
|
- const { getByRole } = render(
|
|
|
- <ChatInput disabled onSendMessage={onSendMessage} />,
|
|
|
- );
|
|
|
- const textarea = getByRole("textbox");
|
|
|
- const button = getByRole("button");
|
|
|
+ it("should be able to be set as disabled", async () => {
|
|
|
+ const user = userEvent.setup();
|
|
|
+ render(<ChatInput disabled onSendMessage={onSendMessage} />);
|
|
|
+
|
|
|
+ const textarea = screen.getByRole("textbox");
|
|
|
+ const button = screen.getByRole("button");
|
|
|
|
|
|
expect(textarea).not.toBeDisabled(); // user can still type
|
|
|
expect(button).toBeDisabled(); // user cannot submit
|
|
|
|
|
|
- act(() => {
|
|
|
- userEvent.type(textarea, "Hello, world!{enter}");
|
|
|
- });
|
|
|
+ await user.type(textarea, "Hello, world!");
|
|
|
+ await user.keyboard("{Enter}");
|
|
|
|
|
|
expect(onSendMessage).not.toHaveBeenCalled();
|
|
|
});
|
|
|
|
|
|
it("should render with a placeholder", () => {
|
|
|
- const { getByPlaceholderText } = render(
|
|
|
- <ChatInput onSendMessage={onSendMessage} />,
|
|
|
+ render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
+
|
|
|
+ const textarea = screen.getByPlaceholderText(
|
|
|
+ /CHAT_INTERFACE\$INPUT_PLACEHOLDER/i,
|
|
|
);
|
|
|
- const textarea = getByPlaceholderText(/CHAT_INTERFACE\$INPUT_PLACEHOLDER/i);
|
|
|
expect(textarea).toBeInTheDocument();
|
|
|
});
|
|
|
|
|
|
it("should render a send button", () => {
|
|
|
- const { getByRole } = render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
- const button = getByRole("button");
|
|
|
- expect(button).toBeInTheDocument();
|
|
|
+ render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
+ expect(screen.getByRole("button")).toBeInTheDocument();
|
|
|
});
|
|
|
|
|
|
it("should call sendChatMessage with the input when the send button is clicked", async () => {
|
|
|
- const { getByRole } = render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
- const textarea = getByRole("textbox");
|
|
|
- const button = getByRole("button");
|
|
|
+ const user = userEvent.setup();
|
|
|
+ render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
|
|
|
- fireEvent.change(textarea, { target: { value: "Hello, world!" } });
|
|
|
+ const textarea = screen.getByRole("textbox");
|
|
|
+ const button = screen.getByRole("button");
|
|
|
|
|
|
- await act(async () => {
|
|
|
- await userEvent.click(button);
|
|
|
- });
|
|
|
+ await user.type(textarea, "Hello, world!");
|
|
|
+ await user.click(button);
|
|
|
|
|
|
expect(onSendMessage).toHaveBeenCalledWith("Hello, world!");
|
|
|
-
|
|
|
- // Additionally, check if the callback is called exactly once
|
|
|
+ // Additionally, check if it was called exactly once
|
|
|
expect(onSendMessage).toHaveBeenCalledTimes(1);
|
|
|
});
|
|
|
|
|
|
- it("should be able to send a message when the enter key is pressed", () => {
|
|
|
- const { getByRole } = render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
- const textarea = getByRole("textbox");
|
|
|
+ it("should be able to send a message when the enter key is pressed", async () => {
|
|
|
+ const user = userEvent.setup();
|
|
|
+ render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
+ const textarea = screen.getByRole("textbox");
|
|
|
|
|
|
- fireEvent.change(textarea, { target: { value: "Hello, world!" } });
|
|
|
- fireEvent.keyDown(textarea, { key: "Enter", code: "Enter", charCode: 13 });
|
|
|
+ await user.type(textarea, "Hello, world!");
|
|
|
+ await user.keyboard("{Enter}");
|
|
|
|
|
|
expect(onSendMessage).toHaveBeenCalledWith("Hello, world!");
|
|
|
});
|
|
|
|
|
|
- it("should NOT send a message when shift + enter is pressed", () => {
|
|
|
- const { getByRole } = render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
- const textarea = getByRole("textbox");
|
|
|
+ it("should NOT send a message when shift + enter is pressed", async () => {
|
|
|
+ const user = userEvent.setup();
|
|
|
+ render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
+ const textarea = screen.getByRole("textbox");
|
|
|
|
|
|
- act(() => {
|
|
|
- userEvent.type(textarea, "Hello, world!{shift}{enter}");
|
|
|
- });
|
|
|
+ await user.type(textarea, "Hello, world!");
|
|
|
+ await user.keyboard("{Shift>} {Enter}"); // Shift + Enter
|
|
|
|
|
|
expect(onSendMessage).not.toHaveBeenCalled();
|
|
|
});
|
|
|
|
|
|
- it("should NOT send an empty message", () => {
|
|
|
- const { getByRole } = render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
- const textarea = getByRole("textbox");
|
|
|
- const button = getByRole("button");
|
|
|
+ it("should NOT send an empty message", async () => {
|
|
|
+ const user = userEvent.setup();
|
|
|
+ render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
+ const textarea = screen.getByRole("textbox");
|
|
|
+ const button = screen.getByRole("button");
|
|
|
|
|
|
- act(() => {
|
|
|
- userEvent.type(textarea, " {enter}"); // Only whitespace
|
|
|
- });
|
|
|
+ await user.type(textarea, " ");
|
|
|
|
|
|
+ // with enter key
|
|
|
+ await user.keyboard("{Enter}");
|
|
|
expect(onSendMessage).not.toHaveBeenCalled();
|
|
|
|
|
|
- act(() => {
|
|
|
- userEvent.click(button);
|
|
|
- });
|
|
|
-
|
|
|
+ // with button click
|
|
|
+ await user.click(button);
|
|
|
expect(onSendMessage).not.toHaveBeenCalled();
|
|
|
});
|
|
|
|
|
|
it("should clear the input message after sending a message", async () => {
|
|
|
- const { getByRole } = render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
- const textarea = getByRole("textbox");
|
|
|
- const button = getByRole("button");
|
|
|
-
|
|
|
- fireEvent.change(textarea, { target: { value: "Hello, world!" } });
|
|
|
+ const user = userEvent.setup();
|
|
|
+ render(<ChatInput onSendMessage={onSendMessage} />);
|
|
|
+ const textarea = screen.getByRole("textbox");
|
|
|
+ const button = screen.getByRole("button");
|
|
|
|
|
|
+ await user.type(textarea, "Hello, world!");
|
|
|
expect(textarea).toHaveValue("Hello, world!");
|
|
|
|
|
|
- fireEvent.click(button);
|
|
|
-
|
|
|
+ await user.click(button);
|
|
|
expect(textarea).toHaveValue("");
|
|
|
});
|
|
|
|