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
| import { describe, expect, it } from 'vitest';
|
| import { CONTROL_TYPES, countTag, normalizeControlType, validateTypeConfig } from '../../src/domain/field-types';
|
| describe('field type domain rules', () => {
| it('exposes the five supported control types as a readonly list', () => {
| expect(CONTROL_TYPES).toEqual(['text', 'dropdown', 'checkbox', 'date', 'multiline']);
| });
|
| it('prevents callers from changing the supported control type set', () => {
| const originalLength = CONTROL_TYPES.length;
|
| try {
| expect(Reflect.set(CONTROL_TYPES, originalLength, 'unknown')).toBe(false);
| expect(normalizeControlType('unknown')).toBe('text');
| expect(validateTypeConfig({ controlType: 'unknown' })).toEqual({
| ok: false,
| field: 'control-type',
| message: '请选择有效的字段类型',
| });
| } finally {
| Reflect.deleteProperty(CONTROL_TYPES, String(originalLength));
| Reflect.set(CONTROL_TYPES, 'length', originalLength);
| }
| });
|
| it('normalizes supported and unknown control types', () => {
| expect(normalizeControlType('date')).toBe('date');
| expect(normalizeControlType('unknown')).toBe('text');
| });
|
| it('rejects an unknown control type', () => {
| expect(validateTypeConfig({ controlType: 'unknown' })).toEqual({
| ok: false,
| field: 'control-type',
| message: '请选择有效的字段类型',
| });
| });
|
| it.each([null, undefined])('rejects a missing type config (%s)', (raw) => {
| expect(validateTypeConfig(raw)).toEqual({
| ok: false,
| field: 'control-type',
| message: '请选择有效的字段类型',
| });
| });
|
| it('trims dropdown labels, values, placeholder, and default selection', () => {
| expect(
| validateTypeConfig({
| controlType: 'dropdown',
| placeholder: ' 请选择 ',
| options: [
| { display: ' 合格 ', value: ' pass ' },
| { display: '不合格', value: 'fail' },
| ],
| defaultOptionValue: ' pass ',
| }),
| ).toEqual({
| ok: true,
| value: {
| controlType: 'dropdown',
| placeholder: '请选择',
| options: [
| { display: '合格', value: 'pass' },
| { display: '不合格', value: 'fail' },
| ],
| selectedIndex: 0,
| },
| });
| });
|
| it('uses -1 when a dropdown has no default selection', () => {
| const result = validateTypeConfig({
| controlType: 'dropdown',
| options: [
| { display: '甲', value: 'a' },
| { display: '乙', value: 'b' },
| ],
| });
|
| expect(result.ok).toBe(true);
| if (result.ok) {
| expect(result.value.controlType).toBe('dropdown');
| if (result.value.controlType === 'dropdown') {
| expect(result.value.selectedIndex).toBe(-1);
| }
| }
| });
|
| it.each([
| [[{ display: '合格', value: 'pass' }]],
| [
| [
| { display: '合格', value: 'pass' },
| { display: '', value: 'fail' },
| ],
| ],
| [
| [
| { display: '合格', value: 'pass' },
| { display: '不合格', value: '' },
| ],
| ],
| ])('rejects dropdowns with fewer than two complete options', (options) => {
| expect(validateTypeConfig({ controlType: 'dropdown', options })).toEqual({
| ok: false,
| field: 'dropdown-options',
| message: '下拉框至少需要两个完整选项',
| });
| });
|
| it('rejects duplicate dropdown business values', () => {
| expect(
| validateTypeConfig({
| controlType: 'dropdown',
| options: [
| { display: '甲', value: 'same' },
| { display: '乙', value: 'same' },
| ],
| }),
| ).toEqual({
| ok: false,
| field: 'dropdown-options',
| message: '下拉选项业务值不能重复',
| });
| });
|
| it('rejects a dropdown default that is not an option', () => {
| expect(
| validateTypeConfig({
| controlType: 'dropdown',
| options: [
| { display: '甲', value: 'a' },
| { display: '乙', value: 'b' },
| ],
| defaultOptionValue: 'c',
| }),
| ).toEqual({
| ok: false,
| field: 'dropdown-default',
| message: '默认项必须来自下拉选项',
| });
| });
|
| it('accepts a valid leap-day date', () => {
| expect(validateTypeConfig({ controlType: 'date', defaultValue: '2024-02-29' }).ok).toBe(true);
| });
|
| it.each(['2025-02-29', '2025-2-03'])('rejects invalid strict calendar date %s', (defaultValue) => {
| expect(validateTypeConfig({ controlType: 'date', defaultValue })).toEqual({
| ok: false,
| field: 'date-default',
| message: '请输入有效的日期',
| });
| });
|
| it('rejects line breaks in a single-line text default', () => {
| expect(validateTypeConfig({ controlType: 'text', defaultValue: 'a\nb' })).toEqual({
| ok: false,
| field: 'text-default',
| message: '单行文本默认值不能换行',
| });
| });
|
| it('permits line breaks in a multiline default', () => {
| const result = validateTypeConfig({
| controlType: 'multiline',
| defaultValue: 'a\nb',
| });
|
| expect(result.ok).toBe(true);
| if (result.ok) {
| expect(result.value).toMatchObject({
| controlType: 'multiline',
| defaultValue: 'a\nb',
| });
| }
| });
|
| it('normalizes checkbox state to a boolean', () => {
| const result = validateTypeConfig({
| controlType: 'checkbox',
| defaultChecked: true,
| });
|
| expect(result).toEqual({
| ok: true,
| value: {
| controlType: 'checkbox',
| placeholder: '',
| defaultValue: '',
| defaultChecked: true,
| },
| });
|
| expect(validateTypeConfig({ controlType: 'checkbox' })).toEqual({
| ok: true,
| value: {
| controlType: 'checkbox',
| placeholder: '',
| defaultValue: '',
| defaultChecked: false,
| },
| });
| });
|
| it('counts exact content-control tags and returns zero for non-arrays', () => {
| expect(countTag([{ Tag: 'a' }, { Tag: 'b' }, { Tag: 'a' }], 'a')).toBe(2);
| expect(countTag([{ Tag: 'A' }, { Tag: 'a ' }, {}, null], 'a')).toBe(0);
| expect(countTag(null, 'a')).toBe(0);
| });
| });
|
|