agePickerOptions.test.js 1.19 KB
import { describe, expect, it } from 'vitest'
import {
  buildAgePickerColumn,
  formatAgeDisplayValue,
  normalizeAgePickerValue,
  parseAgePickerValue,
  SPECIAL_AGE_VALUE_PREFIX
} from '../agePickerOptions'

describe('agePickerOptions', () => {
  it('should prepend special age options before numeric ages', () => {
    const column = buildAgePickerColumn({ specialOptions: ['孕22周'] })

    expect(column[0]).toEqual({
      text: '孕22周',
      value: `${SPECIAL_AGE_VALUE_PREFIX}孕22周`
    })
    expect(column[1]).toEqual({
      text: '0 岁',
      value: '000'
    })
  })

  it('should format special and numeric age display values', () => {
    expect(formatAgeDisplayValue('孕22周')).toBe('孕22周')
    expect(formatAgeDisplayValue(0)).toBe('0 岁')
    expect(formatAgeDisplayValue('12')).toBe('12 岁')
  })

  it('should normalize and parse picker values correctly', () => {
    expect(normalizeAgePickerValue('孕22周', ['孕22周'])).toBe(`${SPECIAL_AGE_VALUE_PREFIX}孕22周`)
    expect(normalizeAgePickerValue(3, ['孕22周'])).toBe('003')
    expect(parseAgePickerValue(`${SPECIAL_AGE_VALUE_PREFIX}孕22周`)).toBe('孕22周')
    expect(parseAgePickerValue('003')).toBe(3)
  })
})