planSubmitTransformers.test.js 2.03 KB
import { describe, expect, it, vi } from 'vitest'
import {
  transformSubmitValue,
  transformWithdrawalStagesForSubmit
} from '../planSubmitTransformers'

describe('transformWithdrawalStagesForSubmit', () => {
  it('should convert withdrawal stage amount from fen to yuan', () => {
    const result = transformWithdrawalStagesForSubmit([
      {
        annual_withdrawal_amount: 12300,
        withdrawal_start_age: 18,
        withdrawal_period: '1年',
        annual_increase_percentage: '12'
      }
    ])

    expect(result).toEqual([
      {
        annual_withdrawal_amount: '123.00',
        withdrawal_start_age: 18,
        withdrawal_period: '1年',
        annual_increase_percentage: '12'
      }
    ])
  })

  it('should keep empty stage amount unchanged', () => {
    const result = transformWithdrawalStagesForSubmit([
      {
        annual_withdrawal_amount: null,
        withdrawal_start_age: 18
      }
    ])

    expect(result[0].annual_withdrawal_amount).toBeNull()
  })
})

describe('transformSubmitValue', () => {
  it('should use fen_to_yuan mapping for simple amount fields', () => {
    const toYuan = vi.fn().mockReturnValue('123.00')

    const result = transformSubmitValue({
      fieldKey: 'annual_withdrawal_amount',
      value: 12300,
      mapping: {
        api_field: 'annual_withdrawal_amount',
        transform: 'fen_to_yuan'
      },
      toYuan
    })

    expect(result).toBe('123.00')
    expect(toYuan).toHaveBeenCalledWith('annual_withdrawal_amount', 12300)
  })

  it('should use custom mapping transform for withdrawal stages', () => {
    const result = transformSubmitValue({
      fieldKey: 'withdrawal_stages',
      value: [
        {
          annual_withdrawal_amount: 12300,
          withdrawal_start_age: 18
        }
      ],
      mapping: {
        api_field: 'withdrawal_stages',
        transform: transformWithdrawalStagesForSubmit
      },
      toYuan: vi.fn()
    })

    expect(result).toEqual([
      {
        annual_withdrawal_amount: '123.00',
        withdrawal_start_age: 18
      }
    ])
  })
})