| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- /**
- * 分成金额弹窗(册 4 契约 14.2):
- * ① 前两卡=第 6 册已出账分成行聚合(`share_param_summary` 带回 `share_total_cents`/`settled_periods`);
- * 四态须分开:无已出账行=「尚无分成记录」、聚合读失败=「取数失败」、整接口未接线=「后端未下发」;
- * ⛔ 任何一态写成「未配置」(未配置说的是分成方式/取值没配),⛔ 把缺值渲染成 ¥0.00;
- * ② 弹窗只读,配置入口=页脚「去档案里配置分成」⇒ 带 `?edit=<档案 id>` 跳到对应档案名录(分成§5.1 写入口只有档案弹窗)。
- */
- import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
- import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
- import { MemoryRouter, useLocation } from "react-router";
- class ResizeObserverStub { observe() {} unobserve() {} disconnect() {} }
- vi.stubGlobal("ResizeObserver", ResizeObserverStub);
- if (!window.matchMedia) {
- vi.stubGlobal("matchMedia", (query: string) => ({
- matches: false, media: query, onchange: null,
- addEventListener() {}, removeEventListener() {}, addListener() {}, removeListener() {}, dispatchEvent: () => false,
- }));
- }
- vi.mock("../../lib/use-business-view", () => ({
- useBusinessView: () => ({
- identity: { role: "admin", name: "林管" },
- organization: {
- regions: [{ id: "p1", name: "江苏省", level: 0, pathName: "全网/江苏省" }],
- operators: [{ id: "o1", name: "江苏电信", brand: "电信", region: "p1", reconcileDay: null, payCycle: "按年", shareMode: "按比例", shareValue: 0.1 }],
- partners: [],
- operationsTeams: [],
- maintenanceTeams: [],
- enterprises: [],
- },
- customers: [],
- readOnly: false,
- }),
- }));
- vi.mock("../../lib/customer-onboarding", () => ({
- onboardingRegions: () => ["p1"],
- }));
- /** `share_param_summary` 读口桩:逐用例改写(`vi.hoisted` 才能被 mock 工厂闭包引用)。 */
- const shareRead = vi.hoisted(() => ({
- summary: {
- items: new Map<string, unknown>(),
- loading: false,
- fail: null as string | null,
- message: "",
- statsError: "",
- reload: () => {},
- },
- }));
- vi.mock("../../lib/share-summary", () => ({
- shareSummaryKey: (type: string, id: string) => `${type}|${id}`,
- useShareParamSummary: () => shareRead.summary,
- }));
- vi.mock("../../lib/api/bos", () => ({
- billingConfigGet: async () => ({ default_recon_day: 5 }),
- }));
- function summaryItem(extra: Record<string, unknown> = {}) {
- return {
- holder_type: "operator", holder_id: "o1", configured: true,
- share_mode: "ratio", share_value: 0.1, customer_count: 13,
- share_total_cents: null, settled_periods: null, settled_months: null,
- ...extra,
- };
- }
- function prime(items: Record<string, unknown>[], statsError = "", fail: string | null = null) {
- shareRead.summary = {
- items: new Map(items.map((item) => [`${item.holder_type}|${item.holder_id}`, item])),
- loading: false, fail, message: "", statsError, reload: () => {},
- };
- }
- function LocationProbe() {
- const location = useLocation();
- return <output data-testid="location">{`${location.pathname}${location.search}`}</output>;
- }
- async function openDialog() {
- const { ShareRulePage } = await import("../SettlementPages");
- render(<MemoryRouter initialEntries={["/share-rules"]}><ShareRulePage /><LocationProbe /></MemoryRouter>);
- fireEvent.click(await screen.findByRole("button", { name: /分成金额/ }));
- return screen.findByRole("dialog");
- }
- describe("分成金额弹窗", () => {
- beforeEach(() => { cleanup(); prime([summaryItem()]); });
- afterEach(() => cleanup());
- it("尚无已出账分成行时两卡写「尚无分成记录」,⛔ 写「未配置」/¥0.00", async () => {
- prime([summaryItem({ share_total_cents: 0, settled_periods: 0, settled_months: [] })]);
- const dialog = await openDialog();
- expect(within(dialog).getByRole("heading", { name: /江苏电信 · 分成金额/ })).toBeTruthy();
- expect(within(dialog).getAllByText("尚无分成记录")).toHaveLength(2);
- expect(within(dialog).queryByText("未配置")).toBeNull();
- expect(within(dialog).queryByText("¥ 0.00")).toBeNull();
- // 分成方式与取值仍由 share_param_summary 出真实值。
- expect(within(dialog).getAllByText("按比例 10%").length).toBeGreaterThan(0);
- expect(within(dialog).getByText("13 家")).toBeTruthy();
- });
- it("已出账则显示累计金额与期数(分→元、月份倒序注明)", async () => {
- prime([summaryItem({ share_total_cents: 125000, settled_periods: 2, settled_months: ["2026-09", "2026-08"] })]);
- const dialog = await openDialog();
- expect(within(dialog).getByText("¥ 1,250.00")).toBeTruthy();
- expect(within(dialog).getByText("2 期")).toBeTruthy();
- expect(within(dialog).getAllByText(/已出账月份:2026-09、2026-08/).length).toBe(2);
- });
- it("聚合读失败写「取数失败」,⛔ 当成 0/当成尚无记录", async () => {
- prime([summaryItem()], "bill_detail 不可读");
- const dialog = await openDialog();
- expect(within(dialog).getAllByText("取数失败")).toHaveLength(2);
- expect(within(dialog).queryByText("尚无分成记录")).toBeNull();
- });
- it("整接口未接线仍是「后端未下发」(与聚合读失败、尚无记录分开)", async () => {
- prime([], "", "wiring");
- const dialog = await openDialog();
- expect(within(dialog).getAllByText("后端未下发").length).toBeGreaterThanOrEqual(2);
- expect(within(dialog).queryByText("取数失败")).toBeNull();
- expect(within(dialog).queryByText("尚无分成记录")).toBeNull();
- });
- it("「去档案里配置分成」按档案类型带 ?edit=<id> 跳转", async () => {
- const dialog = await openDialog();
- fireEvent.click(within(dialog).getByRole("button", { name: "去档案里配置分成" }));
- await waitFor(() => expect(screen.getByTestId("location").textContent).toBe("/organization/operators?edit=o1"));
- expect(screen.queryByRole("dialog")).toBeNull();
- });
- });
|