overlayWindowControls.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  4. import { afterEach, expect, it, vi } from "vitest";
  5. import { DesktopCommandIds } from "@zcode/shared";
  6. import { EmbeddedWebsiteHeader } from "@/components/EmbeddedWebsiteHeader.js";
  7. const { executeDesktopCommand } = vi.hoisted(() => ({
  8. executeDesktopCommand: vi.fn().mockResolvedValue(undefined),
  9. }));
  10. vi.mock("@/hooks/usePlatform.js", () => ({
  11. usePlatform: () => ({ executeDesktopCommand }),
  12. }));
  13. vi.mock("@/i18n/IntlProvider.js", () => ({
  14. useZCodeIntl: () => ({
  15. intl: { formatMessage: ({ id }: { id: string }) => id },
  16. }),
  17. }));
  18. afterEach(() => {
  19. cleanup();
  20. vi.clearAllMocks();
  21. });
  22. it("升级和奖励的网页关闭与窗口关闭分开,三种窗口操作走现有命令", () => {
  23. const onClose = vi.fn();
  24. render(
  25. createElement(EmbeddedWebsiteHeader, {
  26. title: "Rewards",
  27. loading: false,
  28. canGoBack: false,
  29. canGoForward: false,
  30. onBack: vi.fn(),
  31. onForward: vi.fn(),
  32. onReload: vi.fn(),
  33. onClose,
  34. }),
  35. );
  36. fireEvent.click(screen.getByRole("button", { name: "common.close" }));
  37. expect(onClose).toHaveBeenCalledTimes(1);
  38. expect(executeDesktopCommand).not.toHaveBeenCalled();
  39. for (const [id, command] of [
  40. ["minimize", DesktopCommandIds.MinimizeWindow],
  41. ["maximize", DesktopCommandIds.ToggleMaximizeWindow],
  42. ["close", DesktopCommandIds.CloseWindow],
  43. ]) {
  44. fireEvent.click(screen.getByTestId(`window-control-${id}`));
  45. expect(executeDesktopCommand).toHaveBeenLastCalledWith(command);
  46. }
  47. expect(onClose).toHaveBeenCalledTimes(1);
  48. });