-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathitem-value.unit.test.ts
More file actions
393 lines (340 loc) · 10.9 KB
/
Copy pathitem-value.unit.test.ts
File metadata and controls
393 lines (340 loc) · 10.9 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import { bold } from 'ansis';
import type Details from 'lighthouse/types/lhr/audit-details';
import { beforeAll, describe, expect, it } from 'vitest';
import { ui } from '@code-pushup/utils';
import {
type SimpleItemValue,
formatTableItemPropertyValue,
parseNodeValue,
parseSimpleItemValue,
parseTableItemPropertyValue,
} from './item-value.js';
describe('parseNodeValue', () => {
it('should parse selector of node', () => {
expect(parseNodeValue({ type: 'node', selector: 'h1 > span.icon' })).toBe(
'h1 > span.icon',
);
});
it('should parse empty node', () => {
expect(parseNodeValue(undefined)).toBe('');
});
});
describe('parseSimpleItemValue', () => {
it('should parse primitive ItemValue type string', () => {
expect(parseSimpleItemValue('42')).toBe('42');
});
it('should parse primitive ItemValue type number', () => {
expect(parseSimpleItemValue(42)).toBe(42);
});
it('should parse primitive ItemValue type boolean', () => {
expect(parseSimpleItemValue(false)).toBe(false);
});
it('should parse ObjectItemValue ItemValue type boolean', () => {
expect(parseSimpleItemValue({ type: 'numeric', value: 42 })).toBe(42);
});
it('should parse IcuMessage', () => {
expect(
parseSimpleItemValue({
value: { i18nId: 'i18nId-42', formattedDefault: '42' },
} as any),
).toBe('42');
});
});
describe('parseTableItemPropertyValue', () => {
beforeAll(() => {
ui().switchMode('raw');
});
it('should parse undefined', () => {
expect(parseTableItemPropertyValue(undefined)).toBe('');
});
it('should parse primitive string value', () => {
expect(parseTableItemPropertyValue('42')).toBe('42');
});
it('should parse primitive number float value', () => {
expect(parseTableItemPropertyValue(42.21)).toBe(42.21);
});
it('should parse primitive number int value', () => {
expect(parseTableItemPropertyValue(42)).toBe(42);
});
it('should parse primitive boolean value', () => {
expect(parseTableItemPropertyValue(false)).toBe(false);
});
it('should parse value item code', () => {
expect(
parseTableItemPropertyValue({ type: 'code', value: 'const num = 42;' }),
).toBe('const num = 42;');
});
it('should parse value item url', () => {
expect(
parseTableItemPropertyValue({
type: 'url',
value: 'https://www.code-pushup.dev',
}),
).toBe('https://www.code-pushup.dev');
});
it('should parse value item link', () => {
expect(
parseTableItemPropertyValue({
type: 'link',
url: 'https://www.code-pushup.dev',
text: 'Code Pushup',
}),
).toEqual({
type: 'link',
url: 'https://www.code-pushup.dev',
text: 'Code Pushup',
});
});
it('should parse value item node', () => {
expect(
parseTableItemPropertyValue({ type: 'node', selector: 'h1 > span.icon' }),
).toBe('h1 > span.icon');
});
it('should parse value item numeric', () => {
expect(parseTableItemPropertyValue({ type: 'numeric', value: 42 })).toBe(
42,
);
});
it('should parse value item source-location', () => {
expect(
parseTableItemPropertyValue({
type: 'code',
value: 'Legible text',
}),
).toBe('Legible text');
});
it('should parse value item subitems to empty string and log implemented', () => {
expect(
parseTableItemPropertyValue({
type: 'subitems',
items: [
{
url: 'https://fonts.gstatic.com/s/spacegrotesk/v16/V8mDoQDjQSkFtoMM3T6r8E7mPbF4Cw.woff2',
mainThreadTime: 0,
blockingTime: 0,
transferSize: 22_826,
tbtImpact: 0,
},
{
url: 'https://fonts.gstatic.com/s/spacegrotesk/v16/V8mDoQDjQSkFtoMM3T6r8E7mPb94C-s0.woff2',
mainThreadTime: 0,
blockingTime: 0,
transferSize: 18_380,
tbtImpact: 0,
},
],
}),
).toBe('');
expect(ui()).toHaveLogged(
'info',
`Value type ${bold('subitems')} is not implemented`,
);
});
it('should parse value item debugdata to empty string and log implemented', () => {
expect(parseTableItemPropertyValue({ type: 'debugdata' })).toBe('');
expect(ui()).toHaveLogged(
'info',
`Value type ${bold('debugdata')} is not implemented`,
);
});
it('should parse value item IcuMessage', () => {
expect(
parseTableItemPropertyValue({
value: { i18nId: 'i18nId-42', formattedDefault: 'IcuMessageValue' },
} as SimpleItemValue),
).toBe('IcuMessageValue');
});
});
describe('formatTableItemPropertyValue', () => {
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
const stringOfLength = (length: number, postfix = ''): string => {
const maxLength = length - postfix.length;
let result = '';
// eslint-disable-next-line functional/no-loop-statements
for (let i = 0; i < maxLength; i++) {
result += alphabet.at((alphabet.length - 1) % i);
}
if (postfix) {
result += postfix;
}
return result;
};
beforeAll(() => {
ui().switchMode('raw');
});
it('should format undefined to empty string', () => {
expect(formatTableItemPropertyValue(undefined)).toBe('');
});
it('should format primitive string value without extra type format', () => {
expect(formatTableItemPropertyValue('42 ')).toBe('42');
});
it('should format primitive number', () => {
expect(formatTableItemPropertyValue(42.213_123_123)).toBe(42.213_123_123);
});
it('should format primitive number value have no floating numbers if all are zeros', () => {
expect(formatTableItemPropertyValue(42)).toBe(42);
});
it('should format primitive boolean value', () => {
expect(formatTableItemPropertyValue(false)).toBe(false);
});
it('should forward non primitive value directly if no format is provided', () => {
expect(
formatTableItemPropertyValue(
{ type: 'debugdata', value: '42' },
undefined,
),
).toStrictEqual({ type: 'debugdata', value: '42' });
});
it('should format value based on itemValueFormat "bytes"', () => {
expect(formatTableItemPropertyValue('100000', 'numeric')).toBe('100000');
});
it('should format value based on itemValueFormat "code"', () => {
expect(
formatTableItemPropertyValue(
{ type: 'code', value: '<body><h1>Code Pushup</h1></body>' },
'code',
),
).toBe('<code><body><h1>Code Pushup</h1></body></code>');
});
it('should format value based on itemValueFormat "link"', () => {
expect(
formatTableItemPropertyValue(
{
type: 'link',
url: 'https://code-pushup.dev',
text: 'code-pushup.dev',
},
'link',
),
).toBe('<a href="https://code-pushup.dev">code-pushup.dev</a>');
});
it('should format value based on itemValueFormat "url"', () => {
expect(
formatTableItemPropertyValue(
{ type: 'url', value: 'https://code-pushup.dev' },
'url',
),
).toBe('<a href="https://code-pushup.dev">https://code-pushup.dev</a>');
});
it('should format value based on itemValueFormat "timespanMs"', () => {
expect(
formatTableItemPropertyValue(
{ type: 'numeric', value: 2142 },
'timespanMs',
),
).toBe('2.142 s');
});
it('should format value based on itemValueFormat "ms"', () => {
expect(
formatTableItemPropertyValue({ type: 'numeric', value: 2142 }, 'ms'),
).toBe('2.142 s');
});
it('should format value based on itemValueFormat "node"', () => {
expect(
formatTableItemPropertyValue(
{ type: 'node', selector: 'h1 > span' },
'node',
),
).toBe('h1 > span');
});
it('should format value based on itemValueFormat "source-location"', () => {
expect(
formatTableItemPropertyValue(
{
type: 'source-location',
url: 'https://code-pushup.dev',
} as Details.ItemValue,
'source-location',
),
).toBe('https://code-pushup.dev');
});
it('should format value based on itemValueFormat "source-location" to a length of 200 and add "..."', () => {
const formattedStr = formatTableItemPropertyValue(
{
type: 'source-location',
url: stringOfLength(210, 'https://code-pushup.dev'),
} as Details.ItemValue,
'source-location',
) as string;
expect(formattedStr.length).toBeLessThanOrEqual(200);
expect(formattedStr.slice(-3)).toBe('...');
});
it('should format value based on itemValueFormat "numeric" as int', () => {
expect(
formatTableItemPropertyValue(
{ type: 'numeric', value: 42 } as Details.ItemValue,
'numeric',
),
).toBe('42');
});
it('should format value based on itemValueFormat "numeric" as float', () => {
expect(
formatTableItemPropertyValue(
{ type: 'numeric', value: 42.1 } as Details.ItemValue,
'numeric',
),
).toBe('42.1');
});
it('should format value based on itemValueFormat "numeric" as int if float has only 0 post comma', () => {
expect(
formatTableItemPropertyValue(
{ type: 'numeric', value: Number('42.0') } as Details.ItemValue,
'numeric',
),
).toBe('42');
});
it('should format value based on itemValueFormat "text"', () => {
expect(
formatTableItemPropertyValue(
{
type: 'url',
value: 'https://github.com/code-pushup/cli/blob/main/README.md',
} as Details.ItemValue,
'text',
),
).toBe('https://github.com/code-pushup/cli/blob/main/README.md');
});
it('should format value based on itemValueFormat "text" to a length of 500 and add "..."', () => {
const formattedStr = formatTableItemPropertyValue(
{
type: 'url',
value: stringOfLength(510, 'https://github.com/'),
} as Details.ItemValue,
'text',
) as string;
expect(formattedStr.length).toBeLessThanOrEqual(500);
expect(formattedStr.slice(-3)).toBe('...');
});
it('should format value based on itemValueFormat "multi"', () => {
expect(
formatTableItemPropertyValue(
{ type: 'numeric', value: 42 } as Details.ItemValue,
'multi',
),
).toBe('');
expect(ui()).toHaveLogged(
'info',
`Format type ${bold('multi')} is not implemented`,
);
});
it('should format value based on itemValueFormat "thumbnail"', () => {
expect(
formatTableItemPropertyValue(
{ type: 'numeric', value: 42 } as Details.ItemValue,
'thumbnail',
),
).toBe('');
expect(ui()).toHaveLogged(
'info',
`Format type ${bold('thumbnail')} is not implemented`,
);
});
it('should not format value based on itemValueFormat type that is unsupported', () => {
expect(
formatTableItemPropertyValue(
{ type: 'unsupported' as any, value: 43 },
'unsupported' as Details.ItemValueType,
),
).toStrictEqual({ type: 'unsupported', value: 43 });
});
});