share-amount-dialog.test.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * 分成金额弹窗(册 4 契约 14.2):
  3. * ① 前两卡=第 6 册已出账分成行聚合(`share_param_summary` 带回 `share_total_cents`/`settled_periods`);
  4. * 四态须分开:无已出账行=「尚无分成记录」、聚合读失败=「取数失败」、整接口未接线=「后端未下发」;
  5. * ⛔ 任何一态写成「未配置」(未配置说的是分成方式/取值没配),⛔ 把缺值渲染成 ¥0.00;
  6. * ② 弹窗只读,配置入口=页脚「去档案里配置分成」⇒ 带 `?edit=<档案 id>` 跳到对应档案名录(分成§5.1 写入口只有档案弹窗)。
  7. */
  8. import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
  9. import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
  10. import { MemoryRouter, useLocation } from "react-router";
  11. class ResizeObserverStub { observe() {} unobserve() {} disconnect() {} }
  12. vi.stubGlobal("ResizeObserver", ResizeObserverStub);
  13. if (!window.matchMedia) {
  14. vi.stubGlobal("matchMedia", (query: string) => ({
  15. matches: false, media: query, onchange: null,
  16. addEventListener() {}, removeEventListener() {}, addListener() {}, removeListener() {}, dispatchEvent: () => false,
  17. }));
  18. }
  19. vi.mock("../../lib/use-business-view", () => ({
  20. useBusinessView: () => ({
  21. identity: { role: "admin", name: "林管" },
  22. organization: {
  23. regions: [{ id: "p1", name: "江苏省", level: 0, pathName: "全网/江苏省" }],
  24. operators: [{ id: "o1", name: "江苏电信", brand: "电信", region: "p1", reconcileDay: null, payCycle: "按年", shareMode: "按比例", shareValue: 0.1 }],
  25. partners: [],
  26. operationsTeams: [],
  27. maintenanceTeams: [],
  28. enterprises: [],
  29. },
  30. customers: [],
  31. readOnly: false,
  32. }),
  33. }));
  34. vi.mock("../../lib/customer-onboarding", () => ({
  35. onboardingRegions: () => ["p1"],
  36. }));
  37. /** `share_param_summary` 读口桩:逐用例改写(`vi.hoisted` 才能被 mock 工厂闭包引用)。 */
  38. const shareRead = vi.hoisted(() => ({
  39. summary: {
  40. items: new Map<string, unknown>(),
  41. loading: false,
  42. fail: null as string | null,
  43. message: "",
  44. statsError: "",
  45. reload: () => {},
  46. },
  47. }));
  48. vi.mock("../../lib/share-summary", () => ({
  49. shareSummaryKey: (type: string, id: string) => `${type}|${id}`,
  50. useShareParamSummary: () => shareRead.summary,
  51. }));
  52. vi.mock("../../lib/api/bos", () => ({
  53. billingConfigGet: async () => ({ default_recon_day: 5 }),
  54. }));
  55. function summaryItem(extra: Record<string, unknown> = {}) {
  56. return {
  57. holder_type: "operator", holder_id: "o1", configured: true,
  58. share_mode: "ratio", share_value: 0.1, customer_count: 13,
  59. share_total_cents: null, settled_periods: null, settled_months: null,
  60. ...extra,
  61. };
  62. }
  63. function prime(items: Record<string, unknown>[], statsError = "", fail: string | null = null) {
  64. shareRead.summary = {
  65. items: new Map(items.map((item) => [`${item.holder_type}|${item.holder_id}`, item])),
  66. loading: false, fail, message: "", statsError, reload: () => {},
  67. };
  68. }
  69. function LocationProbe() {
  70. const location = useLocation();
  71. return <output data-testid="location">{`${location.pathname}${location.search}`}</output>;
  72. }
  73. async function openDialog() {
  74. const { ShareRulePage } = await import("../SettlementPages");
  75. render(<MemoryRouter initialEntries={["/share-rules"]}><ShareRulePage /><LocationProbe /></MemoryRouter>);
  76. fireEvent.click(await screen.findByRole("button", { name: /分成金额/ }));
  77. return screen.findByRole("dialog");
  78. }
  79. describe("分成金额弹窗", () => {
  80. beforeEach(() => { cleanup(); prime([summaryItem()]); });
  81. afterEach(() => cleanup());
  82. it("尚无已出账分成行时两卡写「尚无分成记录」,⛔ 写「未配置」/¥0.00", async () => {
  83. prime([summaryItem({ share_total_cents: 0, settled_periods: 0, settled_months: [] })]);
  84. const dialog = await openDialog();
  85. expect(within(dialog).getByRole("heading", { name: /江苏电信 · 分成金额/ })).toBeTruthy();
  86. expect(within(dialog).getAllByText("尚无分成记录")).toHaveLength(2);
  87. expect(within(dialog).queryByText("未配置")).toBeNull();
  88. expect(within(dialog).queryByText("¥ 0.00")).toBeNull();
  89. // 分成方式与取值仍由 share_param_summary 出真实值。
  90. expect(within(dialog).getAllByText("按比例 10%").length).toBeGreaterThan(0);
  91. expect(within(dialog).getByText("13 家")).toBeTruthy();
  92. });
  93. it("已出账则显示累计金额与期数(分→元、月份倒序注明)", async () => {
  94. prime([summaryItem({ share_total_cents: 125000, settled_periods: 2, settled_months: ["2026-09", "2026-08"] })]);
  95. const dialog = await openDialog();
  96. expect(within(dialog).getByText("¥ 1,250.00")).toBeTruthy();
  97. expect(within(dialog).getByText("2 期")).toBeTruthy();
  98. expect(within(dialog).getAllByText(/已出账月份:2026-09、2026-08/).length).toBe(2);
  99. });
  100. it("聚合读失败写「取数失败」,⛔ 当成 0/当成尚无记录", async () => {
  101. prime([summaryItem()], "bill_detail 不可读");
  102. const dialog = await openDialog();
  103. expect(within(dialog).getAllByText("取数失败")).toHaveLength(2);
  104. expect(within(dialog).queryByText("尚无分成记录")).toBeNull();
  105. });
  106. it("整接口未接线仍是「后端未下发」(与聚合读失败、尚无记录分开)", async () => {
  107. prime([], "", "wiring");
  108. const dialog = await openDialog();
  109. expect(within(dialog).getAllByText("后端未下发").length).toBeGreaterThanOrEqual(2);
  110. expect(within(dialog).queryByText("取数失败")).toBeNull();
  111. expect(within(dialog).queryByText("尚无分成记录")).toBeNull();
  112. });
  113. it("「去档案里配置分成」按档案类型带 ?edit=<id> 跳转", async () => {
  114. const dialog = await openDialog();
  115. fireEvent.click(within(dialog).getByRole("button", { name: "去档案里配置分成" }));
  116. await waitFor(() => expect(screen.getByTestId("location").textContent).toBe("/organization/operators?edit=o1"));
  117. expect(screen.queryByRole("dialog")).toBeNull();
  118. });
  119. });