interactive-chat-box.test.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { render, screen, within } from "@testing-library/react";
  2. import userEvent from "@testing-library/user-event";
  3. import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
  4. import { InteractiveChatBox } from "#/components/interactive-chat-box";
  5. describe("InteractiveChatBox", () => {
  6. const onSubmitMock = vi.fn();
  7. const onStopMock = vi.fn();
  8. beforeAll(() => {
  9. global.URL.createObjectURL = vi
  10. .fn()
  11. .mockReturnValue("blob:http://example.com");
  12. });
  13. afterEach(() => {
  14. vi.clearAllMocks();
  15. });
  16. it("should render", () => {
  17. render(<InteractiveChatBox onSubmit={onSubmitMock} onStop={onStopMock} />);
  18. const chatBox = screen.getByTestId("interactive-chat-box");
  19. within(chatBox).getByTestId("chat-input");
  20. within(chatBox).getByTestId("upload-image-input");
  21. });
  22. it.fails("should set custom values", () => {
  23. render(
  24. <InteractiveChatBox
  25. onSubmit={onSubmitMock}
  26. onStop={onStopMock}
  27. value="Hello, world!"
  28. />,
  29. );
  30. const chatBox = screen.getByTestId("interactive-chat-box");
  31. const chatInput = within(chatBox).getByTestId("chat-input");
  32. expect(chatInput).toHaveValue("Hello, world!");
  33. });
  34. it("should display the image previews when images are uploaded", async () => {
  35. const user = userEvent.setup();
  36. render(<InteractiveChatBox onSubmit={onSubmitMock} onStop={onStopMock} />);
  37. const file = new File(["(⌐□_□)"], "chucknorris.png", { type: "image/png" });
  38. const input = screen.getByTestId("upload-image-input");
  39. expect(screen.queryAllByTestId("image-preview")).toHaveLength(0);
  40. await user.upload(input, file);
  41. expect(screen.queryAllByTestId("image-preview")).toHaveLength(1);
  42. const files = [
  43. new File(["(⌐□_□)"], "chucknorris2.png", { type: "image/png" }),
  44. new File(["(⌐□_□)"], "chucknorris3.png", { type: "image/png" }),
  45. ];
  46. await user.upload(input, files);
  47. expect(screen.queryAllByTestId("image-preview")).toHaveLength(3);
  48. });
  49. it("should remove the image preview when the close button is clicked", async () => {
  50. const user = userEvent.setup();
  51. render(<InteractiveChatBox onSubmit={onSubmitMock} onStop={onStopMock} />);
  52. const file = new File(["(⌐□_□)"], "chucknorris.png", { type: "image/png" });
  53. const input = screen.getByTestId("upload-image-input");
  54. await user.upload(input, file);
  55. expect(screen.queryAllByTestId("image-preview")).toHaveLength(1);
  56. const imagePreview = screen.getByTestId("image-preview");
  57. const closeButton = within(imagePreview).getByRole("button");
  58. await user.click(closeButton);
  59. expect(screen.queryAllByTestId("image-preview")).toHaveLength(0);
  60. });
  61. it("should call onSubmit with the message and images", async () => {
  62. const user = userEvent.setup();
  63. render(<InteractiveChatBox onSubmit={onSubmitMock} onStop={onStopMock} />);
  64. const textarea = within(screen.getByTestId("chat-input")).getByRole(
  65. "textbox",
  66. );
  67. const input = screen.getByTestId("upload-image-input");
  68. const file = new File(["(⌐□_□)"], "chucknorris.png", { type: "image/png" });
  69. await user.upload(input, file);
  70. await user.type(textarea, "Hello, world!");
  71. await user.keyboard("{Enter}");
  72. expect(onSubmitMock).toHaveBeenCalledWith("Hello, world!", [file]);
  73. // clear images after submission
  74. expect(screen.queryAllByTestId("image-preview")).toHaveLength(0);
  75. });
  76. it("should disable the submit button", async () => {
  77. const user = userEvent.setup();
  78. render(
  79. <InteractiveChatBox
  80. isDisabled
  81. onSubmit={onSubmitMock}
  82. onStop={onStopMock}
  83. />,
  84. );
  85. const button = screen.getByRole("button");
  86. expect(button).toBeDisabled();
  87. await user.click(button);
  88. expect(onSubmitMock).not.toHaveBeenCalled();
  89. });
  90. it("should display the stop button if set and call onStop when clicked", async () => {
  91. const user = userEvent.setup();
  92. render(
  93. <InteractiveChatBox
  94. mode="stop"
  95. onSubmit={onSubmitMock}
  96. onStop={onStopMock}
  97. />,
  98. );
  99. const stopButton = screen.getByTestId("stop-button");
  100. expect(stopButton).toBeInTheDocument();
  101. await user.click(stopButton);
  102. expect(onStopMock).toHaveBeenCalledOnce();
  103. });
  104. });