-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconvert.spec.ts
More file actions
341 lines (283 loc) · 10.4 KB
/
convert.spec.ts
File metadata and controls
341 lines (283 loc) · 10.4 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import {anyToBoolean, anyToNumber, emptyToNull, hexToRGB, objToFormData, objToFormUrl, rgbToHex} from './convert';
describe('rgbToHex', () => {
it('should return a hex string for rgb(0,0,0)', () => {
expect(rgbToHex('rgb(0,0,0)')).toBe('#000000');
});
it('should return a hex string for RGB(255,255,255)', () => {
expect(rgbToHex('RGB(255,255,255)')).toBe('#ffffff');
});
it('should return a undefined for RG255,255,0', () => {
expect(rgbToHex('RG255,255,0')).toBeUndefined();
});
});
describe('hexToRGB', () => {
it('should return a rgba string for #000000 .7', () => {
expect(hexToRGB('#000000', .7)).toBe('rgba(0,0,0,.7)');
});
it('should return a rgb string for #ffffff', () => {
expect(hexToRGB('#ffffff')).toBe('rgb(255,255,255)');
});
it('should return a undefined for fail hex string', () => {
expect(hexToRGB('#12312312')).toBeUndefined();
});
});
describe('emptyToNull', () => {
it('should return a number for number', () => {
expect(emptyToNull(7)).toBe(7);
});
it('should return a string string for string', () => {
expect(emptyToNull('07')).toBe('07');
expect(emptyToNull('0')).toBe('0');
});
it('should return a null for empty string', () => {
expect(emptyToNull('')).toBe(null);
});
it('should return a null for zero', () => {
expect(emptyToNull(0)).toBe(null);
});
it('should return a null for undefined', () => {
expect(emptyToNull(undefined)).toBe(null);
});
});
describe('anyToNumber', () => {
it('should return a number for number', () => {
expect(anyToNumber(7)).toBe(7);
});
it('should return a number string for string', () => {
expect(anyToNumber('07')).toBe(7);
expect(anyToNumber('test')).toBe(0);
});
it('should return a number for boolean', () => {
expect(anyToNumber(true)).toBe(1);
expect(anyToNumber(false)).toBe(0);
});
});
describe('anyToBoolean', () => {
it('should return a undefined for number', () => {
expect(anyToBoolean(7)).toBeUndefined();
});
it('should return a false string for string and not boolean return true', () => {
expect(anyToBoolean('07', true)).toBeFalsy();
});
it('should return a false for string', () => {
expect(anyToBoolean('0')).toBeFalsy();
expect(anyToBoolean('1')).toBeFalsy();
});
it('should return a false for string boolean', () => {
expect(anyToBoolean('false')).toBeFalsy();
expect(anyToBoolean('true')).toBeTruthy();
});
it('should return a number for boolean', () => {
expect(anyToBoolean(0)).toBeFalsy();
expect(anyToBoolean(1)).toBeTruthy();
});
it('should return a boolean for boolean', () => {
expect(anyToBoolean(true)).toBeTruthy();
expect(anyToBoolean(false)).toBeFalsy();
});
});
describe('objToFormData', () => {
const formDataToObject = (formData: FormData) => {
const obj: any = {};
formData.forEach((value, key) => {
if (key.includes('[')) {
// Handles array-like keys, such as `profile[0][name]`
const keys = key.split(/\[|]\[|]/).filter(Boolean);
keys.reduce((acc, cur, idx) => {
if (idx === keys.length - 1) {
acc[cur] = value;
} else {
if (!acc[cur]) {
acc[cur] = isNaN(Number(keys[idx + 1])) ? {} : [];
}
}
return acc[cur];
}, obj);
} else {
obj[key] = value;
}
});
return obj;
};
it('should convert an object to FormData', () => {
const data = {
name: 'Jack',
age: 30,
profile: {
username: 'jacky'
}
};
const formData = objToFormData(data);
const result = formDataToObject(formData);
expect(result).toEqual({
name: 'Jack',
age: '30', // FormData always stores values as strings
profile: {
username: 'jacky'
}
});
});
it('should handle arrays and nested objects in FormData', () => {
const data = {
files: [
{name: 'file1.txt', size: '123KB'},
{name: 'file2.txt', size: '456KB'}
]
};
const formData = objToFormData(data);
const result = formDataToObject(formData);
expect(result).toEqual({
files: [
{name: 'file1.txt', size: '123KB'},
{name: 'file2.txt', size: '456KB'}
]
});
});
it('should skip undefined or null values', () => {
const data = {
name: 'Jack',
age: null,
profile: undefined
};
const formData = objToFormData(data);
const result = formDataToObject(formData);
expect(result).toEqual({
name: 'Jack'
});
});
it('should handle File objects in FormData', () => {
const file = new File(['test content'], 'test.txt', {type: 'text/plain'});
const data = {
name: 'Jack',
file: file
};
const formData = objToFormData(data);
// 验证 FormData 中是否包含文件
expect(formData.get('name')).toBe('Jack');
expect(formData.get('file')).toBeInstanceOf(File);
expect(formData.get('file')).toBe(file);
});
it('should handle File objects with correct extension in FormData', () => {
const file = new File(['test content'], 'test.png', {type: 'image/png'});
const data = {
name: 'Jack',
image: file
};
const formData = objToFormData(data);
// 验证 FormData 中是否包含文件
expect(formData.get('name')).toBe('Jack');
expect(formData.get('image')).toBeInstanceOf(File);
expect(formData.get('image')).toBe(file);
// 验证文件名是否正确
const formDataEntry = formData.get('image') as File;
expect(formDataEntry.name).toBe('test.png');
expect(formDataEntry.type).toBe('image/png');
});
it('should handle Blob objects with correct extension in FormData', () => {
const blob = new Blob(['test content'], {type: 'image/jpeg'});
const data = {
name: 'Jack',
image: blob
};
const formData = objToFormData(data);
// 验证 FormData 中是否包含文件
expect(formData.get('name')).toBe('Jack');
expect(formData.get('image')).toBeInstanceOf(File); // Blob 会被转换为 File
expect(formData.get('image')).not.toBe(blob); // 不是同一个对象
// 验证 File 的类型和文件名
const formDataEntry = formData.get('image') as File;
expect(formDataEntry.type).toBe('image/jpeg');
expect(formDataEntry.name).toBe('image.jpeg');
});
});
describe('objToFormUrl', () => {
it('should convert a simple object to URLSearchParams', () => {
const data = {
account: 'xxxxx',
password: '12345'
};
const urlencoded = objToFormUrl(data);
expect(urlencoded.get('account')).toBe('xxxxx');
expect(urlencoded.get('password')).toBe('12345');
expect(urlencoded.toString()).toBe('account=xxxxx&password=12345');
});
it('should convert an object with nested properties to URLSearchParams', () => {
const data = {
name: 'Jack',
age: 30,
profile: {
username: 'jacky'
}
};
const urlencoded = objToFormUrl(data);
expect(urlencoded.get('name')).toBe('Jack');
expect(urlencoded.get('age')).toBe('30');
expect(urlencoded.get('profile[username]')).toBe('jacky');
});
it('should handle arrays in URLSearchParams', () => {
const data = {
tags: ['javascript', 'typescript', 'react']
};
const urlencoded = objToFormUrl(data);
expect(urlencoded.get('tags[0]')).toBe('javascript');
expect(urlencoded.get('tags[1]')).toBe('typescript');
expect(urlencoded.get('tags[2]')).toBe('react');
});
it('should handle arrays with nested objects in URLSearchParams', () => {
const data = {
users: [
{name: 'Jack', age: 30},
{name: 'Mary', age: 25}
]
};
const urlencoded = objToFormUrl(data);
expect(urlencoded.get('users[0][name]')).toBe('Jack');
expect(urlencoded.get('users[0][age]')).toBe('30');
expect(urlencoded.get('users[1][name]')).toBe('Mary');
expect(urlencoded.get('users[1][age]')).toBe('25');
});
it('should skip undefined or null values', () => {
const data = {
name: 'Jack',
age: null,
profile: undefined,
active: true
};
const urlencoded = objToFormUrl(data);
expect(urlencoded.get('name')).toBe('Jack');
expect(urlencoded.get('age')).toBeNull();
expect(urlencoded.get('profile')).toBeNull();
expect(urlencoded.get('active')).toBe('true');
});
it('should convert boolean and number values to strings', () => {
const data = {
active: true,
count: 100,
price: 99.99
};
const urlencoded = objToFormUrl(data);
expect(urlencoded.get('active')).toBe('true');
expect(urlencoded.get('count')).toBe('100');
expect(urlencoded.get('price')).toBe('99.99');
});
it('should handle empty object', () => {
const data = {};
const urlencoded = objToFormUrl(data);
expect(urlencoded.toString()).toBe('');
});
it('should handle complex nested structure', () => {
const data = {
user: {
profile: {
name: 'Jack',
contact: {
email: 'jack@example.com'
}
}
}
};
const urlencoded = objToFormUrl(data);
expect(urlencoded.get('user[profile][name]')).toBe('Jack');
expect(urlencoded.get('user[profile][contact][email]')).toBe('jack@example.com');
});
});