useFieldValueTransform.test.js
6.82 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
/**
* useFieldValueTransform 单元测试
*
* @description 测试字段值转换 Composable
* @module composables/__tests__/useFieldValueTransform.test
*/
import { ref } from 'vue'
import { describe, it, expect, beforeEach } from 'vitest'
import { useFieldValueTransform } from '../useFieldValueTransform'
import { PLAN_FIELD_DEFINITIONS, TRANSFORM_TYPES } from '@/config/plan-fields'
describe('useFieldValueTransform', () => {
describe('toYuan - 分转元(用于显示)', () => {
it('should convert fen value to yuan format', () => {
const formData = ref({ coverage: '1000000' }) // 分值整数(10000元×100)
const fieldDefinitions = PLAN_FIELD_DEFINITIONS
const { toYuan } = useFieldValueTransform(formData, fieldDefinitions)
// 分值 1000000(10000元×100)转为元值:÷100 = 10000.00(保留两位小数)
expect(toYuan('coverage', '1000000')).toBe('10000.00')
expect(toYuan('coverage', '1500000')).toBe('15000.00')
})
it('should convert yuan decimal string correctly', () => {
const formData = ref({ coverage: '1000050' }) // 分值整数
const { toYuan } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
expect(toYuan('coverage', '1000050')).toBe('10000.50') // 分转元:÷100,保留两位小数
})
it('should return yuan value directly for fields without transform', () => {
const formData = ref({ customer_name: '张三' })
const { toYuan } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
expect(toYuan('customer_name', '张三')).toBe('张三')
})
it('should handle null values', () => {
const formData = ref({ coverage: null })
const { toYuan } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
expect(toYuan('coverage', null)).toBe(null)
})
it('should handle undefined values', () => {
const formData = ref({ coverage: undefined })
const { toYuan } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
expect(toYuan('coverage', undefined)).toBe(undefined)
})
it('should return string for fen values (keep 2 decimal places)', () => {
const formData = ref({ coverage: '100005' }) // 分值字符串(10000.05元×100)
const { toYuan } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
// fenToYuan 返回字符串格式的元值
expect(toYuan('coverage', '100005')).toBe('1000.05') // 分→元:÷100,保留两位小数
})
})
describe('toFen - 元转分(用于提交)', () => {
it('should convert yuan value to fen', () => {
const formData = ref({ coverage: '10000' }) // 元值
const { toFen } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
expect(toFen('coverage', '10000')).toBe(1000000) // 元→分:×100
})
it('should convert yuan string to fen', () => {
const formData = ref({ coverage: '10000.00' }) // 元值字符串
const { toFen } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
expect(toFen('coverage', '10000.00')).toBe(1000000) // 元→分:×100
})
it('should handle null values', () => {
const formData = ref({ coverage: null })
const { toFen } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
expect(toFen('coverage', null)).toBe(null)
})
it('should handle undefined values', () => {
const formData = ref({ coverage: undefined })
const { toFen } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
expect(toFen('coverage', undefined)).toBe(undefined)
})
it('should return original value for fields without transform', () => {
const formData = ref({ customer_name: '张三' })
const { toFen } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
expect(toFen('customer_name', '张三')).toBe('张三')
})
})
describe('batchToFen - 批量元转分', () => {
it('should convert all yuan fields to fen', () => {
const formData = ref({
coverage: 10000, // 元值→分值
withdrawal_period: 3,
customer_name: '张三'
})
const { submitData } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
const result = submitData.value
expect(result.coverage).toBe(1000000) // 元转分:×100
expect(result.withdrawal_period).toBe(3)
expect(result.customer_name).toBe('张三')
})
it('should skip fields without transform attribute', () => {
const formData = ref({
customer_name: '张三',
gender: 'male'
})
const { submitData } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
const result = submitData.value
expect(result.customer_name).toBe('张三')
expect(result.gender).toBe('male')
})
})
describe('batchToFen - 批量元转分(用于提交)', () => {
it('should convert all yuan fields to fen', () => {
const formData = ref({
coverage: '10000', // 元值→分值
withdrawal_period: 3
})
const { submitData } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
const result = submitData.value
expect(result.coverage).toBe(1000000) // 元→分:×100
expect(result.withdrawal_period).toBe(3)
})
it('should skip fields without transform attribute', () => {
const formData = ref({
customer_name: '张三',
gender: 'male'
})
const { submitData } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
const result = submitData.value
expect(result.customer_name).toBe('张三')
expect(result.gender).toBe('male')
})
})
describe('displayData - 表单显示数据(元值)', () => {
it('should provide fen values for display', () => {
const formData = ref({
coverage: 1000000, // 分值(API存储)
withdrawal_period: 3
})
const { displayData } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
expect(displayData.value.coverage).toBe('10000.00') // 分→元显示
expect(displayData.value.withdrawal_period).toBe(3)
})
it('should be reactive', () => {
const formData = ref({ annual_premium: 10000 })
const { displayData } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
expect(displayData).toHaveProperty('value')
expect(displayData.value).toHaveProperty('annual_premium')
})
})
describe('submitData - API 提交数据(元值)', () => {
it('should provide yuan values for submit', () => {
const formData = ref({
coverage: 10000, // 元值整数,×100转分值
withdrawal_period: 3
})
const { submitData } = useFieldValueTransform(formData, PLAN_FIELD_DEFINITIONS)
expect(submitData.value.coverage).toBe(1000000) // 元→分:×100
expect(submitData.value.withdrawal_period).toBe(3)
})
})
})