calendarMonthMode.test.mjs 4.39 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  getInitialDayType,
  resolveMonthOnly,
  resolveDayOnly,
  shouldShowDayMonthToggle,
  shouldShowMonthPanel,
  shouldBuildYearList,
  shouldUseSingleMonthView,
  buildAllMonthsMeta,
  buildDisplayMonthWindow,
  getMonthDifference,
  shouldRebuildMonthListOnBoundsChange
} from '../components/CalendarMonth/calendarMonth.helpers.js'

describe('calendarMonthMode', () => {
  it('CAL-01 monthOnly 初始 dayType 为 0', () => {
    assert.equal(getInitialDayType({ monthOnly: true }), 0)
  })

  it('CAL-02 默认 dayType 为 1', () => {
    assert.equal(getInitialDayType({ monthOnly: false }), 1)
  })

  it('CAL-05 monType 兼容别名', () => {
    assert.equal(resolveMonthOnly(false, true), true)
    assert.equal(resolveMonthOnly(true, false), true)
  })

  it('CAL-06 dayOnly 初始 dayType 为 1', () => {
    assert.equal(getInitialDayType({ dayOnly: true }), 1)
    assert.equal(shouldShowDayMonthToggle(false, true), false)
  })

  it('CAL-07 全功能模式展示日/月切换', () => {
    assert.equal(shouldShowDayMonthToggle(false, false), true)
    assert.equal(getInitialDayType({ monthOnly: false, dayOnly: false }), 1)
  })

  it('CAL-08 monthOnly 优先于 dayOnly', () => {
    assert.equal(resolveDayOnly(true, true), false)
    assert.equal(getInitialDayType({ monthOnly: true, dayOnly: true }), 0)
    assert.equal(shouldShowDayMonthToggle(true, true), false)
  })

  it('CAL-09 dayOnly 不渲染月份面板', () => {
    assert.equal(shouldShowMonthPanel({ dayOnly: true, dayType: 1 }), false)
    assert.equal(shouldShowMonthPanel({ dayOnly: false, dayType: 0 }), true)
    assert.equal(shouldShowMonthPanel({ dayOnly: false, dayType: 1 }), false)
    assert.equal(shouldShowMonthPanel({ monthOnly: true, dayType: 1 }), true)
  })

  it('CAL-10 buildAllMonthsMeta 一次构建多月份', () => {
    const now = new Date(2026, 6, 6)
    const built = buildAllMonthsMeta({
      minDate: '2026-01-01',
      maxDate: '2026-07-06',
      nowDate: now
    })
    assert.ok(built.monthList.length >= 7)
    assert.equal(built.dateList.length, built.monthList.length)
    assert.ok(built.current >= 0)
    assert.equal(getMonthDifference('2026-01-01', '2026-07-06'), 6)
  })

  it('CAL-11 dayOnly 跳过 yearList 构建判定', () => {
    assert.equal(shouldBuildYearList({ dayOnly: true }), false)
    assert.equal(shouldBuildYearList({ dayOnly: false }), true)
    assert.equal(shouldBuildYearList({ dayOnly: true, monthOnly: true }), true)
  })

  it('CAL-12 buildDisplayMonthWindow 边界', () => {
    const monthList = [
      { mark: 1, data: [{ day: 1, data: '2026-01-01' }] },
      { mark: 2, data: [{ day: 1, data: '2026-02-01' }] },
      { mark: 3, data: [{ day: 1, data: '2026-03-01' }] }
    ]
    const first = buildDisplayMonthWindow(monthList, 0)
    assert.equal(first.length, 3)
    assert.equal(first[0].monthIndex, 0)
    assert.equal(first[1].monthIndex, 0)
    assert.equal(first[2].monthIndex, 1)

    const last = buildDisplayMonthWindow(monthList, 2)
    assert.equal(last[0].monthIndex, 1)
    assert.equal(last[2].monthIndex, 2)

    const mid = buildDisplayMonthWindow(monthList, 1)
    assert.equal(mid[0].monthIndex, 0)
    assert.equal(mid[1].monthIndex, 1)
    assert.equal(mid[2].monthIndex, 2)
  })

  it('CAL-13 dayOnly 走单月渲染分支', () => {
    assert.equal(shouldUseSingleMonthView(true), true)
    assert.equal(shouldUseSingleMonthView(false), false)
    assert.equal(shouldUseSingleMonthView(true, true), false)
  })

  it('CAL-14 shouldRebuildMonthListOnBoundsChange detects bound changes', () => {
    assert.equal(
      shouldRebuildMonthListOnBoundsChange('', '2026-07-06', '', '2026-07-06'),
      false
    )
    assert.equal(
      shouldRebuildMonthListOnBoundsChange('', '', '', '2026-07-06'),
      true
    )
    assert.equal(
      shouldRebuildMonthListOnBoundsChange('2026-01-01', '2026-07-06', '2026-01-01', '2026-07-07'),
      true
    )
  })

  it('CAL-15 buildAllMonthsMeta disables future days when maxDate is today', () => {
    const now = new Date(2026, 6, 6)
    const built = buildAllMonthsMeta({
      minDate: '2026-01-01',
      maxDate: '2026-07-06',
      nowDate: now
    })
    const july = built.monthList.find(m => m.mark === 7)
    assert.ok(july)
    const futureDay = july.data.find(d => d.data === '2026-07-07')
    assert.ok(futureDay)
    assert.equal(futureDay.disable, true)
  })
})