| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import { cleanup, fireEvent, render, screen, within } from "@testing-library/react";
- import { MemoryRouter, Route, Routes } from "react-router";
- import { afterEach, expect, it, vi } from "vitest";
- import { uploadTicketMaterial } from "../../lib/api/oss-upload";
- import { ticketMaterialAttach, ticketMaterialGrant } from "../../lib/api/ticket";
- import { WorkOrderRecordPage } from "../WorkOrderRecordPage";
- const { useTicketDetail } = vi.hoisted(() => {
- vi.stubGlobal("__SEO_CTX__", undefined);
- return { useTicketDetail: vi.fn() };
- });
- vi.mock("../../lib/ticket", async (load) => ({
- ...await load<typeof import("../../lib/ticket")>(),
- useTicketDetail,
- }));
- vi.mock("../../lib/api/oss-upload", () => ({ uploadTicketMaterial: vi.fn() }));
- vi.mock("../../lib/api/ticket", async (load) => ({
- ...await load<typeof import("../../lib/api/ticket")>(),
- ticketMaterialGrant: vi.fn(async () => ({ purpose: "upload", oss_key: "materials/photo.jpg" })),
- ticketMaterialAttach: vi.fn(async () => ({ material_id: "photo-1", file_name: "现场照片.jpg" })),
- }));
- const detail = {
- ticket: {
- ticket_id: "1001",
- ticket_no: "T-20261129-01",
- ticket_type: "service_open",
- status: "processing",
- title: "开通办公线路",
- detail: "请完成办公线路核对后回传。",
- },
- construction_guide: { hint: "核对客户侧接入参数。", pending_accounts: [{ sub_account_id: "ACC-1", display_name: "待开通测试账号" }] },
- actions: { check_done: true },
- service_items: [{ item_id: "item-1", seq_no: "1", item_name: "办公专线", expect_qty: "0", check_result: "pending" }],
- materials: [], messages: [], process_logs: [], gaps: [],
- };
- function renderRecord(overrides: object = {}) {
- useTicketDetail.mockReturnValue({ data: { ...detail, ...overrides }, reload: vi.fn(), reloadAsync: async () => {} });
- return render(<MemoryRouter initialEntries={["/work-orders/1001"]}>
- <Routes><Route path="/work-orders/:id" element={<WorkOrderRecordPage />} /></Routes>
- </MemoryRouter>);
- }
- afterEach(cleanup);
- it.each([
- { gap: "route_or_stop_account", focus: "routes", label: "去回填线路", title: "施工回填" },
- { gap: "exec_conclusion", focus: "conclusion", label: "去登记结论", title: "登记执行结论" },
- ])("从 $label 打开 $title 对话框并定位到对应字段", async ({ gap, focus, label, title }) => {
- const prototype = HTMLDialogElement.prototype;
- const { showModal, close } = prototype;
- Object.assign(prototype, {
- showModal(this: HTMLDialogElement) { this.open = true; },
- close(this: HTMLDialogElement) { this.open = false; },
- });
- try {
- renderRecord({
- ticket: { ...detail.ticket, ticket_type: "open" },
- actions: { backfill: true, mark_done: true },
- gaps: [gap, "处理记录"],
- backfill: { ext: { template_version: 2, schema: { layout: { columns: 3 }, fields: [
- { key: "network_vendor", label: "网络侧厂商", type: "select", span: 2, options: [{ value: "a", label: "A 厂" }] },
- { key: "acceptance_note", label: "验收补充", type: "textarea" },
- { key: "crew", label: "随工人员", type: "text", span: 2 },
- { key: "site", label: "施工地点", type: "text", required: true, help: "填写现场地址" },
- { key: "connection", label: "接入方式", type: "select", options: [{ value: "fixed", label: "固定" }] },
- { key: "verified", label: "已核实", type: "boolean" },
- { key: "tests", label: "检测项", type: "multiselect", options: [{ value: "speed", label: "测速" }] },
- { key: "ports", label: "端口明细", itemLabel: "端口", type: "array", maxItems: 2, fields: [
- { key: "vlan", label: "接入 VLAN", type: "text", format: "vlan", required: true },
- ] },
- ] } } },
- });
- const entry = screen.getByRole("button", { name: label });
- expect(entry.classList.contains("org-button-scope")).toBe(true);
- fireEvent.click(entry);
- const dialog = await screen.findByRole("dialog", { name: title });
- expect(dialog.classList.contains("ops-content-dialog")).toBe(true);
- const controls = within(dialog);
- expect(controls.getByRole("button", { name: /^通路 1/, expanded: focus === "routes" })).toBeTruthy();
- expect(dialog.querySelector(`[data-gap-focus="${focus}"]`)?.classList.contains("is-gap-focus")).toBe(true);
- expect(controls.getByRole("combobox", { name: /执行结论/ })).toBeTruthy();
- const upload = controls.getByRole("button", { name: "上传图片" });
- const camera = controls.getByRole("button", { name: "拍照" });
- const save = controls.getByRole<HTMLButtonElement>("button", { name: "暂存草稿" });
- const submit = controls.getByRole<HTMLButtonElement>("button", { name: "提交回填" });
- expect(upload.closest(".wo-backfill-panel")).toBe(controls.getByRole("heading", { name: "现场凭证" }).parentElement);
- expect(save.closest("footer")).toBeTruthy();
- expect(submit.closest("footer")).toBeTruthy();
- if (focus === "conclusion") {
- const template = within(controls.getByRole("region", { name: "扩展信息" }));
- expect(template.getByText("模板 v2").closest("header")).toBeTruthy();
- const pairedFields = template.getByRole("combobox", { name: "网络侧厂商" }).closest(".work-order-ext-paired");
- expect(pairedFields?.contains(template.getByPlaceholderText("请输入随工人员姓名"))).toBe(true);
- expect(pairedFields?.contains(template.getByPlaceholderText("请填写验收补充说明"))).toBe(false);
- const site = template.getByRole("textbox", { name: /^施工地点/ });
- fireEvent.blur(site);
- expect(site.getAttribute("aria-describedby")).toBe(template.getByRole("alert").id);
- fireEvent.change(site, { target: { value: "上海" } });
- expect(template.queryByRole("alert")).toBeNull();
- fireEvent.click(template.getByRole("checkbox", { name: "已核实" }));
- expect(template.getByRole<HTMLInputElement>("checkbox", { name: "已核实" }).checked).toBe(true);
- const tests = within(template.getByRole("group", { name: "检测项" }));
- fireEvent.click(tests.getByRole("checkbox", { name: "测速" }));
- expect(tests.getByRole<HTMLInputElement>("checkbox", { name: "测速" }).checked).toBe(true);
- const addPort = template.getByRole("button", { name: "添加端口" });
- fireEvent.click(addPort);
- fireEvent.click(addPort);
- expect((addPort as HTMLButtonElement).disabled).toBe(true);
- const ports = template.getAllByRole("textbox", { name: /^接入 VLAN/ });
- expect(ports[0].id).not.toBe(ports[1].id);
- fireEvent.change(ports[1], { target: { value: "200" } });
- fireEvent.click(template.getByRole("button", { name: "删除端口 1" }));
- expect(template.getByRole<HTMLInputElement>("textbox", { name: /^接入 VLAN/ }).value).toBe("200");
- const photo = new File(["photo"], "现场照片.jpg", { type: "image/jpeg" });
- const fileInput = dialog.querySelector('input[type="file"]:not([capture])')!;
- const cameraInput = dialog.querySelector('input[capture="environment"]')!;
- const pickFile = vi.fn();
- const takePhoto = vi.fn();
- fileInput.addEventListener("click", pickFile);
- cameraInput.addEventListener("click", takePhoto);
- fireEvent.click(upload);
- fireEvent.click(camera);
- expect(pickFile).toHaveBeenCalledOnce();
- expect(takePhoto).toHaveBeenCalledOnce();
- fireEvent.change(fileInput, { target: { files: [photo] } });
- expect(submit.disabled && save.disabled).toBe(true);
- expect(await controls.findByText("现场照片.jpg")).toBeTruthy();
- expect(ticketMaterialGrant).toHaveBeenCalledWith(expect.objectContaining({ kind: "backfill_evidence" }));
- expect(uploadTicketMaterial).toHaveBeenCalledWith(photo, "materials/photo.jpg");
- expect(ticketMaterialAttach).toHaveBeenCalledWith(expect.objectContaining({ file_name: photo.name }));
- expect(submit.disabled || save.disabled).toBe(false);
- fireEvent.click(controls.getByRole("button", { name: "移除 现场照片.jpg" }));
- expect(controls.queryByText(photo.name)).toBeNull();
- }
- fireEvent.click(controls.getByRole("button", { name: "取消" }));
- expect(screen.queryByRole("dialog", { name: title })).toBeNull();
- expect(screen.getByRole("button", { name: "去补处理记录" }).classList.contains("org-button-scope")).toBe(false);
- } finally {
- cleanup();
- Object.assign(prototype, { showModal, close });
- }
- });
|