work-order-backfill-dialog.test.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { cleanup, fireEvent, render, screen, within } from "@testing-library/react";
  2. import { MemoryRouter, Route, Routes } from "react-router";
  3. import { afterEach, expect, it, vi } from "vitest";
  4. import { uploadTicketMaterial } from "../../lib/api/oss-upload";
  5. import { ticketMaterialAttach, ticketMaterialGrant } from "../../lib/api/ticket";
  6. import { WorkOrderRecordPage } from "../WorkOrderRecordPage";
  7. const { useTicketDetail } = vi.hoisted(() => {
  8. vi.stubGlobal("__SEO_CTX__", undefined);
  9. return { useTicketDetail: vi.fn() };
  10. });
  11. vi.mock("../../lib/ticket", async (load) => ({
  12. ...await load<typeof import("../../lib/ticket")>(),
  13. useTicketDetail,
  14. }));
  15. vi.mock("../../lib/api/oss-upload", () => ({ uploadTicketMaterial: vi.fn() }));
  16. vi.mock("../../lib/api/ticket", async (load) => ({
  17. ...await load<typeof import("../../lib/api/ticket")>(),
  18. ticketMaterialGrant: vi.fn(async () => ({ purpose: "upload", oss_key: "materials/photo.jpg" })),
  19. ticketMaterialAttach: vi.fn(async () => ({ material_id: "photo-1", file_name: "现场照片.jpg" })),
  20. }));
  21. const detail = {
  22. ticket: {
  23. ticket_id: "1001",
  24. ticket_no: "T-20261129-01",
  25. ticket_type: "service_open",
  26. status: "processing",
  27. title: "开通办公线路",
  28. detail: "请完成办公线路核对后回传。",
  29. },
  30. construction_guide: { hint: "核对客户侧接入参数。", pending_accounts: [{ sub_account_id: "ACC-1", display_name: "待开通测试账号" }] },
  31. actions: { check_done: true },
  32. service_items: [{ item_id: "item-1", seq_no: "1", item_name: "办公专线", expect_qty: "0", check_result: "pending" }],
  33. materials: [], messages: [], process_logs: [], gaps: [],
  34. };
  35. function renderRecord(overrides: object = {}) {
  36. useTicketDetail.mockReturnValue({ data: { ...detail, ...overrides }, reload: vi.fn(), reloadAsync: async () => {} });
  37. return render(<MemoryRouter initialEntries={["/work-orders/1001"]}>
  38. <Routes><Route path="/work-orders/:id" element={<WorkOrderRecordPage />} /></Routes>
  39. </MemoryRouter>);
  40. }
  41. afterEach(cleanup);
  42. it.each([
  43. { gap: "route_or_stop_account", focus: "routes", label: "去回填线路", title: "施工回填" },
  44. { gap: "exec_conclusion", focus: "conclusion", label: "去登记结论", title: "登记执行结论" },
  45. ])("从 $label 打开 $title 对话框并定位到对应字段", async ({ gap, focus, label, title }) => {
  46. const prototype = HTMLDialogElement.prototype;
  47. const { showModal, close } = prototype;
  48. Object.assign(prototype, {
  49. showModal(this: HTMLDialogElement) { this.open = true; },
  50. close(this: HTMLDialogElement) { this.open = false; },
  51. });
  52. try {
  53. renderRecord({
  54. ticket: { ...detail.ticket, ticket_type: "open" },
  55. actions: { backfill: true, mark_done: true },
  56. gaps: [gap, "处理记录"],
  57. backfill: { ext: { template_version: 2, schema: { layout: { columns: 3 }, fields: [
  58. { key: "network_vendor", label: "网络侧厂商", type: "select", span: 2, options: [{ value: "a", label: "A 厂" }] },
  59. { key: "acceptance_note", label: "验收补充", type: "textarea" },
  60. { key: "crew", label: "随工人员", type: "text", span: 2 },
  61. { key: "site", label: "施工地点", type: "text", required: true, help: "填写现场地址" },
  62. { key: "connection", label: "接入方式", type: "select", options: [{ value: "fixed", label: "固定" }] },
  63. { key: "verified", label: "已核实", type: "boolean" },
  64. { key: "tests", label: "检测项", type: "multiselect", options: [{ value: "speed", label: "测速" }] },
  65. { key: "ports", label: "端口明细", itemLabel: "端口", type: "array", maxItems: 2, fields: [
  66. { key: "vlan", label: "接入 VLAN", type: "text", format: "vlan", required: true },
  67. ] },
  68. ] } } },
  69. });
  70. const entry = screen.getByRole("button", { name: label });
  71. expect(entry.classList.contains("org-button-scope")).toBe(true);
  72. fireEvent.click(entry);
  73. const dialog = await screen.findByRole("dialog", { name: title });
  74. expect(dialog.classList.contains("ops-content-dialog")).toBe(true);
  75. const controls = within(dialog);
  76. expect(controls.getByRole("button", { name: /^通路 1/, expanded: focus === "routes" })).toBeTruthy();
  77. expect(dialog.querySelector(`[data-gap-focus="${focus}"]`)?.classList.contains("is-gap-focus")).toBe(true);
  78. expect(controls.getByRole("combobox", { name: /执行结论/ })).toBeTruthy();
  79. const upload = controls.getByRole("button", { name: "上传图片" });
  80. const camera = controls.getByRole("button", { name: "拍照" });
  81. const save = controls.getByRole<HTMLButtonElement>("button", { name: "暂存草稿" });
  82. const submit = controls.getByRole<HTMLButtonElement>("button", { name: "提交回填" });
  83. expect(upload.closest(".wo-backfill-panel")).toBe(controls.getByRole("heading", { name: "现场凭证" }).parentElement);
  84. expect(save.closest("footer")).toBeTruthy();
  85. expect(submit.closest("footer")).toBeTruthy();
  86. if (focus === "conclusion") {
  87. const template = within(controls.getByRole("region", { name: "扩展信息" }));
  88. expect(template.getByText("模板 v2").closest("header")).toBeTruthy();
  89. const pairedFields = template.getByRole("combobox", { name: "网络侧厂商" }).closest(".work-order-ext-paired");
  90. expect(pairedFields?.contains(template.getByPlaceholderText("请输入随工人员姓名"))).toBe(true);
  91. expect(pairedFields?.contains(template.getByPlaceholderText("请填写验收补充说明"))).toBe(false);
  92. const site = template.getByRole("textbox", { name: /^施工地点/ });
  93. fireEvent.blur(site);
  94. expect(site.getAttribute("aria-describedby")).toBe(template.getByRole("alert").id);
  95. fireEvent.change(site, { target: { value: "上海" } });
  96. expect(template.queryByRole("alert")).toBeNull();
  97. fireEvent.click(template.getByRole("checkbox", { name: "已核实" }));
  98. expect(template.getByRole<HTMLInputElement>("checkbox", { name: "已核实" }).checked).toBe(true);
  99. const tests = within(template.getByRole("group", { name: "检测项" }));
  100. fireEvent.click(tests.getByRole("checkbox", { name: "测速" }));
  101. expect(tests.getByRole<HTMLInputElement>("checkbox", { name: "测速" }).checked).toBe(true);
  102. const addPort = template.getByRole("button", { name: "添加端口" });
  103. fireEvent.click(addPort);
  104. fireEvent.click(addPort);
  105. expect((addPort as HTMLButtonElement).disabled).toBe(true);
  106. const ports = template.getAllByRole("textbox", { name: /^接入 VLAN/ });
  107. expect(ports[0].id).not.toBe(ports[1].id);
  108. fireEvent.change(ports[1], { target: { value: "200" } });
  109. fireEvent.click(template.getByRole("button", { name: "删除端口 1" }));
  110. expect(template.getByRole<HTMLInputElement>("textbox", { name: /^接入 VLAN/ }).value).toBe("200");
  111. const photo = new File(["photo"], "现场照片.jpg", { type: "image/jpeg" });
  112. const fileInput = dialog.querySelector('input[type="file"]:not([capture])')!;
  113. const cameraInput = dialog.querySelector('input[capture="environment"]')!;
  114. const pickFile = vi.fn();
  115. const takePhoto = vi.fn();
  116. fileInput.addEventListener("click", pickFile);
  117. cameraInput.addEventListener("click", takePhoto);
  118. fireEvent.click(upload);
  119. fireEvent.click(camera);
  120. expect(pickFile).toHaveBeenCalledOnce();
  121. expect(takePhoto).toHaveBeenCalledOnce();
  122. fireEvent.change(fileInput, { target: { files: [photo] } });
  123. expect(submit.disabled && save.disabled).toBe(true);
  124. expect(await controls.findByText("现场照片.jpg")).toBeTruthy();
  125. expect(ticketMaterialGrant).toHaveBeenCalledWith(expect.objectContaining({ kind: "backfill_evidence" }));
  126. expect(uploadTicketMaterial).toHaveBeenCalledWith(photo, "materials/photo.jpg");
  127. expect(ticketMaterialAttach).toHaveBeenCalledWith(expect.objectContaining({ file_name: photo.name }));
  128. expect(submit.disabled || save.disabled).toBe(false);
  129. fireEvent.click(controls.getByRole("button", { name: "移除 现场照片.jpg" }));
  130. expect(controls.queryByText(photo.name)).toBeNull();
  131. }
  132. fireEvent.click(controls.getByRole("button", { name: "取消" }));
  133. expect(screen.queryByRole("dialog", { name: title })).toBeNull();
  134. expect(screen.getByRole("button", { name: "去补处理记录" }).classList.contains("org-button-scope")).toBe(false);
  135. } finally {
  136. cleanup();
  137. Object.assign(prototype, { showModal, close });
  138. }
  139. });