feedback-submit-fixture.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { createElement } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import { FeedbackSubmitForm } from "@/feedback/FeedbackSubmitForm.js";
  4. import {
  5. getFeedbackSubmissionJob,
  6. getFeedbackSubmissionJobsSnapshot,
  7. } from "@/feedback/feedbackSubmissionJob.js";
  8. import { TabStoreProvider } from "@/store/TabStoreProvider.js";
  9. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  10. // 仅用于隔离窗口:真实表单/helper/job 调用内存服务,不创建真实工单或读取本机日志。
  11. const calls = { create: 0, prepare: 0, upload: [], cleanup: 0 };
  12. const createInputs = [];
  13. let rejectNextCreate = false;
  14. const feedbackService = {
  15. getDeviceSnapshot: async () => ({}),
  16. create: async (input) => {
  17. createInputs.push(structuredClone(input));
  18. calls.create += 1;
  19. if (rejectNextCreate) {
  20. rejectNextCreate = false;
  21. throw new Error("E2E_FEEDBACK_CREATE_FAILED");
  22. }
  23. return { id: `E2E_FEEDBACK_${calls.create}` };
  24. },
  25. prepareCompactLogArchive: async () => {
  26. calls.prepare += 1;
  27. return { path: "E2E_SYNTHETIC_LOG.zip", size: 4 };
  28. },
  29. uploadAttachmentWithProgress: async (_id, kind) => {
  30. calls.upload.push(kind);
  31. return {};
  32. },
  33. cleanupPreparedLogArchive: async () => {
  34. calls.cleanup += 1;
  35. },
  36. onDynamicUploadProgress: () => () => ({ dispose() {} }),
  37. cancelCreate: async () => {},
  38. };
  39. const container = document.createElement("main");
  40. document.body.append(container);
  41. const root = createRoot(container);
  42. let generation = 0;
  43. function mount(submissionJobId) {
  44. root.render(
  45. createElement(
  46. ZCodeIntlProvider,
  47. { initialLocale: "en-US" },
  48. createElement(
  49. TabStoreProvider,
  50. null,
  51. createElement(FeedbackSubmitForm, {
  52. key: ++generation,
  53. feedbackService,
  54. platform: {},
  55. submissionJobId,
  56. onSubmitted() {},
  57. onViewTickets() {},
  58. onCancel() {},
  59. }),
  60. ),
  61. ),
  62. );
  63. }
  64. const fixture = {
  65. failNextCreate() {
  66. rejectNextCreate = true;
  67. },
  68. restore(jobId) {
  69. mount(jobId);
  70. },
  71. snapshot() {
  72. return {
  73. calls,
  74. createInputs,
  75. jobs: getFeedbackSubmissionJobsSnapshot().map((state) => ({
  76. id: state.id,
  77. status: state.status,
  78. includeLogs: getFeedbackSubmissionJob(state.id)?.formDraft.includeLogs,
  79. })),
  80. };
  81. },
  82. };
  83. window.feedbackSubmitFixture = fixture;
  84. mount();