feedback-submit-fixture.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { createElement } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import { FeedbackSubmitForm } from "@/feedback/FeedbackSubmitForm.js";
  4. import { useFeedbackStore } from "@/feedback/feedbackStore.js";
  5. import { PlatformProvider } from "@/hooks/usePlatform.js";
  6. import { RemoteConnectionConnectingStep } from "@/remote-connection/RemoteConnectionConnectingStep.js";
  7. import {
  8. getFeedbackSubmissionJob,
  9. getFeedbackSubmissionJobsSnapshot,
  10. } from "@/feedback/feedbackSubmissionJob.js";
  11. import { TabStoreProvider } from "@/store/TabStoreProvider.js";
  12. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  13. // 仅用于隔离窗口:真实表单/helper/job 调用内存服务,不创建真实工单或读取本机日志。
  14. const calls = { create: 0, prepare: 0, upload: [], cleanup: 0 };
  15. const createInputs = [];
  16. let rejectNextCreate = false;
  17. const feedbackService = {
  18. getDeviceSnapshot: async () => ({}),
  19. create: async (input) => {
  20. createInputs.push(structuredClone(input));
  21. calls.create += 1;
  22. if (rejectNextCreate) {
  23. rejectNextCreate = false;
  24. throw new Error("E2E_FEEDBACK_CREATE_FAILED");
  25. }
  26. return { id: `E2E_FEEDBACK_${calls.create}` };
  27. },
  28. prepareCompactLogArchive: async () => {
  29. calls.prepare += 1;
  30. return { path: "E2E_SYNTHETIC_LOG.zip", size: 4 };
  31. },
  32. uploadAttachmentWithProgress: async (_id, kind) => {
  33. calls.upload.push(kind);
  34. return {};
  35. },
  36. cleanupPreparedLogArchive: async () => {
  37. calls.cleanup += 1;
  38. },
  39. onDynamicUploadProgress: () => () => ({ dispose() {} }),
  40. cancelCreate: async () => {},
  41. };
  42. const container = document.createElement("main");
  43. document.body.append(container);
  44. const root = createRoot(container);
  45. let generation = 0;
  46. function FixtureContent({ submissionJobId }) {
  47. const draft = useFeedbackStore((state) => state.submitDraft);
  48. const kind = new URLSearchParams(window.location.hash.slice(1)).get("remoteKind");
  49. // 从真实连接反馈按钮进入表单,防止仅验证表单默认值而漏掉入口草稿的覆盖。
  50. if (kind && !draft && !submissionJobId) {
  51. return createElement(RemoteConnectionConnectingStep, {
  52. kind,
  53. loading: false,
  54. errorMessage: "E2E_REMOTE_FEEDBACK_FAILED",
  55. logs: [
  56. { id: "1", level: "error", message: "E2E_REMOTE_CONNECTION_LOG", timestamp: "12:00:00" },
  57. ],
  58. onBack() {},
  59. onRetry() {},
  60. });
  61. }
  62. return createElement(FeedbackSubmitForm, {
  63. feedbackService,
  64. platform: {},
  65. submissionJobId,
  66. initialDraft: draft ?? undefined,
  67. onSubmitted() {
  68. if (kind) useFeedbackStore.getState().close();
  69. },
  70. onViewTickets() {},
  71. onCancel() {},
  72. });
  73. }
  74. function mount(submissionJobId) {
  75. root.render(
  76. createElement(
  77. ZCodeIntlProvider,
  78. { initialLocale: "en-US" },
  79. createElement(
  80. TabStoreProvider,
  81. null,
  82. createElement(
  83. PlatformProvider,
  84. { platform: {} },
  85. createElement(FixtureContent, { key: ++generation, submissionJobId }),
  86. ),
  87. ),
  88. ),
  89. );
  90. }
  91. const fixture = {
  92. failNextCreate() {
  93. rejectNextCreate = true;
  94. },
  95. restore(jobId) {
  96. mount(jobId);
  97. },
  98. snapshot() {
  99. return {
  100. calls,
  101. createInputs,
  102. jobs: getFeedbackSubmissionJobsSnapshot().map((state) => ({
  103. id: state.id,
  104. status: state.status,
  105. includeLogs: getFeedbackSubmissionJob(state.id)?.formDraft.includeLogs,
  106. })),
  107. };
  108. },
  109. };
  110. window.feedbackSubmitFixture = fixture;
  111. mount();