automationsPageTabs.test.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // @vitest-environment jsdom
  2. // 自动化页顶级标签「自动化 / 工作流」(docs/dynamic-workflow/launch.md「Where it lives」「标签记忆」)。
  3. import { createElement, useState } from "react";
  4. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  5. import { afterEach, describe, expect, it } from "vitest";
  6. import { TID_AUTOMATIONS_PAGE_TAB } from "@zcode/shared";
  7. import { testId } from "@zcode/shared/test-ids";
  8. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  9. import {
  10. AutomationsPageTitleSwitch,
  11. type AutomationsPageTab,
  12. } from "@/settings/saved-workflows/AutomationsPageTitleSwitch.js";
  13. import {
  14. readAutomationsPageTab,
  15. writeAutomationsPageTab,
  16. } from "@/settings/saved-workflows/automationsPageTabMemory.js";
  17. function Harness({ initial = "automation" }: { initial?: AutomationsPageTab }) {
  18. const [tab, setTab] = useState<AutomationsPageTab>(initial);
  19. return createElement(
  20. ZCodeIntlProvider,
  21. { initialLocale: "zh-CN" },
  22. createElement(AutomationsPageTitleSwitch, { value: tab, onValueChange: setTab }),
  23. );
  24. }
  25. describe("AutomationsPageTitleSwitch", () => {
  26. afterEach(() => {
  27. cleanup();
  28. sessionStorage.clear();
  29. });
  30. it("两个标题词是 tablist 里的 tab,选中态跟随 value", () => {
  31. render(createElement(Harness));
  32. const tabs = screen.getAllByRole("tab");
  33. expect(tabs.map((tab) => tab.textContent)).toEqual(["自动化", "工作流"]);
  34. expect(tabs[0]?.getAttribute("aria-selected")).toBe("true");
  35. expect(tabs[1]?.getAttribute("aria-selected")).toBe("false");
  36. // 只有选中项进 Tab 序;未选中项靠方向键。
  37. expect(tabs[0]?.getAttribute("tabindex")).toBe("0");
  38. expect(tabs[1]?.getAttribute("tabindex")).toBe("-1");
  39. fireEvent.click(screen.getByTestId(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow")));
  40. expect(
  41. screen
  42. .getByTestId(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"))
  43. .getAttribute("aria-selected"),
  44. ).toBe("true");
  45. });
  46. it("方向键在两个标签间循环切换", () => {
  47. render(createElement(Harness));
  48. const tablist = screen.getByRole("tablist");
  49. fireEvent.keyDown(tablist, { key: "ArrowRight" });
  50. expect(
  51. screen
  52. .getByTestId(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"))
  53. .getAttribute("aria-selected"),
  54. ).toBe("true");
  55. fireEvent.keyDown(tablist, { key: "ArrowRight" });
  56. expect(
  57. screen
  58. .getByTestId(testId(TID_AUTOMATIONS_PAGE_TAB, "automation"))
  59. .getAttribute("aria-selected"),
  60. ).toBe("true");
  61. fireEvent.keyDown(tablist, { key: "ArrowLeft" });
  62. expect(
  63. screen
  64. .getByTestId(testId(TID_AUTOMATIONS_PAGE_TAB, "workflow"))
  65. .getAttribute("aria-selected"),
  66. ).toBe("true");
  67. });
  68. });
  69. describe("automationsPageTabMemory", () => {
  70. afterEach(() => {
  71. sessionStorage.clear();
  72. });
  73. it("app 级单 key 记在 sessionStorage,不再按项目分桶;未知值回默认", () => {
  74. expect(readAutomationsPageTab()).toBe("automation");
  75. writeAutomationsPageTab("workflow");
  76. expect(readAutomationsPageTab()).toBe("workflow");
  77. // 只用一个 key,与活动项目无关。
  78. expect(sessionStorage.length).toBe(1);
  79. expect(sessionStorage.getItem("zcode-automations-page-tab")).toBe("workflow");
  80. sessionStorage.setItem("zcode-automations-page-tab", "bogus");
  81. expect(readAutomationsPageTab()).toBe("automation");
  82. writeAutomationsPageTab("automation");
  83. expect(readAutomationsPageTab()).toBe("automation");
  84. expect(sessionStorage.length).toBe(1);
  85. });
  86. });