pluginUiSidePaneVisibility.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // @vitest-environment jsdom
  2. import { cleanup, render } from "@testing-library/react";
  3. import { createElement } from "react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { PluginUiSidePaneView } from "@/plugin-ui/components/PluginUiSidePane.js";
  6. import type { PluginUiSidePaneViewProps } from "@/plugin-ui/contract.js";
  7. const { bind, release } = vi.hoisted(() => {
  8. const release = vi.fn();
  9. return { bind: vi.fn(() => release), release };
  10. });
  11. vi.mock("@/plugin-ui/adapters/pluginUiPageRegistry.js", () => ({ bindPluginUiPage: bind }));
  12. afterEach(() => {
  13. cleanup();
  14. vi.clearAllMocks();
  15. });
  16. const props: PluginUiSidePaneViewProps = {
  17. visible: true,
  18. scopeRef: { kind: "surface", surfaceId: "editor" },
  19. host: {
  20. pageKey: "fixture-page",
  21. handle: null,
  22. phase: "preparing",
  23. height: 240,
  24. error: null,
  25. reload: vi.fn(),
  26. },
  27. loadingLabel: "loading",
  28. errorLabel: "error",
  29. retryLabel: "retry",
  30. onWidthChange: vi.fn(),
  31. };
  32. describe("plugin side pane visibility", () => {
  33. it("unbinds a retained hidden tab synchronously and reuses its anchor when reopened", () => {
  34. const view = render(createElement(PluginUiSidePaneView, props));
  35. const anchor = view.getByTestId("plugin-ui-card");
  36. expect(bind).toHaveBeenCalledWith("fixture-page", anchor, true);
  37. view.rerender(createElement(PluginUiSidePaneView, { ...props, visible: false }));
  38. expect(release).toHaveBeenCalledTimes(1);
  39. expect(anchor.isConnected).toBe(true);
  40. expect(bind).toHaveBeenCalledTimes(1);
  41. view.rerender(createElement(PluginUiSidePaneView, props));
  42. expect(bind).toHaveBeenCalledTimes(2);
  43. expect(bind).toHaveBeenLastCalledWith("fixture-page", anchor, true);
  44. });
  45. it("does not bind a page created while its tab is hidden", () => {
  46. const view = render(createElement(PluginUiSidePaneView, { ...props, visible: false }));
  47. view.rerender(
  48. createElement(PluginUiSidePaneView, {
  49. ...props,
  50. visible: false,
  51. host: { ...props.host, phase: "running" },
  52. }),
  53. );
  54. expect(bind).not.toHaveBeenCalled();
  55. view.rerender(createElement(PluginUiSidePaneView, props));
  56. expect(bind).toHaveBeenCalledTimes(1);
  57. });
  58. });