chat-interface.test.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { render, screen, within } from "@testing-library/react";
  3. import userEvent from "@testing-library/user-event";
  4. import { ChatInterface } from "#/components/chat-interface";
  5. import { SocketProvider } from "#/context/socket";
  6. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  7. const renderChatInterface = (messages: (Message | ErrorMessage)[]) =>
  8. render(<ChatInterface />, { wrapper: SocketProvider });
  9. describe.skip("ChatInterface", () => {
  10. afterEach(() => {
  11. vi.clearAllMocks();
  12. });
  13. it.todo("should render suggestions if empty");
  14. it("should render messages", () => {
  15. const messages: Message[] = [
  16. {
  17. sender: "user",
  18. content: "Hello",
  19. imageUrls: [],
  20. timestamp: new Date().toISOString(),
  21. },
  22. {
  23. sender: "assistant",
  24. content: "Hi",
  25. imageUrls: [],
  26. timestamp: new Date().toISOString(),
  27. },
  28. ];
  29. renderChatInterface(messages);
  30. expect(screen.getAllByTestId(/-message/)).toHaveLength(2);
  31. });
  32. it("should render a chat input", () => {
  33. const messages: Message[] = [];
  34. renderChatInterface(messages);
  35. expect(screen.getByTestId("chat-input")).toBeInTheDocument();
  36. });
  37. it.todo("should call socket send when submitting a message", async () => {
  38. const user = userEvent.setup();
  39. const messages: Message[] = [];
  40. renderChatInterface(messages);
  41. const input = screen.getByTestId("chat-input");
  42. await user.type(input, "Hello");
  43. await user.keyboard("{Enter}");
  44. // spy on send and expect to have been called
  45. });
  46. it("should render an image carousel with a message", () => {
  47. let messages: Message[] = [
  48. {
  49. sender: "assistant",
  50. content: "Here are some images",
  51. imageUrls: [],
  52. timestamp: new Date().toISOString(),
  53. },
  54. ];
  55. const { rerender } = renderChatInterface(messages);
  56. expect(screen.queryByTestId("image-carousel")).not.toBeInTheDocument();
  57. messages = [
  58. {
  59. sender: "assistant",
  60. content: "Here are some images",
  61. imageUrls: ["image1", "image2"],
  62. timestamp: new Date().toISOString(),
  63. },
  64. ];
  65. rerender(<ChatInterface />);
  66. const imageCarousel = screen.getByTestId("image-carousel");
  67. expect(imageCarousel).toBeInTheDocument();
  68. expect(within(imageCarousel).getAllByTestId("image-preview")).toHaveLength(
  69. 2,
  70. );
  71. });
  72. it.todo("should render confirmation buttons");
  73. it("should render a 'continue' action when there are more than 2 messages and awaiting user input", () => {
  74. const messages: Message[] = [
  75. {
  76. sender: "assistant",
  77. content: "Hello",
  78. imageUrls: [],
  79. timestamp: new Date().toISOString(),
  80. },
  81. {
  82. sender: "user",
  83. content: "Hi",
  84. imageUrls: [],
  85. timestamp: new Date().toISOString(),
  86. },
  87. ];
  88. const { rerender } = renderChatInterface(messages);
  89. expect(
  90. screen.queryByTestId("continue-action-button"),
  91. ).not.toBeInTheDocument();
  92. messages.push({
  93. sender: "assistant",
  94. content: "How can I help you?",
  95. imageUrls: [],
  96. timestamp: new Date().toISOString(),
  97. });
  98. rerender(<ChatInterface />);
  99. expect(screen.getByTestId("continue-action-button")).toBeInTheDocument();
  100. });
  101. it("should render inline errors", () => {
  102. const messages: (Message | ErrorMessage)[] = [
  103. {
  104. sender: "assistant",
  105. content: "Hello",
  106. imageUrls: [],
  107. timestamp: new Date().toISOString(),
  108. },
  109. {
  110. error: "Woops!",
  111. message: "Something went wrong",
  112. },
  113. ];
  114. renderChatInterface(messages);
  115. const error = screen.getByTestId("error-message");
  116. expect(within(error).getByText("Woops!")).toBeInTheDocument();
  117. expect(within(error).getByText("Something went wrong")).toBeInTheDocument();
  118. });
  119. it("should render feedback actions if there are more than 3 messages", () => {
  120. const messages: Message[] = [
  121. {
  122. sender: "assistant",
  123. content: "Hello",
  124. imageUrls: [],
  125. timestamp: new Date().toISOString(),
  126. },
  127. {
  128. sender: "user",
  129. content: "Hi",
  130. imageUrls: [],
  131. timestamp: new Date().toISOString(),
  132. },
  133. {
  134. sender: "assistant",
  135. content: "How can I help you?",
  136. imageUrls: [],
  137. timestamp: new Date().toISOString(),
  138. },
  139. ];
  140. const { rerender } = renderChatInterface(messages);
  141. expect(screen.queryByTestId("feedback-actions")).not.toBeInTheDocument();
  142. messages.push({
  143. sender: "user",
  144. content: "I need help",
  145. imageUrls: [],
  146. timestamp: new Date().toISOString(),
  147. });
  148. rerender(<ChatInterface />);
  149. expect(screen.getByTestId("feedback-actions")).toBeInTheDocument();
  150. });
  151. describe("feedback", () => {
  152. it.todo("should open the feedback modal when a feedback action is clicked");
  153. it.todo(
  154. "should submit feedback and hide the actions when feedback is shared",
  155. );
  156. it.todo("should render the actions once more after new messages are added");
  157. });
  158. });