featureRequestDialog.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import type { IFeedbackService } from "@zcode/services";
  5. import {
  6. FeatureRequestDialog,
  7. buildFeatureRequestDescription,
  8. } from "@/feedback/FeatureRequestDialog.js";
  9. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  10. const capturedFeatureRequestStore = vi.hoisted(() => ({
  11. open: true,
  12. close: vi.fn(),
  13. openTickets: vi.fn(),
  14. }));
  15. vi.mock("@/feedback/feedbackStore.js", () => ({
  16. useFeedbackStore: (
  17. selector: (state: {
  18. featureRequestOpen: boolean;
  19. close: () => void;
  20. openTickets: (ticketId?: string) => void;
  21. }) => unknown,
  22. ) =>
  23. selector({
  24. featureRequestOpen: capturedFeatureRequestStore.open,
  25. close: capturedFeatureRequestStore.close,
  26. openTickets: capturedFeatureRequestStore.openTickets,
  27. }),
  28. }));
  29. vi.mock("@/components/ui/dialog.js", () => ({
  30. Dialog: ({ open, children }: { open?: boolean; children: ReactNode }) =>
  31. open ? createElement("div", null, children) : null,
  32. DialogContent: ({ children, ...props }: { children: ReactNode; className?: string }) =>
  33. createElement("div", props, children),
  34. DialogHeader: ({ children, ...props }: { children: ReactNode; className?: string }) =>
  35. createElement("div", props, children),
  36. DialogTitle: ({ children, ...props }: { children: ReactNode; className?: string }) =>
  37. createElement("h2", props, children),
  38. }));
  39. vi.mock("@/components/ui/toast.js", () => ({
  40. toast: vi.fn(),
  41. }));
  42. function createFeedbackService(): IFeedbackService {
  43. return {
  44. getDeviceSnapshot: vi.fn(),
  45. create: vi.fn(),
  46. list: vi.fn(),
  47. get: vi.fn(),
  48. comment: vi.fn(),
  49. uploadAttachmentData: vi.fn(),
  50. uploadAttachmentWithProgress: vi.fn(),
  51. prepareCompactLogArchive: vi.fn(),
  52. cleanupPreparedLogArchive: vi.fn(),
  53. onDynamicUploadProgress: vi.fn(),
  54. cancelCreate: vi.fn(),
  55. cancelUpload: vi.fn(),
  56. onDynamicUpdate: vi.fn(),
  57. } as unknown as IFeedbackService;
  58. }
  59. describe("FeatureRequestDialog", () => {
  60. it("renders a dedicated product request form instead of bug feedback controls", () => {
  61. const html = renderToStaticMarkup(
  62. createElement(
  63. ZCodeIntlProvider,
  64. { initialLocale: "zh-CN" },
  65. createElement(FeatureRequestDialog, {
  66. feedbackService: createFeedbackService(),
  67. }),
  68. ),
  69. );
  70. expect(html).toContain("给产品提需求");
  71. expect(html).toContain("需求描述");
  72. expect(html).toContain("期望的解决方案");
  73. expect(html).toContain("重置内容");
  74. expect(html).toContain("提交需求");
  75. expect(html).not.toContain("截图");
  76. expect(html).not.toContain("上传诊断日志");
  77. });
  78. it("aligns the feature request shell with the feedback dialog style", () => {
  79. const html = renderToStaticMarkup(
  80. createElement(
  81. ZCodeIntlProvider,
  82. { initialLocale: "zh-CN" },
  83. createElement(FeatureRequestDialog, {
  84. feedbackService: createFeedbackService(),
  85. }),
  86. ),
  87. );
  88. expect(html).toContain("h-[min(38rem,calc(100vh-2rem))]");
  89. expect(html).toContain("w-[min(34rem,calc(100vw-2rem))]");
  90. expect(html).toContain("p-6 pb-0");
  91. expect(html).toContain("px-6 pb-4 pt-3");
  92. expect(html).toContain("px-6 pb-6 pt-2");
  93. expect(html).not.toContain("border-b border-border");
  94. expect(html).not.toContain("border-t border-border");
  95. });
  96. it("产品需求输入框使用固定尺寸并允许长连续文本换行", () => {
  97. const html = renderToStaticMarkup(
  98. createElement(
  99. ZCodeIntlProvider,
  100. { initialLocale: "zh-CN" },
  101. createElement(FeatureRequestDialog, {
  102. feedbackService: createFeedbackService(),
  103. }),
  104. ),
  105. );
  106. expect(html).toContain("field-sizing-fixed");
  107. expect(html).toContain("max-w-full");
  108. expect(html).toContain("min-w-0");
  109. expect(html).toContain("whitespace-pre-wrap");
  110. expect(html).toContain("break-words");
  111. expect(html).not.toContain("field-sizing-content");
  112. });
  113. it("builds a structured feature request description for feedback service", () => {
  114. const mockFormatMessage = (id: string) => {
  115. const map: Record<string, string> = {
  116. "feedback.featureRequest.descriptionLabel": "需求描述",
  117. "feedback.featureRequest.solutionLabel": "期望的解决方案",
  118. "feedback.submit.template.section.featureSource": "来源",
  119. };
  120. return map[id] ?? id;
  121. };
  122. expect(
  123. buildFeatureRequestDescription({
  124. description: "希望支持快捷指令",
  125. solution: "输入框旁增加快捷菜单",
  126. source: "Workspace Header 帮助菜单 / 给产品提需求",
  127. formatMessage: mockFormatMessage,
  128. }),
  129. ).toBe(
  130. [
  131. "## 需求描述",
  132. "希望支持快捷指令",
  133. "",
  134. "## 期望的解决方案",
  135. "输入框旁增加快捷菜单",
  136. "",
  137. "## 来源",
  138. "Workspace Header 帮助菜单 / 给产品提需求",
  139. ].join("\n"),
  140. );
  141. });
  142. });