StoreDetailDrawer.test.tsx 17.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
import { cleanup, fireEvent, render, screen, within } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { StoreListingDetail } from '@/api';
import { STORE_DICTIONARIES, type StoreDictionaries } from './dictionaries';
import { StoreDetailDrawer } from './StoreDetailDrawer';

afterEach(cleanup);

// Swagger-shaped fixtures: live endpoints contain no records, so these are not real money samples.
const spec: StoreDictionaries = {
  businessScenes: [
    { value: 'AFTER_SALES', label: '售后会员权益升级' },
    { value: 'PRE_SALES', label: '售前延保' },
  ],
  productCategories: [
    { value: 'FILM', label: '太阳膜-原厂' },
    { value: 'WRAP', label: '车衣-原厂' },
  ],
  calcModes: [{ value: 'CATEGORY', label: '按品类计算' }],
  calcWays: [
    { value: 'INCOME', label: '收入' },
    { value: 'FIXED', label: '固定' },
  ],
  onlinePointFlags: [
    { value: '1', label: '是' },
    { value: '0', label: '否' },
  ],
};

const detail: StoreListingDetail = {
  listingId: '9007199254740993',
  storeName: '当前门店',
  merchantNo: 'SQ001',
  businessScene: 'AFTER_SALES',
  status: 'PENDING',
  currentLevel: 1,
  totalLevel: 2,
  submitUserName: '运营甲',
  submitTime: '2026-10-09T10:00:00',
  costDecoration: '500',
  costMonthly: '500',
  costLabor: '500',
  costTotal: '90071992547409.91',
  items: [
    {
      itemId: '1',
      productCategory: 'FILM',
      marketFee1CalcMode: 'CATEGORY',
      marketFee1CalcWay: 'INCOME',
      marketFee1Value: '5',
      marketFee1Unit: 'RATIO',
      sunshine1OnlinePoint: 0,
      sunshine1Value: '0',
      sunshine1Unit: 'RATIO',
    },
    {
      itemId: '2',
      productCategory: 'WRAP',
      marketFee1CalcMode: 'CATEGORY',
      marketFee1CalcWay: 'FIXED',
      marketFee1Value: '123.45',
      marketFee1Unit: 'AMOUNT',
    },
    ...Array.from({ length: 9 }, (_, index) => ({
      itemId: `extra-${index}`,
      productCategory: `分类${index}`,
    })),
  ],
  approvalLogs: [
    {
      logId: '1',
      action: 'REJECT',
      operatorName: '商务甲',
      operatorRole: 'YJ_BUSINESS_MGR',
      preStatus: 'PENDING',
      postStatus: 'REJECTED',
      reason: '请核对政策',
      operateTime: '2026-10-09T11:00:00',
      operateIp: '192.0.2.1',
    },
  ],
  changeHistory: [
    {
      changeId: 'old',
      versionNo: 1,
      changeField: '运营成本',
      beforeValue: '0',
      afterValue: '100',
      changeUserName: '运营甲',
      effectiveTime: '2026-09-01T10:00:00',
    },
    {
      changeId: 'new',
      versionNo: 2,
      changeField: '管理层市场费',
      beforeValue: '3%',
      afterValue: '5%',
      changeUserName: '运营乙',
      effectiveTime: '2026-10-01T10:00:00',
    },
  ],
};

function show(overrides: Partial<React.ComponentProps<typeof StoreDetailDrawer>> = {}) {
  return render(
    <StoreDetailDrawer
      open
      mode="approval"
      detail={detail}
      spec={spec}
      onClose={() => undefined}
      {...overrides}
    />,
  );
}

describe('store detail presentation', () => {
  it('renders the approval prototype header and audit cards without inventing a single-stage flow', () => {
    show();
    const header = within(screen.getByLabelText('审批概览'));
    expect(header.getByRole('heading', { name: '当前门店' })).toBeInTheDocument();
    expect(header.getByText('待审批')).toBeInTheDocument();
    expect(header.getByText('90,071,992,547,409.91 元')).toBeInTheDocument();
    const flow = within(screen.getByLabelText('审批流程'));
    expect(flow.getByText('两级审批')).toBeInTheDocument();
    expect(flow.getByText('优加商务经理')).toBeInTheDocument();
    expect(flow.getByText('优加财务经理')).toBeInTheDocument();
    fireEvent.click(screen.getByRole('tab', { name: '审批留痕' }));
    const trail = within(screen.getByLabelText('审批留痕列表'));
    expect(trail.getByText(/商务甲/)).toBeInTheDocument();
    expect(trail.getByText('请核对政策')).toBeInTheDocument();
    expect(trail.getByText(/192\.0\.2\.1/)).toBeInTheDocument();
  });

  it('renders the listing prototype overview and store information using real record fields', () => {
    const onClose = vi.fn();
    show({
      mode: 'listing',
      onClose,
      detail: {
        ...detail,
        status: 'APPROVED',
        categoryCount: 6,
        effectiveTime: '2026-10-09T15:03:37',
        changeHistory: [
          { versionNo: 3, effectiveTime: '2026-10-01T10:00:00' },
          { versionNo: 1, effectiveTime: '2026-10-09T10:00:00' },
        ],
      },
    });
    const overview = within(screen.getByLabelText('门店概览'));
    expect(overview.getByRole('heading', { name: '当前门店' })).toBeInTheDocument();
    for (const text of ['SQ001', '售后会员权益升级', '6 个', '2026-10-09', 'v3', '已生效']) {
      expect(overview.getByText(text)).toBeInTheDocument();
    }
    expect(overview.queryByText('提交人')).not.toBeInTheDocument();
    expect(overview.queryByText('运营成本合计')).not.toBeInTheDocument();
    const panel = within(screen.getByRole('tabpanel', { name: '门店信息' }));
    expect(panel.queryByRole('table')).not.toBeInTheDocument();
    expect(panel.getByRole('heading', { name: '门店信息' })).toBeInTheDocument();
    expect(panel.getByText('售后会员权益升级')).toBeInTheDocument();
    expect(panel.queryByText('售前延保')).not.toBeInTheDocument();
    expect(panel.getByText('完整清单见「产品与场景」页签')).toBeInTheDocument();
    expect(panel.getByText('2026-10-09 15:03')).toBeInTheDocument();
    fireEvent.click(overview.getByRole('button', { name: '关闭' }));
    expect(onClose).toHaveBeenCalledOnce();
  });

  it('does not invent a version or scene when listing fields are missing', () => {
    show({
      mode: 'listing',
      detail: {
        ...detail,
        businessScene: '',
        items: undefined,
        categoryCount: undefined,
        changeHistory: [{ changeField: 'status' }],
      },
    });
    const overview = within(screen.getByLabelText('门店概览'));
    expect(overview.getByText('当前版本')).toBeInTheDocument();
    expect(overview.getByText('业务场景').nextElementSibling).toHaveTextContent('—');
    expect(overview.queryByText(/^v\d/)).not.toBeInTheDocument();
    expect(overview.queryByText('1 个')).not.toBeInTheDocument();
    expect(overview.queryByText('0 个')).not.toBeInTheDocument();
  });

  it('uses local dictionaries for real Chinese policy values and numeric point flags', () => {
    show({
      spec: STORE_DICTIONARIES,
      detail: {
        ...detail,
        businessScene: '精品升级',
        items: [
          {
            productCategory: '车辆美容',
            sunshine1CalcMode: '按品类计算',
            sunshine1CalcWay: '收入',
            sunshine1OnlinePoint: 1,
            sunshine2CalcMode: '按整单计算',
            sunshine2CalcWay: '毛利',
            sunshine2OnlinePoint: 0,
          },
          { productCategory: '后续新增分类' },
        ],
      },
    });
    expect(screen.getByText('精品升级')).toBeInTheDocument();
    fireEvent.click(screen.getByRole('tab', { name: '一线阳光激励' }));
    const panel = within(screen.getByRole('tabpanel', { name: '一线阳光激励' }));
    for (const label of [
      '车辆美容',
      '后续新增分类',
      '按品类计算',
      '按整单计算',
      '收入',
      '毛利',
      '是',
      '否',
    ]) {
      expect(panel.getByText(label)).toBeInTheDocument();
    }
  });

  it('shows only the current scene, every category and the policy value unit', () => {
    show();
    expect(screen.getByText('当前门店')).toBeInTheDocument();
    expect(screen.getByText('售后会员权益升级')).toBeInTheDocument();
    expect(screen.queryByText('售前延保')).not.toBeInTheDocument();
    expect(screen.getByText('分类8')).toBeInTheDocument();
    expect(screen.getByText('5%')).toBeInTheDocument();
    expect(screen.getByText('¥123.45')).toBeInTheDocument();
    expect(screen.queryByRole('button', { name: '通过' })).not.toBeInTheDocument();
  });

  it.each(['approval', 'listing'] as const)(
    'displays the authoritative %s total even when the cost components differ',
    (mode) => {
      show({ mode });
      fireEvent.click(screen.getByRole('tab', { name: '运营成本' }));
      const panel = within(screen.getByRole('tabpanel', { name: '运营成本' }));
      const currency = mode === 'listing' ? '¥' : '';
      expect(panel.getByText(`${currency}90,071,992,547,409.91`)).toBeInTheDocument();
      expect(panel.queryByText(`${currency}1,500.00`)).not.toBeInTheDocument();
      expect(panel.getAllByText(`${currency}500.00`)).toHaveLength(3);
      if (mode === 'listing') {
        expect(panel.getByRole('heading', { name: '运营成本(门店维度)' })).toBeInTheDocument();
        expect(panel.queryByRole('table')).not.toBeInTheDocument();
        expect(panel.getByText('当月产生费用')).toBeInTheDocument();
        expect(panel.getByText('房租、水电、耗材等')).toBeInTheDocument();
      }
    },
  );

  it('renders zero point flags and zero policy values without treating them as missing', () => {
    show();
    fireEvent.click(screen.getByRole('tab', { name: '一线阳光激励' }));
    expect(screen.getByText('否')).toBeInTheDocument();
    expect(screen.getByText('0%')).toBeInTheDocument();
  });

  it('shows two approval stages by default and the returned single finance stage', () => {
    const view = show({ detail: { ...detail, totalLevel: undefined } });
    expect(within(screen.getByLabelText('审批流程')).getByText('优加商务经理')).toBeInTheDocument();
    view.rerender(
      <StoreDetailDrawer
        open
        mode="approval"
        detail={{ ...detail, totalLevel: 1 }}
        onClose={() => undefined}
      />,
    );
    const flow = within(screen.getByLabelText('审批流程'));
    expect(flow.queryByText('优加商务经理')).not.toBeInTheDocument();
    expect(flow.getByText('优加财务经理')).toBeInTheDocument();
    for (const [title, number] of [
      ['提交', '1'],
      ['优加财务经理', '2'],
      ['生效', '3'],
    ]) {
      const stage = flow.getByText(title).closest('li')!;
      expect(within(stage).getByText(number)).toBeInTheDocument();
    }
  });

  it.each(['PENDING', 'APPROVED', 'REJECTED', 'REVOKED'] as const)(
    'keeps all flow nodes continuously numbered after the flow advances to %s',
    (status) => {
      show({
        detail: {
          ...detail,
          status,
          currentLevel: 2,
          approvalLogs: [
            {
              action: 'PASS',
              operatorRole: 'YJ_BUSINESS_MGR',
              operatorName: '商务甲',
              operateTime: '2026-10-09T11:00:00',
            },
          ],
        },
      });
      const flow = within(screen.getByLabelText('审批流程'));
      for (const [title, number] of [
        ['提交', '1'],
        ['优加商务经理', '2'],
        ['优加财务经理', '3'],
        ['生效', '4'],
      ]) {
        const stage = flow.getByText(title).closest('li')!;
        expect(within(stage).getByText(number)).toBeInTheDocument();
      }
    },
  );

  it('does not describe a rejected record as awaiting approval and preserves the audit reason', () => {
    show({ detail: { ...detail, status: 'REJECTED', rejectReason: '请核对政策' } });
    expect(
      within(screen.getByLabelText('审批流程')).queryByText('当前审批节点'),
    ).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole('tab', { name: '审批留痕' }));
    expect(screen.getByText('商务甲')).toBeInTheDocument();
    expect(screen.getAllByText('请核对政策').length).toBeGreaterThan(0);
    expect(screen.getByText(/192\.0\.2\.1/)).toBeInTheDocument();
  });

  it('includes the future finance node between the current business node and activation', () => {
    show({
      initialTab: 'logs',
      detail: {
        ...detail,
        approvalLogs: [
          { action: 'SUBMIT', operatorName: '运营甲', operateTime: detail.submitTime },
        ],
      },
    });
    const trail = within(screen.getByLabelText('审批留痕列表'));
    const cards = trail.getAllByRole('article');
    expect(cards).toHaveLength(4);
    expect(cards[1]).toHaveTextContent('优加商务经理');
    expect(cards[2]).toHaveTextContent('优加财务经理');
    expect(cards[2]).toHaveTextContent('待前序节点通过');
    expect(cards[2]).not.toHaveTextContent('当前节点');
    expect(cards[3]).toHaveTextContent('生效 · 写入门店列表');
  });

  it.each([1, 2])(
    'does not duplicate finance or imply more approval at final level %s',
    (totalLevel) => {
      show({
        initialTab: 'logs',
        detail: { ...detail, totalLevel, currentLevel: totalLevel, approvalLogs: [] },
      });
      const trail = within(screen.getByLabelText('审批留痕列表'));
      expect(trail.getAllByText(/优加财务经理/)).toHaveLength(1);
      expect(trail.queryByLabelText('后续审批节点')).not.toBeInTheDocument();
      expect(trail.getByLabelText('当前审批节点')).toHaveTextContent('通过后即可生效');
    },
  );

  it('opens listing history directly and shows newest changes first', () => {
    show({ mode: 'listing', initialTab: 'history', detail: { ...detail, status: 'APPROVED' } });
    expect(screen.getByRole('tab', { name: '变更历史' })).toHaveAttribute('aria-selected', 'true');
    const panel = screen.getByRole('tabpanel');
    expect(panel.textContent?.indexOf('管理层市场费')).toBeLessThan(
      panel.textContent?.indexOf('运营成本') ?? -1,
    );
    expect(within(panel).getByText('3% → 5%')).toBeInTheDocument();
    expect(screen.queryByRole('tab', { name: '审批留痕' })).not.toBeInTheDocument();
  });

  it('renders the real first-launch history in the prototype layout with readable status values', () => {
    show({
      mode: 'listing',
      initialTab: 'history',
      detail: {
        ...detail,
        status: 'APPROVED',
        changeHistory: [
          {
            changeId: 9,
            listingId: 1,
            versionNo: 1,
            changeField: 'status',
            beforeValue: 'PENDING',
            afterValue: 'APPROVED',
            changeUserName: '优加财务',
            effectiveTime: '2026-10-09T15:03:37',
          },
        ],
      },
    });
    const panel = within(screen.getByRole('tabpanel', { name: '变更历史' }));
    expect(panel.getByRole('heading', { name: '变更历史(只读)' })).toBeInTheDocument();
    expect(panel.getByText('首次上架')).toBeInTheDocument();
    expect(panel.getByText('门店在系统中完成首次上架并生效。')).toBeInTheDocument();
    expect(panel.getByText('待审批 → 已生效')).toBeInTheDocument();
    expect(panel.getByText('2026-10-09')).toBeInTheDocument();
    expect(panel.getByText(/变更人:.*优加财务.*生效时间.*2026-10-09 15:03/)).toBeInTheDocument();
    expect(panel.queryByText(/PENDING|APPROVED/)).not.toBeInTheDocument();
  });

  it('groups history by version and preserves cost values, zero values and policy units', () => {
    show({
      mode: 'listing',
      initialTab: 'history',
      detail: {
        ...detail,
        changeHistory: [
          {
            versionNo: 2,
            changeField: 'businessScene',
            beforeValue: 'AFTER_SALES',
            afterValue: 'PRE_SALES',
          },
          { versionNo: 3, changeField: 'costDecoration', beforeValue: '800', afterValue: '500' },
          { versionNo: 3, changeField: 'costLabor', beforeValue: null, afterValue: '0' },
          { versionNo: 3, changeField: 'sunshine1Value', beforeValue: '3%', afterValue: '5%' },
        ],
      },
    });
    const panel = within(screen.getByRole('tabpanel', { name: '变更历史' }));
    const versions = panel.getAllByRole('article');
    expect(versions).toHaveLength(2);
    expect(within(versions[0]).getByText('v3')).toBeInTheDocument();
    expect(within(versions[0]).getByText('¥800.00 → ¥500.00')).toBeInTheDocument();
    expect(within(versions[0]).getByText('— → ¥0.00')).toBeInTheDocument();
    expect(within(versions[0]).getByText('3% → 5%')).toBeInTheDocument();
    expect(within(versions[1]).getByText('v2')).toBeInTheDocument();
    expect(within(versions[1]).getByText('售后会员权益升级 → 售前延保')).toBeInTheDocument();
  });

  it('renders the batch-pass audit action as a readable business action', () => {
    show({ detail: { ...detail, approvalLogs: [{ logId: 'batch', action: 'BATCH_PASS' }] } });
    fireEvent.click(screen.getByRole('tab', { name: '审批留痕' }));
    expect(screen.getByText('批量通过')).toBeInTheDocument();
    expect(screen.queryByText('BATCH_PASS')).not.toBeInTheDocument();
  });

  it.each(['approval', 'listing'] as const)(
    'uses approveTime for the %s effective date when effectiveTime is absent',
    (mode) => {
      show({ mode, detail: { ...detail, status: 'APPROVED', approveTime: '2026-10-09T12:30:45' } });
      expect(
        screen.getByText(mode === 'listing' ? '2026-10-09 12:30' : '2026-10-09 12:30:45'),
      ).toBeInTheDocument();
    },
  );

  it('keeps effectiveTime authoritative when both timestamp fields are present', () => {
    show({
      mode: 'listing',
      detail: {
        ...detail,
        status: 'APPROVED',
        effectiveTime: '2026-10-09T12:45:00',
        approveTime: '2026-10-09T12:30:45',
      },
    });
    expect(screen.getByText('2026-10-09 12:45')).toBeInTheDocument();
    expect(screen.queryByText('2026-10-09 12:30:45')).not.toBeInTheDocument();
  });

  it('replaces stale detail with a retryable error', () => {
    const retry = vi.fn();
    show({ error: new Error('请求失败'), onRetry: retry });
    expect(screen.queryByText('太阳膜-原厂')).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole('button', { name: /重\s*试/ }));
    expect(retry).toHaveBeenCalledOnce();
  });
});