|
|
@@ -0,0 +1,99 @@
|
|
|
+/**
|
|
|
+ * 「发起提前停机」弹窗:外壳与「全部订阅」的订阅办理弹窗同款(`customer-sub-dialog`)——
|
|
|
+ * 头部小标题+标题+关闭,正文=对象说明+提示+必填原因,底部=取消/确认发起。
|
|
|
+ * 只登记工单(`ticket/create`),实际停机由运维回填。
|
|
|
+ */
|
|
|
+import type { ReactNode } from "react";
|
|
|
+import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
|
|
+import { MemoryRouter } from "react-router";
|
|
|
+import { afterEach, beforeAll, beforeEach, afterAll, expect, it, vi } from "vitest";
|
|
|
+import { CustomerServiceStopAction } from "../CustomerServiceStopAction";
|
|
|
+
|
|
|
+type Props = Parameters<typeof CustomerServiceStopAction>[0];
|
|
|
+
|
|
|
+/** jsdom 未实现原生 `<dialog>` 的 `showModal()`/`close()`:与 operations-dialog/job-runs-dialog 测试同款最小桩。 */
|
|
|
+const dialogPrototype = HTMLDialogElement.prototype;
|
|
|
+const nativeDialog = { showModal: dialogPrototype.showModal, close: dialogPrototype.close };
|
|
|
+
|
|
|
+const { ticketCreate } = vi.hoisted(() => ({ ticketCreate: vi.fn(async () => ({ ticket_id: "TK-1" })) }));
|
|
|
+vi.mock("../../lib/demo-session", () => ({ useDemoIdentity: () => ({ role: "admin" }) }));
|
|
|
+vi.mock("../../lib/api/ticket", () => ({ ticketCreate }));
|
|
|
+
|
|
|
+beforeAll(() => {
|
|
|
+ Object.assign(dialogPrototype, {
|
|
|
+ showModal(this: HTMLDialogElement) { this.open = true; },
|
|
|
+ close(this: HTMLDialogElement) { this.open = false; },
|
|
|
+ });
|
|
|
+});
|
|
|
+beforeEach(() => { ticketCreate.mockClear(); });
|
|
|
+afterEach(cleanup);
|
|
|
+afterAll(() => { Object.assign(dialogPrototype, nativeDialog); });
|
|
|
+
|
|
|
+/** 弹窗经 createPortal 挂在 body 上(⛔ 留在触发表格行的单元格里):一律从 document 取。 */
|
|
|
+function findDialog() {
|
|
|
+ const dialog = document.querySelector("dialog.customer-stop-dialog");
|
|
|
+ if (!(dialog instanceof HTMLDialogElement)) throw new Error("弹窗未渲染");
|
|
|
+ return dialog;
|
|
|
+}
|
|
|
+
|
|
|
+/** `wrap` 可把入口包进表格行等真实容器,验证祖先样式不会漏进弹窗。 */
|
|
|
+function openDialog(props: Partial<Props> = {}, wrap: (node: ReactNode) => ReactNode = (node) => node) {
|
|
|
+ render(<MemoryRouter>{wrap(<CustomerServiceStopAction customerId="C-1" customerName="客户甲" orderId="SUB-9" orderLabel="在用" {...props} />)}</MemoryRouter>);
|
|
|
+ fireEvent.click(screen.getByRole("button", { name: "发起提前停机" }));
|
|
|
+ return findDialog();
|
|
|
+}
|
|
|
+
|
|
|
+it("在表格行内渲染时弹窗挂到 body,⛔ 留在单元格里被 .ops-inline-actions/td 的祖先规则改写", () => {
|
|
|
+ const dialog = openDialog({}, (node) => <table className="ops-table"><tbody><tr><td><div className="ops-inline-actions">{node}</div></td></tr></tbody></table>);
|
|
|
+ expect(dialog.parentElement).toBe(document.body);
|
|
|
+ expect(document.querySelector(".ops-inline-actions dialog")).toBeNull();
|
|
|
+ expect(document.querySelector(".ops-inline-actions button")?.textContent).toBe("发起提前停机");
|
|
|
+});
|
|
|
+
|
|
|
+it("外壳与订阅办理弹窗同款:小标题、标题、关闭,底部依次是取消和确认发起", () => {
|
|
|
+ const dialog = openDialog();
|
|
|
+ expect(dialog.open).toBe(true);
|
|
|
+ expect(dialog.classList.contains("customer-sub-dialog")).toBe(true);
|
|
|
+ expect(within(dialog).getByText("订阅办理")).toBeTruthy();
|
|
|
+ expect(within(dialog).getByRole("heading", { name: "确认发起提前停机" })).toBeTruthy();
|
|
|
+ expect(within(dialog).getByRole("button", { name: "关闭" })).toBeTruthy();
|
|
|
+ expect(dialog.querySelector(".customer-sub-context")?.textContent).toBe("将为客户 客户甲 的订阅 SUB-9(在用)创建提前停机工单。");
|
|
|
+ expect(within(dialog).getByText("实际停机以运维回填为准。订阅起止日和已出账期次保留。")).toBeTruthy();
|
|
|
+ const buttons = within(dialog.querySelector("footer")!).getAllByRole("button");
|
|
|
+ expect(buttons.map((button) => button.textContent)).toEqual(["取消", "确认发起"]);
|
|
|
+});
|
|
|
+
|
|
|
+it.each(["取消", "关闭"])("点「%s」关闭弹窗,不建单", (name) => {
|
|
|
+ const dialog = openDialog();
|
|
|
+ fireEvent.click(within(dialog).getByRole("button", { name }));
|
|
|
+ expect(dialog.open).toBe(false);
|
|
|
+ expect(ticketCreate).not.toHaveBeenCalled();
|
|
|
+});
|
|
|
+
|
|
|
+it("原因不足 2 个字 ⇒ 报错且不建单", () => {
|
|
|
+ const dialog = openDialog();
|
|
|
+ fireEvent.change(within(dialog).getByRole("textbox", { name: /停机原因/ }), { target: { value: "短" } });
|
|
|
+ fireEvent.click(within(dialog).getByRole("button", { name: "确认发起" }));
|
|
|
+ expect(within(dialog).getByRole("alert").textContent).toBe("请填写至少 2 个字的停机原因。");
|
|
|
+ expect(ticketCreate).not.toHaveBeenCalled();
|
|
|
+});
|
|
|
+
|
|
|
+it("未取到订单号 ⇒ 报错且不建单(⛔ 按客户整户停用)", () => {
|
|
|
+ const dialog = openDialog({ orderId: undefined });
|
|
|
+ fireEvent.change(within(dialog).getByRole("textbox", { name: /停机原因/ }), { target: { value: "设备故障,需提前停机" } });
|
|
|
+ fireEvent.click(within(dialog).getByRole("button", { name: "确认发起" }));
|
|
|
+ expect(within(dialog).getByRole("alert").textContent).toBe("未取到本条订单号:提前停机单必须挂在具体订阅上。");
|
|
|
+ expect(ticketCreate).not.toHaveBeenCalled();
|
|
|
+});
|
|
|
+
|
|
|
+it("原因合规 ⇒ 按本条订阅建提前停机工单并关窗", async () => {
|
|
|
+ const dialog = openDialog();
|
|
|
+ fireEvent.change(within(dialog).getByRole("textbox", { name: /停机原因/ }), { target: { value: "设备故障,需提前停机" } });
|
|
|
+ fireEvent.click(within(dialog).getByRole("button", { name: "确认发起" }));
|
|
|
+ await waitFor(() => expect(ticketCreate).toHaveBeenCalledTimes(1));
|
|
|
+ expect(ticketCreate).toHaveBeenCalledWith({
|
|
|
+ ticket_type: "service_stop", customer_id: "C-1", subscription_id: "SUB-9",
|
|
|
+ title: "客户甲 提前停机", detail: "设备故障,需提前停机", confirm: true,
|
|
|
+ });
|
|
|
+ await waitFor(() => expect(dialog.open).toBe(false));
|
|
|
+});
|