interactive-chat-box.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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("should display the image previews when images are uploaded", async () => {
  23. const user = userEvent.setup();
  24. render(<InteractiveChatBox onSubmit={onSubmitMock} onStop={onStopMock} />);
  25. const file = new File(["(⌐□_□)"], "chucknorris.png", { type: "image/png" });
  26. const input = screen.getByTestId("upload-image-input");
  27. expect(screen.queryAllByTestId("image-preview")).toHaveLength(0);
  28. await user.upload(input, file);
  29. expect(screen.queryAllByTestId("image-preview")).toHaveLength(1);
  30. const files = [
  31. new File(["(⌐□_□)"], "chucknorris2.png", { type: "image/png" }),
  32. new File(["(⌐□_□)"], "chucknorris3.png", { type: "image/png" }),
  33. ];
  34. await user.upload(input, files);
  35. expect(screen.queryAllByTestId("image-preview")).toHaveLength(3);
  36. });
  37. it("should remove the image preview when the close button is clicked", async () => {
  38. const user = userEvent.setup();
  39. render(<InteractiveChatBox onSubmit={onSubmitMock} onStop={onStopMock} />);
  40. const file = new File(["(⌐□_□)"], "chucknorris.png", { type: "image/png" });
  41. const input = screen.getByTestId("upload-image-input");
  42. await user.upload(input, file);
  43. expect(screen.queryAllByTestId("image-preview")).toHaveLength(1);
  44. const imagePreview = screen.getByTestId("image-preview");
  45. const closeButton = within(imagePreview).getByRole("button");
  46. await user.click(closeButton);
  47. expect(screen.queryAllByTestId("image-preview")).toHaveLength(0);
  48. });
  49. it("should call onSubmit with the message and images", async () => {
  50. const user = userEvent.setup();
  51. render(<InteractiveChatBox onSubmit={onSubmitMock} onStop={onStopMock} />);
  52. const textarea = within(screen.getByTestId("chat-input")).getByRole(
  53. "textbox",
  54. );
  55. const input = screen.getByTestId("upload-image-input");
  56. const file = new File(["(⌐□_□)"], "chucknorris.png", { type: "image/png" });
  57. await user.upload(input, file);
  58. await user.type(textarea, "Hello, world!");
  59. await user.keyboard("{Enter}");
  60. expect(onSubmitMock).toHaveBeenCalledWith("Hello, world!", [file]);
  61. // clear images after submission
  62. expect(screen.queryAllByTestId("image-preview")).toHaveLength(0);
  63. });
  64. it("should disable the submit button", async () => {
  65. const user = userEvent.setup();
  66. render(
  67. <InteractiveChatBox
  68. isDisabled
  69. onSubmit={onSubmitMock}
  70. onStop={onStopMock}
  71. />,
  72. );
  73. const button = screen.getByRole("button");
  74. expect(button).toBeDisabled();
  75. await user.click(button);
  76. expect(onSubmitMock).not.toHaveBeenCalled();
  77. });
  78. it("should display the stop button if set and call onStop when clicked", async () => {
  79. const user = userEvent.setup();
  80. render(
  81. <InteractiveChatBox
  82. mode="stop"
  83. onSubmit={onSubmitMock}
  84. onStop={onStopMock}
  85. />,
  86. );
  87. const stopButton = screen.getByTestId("stop-button");
  88. expect(stopButton).toBeInTheDocument();
  89. await user.click(stopButton);
  90. expect(onStopMock).toHaveBeenCalledOnce();
  91. });
  92. });