FileExplorer.test.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { screen } from "@testing-library/react";
  2. import userEvent from "@testing-library/user-event";
  3. import { renderWithProviders } from "test-utils";
  4. import { describe, it, expect, vi, Mock, afterEach } from "vitest";
  5. import toast from "#/utils/toast";
  6. import AgentState from "#/types/AgentState";
  7. import FileExplorer from "#/components/file-explorer/FileExplorer";
  8. import OpenHands from "#/api/open-hands";
  9. const toastSpy = vi.spyOn(toast, "error");
  10. const uploadFilesSpy = vi.spyOn(OpenHands, "uploadFiles");
  11. const getFilesSpy = vi.spyOn(OpenHands, "getFiles");
  12. vi.mock("../../services/fileService", async () => ({
  13. uploadFiles: vi.fn(),
  14. }));
  15. const renderFileExplorerWithRunningAgentState = () =>
  16. renderWithProviders(<FileExplorer error={null} />, {
  17. preloadedState: {
  18. agent: {
  19. curAgentState: AgentState.RUNNING,
  20. },
  21. },
  22. });
  23. describe.skip("FileExplorer", () => {
  24. afterEach(() => {
  25. vi.clearAllMocks();
  26. });
  27. it("should get the workspace directory", async () => {
  28. renderFileExplorerWithRunningAgentState();
  29. expect(await screen.findByText("folder1")).toBeInTheDocument();
  30. expect(await screen.findByText("file1.ts")).toBeInTheDocument();
  31. expect(getFilesSpy).toHaveBeenCalledTimes(1); // once for root
  32. });
  33. it.todo("should render an empty workspace");
  34. it("should refetch the workspace when clicking the refresh button", async () => {
  35. const user = userEvent.setup();
  36. renderFileExplorerWithRunningAgentState();
  37. expect(await screen.findByText("folder1")).toBeInTheDocument();
  38. expect(await screen.findByText("file1.ts")).toBeInTheDocument();
  39. expect(getFilesSpy).toHaveBeenCalledTimes(1); // once for root
  40. const refreshButton = screen.getByTestId("refresh");
  41. await user.click(refreshButton);
  42. expect(getFilesSpy).toHaveBeenCalledTimes(2); // once for root, once for refresh button
  43. });
  44. it("should toggle the explorer visibility when clicking the toggle button", async () => {
  45. const user = userEvent.setup();
  46. renderFileExplorerWithRunningAgentState();
  47. const folder1 = await screen.findByText("folder1");
  48. expect(folder1).toBeInTheDocument();
  49. const toggleButton = screen.getByTestId("toggle");
  50. await user.click(toggleButton);
  51. expect(folder1).toBeInTheDocument();
  52. expect(folder1).not.toBeVisible();
  53. });
  54. it("should upload files", async () => {
  55. const user = userEvent.setup();
  56. renderFileExplorerWithRunningAgentState();
  57. const file = new File([""], "file-name");
  58. const uploadFileInput = await screen.findByTestId("file-input");
  59. await user.upload(uploadFileInput, file);
  60. // TODO: Improve this test by passing expected argument to `uploadFiles`
  61. expect(uploadFilesSpy).toHaveBeenCalledOnce();
  62. expect(getFilesSpy).toHaveBeenCalled();
  63. const file2 = new File([""], "file-name-2");
  64. const uploadDirInput = await screen.findByTestId("file-input");
  65. await user.upload(uploadDirInput, [file, file2]);
  66. expect(uploadFilesSpy).toHaveBeenCalledTimes(2);
  67. expect(getFilesSpy).toHaveBeenCalled();
  68. });
  69. it.todo("should upload files when dragging them to the explorer", () => {
  70. // It will require too much work to mock drag logic, especially for our case
  71. // https://github.com/testing-library/user-event/issues/440#issuecomment-685010755
  72. // TODO: should be tested in an e2e environment such as Cypress/Playwright
  73. });
  74. it.todo("should download a file");
  75. it("should display an error toast if file upload fails", async () => {
  76. (uploadFilesSpy as Mock).mockRejectedValue(new Error());
  77. const user = userEvent.setup();
  78. renderFileExplorerWithRunningAgentState();
  79. const uploadFileInput = await screen.findByTestId("file-input");
  80. const file = new File([""], "test");
  81. await user.upload(uploadFileInput, file);
  82. expect(uploadFilesSpy).rejects.toThrow();
  83. expect(toastSpy).toHaveBeenCalledWith(
  84. expect.stringContaining("upload-error"),
  85. expect.any(String),
  86. );
  87. });
  88. });