transitions.test.ts 2.76 KB
/**
 * 视图状态转换的等价性回归(阶段 1b)。
 *
 * 每个断言都逐字段照抄抽取前 action 的 set 参数,不是照新的实现写。
 * 抽 3 处的判据是「同一个连带在两处写过」——railTabSwitchState 的注释
 * 明说与 setAnalysisEnabled(true) 同一套;setMapScope 的 casePin 无条件清
 * 也是与原实现一致,不能"顺手优化"成只在有值时清。
 */
import { describe, expect, it } from 'vitest';
import type { MapScope } from './useHeatmapStore';
import {
  analysisSwitchState,
  mapScopeSwitchState,
  railTabSwitchState,
} from './transitions';

describe('railTabSwitchState(原 store 492-514)', () => {
  it('切到 analysis:开关开、清聚焦/选中/收起态/临时收窄', () => {
    expect(railTabSwitchState('analysis')).toEqual({
      analysisOn: true,
      activeHotspot: null,
      dockHidden: false,
      spotlight: null,
      mapScope: null,
    });
  });

  it('切到 stats/filter:关降噪、清收窄/聚焦/定位针', () => {
    const expected = { analysisOn: false, mapScope: null, spotlight: null, casePin: null };
    expect(railTabSwitchState('stats')).toEqual(expected);
    expect(railTabSwitchState('filter')).toEqual(expected);
  });

  it('收起面板(null)不动任何开关', () => {
    expect(railTabSwitchState(null)).toEqual({});
  });
});

describe('analysisSwitchState(原 setAnalysisEnabled 1101-1107)', () => {
  it('开启:清高亮与清单收起态,并收回一屏聚焦', () => {
    expect(analysisSwitchState(true)).toEqual({
      analysisOn: true,
      activeHotspot: null,
      dockHidden: false,
      spotlight: null,
    });
  });

  it('关闭:清高亮与清单收起态,但不动 spotlight', () => {
    expect(analysisSwitchState(false)).toEqual({
      analysisOn: false,
      activeHotspot: null,
      dockHidden: false,
    });
  });

  it('与 railTabSwitchState(analysis) 的差异仅在一处:后者还清 mapScope/casePin', () => {
    const a = analysisSwitchState(true);
    const r = railTabSwitchState('analysis');
    expect(r).toMatchObject({ analysisOn: a.analysisOn, activeHotspot: null, spotlight: null });
    expect(r).toHaveProperty('mapScope', null);
    expect(a).not.toHaveProperty('mapScope');
  });
});

describe('mapScopeSwitchState(原 setMapScope 1345-1353)', () => {
  const scope: MapScope = { label: '营运车', vehicleClasses: [1, 2] };

  it('设收窄:与聚焦互斥、清定位针', () => {
    expect(mapScopeSwitchState(scope)).toEqual({
      mapScope: scope,
      spotlight: null,
      casePin: null,
    });
  });

  it('传 null 恢复画全部:仍清定位针(与原实现一致,不是顺手优化)', () => {
    expect(mapScopeSwitchState(null)).toEqual({ mapScope: null, casePin: null });
  });
});