resourceManagerPolling.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { act, cleanup, fireEvent, render } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { ResourceManagerApp } from "@/resource-manager/ResourceManagerApp.js";
  6. const { intl } = vi.hoisted(() => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }));
  7. vi.mock("@/i18n/index.js", () => ({ useZCodeIntl: () => ({ intl }) }));
  8. vi.mock("@/resource-manager/storage/StorageSection.js", () => ({ StorageSection: () => null }));
  9. vi.mock("@/resource-manager/NetworkSection.js", () => ({ NetworkSection: () => null }));
  10. afterEach(() => {
  11. cleanup();
  12. vi.useRealTimers();
  13. });
  14. describe("resource manager polling", () => {
  15. it("仅 CPU/内存页启用,切页和卸载停用,慢查询不叠加", async () => {
  16. vi.useFakeTimers();
  17. const getSnapshot = vi.fn(() => new Promise<never>(() => {}));
  18. const setSamplingActive = vi.fn();
  19. const view = render(createElement(ResourceManagerApp, { getSnapshot, setSamplingActive }));
  20. expect(setSamplingActive).toHaveBeenLastCalledWith(true);
  21. await act(() => vi.advanceTimersByTimeAsync(3_000));
  22. expect(getSnapshot).toHaveBeenCalledTimes(1);
  23. fireEvent.mouseDown(view.getByRole("tab", { name: "resourceManager.storage" }), {
  24. button: 0,
  25. ctrlKey: false,
  26. });
  27. expect(setSamplingActive).toHaveBeenLastCalledWith(false);
  28. await act(() => vi.advanceTimersByTimeAsync(3_000));
  29. expect(getSnapshot).toHaveBeenCalledTimes(1);
  30. fireEvent.mouseDown(view.getByRole("tab", { name: "resourceManager.network" }), {
  31. button: 0,
  32. ctrlKey: false,
  33. });
  34. await act(() => vi.advanceTimersByTimeAsync(3_000));
  35. expect(getSnapshot).toHaveBeenCalledTimes(1);
  36. expect(setSamplingActive).toHaveBeenLastCalledWith(false);
  37. fireEvent.mouseDown(view.getByRole("tab", { name: "resourceManager.memory" }), {
  38. button: 0,
  39. ctrlKey: false,
  40. });
  41. expect(setSamplingActive).toHaveBeenLastCalledWith(true);
  42. expect(getSnapshot).toHaveBeenCalledTimes(2);
  43. view.unmount();
  44. expect(setSamplingActive).toHaveBeenLastCalledWith(false);
  45. });
  46. });