OperatingWidget.test.tsx 15.1 KB
/**
 * 营运区分那块界面的渲染与交互冒烟。
 *
 * 钉的是几件最容易静默坏掉的事:
 *   1. 老产物(没有 operatingSegments 字典)时整块不出现,而不是画一个空图;
 *   2. 一级比例条上一截、二级榜上一行点出来的下标是**同一批**,且点「营运」带上全部
 *      五类营运车 —— 只带一个下标的话,地图上会只剩出租/租赁,而条上明明画的是整整一截;
 *   3. 字典在、统计里这一维却没数时,说的是「统计没跟上」,而不是画一个全 0 的图
 *      —— 全 0 会被读成「业务上就是没有营运车」,真相往往只是统计进程还停在旧版本;
 *   4. 非营运 / 未填 只出现在一级,不进二级那张「营运内部构成」榜 ——
 *      混进去会读成「非营运占营运 88%」这种自相矛盾的话;
 *   5. 新能源率那一列念的是「本档里的新能源占比」,不是全量占比。
 */
import { describe, expect, it, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { OperatingWidget } from './OperatingWidget';
import type { MetaFile, StatsResult } from '../aggregate/types';
import { OPERATING_ORDER } from './operatingView';
import { OPERATING_NON } from '../data/operatingSegment';

const meta = {
  v: 1,
  generatedAt: '',
  source: 't',
  count: 5173 + 37633,
  rejected: 0,
  rejectedReasons: {},
  timeRange: { min: 0, max: 1 },
  bounds: { minLng: 0, maxLng: 1, minLat: 0, maxLat: 1 },
  center: { lng: 0, lat: 0 },
  coordScale: 1e6,
  coordSystem: 'GCJ-02',
  dict: {
    districts: ['未识别'],
    streets: [],
    addrDetails: [],
    roads: [''],
    useNatures: ['', '营业', '非营业'],
    newEnergy: ['', '否', '是'],
    vehicleClasses: ['', '客车', '货车'],
    operatingSegments: [
      '未填',
      '非营运',
      '出租/租赁',
      '网约车',
      '公交/客运',
      '营运货车',
      '营运特种车',
    ],
    ownNatures: ['', '企业', '私人'],
  },
  districtCounts: [],
} as unknown as MetaFile;

/** 与报表同形的五类数字;新能源那两列走 operatingEnergy 交叉表 */
const stats = {
  matched: 5173 + 37633,
  operatingCounts: [
    [2, 1967],
    [3, 780],
    [4, 46],
    [5, 1988],
    [6, 392],
    [1, 37633],
  ],
  operatingUnresolved: 0,
  operatingEnergy: [
    [2, 2, 1954],
    [3, 2, 780],
    [4, 2, 42],
    [5, 2, 971],
    [6, 2, 154],
  ],
} as unknown as StatsResult;

const segs = (container: HTMLElement) =>
  Array.from(container.querySelectorAll('.hm-opbar__seg')) as HTMLElement[];
const rows = (container: HTMLElement) =>
  Array.from(container.querySelectorAll('.hm-oprow')) as HTMLElement[];

describe('营运区分组件', () => {
  it('老产物没有这一维时整块不渲染(而不是画一个空图)', () => {
    const { container } = render(
      <OperatingWidget
        meta={{ ...meta, dict: { ...meta.dict, operatingSegments: undefined } } as MetaFile}
        stats={stats}
        variant="panel"
      />,
    );
    expect(container.querySelector('.hm-op')).toBeNull();
  });

  it('一级比例条与二级榜都在,且点出来的下标与图上那一块同源', () => {
    const onScope = vi.fn();
    const { container } = render(
      <OperatingWidget meta={meta} stats={stats} variant="panel" onScope={onScope} />,
    );

    // 一级:营运 + 非营运 两截(未填 0 起,不画)
    expect(segs(container).length).toBe(2);
    // 二级:五类营运车一次铺满(不再有开关)+ 营运合计;卡末不再挂「非营运合计」(见下一条)
    expect(rows(container).length).toBe(6);
    expect(screen.getByText('出租/租赁')).toBeTruthy();
    expect(screen.getByText('网约车')).toBeTruthy();
    expect(screen.getByText('公交/客运')).toBeTruthy();

    // 点一级「营运」那一截 → 带上全部五类营运车。
    // 只给一个下标的话,地图上会只剩出租/租赁,而条上明明画的是整整一截。
    onScope.mockClear();
    const operating = segs(container).find((s) => s.getAttribute('title')?.startsWith('营运:'));
    expect(operating).toBeTruthy();
    operating?.click();
    expect(onScope).toHaveBeenCalled();
    expect(onScope.mock.calls[0][0]).toEqual([...OPERATING_ORDER]);

    // 点二级「网约车」那一行 → 只带网约车这一类
    onScope.mockClear();
    const ride = rows(container).find((r) => r.getAttribute('title')?.startsWith('网约车:'));
    expect(ride).toBeTruthy();
    ride?.click();
    expect(onScope.mock.calls[0][0]).toEqual([3]);
  });

  it('非营运不作为一档混进「营运内部构成」,只在合计行与一级出现', () => {
    const { container } = render(
      <OperatingWidget meta={meta} stats={stats} variant="panel" onScope={() => undefined} />,
    );
    // 一级条上有它
    expect(segs(container).some((s) => s.getAttribute('title')?.startsWith('非营运:'))).toBe(true);
    // 二级榜的**构成行**(.hm-opcard__list 里的前五类 + 营运合计)不能把它当成一档:
    // 那会读成「非营运占营运 88%」这种自相矛盾的话。合计行另算,见下一条断言。
    const constituent = Array.from(container.querySelectorAll('.hm-opcard__list > li')).filter(
      (li) => !li.classList.contains('hm-opcard__total-li'),
    );
    const labels = constituent.map((li) => li.textContent ?? '');
    expect(labels.some((t) => t.includes('非营运'))).toBe(false);
    expect(labels.some((t) => t.includes('未填'))).toBe(false);
  });

  /**
   * 领地卡里只有「营运合计」这一个合计,卡末不再挂「非营运合计」。
   *
   * 那一行原先与营运合计并排(点 10 / 点 11 补的),但卡头那句话就是
   * 「营运 5,173 起的内部构成(每行条的满格 = 下面那条营运合计)」——
   * 满格这把尺子是营运合计,卡末再并一个分母为全量的非营运总数,
   * 同一张卡里就有两把尺子。全量结构(营运 / 非营运 / 未填)由一级那三个
   * 大读数与比例条回答,两处都不缺这一行。所以这里钉的是「它不再回来」。
   */
  it('卡末没有「非营运合计」那一行:全量结构由一级读数回答', () => {
    const { container } = render(
      <OperatingWidget meta={meta} stats={stats} variant="panel" onScope={() => undefined} />,
    );
    expect(container.querySelector('.hm-oprow--non')).toBeNull();
    const list = Array.from(container.querySelectorAll('.hm-opcard__list > li'));
    // 五档营运车 + 营运合计 = 6 行,末行就是营运合计
    expect(list.length).toBe(6);
    expect(list[list.length - 1].querySelector('.hm-oprow--total')).toBeTruthy();
    // 非营运这个数仍然在(一级读数),只是不在这张卡里
    expect(container.querySelector('.hm-op__side-n')?.textContent).toContain('37,633');
  });

  it('统计里没这一维时说清楚,而不是画一个全 0 的图', () => {
    // 复现真实遇到过的一次:面板热更新了、统计还停在旧版本(统计跑在独立进程里)。
    // 此时字典是新的、operatingCounts 一个都没有 —— 画出来就是六段全 0 的空图。
    const staleStats = {
      matched: 42803,
      operatingCounts: [],
      operatingUnresolved: 0,
      operatingEnergy: [],
    } as unknown as StatsResult;

    const { container } = render(
      <OperatingWidget meta={meta} stats={staleStats} variant="panel" onScope={() => undefined} />,
    );

    expect(container.querySelector('.hm-op')).toBeNull();
    expect(screen.getByText(/统计里没有「营运区分」这一维/)).toBeTruthy();
  });

  /**
   * 新能源率不单成一列,挂在这一行的悬浮里。
   *
   * 面板这一屏是扫一眼,四列已经到顶:第五列会把那根条挤到 118px 以下、反而看不清。
   * 抽屉里要看新能源那两列,有下面那张六列明细表(那一屏本来就是照着报表逐列核的),
   * 所以领地卡这一张不再分两种列数 —— 少一处「按落点变形」,也少一处会对不齐的地方。
   */
  it('新能源率挂在这一行的悬浮里,念的仍是「本档里的新能源占比」', () => {
    const { container } = render(
      <OperatingWidget meta={meta} stats={stats} variant="panel" onScope={() => undefined} />,
    );
    expect(container.querySelector('.hm-oprow__ne')).toBeNull();
    // 公交/客运 42 / 46 = 91.30% —— 不是 42 / 42806 那种全量占比
    const busRow = rows(container).find((el) => el.textContent?.includes('公交/客运'));
    expect(busRow?.getAttribute('title')).toContain('91.3%');
  });

  /**
   * 营运合计行(报表表2 的最末一行:5173 / 占营运 100% / 新能源 3901 / 75.41%)。
   *
   * 钉它的理由与钉其它行不同:它不是业务上的一档车,它是**分母**。
   * 上面五行各说一个「占营运 xx%」,分母不出现在屏上,那个百分比就得靠心算五数之和
   * 才验得回来;而新能源合计 3901 更是只在这一行上(五行里哪一行的新能源都不是
   * 「营运的新能源」)。这一行一旦静默消失,整张表照样画得出来、一个数都不报错 ——
   * 所以必须用断言把它按住。
   */
  it('营运合计行:五类之和 5173、占营运 100%、新能源合计 3901(75.4%)', () => {
    const { container } = render(
      <OperatingWidget meta={meta} stats={stats} variant="panel" onScope={() => undefined} />,
    );
    const totalRow = container.querySelector('.hm-oprow--total') as HTMLElement | null;
    expect(totalRow).toBeTruthy();
    expect(totalRow?.textContent).toContain('营运合计');
    // 5173 = 1967 + 780 + 46 + 1988 + 392
    expect(totalRow?.querySelector('.hm-oprow__val')?.textContent).toBe('5,173');
    expect(totalRow?.querySelector('.hm-oprow__pct')?.textContent).toBe('100.0%');
    // 新能源合计改挂在悬浮里:3901 = 1954 + 780 + 42 + 971 + 154,3901 / 5173 = 75.41%
    expect(totalRow?.getAttribute('title')).toContain('新能源 3,901 起,75.4%');
  });

  /** 合计行的 idx 不是任何一档(TOTAL_ROW_IDX = -1),点它必须收窄到全部五档营运车 */
  /**
   * 点 11:一级那两个大读数(「营运 5,173 起」/「非营运 37,633 起」)本身可点。
   *
   * 尤其钉「非营运 37,633 起」这一行 —— 起数原先是按钮外的独立 span,看着像能点、
   * 点下去却没反应。现在整档读数(标签 + 占比 + 起数)合成一颗按钮,数字落在按钮里。
   */
  it('一级读数可点:点「非营运 起数」也收窄到非营运(数字在按钮里)', () => {
    const onScope = vi.fn();
    const { container } = render(
      <OperatingWidget meta={meta} stats={stats} variant="panel" onScope={onScope} />,
    );
    const sideUnit = container.querySelector('.hm-op__side-unit') as HTMLElement | null;
    expect(sideUnit?.tagName).toBe('BUTTON');
    // 起数那一行确实在这颗按钮内部(不是按钮外的兄弟 span)
    const countLine = sideUnit?.querySelector('.hm-op__side-n') as HTMLElement | null;
    expect(countLine?.textContent).toContain('37,633');

    countLine?.click();
    expect(onScope).toHaveBeenCalledTimes(1);
    expect(onScope.mock.calls[0][0]).toEqual([OPERATING_NON]);
    expect(onScope.mock.calls[0][1]).toBe('营运区分 · 非营运');
  });

  it('点合计行收窄的是全部营运车,而不是把 -1 交给后端', () => {
    const onScope = vi.fn();
    const { container } = render(
      <OperatingWidget meta={meta} stats={stats} variant="panel" onScope={onScope} />,
    );
    const totalRow = container.querySelector('.hm-oprow--total') as HTMLElement | null;
    totalRow?.click();
    expect(onScope).toHaveBeenCalledTimes(1);
    expect(onScope.mock.calls[0][0]).toEqual([...OPERATING_ORDER]);
    expect(onScope.mock.calls[0][1]).toBe('营运区分 · 营运合计');
  });

  /** 面板(不带新能源列)也要有合计行:那一屏的「占营运比」同样缺不了分母 */
  it('面板四列版也带合计行', () => {
    const { container } = render(
      <OperatingWidget meta={meta} stats={stats} variant="panel" onScope={() => undefined} />,
    );
    const totalRow = container.querySelector('.hm-oprow--total') as HTMLElement | null;
    expect(totalRow?.textContent).toContain('营运合计');
    expect(totalRow?.querySelector('.hm-oprow__val')?.textContent).toBe('5,173');
    expect(totalRow?.querySelector('.hm-oprow__ne')).toBeNull();
  });

  /**
   * variant 只决定长相(几列),**不决定点下去发生什么** —— 落点跟着传进来的回调走。
   *
   * 早先 board 一律开清单,于是「查看全部 → 营运区分」里点一行直接弹案件清单,
   * 而预览面板上同样一行点下去是地图收窄:同一个名字同一个数,两个位置两种结果。
   * 这条断言把「board 也能走收窄」钉住,回退成按 variant 分流就会红。
   */
  it('board 变体配上 onScope 就走收窄地图:variant 不管落点', () => {
    const onScope = vi.fn();
    const onOpenRoster = vi.fn();
    const { container } = render(
      <OperatingWidget
        meta={meta}
        stats={stats}
        variant="board"
        onScope={onScope}
        onOpenRoster={onOpenRoster}
      />,
    );
    // 抽屉那一档不画领地卡(它下面就是六列明细表),所以这里点的是一级比例条上那一截营运
    const operating = segs(container).find((s) => s.getAttribute('title')?.startsWith('营运:'));
    operating?.click();

    expect(onScope).toHaveBeenCalledTimes(1);
    expect(onScope).toHaveBeenCalledWith([...OPERATING_ORDER], '营运区分 · 营运');
    expect(onOpenRoster).not.toHaveBeenCalled();
  });

  /**
   * 抽屉那一档只有一级读数与比例条,二级领地卡**不画**。
   *
   * 抽屉里紧跟着的是「营运细分逐类明细」六列表:那张表自带条、还多两列新能源。
   * 两张都画就是同一份数上下各画一遍(还各占一屏高度),而两处的条迟早会画得不一样长 ——
   * 那时候没人分得清该信哪一根。
   */
  it('抽屉那一档不画二级领地卡:那一份数在下面的六列表里', () => {
    const { container } = render(
      <OperatingWidget meta={meta} stats={stats} variant="board" onScope={() => undefined} />,
    );
    expect(container.querySelector('.hm-opcard')).toBeNull();
    expect(container.querySelector('.hm-op__toggle')).toBeNull();
    // 一级那一截比例条与两个大数字仍在
    expect(segs(container).length).toBe(2);
  });

  /** 只给 onOpenRoster 时仍直接开清单:这条路径留给确实一上来就要名单的落点 */
  it('只给 onOpenRoster 时走开清单(另一种落点仍然可用)', () => {
    const onOpenRoster = vi.fn();
    const { container } = render(
      <OperatingWidget meta={meta} stats={stats} variant="panel" onOpenRoster={onOpenRoster} />,
    );
    const ride = rows(container).find((el) => el.textContent?.includes('网约车'));
    ride?.click();

    expect(onOpenRoster).toHaveBeenCalledTimes(1);
    expect(onOpenRoster).toHaveBeenCalledWith([3], '营运区分 · 网约车');
  });
});