forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmisc.test.ts
More file actions
334 lines (277 loc) · 9.85 KB
/
misc.test.ts
File metadata and controls
334 lines (277 loc) · 9.85 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
import {
isVariable,
isUseI18NSetter,
convertToI18NObject,
isString,
waitForThing,
arrShallowEquals,
isFromVC,
executePendingFn,
compatStage,
invariant,
isRegExp,
shouldUseVariableSetter,
} from '../../src/misc';
import { IPublicModelComponentMeta } from '@felce/lowcode-types';
describe('isVariable', () => {
it('should return true for a variable object', () => {
const variable = { type: 'variable', variable: 'foo', value: 'bar' };
const result = isVariable(variable);
expect(result).toBe(true);
});
it('should return false for non-variable objects', () => {
const obj = { type: 'object' };
const result = isVariable(obj);
expect(result).toBe(false);
});
});
describe('isUseI18NSetter', () => {
it('should return true for a property with I18nSetter', () => {
const prototype = {
options: {
configure: [{ name: 'propName', setter: { type: { displayName: 'I18nSetter' } } }],
},
};
const propName = 'propName';
const result = isUseI18NSetter(prototype, propName);
expect(result).toBe(true);
});
it('should return false for a property without I18nSetter', () => {
const prototype = {
options: {
configure: [{ name: 'propName', setter: { type: { displayName: 'OtherSetter' } } }],
},
};
const propName = 'propName';
const result = isUseI18NSetter(prototype, propName);
expect(result).toBe(false);
});
});
describe('convertToI18NObject', () => {
it('should return the input if it is already an I18N object', () => {
const i18nObject = { type: 'i18n', use: 'en', en: 'Hello' };
const result = convertToI18NObject(i18nObject);
expect(result).toEqual(i18nObject);
});
it('should convert a string to an I18N object', () => {
const inputString = 'Hello';
const result = convertToI18NObject(inputString);
const expectedOutput = { type: 'i18n', use: 'zh-CN', 'zh-CN': inputString };
expect(result).toEqual(expectedOutput);
});
});
describe('isString', () => {
it('should return true for a string', () => {
const stringValue = 'Hello, world!';
const result = isString(stringValue);
expect(result).toBe(true);
});
it('should return true for an empty string', () => {
const emptyString = '';
const result = isString(emptyString);
expect(result).toBe(true);
});
it('should return false for a number', () => {
const numberValue = 42; // Not a string
const result = isString(numberValue);
expect(result).toBe(false);
});
it('should return false for an object', () => {
const objectValue = { key: 'value' }; // Not a string
const result = isString(objectValue);
expect(result).toBe(false);
});
it('should return false for null', () => {
const result = isString(null);
expect(result).toBe(false);
});
it('should return false for undefined', () => {
const undefinedValue = undefined;
const result = isString(undefinedValue);
expect(result).toBe(false);
});
it('should return false for a boolean', () => {
const booleanValue = true; // Not a string
const result = isString(booleanValue);
expect(result).toBe(false);
});
});
describe('waitForThing', () => {
it('should resolve immediately if the thing is available', async () => {
const obj = { prop: 'value' };
const path = 'prop';
const result = await waitForThing(obj, path);
expect(result).toBe('value');
});
it('should resolve after a delay if the thing becomes available', async () => {
const obj = { prop: undefined };
const path = 'prop';
const delay = 100; // Adjust the delay as needed
setTimeout(() => {
obj.prop = 'value';
}, delay);
const result = await waitForThing(obj, path);
expect(result).toBe('value');
});
});
describe('arrShallowEquals', () => {
it('should return true for two empty arrays', () => {
const arr1 = [];
const arr2 = [];
const result = arrShallowEquals(arr1, arr2);
expect(result).toBe(true);
});
it('should return true for two arrays with the same elements in the same order', () => {
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];
const result = arrShallowEquals(arr1, arr2);
expect(result).toBe(true);
});
it('should return true for two arrays with the same elements in a different order', () => {
const arr1 = [1, 2, 3];
const arr2 = [3, 2, 1];
const result = arrShallowEquals(arr1, arr2);
expect(result).toBe(true);
});
it('should return false for two arrays with different lengths', () => {
const arr1 = [1, 2, 3];
const arr2 = [1, 2];
const result = arrShallowEquals(arr1, arr2);
expect(result).toBe(false);
});
it('should return false for one array and a non-array', () => {
const arr1 = [1, 2, 3];
const nonArray = 'not an array';
const result = arrShallowEquals(arr1, nonArray);
expect(result).toBe(false);
});
it('should return false for two arrays with different elements', () => {
const arr1 = [1, 2, 3];
const arr2 = [3, 4, 5];
const result = arrShallowEquals(arr1, arr2);
expect(result).toBe(false);
});
it('should return true for arrays with duplicate elements', () => {
const arr1 = [1, 2, 2, 3];
const arr2 = [2, 3, 3, 1];
const result = arrShallowEquals(arr1, arr2);
expect(result).toBe(true);
});
});
describe('isFromVC', () => {
it('should return true when advanced configuration is present', () => {
// Create a mock meta object with advanced configuration
const meta: IPublicModelComponentMeta = {
getMetadata: () => ({ configure: { advanced: true } }),
};
const result = isFromVC(meta);
expect(result).toBe(true);
});
it('should return false when advanced configuration is not present', () => {
// Create a mock meta object without advanced configuration
const meta: IPublicModelComponentMeta = {
getMetadata: () => ({ configure: { advanced: false } }),
};
const result = isFromVC(meta);
expect(result).toBe(false);
});
it('should return false when meta is undefined', () => {
const meta: IPublicModelComponentMeta | undefined = undefined;
const result = isFromVC(meta);
expect(result).toBe(false);
});
it('should return false when meta does not have configure information', () => {
// Create a mock meta object without configure information
const meta: IPublicModelComponentMeta = {
getMetadata: () => ({}),
};
const result = isFromVC(meta);
expect(result).toBe(false);
});
it('should return false when configure.advanced is not present', () => {
// Create a mock meta object with incomplete configure information
const meta: IPublicModelComponentMeta = {
getMetadata: () => ({ configure: {} }),
};
const result = isFromVC(meta);
expect(result).toBe(false);
});
});
describe('executePendingFn', () => {
it('should execute the provided function after the specified timeout', async () => {
// Mock the function to execute
const fn = vi.fn();
// Call executePendingFn with the mocked function and a short timeout
executePendingFn(fn, 100);
// Ensure the function has not been called immediately
expect(fn).not.toHaveBeenCalled();
// Wait for the specified timeout
await new Promise((resolve) => setTimeout(resolve, 100));
// Ensure the function has been called after the timeout
expect(fn).toHaveBeenCalled();
});
it('should execute the provided function with a default timeout if not specified', async () => {
// Mock the function to execute
const fn = vi.fn();
// Call executePendingFn with the mocked function without specifying a timeout
executePendingFn(fn);
// Ensure the function has not been called immediately
expect(fn).not.toHaveBeenCalled();
// Wait for the default timeout (2000 milliseconds)
await new Promise((resolve) => setTimeout(resolve, 2000));
// Ensure the function has been called after the default timeout
expect(fn).toHaveBeenCalled();
});
});
describe('compatStage', () => {
it('should convert a number to an enum stage', () => {
const result = compatStage(3);
expect(result).toBe('save');
});
it('should warn about the deprecated usage', () => {
const warnSpy = vi.spyOn(console, 'warn');
const result = compatStage(2);
expect(result).toBe('serilize');
expect(warnSpy).toHaveBeenCalledWith(
'stage 直接指定为数字的使用方式已经过时,将在下一版本移除,请直接使用 IPublicEnumTransformStage.Render|Serilize|Save|Clone|Init|Upgrade',
);
warnSpy.mockRestore();
});
it('should return the enum stage if it is already an enum', () => {
const result = compatStage('render');
expect(result).toBe('render');
});
});
describe('invariant', () => {
it('should not throw an error if the check is true', () => {
expect(() => invariant(true, 'Test invariant', 'thing')).not.toThrow();
});
it('should throw an error if the check is false', () => {
expect(() => invariant(false, 'Test invariant', 'thing')).toThrowError(
"Invariant failed: Test invariant in 'thing'",
);
});
});
describe('isRegExp', () => {
it('should return true for a valid RegExp', () => {
const regex = /test/;
const result = isRegExp(regex);
expect(result).toBe(true);
});
it('should return false for a non-RegExp object', () => {
const nonRegExp = { test: /test/ };
const result = isRegExp(nonRegExp);
expect(result).toBe(false);
});
it('should return false for null', () => {
const result = isRegExp(null);
expect(result).toBe(false);
});
});
it('shouldUseVariableSetter', () => {
expect(shouldUseVariableSetter(false, true)).toBeFalsy();
expect(shouldUseVariableSetter(true, true)).toBeTruthy();
expect(shouldUseVariableSetter(true, false)).toBeTruthy();
expect(shouldUseVariableSetter(undefined, false)).toBeFalsy();
expect(shouldUseVariableSetter(undefined, true)).toBeTruthy();
});