OperatingBoard.test.tsx 14.9 KB
/**
 * 抽屉里那两张逐类明细表:营运一张(与营运分析报表同口径)、非营运一张。
 *
 * 钉的是那张营运表与报表逐列对齐这件事本身,尤其是**营运合计行** —— 报表表2 的最末一行
 * (5173 / 占营运 100% / 新能源 3901 / 75.41%),界面上一度漏了它,
 * 而漏掉之后整张表照样画得出来、一个数都不报错:
 * 上面五行各说一个「占营运 xx%」,分母不见了,那个百分比只能靠心算五数之和才验得回来。
 *
 * 另钉一处:合计行点下去收窄的是**全部营运**(五档全)。合计不是第六档,
 * 它的下标是 -1(见 TOTAL_ROW_IDX),直接把 -1 交给地图只会收成一张空屏 ——
 * 这种错不报异常,只是「点了之后地图空了」,所以要用断言按住。
 *
 * 这一屏的点击落点是**地图收窄**,与预览面板一致(不是直接开案件清单):
 * 营运区分讲的是「这类车出在哪儿」,先看到热力图才知道该往哪一格点下去。
 */
import { describe, expect, it, vi } from 'vitest';
import { render, screen } from '@testing-library/react';

/**
 * 窄抽屉时那张表会主动收掉「新能源车辆数」那一列(见 OperatingBoard 里的注释),
 * 而下面这几条断言钉的是**六列齐全**的报表口径 —— 所以把视口钉在宽档,
 * 免得断言跟着 jsdom 默认的 1024 走进紧凑排版、变成在验另一个东西。
 */
window.innerWidth = 1440;

const { mockMeta, mockStats, setMapScope, openStatsBoard } = vi.hoisted(() => {
  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: [],
  };
  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],
    ],
    /**
     * 非营运细分 × 新能源:[细分档, 新能源下标, 起数]。
     * 新能源下标 1 = 否、2 = 是(与 newEnergy 字典同序)。
     *
     * 这九个格子是深圳 9 月全量实算的结果(见 nonOperatingSegment.test.ts 那组对账),
     * 摆在这里是为了让「界面上那张非营运表显示什么」也有一个数可钉:
     * 私家车 33,353(新能源 15,203)/ 单位车 2,510(1,027)/ 货车 1,666(132)/
     * 特种车 100(18)/ 摩托车 4(0),合计 37,633、新能源 16,380。
     */
    nonOperatingCells: [
      [1, 2, 15203],
      [1, 1, 18150],
      [2, 2, 1027],
      [2, 1, 1483],
      [3, 2, 132],
      [3, 1, 1534],
      [4, 2, 18],
      [4, 1, 82],
      [5, 1, 4],
    ],
  };
  return {
    mockMeta: meta,
    mockStats: stats,
    setMapScope: vi.fn(),
    openStatsBoard: vi.fn(),
  };
});

vi.mock('@/store/useHeatmapStore', () => ({
  useHeatmapStore: (sel: (s: unknown) => unknown) =>
    sel({
      meta: mockMeta,
      stats: mockStats,
      setMapScope,
      openStatsBoard,
      // 故意不给 openRoster:这一块现在不该直接开清单,真被调用就是动线回退了
      openRoster: () => {
        throw new Error('查看全部 · 营运区分不该直接开案件清单,应先收窄地图');
      },
    }),
}));

// mock 工厂里用到了上面的 hoisted 值,所以组件本身也要等 mock 生效后再取
const { OperatingBoard } = await import('./OperatingBoard');
const { OPERATING_ORDER } = await import('./operatingView');

/** 档名单独成一段(「?」是另一样可点的东西,不混在名字里),所以按这一段取名字 */
const rowName = (row: Element) =>
  row.querySelector('.hm-optable__label')?.textContent?.trim() ?? '';
const numCells = (row: Element | null | undefined) =>
  Array.from(row?.querySelectorAll('.hm-optable__num') ?? []).map(
    (c) => c.textContent?.trim() ?? '',
  );
/** 第 n 张明细表(0 = 营运,1 = 非营运) */
const table = (container: HTMLElement, n: number) =>
  container.querySelectorAll('.hm-optable')[n] as HTMLElement | undefined;

describe('营运细分逐类明细(抽屉)', () => {
  it('六列齐全,行序与报表表2 一致:五类 → 营运合计(不再单列「非营运」)', () => {
    setMapScope.mockClear();
    const { container } = render(<OperatingBoard />);

    // 索引 0 是表头。点 1 把「非营运」那一栏去掉了:非营运的构成本来就在下面那张
    // 独立的表里,摆进「营运内部构成」这张表既重复、又会读成「非营运占营运 88%」。
    const names = Array.from(table(container, 0)?.querySelectorAll('.hm-optable__row') ?? [])
      .slice(1)
      .map(rowName);
    expect(names).toEqual([
      '出租/租赁',
      '网约车',
      '公交/客运',
      '营运货车',
      '营运特种车',
      '营运合计',
    ]);

    const heads = Array.from(
      table(container, 0)?.querySelectorAll('[role="columnheader"]') ?? [],
    ).map((h) => h.textContent?.trim() ?? '');
    expect(heads).toEqual(['细分类别', '构成', '车辆数', '占营运', '新能源', '新能源占比']);
    // 两张表各一套表头(营运表 / 非营运表)
    expect(screen.getAllByText('新能源占比')).toHaveLength(2);
  });

  /**
   * 判定口径从一列文字改成每行一个「?」。
   *
   * 判据长的那几行(公交/客运那一行要写「车系 / 车型含 公交·客运·旅游·公路,或车辆结构为
   * 中大型客车」)摆成一列会把每行撑成两行、把右侧四列数字挤得对不齐 —— 而这一屏本来就是
   * 拿去和报表逐列核的。收进悬浮之后行高还是一行,判据一个字不少。
   */
  it('判定口径收在行内那个「?」里,口径一个字不少', () => {
    const { container } = render(<OperatingBoard />);
    const rental = Array.from(table(container, 0)?.querySelectorAll('.hm-optable__row') ?? []).find(
      (r) => rowName(r) === '出租/租赁',
    );
    // 这一档的判据逐字在悬浮里
    expect(rental?.querySelector('.hm-oprule')?.getAttribute('aria-label')).toContain(
      '营业 + 客车 + 企业(未命中公交/客运信号)',
    );
    // 口径那一列不再存在(那一列现在是条)
    expect(container.querySelector('.hm-optable__rule')).toBeNull();
  });

  it('「构成」那一列是条,长度与右侧那个百分比同源', () => {
    const { container } = render(<OperatingBoard />);
    const rental = Array.from(table(container, 0)?.querySelectorAll('.hm-optable__row') ?? []).find(
      (r) => rowName(r) === '出租/租赁',
    );
    // 1967 / 5173 = 38.02% —— 条宽与占比列念的是同一个分母
    const fill = rental?.querySelector('.hm-optable__fill') as HTMLElement | null;
    expect(fill?.style.width).toBe(`${(1967 / 5173) * 100}%`);
    expect(numCells(rental)[1]).toBe('38.0%');
  });

  it('营运合计行逐列对齐报表:5173 / 100% / 3901 / 75.4%', () => {
    const { container } = render(<OperatingBoard />);
    const total = table(container, 0)?.querySelector('.hm-optable__row--total') as
      HTMLElement | null | undefined;
    expect(total).toBeTruthy();

    // 车辆数 / 占营运 / 新能源 / 新能源占比。
    // 占营运写 100.0% 而不是 100%:与非营运表那条合计行走同一个 fmtPercent、同一种写法
    expect(numCells(total)).toEqual(['5,173', '100.0%', '3,901', '75.4%']);
  });

  /**
   * 落点钉子:这一屏点一行是**收窄地图**,不是直接开案件清单。
   *
   * 与预览面板同一条动线 —— 同一个名字、同一个数,在两个位置点出两种结果,
   * 人只能靠记住自己在哪一块来预判,这是要消灭的东西。
   * mock 里刻意没给 openRoster(给了会抛错),所以这条断言一旦变成「开清单」就当场炸。
   */
  it('点一行把地图收窄到这一类,但抽屉保持打开(不直接开清单、不关弹窗)', () => {
    setMapScope.mockClear();
    openStatsBoard.mockClear();
    const { container } = render(<OperatingBoard />);

    const ride = Array.from(table(container, 0)?.querySelectorAll('.hm-optable__row') ?? []).find(
      (r) => rowName(r) === '网约车',
    ) as HTMLElement | undefined;
    ride?.click();

    expect(setMapScope).toHaveBeenCalledTimes(1);
    expect(setMapScope.mock.calls[0][0]).toEqual({
      label: '营运区分 · 网约车',
      operatingSegments: [3],
    });
    // 点 9:收窄地图时**不**收起当前这个弹窗。热力和明细讲的是同一批车的两面
    // (构成 vs 位置),要并排看;关掉弹窗就断了这个对照。地图自己会取景到这一类。
    expect(openStatsBoard).not.toHaveBeenCalled();
  });

  it('点合计行收窄到全部营运车(五档全),不是把 -1 交给地图', () => {
    setMapScope.mockClear();
    const { container } = render(<OperatingBoard />);
    const total = table(container, 0)?.querySelector('.hm-optable__row--total') as
      HTMLElement | undefined;
    total?.click();

    expect(setMapScope).toHaveBeenCalledTimes(1);
    expect(setMapScope.mock.calls[0][0].operatingSegments).toEqual([...OPERATING_ORDER]);
    expect(setMapScope.mock.calls[0][0].label).toBe('营运区分 · 营运合计');
  });

  it('点 1:营运细分表下面不再单列「非营运」那一栏', () => {
    const { container } = render(<OperatingBoard />);
    const names = Array.from(table(container, 0)?.querySelectorAll('.hm-optable__row') ?? [])
      .slice(1)
      .map(rowName);
    // 非营运的构成有它自己那张表(下面第二张),不该在「营运内部构成」里再摆一行,
    // 否则那一行要么给「占营运」算出个 88%(自相矛盾),要么给一串「—」白占一行。
    expect(names).not.toContain('非营运');
    // 这一刀没有把非营运的数弄丢:第二张表照旧在,合计仍是 37,633。
    expect(table(container, 1)).toBeTruthy();
    const nonTotal = table(container, 1)?.querySelector('.hm-optable__row--total');
    expect(numCells(nonTotal)[0]).toBe('3.8万');
  });
});

/**
 * 非营运细分逐类明细(抽屉里新加的第二张表)。
 *
 * 与营运那张表共用同一段渲染、同一套列与样式,所以这里钉的是**它自己的**几件事:
 *   ① 行序与合计:五档 + 非营运合计,合计 = 各档之和 = 非营运总数;
 *   ② 每一行点下去收窄的是「非营运 ∩ 这一档的车种 ∩ 所属」——
 *      少了「非营运」那一刀,点「非营运货车」会把 1,988 辆营运货车也一起收进来,
 *      而表上明明写的是非营运货车。这种错不报错,只是地图上多出一批不该在那儿的点;
 *   ③ 收窄下标由分类器按字典现算,不是写死「客车 / 私人」两个词。
 */
describe('非营运细分逐类明细(抽屉)', () => {
  it('行序固定,合计 = 各档之和 = 非营运总数', () => {
    const { container } = render(<OperatingBoard />);
    const rows = Array.from(table(container, 1)?.querySelectorAll('.hm-optable__row') ?? []);
    expect(rows.slice(1).map(rowName)).toEqual([
      '私家车',
      '单位自用客车',
      '非营运货车',
      '非营运特种车',
      '非营运摩托车',
      '非营运合计',
    ]);
    // 兜底档(其他 / 未填)都是 0,不列出来 —— 列出来就是两行「0 起」
    expect(rows.slice(1).map(rowName)).not.toContain('其他非营运车');

    const total = table(container, 1)?.querySelector('.hm-optable__row--total');
    // 37,633 / 100.0% / 新能源 16,380 / 43.5%
    expect(numCells(total)).toEqual(['3.8万', '100.0%', '1.6万', '43.5%']);
  });

  it('各档的起数与新能源占比:私家车 45.6%、非营运货车只有 7.9%', () => {
    const { container } = render(<OperatingBoard />);
    const rows = Array.from(table(container, 1)?.querySelectorAll('.hm-optable__row') ?? []);
    const at = (name: string) => numCells(rows.find((r) => rowName(r) === name));
    // 车辆数 / 占非营运 / 新能源 / 新能源占比
    expect(at('私家车')).toEqual(['3.3万', '88.6%', '1.5万', '45.6%']);
    expect(at('非营运货车')).toEqual(['1,666', '4.4%', '132', '7.9%']);
    expect(at('非营运摩托车')).toEqual(['4', '0.0%', '—', '—']);
  });

  it('点「私家车」收窄的是 非营运 ∩ 客车 ∩ 私人,不是只挑车种', () => {
    setMapScope.mockClear();
    const { container } = render(<OperatingBoard />);
    const privateCar = Array.from(
      table(container, 1)?.querySelectorAll('.hm-optable__row') ?? [],
    ).find((r) => rowName(r) === '私家车') as HTMLElement | undefined;
    privateCar?.click();

    expect(setMapScope).toHaveBeenCalledTimes(1);
    expect(setMapScope.mock.calls[0][0]).toEqual({
      label: '非营运细分 · 私家车',
      operatingSegments: [1],
      // dict.vehicleClasses = ['', '客车', '货车'] → 客车 1
      vehicleClasses: [1],
      // dict.ownNatures = ['', '企业', '私人'] → 私人 2
      ownNatures: [2],
    });
  });

  it('点「非营运货车」只卡车种:这一档不看所属性质,就不该传所属那一维', () => {
    setMapScope.mockClear();
    const { container } = render(<OperatingBoard />);
    const truck = Array.from(table(container, 1)?.querySelectorAll('.hm-optable__row') ?? []).find(
      (r) => rowName(r) === '非营运货车',
    ) as HTMLElement | undefined;
    truck?.click();

    const arg = setMapScope.mock.calls[0][0];
    expect(arg.vehicleClasses).toEqual([2]);
    // 所属那一维铺满了整本字典 → 不传(传全量等价于不传,而「没写」更能说明这一刀不卡在所属上)
    expect(arg.ownNatures).toBeUndefined();
    expect(arg.operatingSegments).toEqual([1]);
  });

  it('点「非营运合计」收窄到全部非营运车(不再叠车种 / 所属)', () => {
    setMapScope.mockClear();
    const { container } = render(<OperatingBoard />);
    const total = table(container, 1)?.querySelector('.hm-optable__row--total') as
      HTMLElement | undefined;
    total?.click();

    expect(setMapScope).toHaveBeenCalledTimes(1);
    expect(setMapScope.mock.calls[0][0]).toEqual({
      label: '非营运细分 · 非营运合计',
      operatingSegments: [1],
    });
  });
});