webRemoteControlDeviceHeader.test.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { WebRemoteControlDeviceHeader } from "@/web-remote-control/WebRemoteControlDeviceHeader.js";
  6. vi.mock("@/i18n/IntlProvider.js", () => ({
  7. useZCodeIntl: () => ({
  8. intl: {
  9. formatMessage: ({ id }: { id: string }, values?: { name: string }) => values?.name ?? id,
  10. },
  11. }),
  12. }));
  13. afterEach(cleanup);
  14. describe("远控设备导航", () => {
  15. it.each([undefined, "", " "])(
  16. "缺失或空名称 %s 显示未命名设备,点击才调用入口命令",
  17. (deviceName) => {
  18. const chooseOtherDevice = vi.fn();
  19. const page = render(
  20. createElement(WebRemoteControlDeviceHeader, {
  21. navigation: { deviceName, chooseOtherDevice },
  22. }),
  23. );
  24. expect(page.getByText("webRemoteControl.unnamedDevice")).toBeTruthy();
  25. expect(chooseOtherDevice).not.toHaveBeenCalled();
  26. fireEvent.click(page.getByRole("button", { name: "webRemoteControl.switchDevice" }));
  27. expect(chooseOtherDevice).toHaveBeenCalledOnce();
  28. },
  29. );
  30. it("整行即切换入口:设备名在按钮内单行截断,不再显示“当前设备”前缀", () => {
  31. const chooseOtherDevice = vi.fn();
  32. const page = render(
  33. createElement(WebRemoteControlDeviceHeader, {
  34. navigation: { deviceName: " 研发电脑 ", chooseOtherDevice },
  35. }),
  36. );
  37. const button = page.getByRole("button", { name: "webRemoteControl.switchDevice" });
  38. const name = page.getByText("研发电脑");
  39. expect(name.textContent).toBe("研发电脑");
  40. expect(button.contains(name)).toBe(true);
  41. expect(name.className).toContain("truncate");
  42. expect(button.getAttribute("title")).toBe("webRemoteControl.switchDevice");
  43. expect(button.textContent).toBe("研发电脑");
  44. fireEvent.click(name);
  45. expect(chooseOtherDevice).toHaveBeenCalledOnce();
  46. });
  47. it.each([
  48. ["darwin", "darwin"],
  49. ["win32", "win32"],
  50. [undefined, "unknown"],
  51. ])("平台 %s 显示对应设备图标 %s", (devicePlatform, icon) => {
  52. const page = render(
  53. createElement(WebRemoteControlDeviceHeader, {
  54. navigation: { deviceName: "Mac", devicePlatform, chooseOtherDevice: vi.fn() },
  55. }),
  56. );
  57. const button = page.getByRole("button", { name: "webRemoteControl.switchDevice" });
  58. expect(button.querySelector(`[data-device-platform-icon="${icon}"]`)).not.toBeNull();
  59. });
  60. });